Project

General

Profile

Download (18.5 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectPreRender.java @ e7daa161

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objectlib.main;
21

    
22
import java.lang.ref.WeakReference;
23

    
24
import android.app.Activity;
25
import android.content.SharedPreferences;
26

    
27
import org.distorted.library.main.DistortedFramebuffer;
28
import org.distorted.library.message.EffectListener;
29

    
30
import org.distorted.library.type.Static3D;
31
import org.distorted.objectlib.helpers.ObjectLibInterface;
32
import org.distorted.objectlib.effects.BaseEffect;
33
import org.distorted.objectlib.effects.scramble.ScrambleEffect;
34
import org.distorted.objectlib.helpers.BlockController;
35
import org.distorted.objectlib.helpers.MovesFinished;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class ObjectPreRender implements EffectListener
40
  {
41
  private final WeakReference<Activity> mAct;
42
  private final ObjectControl mController;
43
  private ObjectType mNextObject;
44
  private TwistyObject mOldObject, mNewObject;
45
  private SharedPreferences mPreferences;
46
  private MovesFinished mAddActionListener;
47
  private final BlockController mBlockController;
48
  private final ObjectLibInterface mInterface;
49
  private String mDebug;
50
  private float mMoveX, mMoveY;
51

    
52
  private boolean mFinishRotation, mRemoveRotation, mRemovePatternRotation, mAddRotation,
53
                  mSetQuat, mChangeObject, mSolveObject, mScrambleObject, mRecreateObject,
54
                  mInitializeObject, mSetTextureMap, mResetAllTextureMaps, mSolve;
55
  private boolean mUIBlocked, mTouchBlocked, mIsSolved;
56
  private long mRotationFinishedID;
57
  private final long[] mEffectID;
58
  private int mScreenWidth, mScreenHeight;
59
  private int[][] mNextMoves;
60
  private int mScrambleObjectNum;
61
  private int mAddRotationAxis, mAddRotationRowBitmap, mAddRotationAngle;
62
  private long mAddRotationDuration;
63
  private long mAddRotationID, mRemoveRotationID;
64
  private int mCubit, mFace, mNewColor;
65
  private int mNearestAngle;
66
  private long mDebugStartTime;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  public ObjectPreRender(Activity act, ObjectControl controller, ObjectLibInterface actioner)
71
    {
72
    mAct = new WeakReference<>(act);
73
    mInterface = actioner;
74
    mController = controller;
75

    
76
    mFinishRotation       = false;
77
    mRemoveRotation       = false;
78
    mRemovePatternRotation= false;
79
    mAddRotation          = false;
80
    mSetQuat              = false;
81
    mChangeObject         = false;
82
    mRecreateObject       = false;
83
    mSolveObject          = false;
84
    mSolve                = false;
85
    mScrambleObject       = false;
86

    
87
    mOldObject = null;
88
    mNewObject = null;
89

    
90
    mDebug = "";
91

    
92
    mScreenWidth  = 0;
93
    mScreenHeight = 0;
94
    mScrambleObjectNum = 0;
95

    
96
    mEffectID = new long[BaseEffect.Type.LENGTH];
97

    
98
    mBlockController = new BlockController(act,this);
99
    unblockEverything();
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  private void createObjectNow(ObjectType object)
105
    {
106
    boolean firstTime = (mNewObject==null);
107

    
108
    if( mOldObject!=null ) mOldObject.releaseResources();
109
    mOldObject = mNewObject;
110
    Static3D move = new Static3D(mMoveX,mMoveY,0);
111

    
112
    mNewObject = object.create( mController.getQuat(), move, mAct.get().getResources(), mScreenWidth, mScreenHeight);
113

    
114
    if( mNewObject!=null )
115
      {
116
      mController.setMovement(mNewObject.getMovement());
117
      if( firstTime && mPreferences!=null ) mNewObject.restorePreferences(mPreferences);
118
      mIsSolved = mNewObject.isSolved();
119
      }
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
124

    
125
  private void doEffectNow(BaseEffect.Type type)
126
    {
127
    try
128
      {
129
      int index = type.ordinal();
130
      DistortedFramebuffer frame = mController.getInterface().getFramebuffer();
131
      mEffectID[index] = type.startEffect(frame,this);
132
      }
133
    catch( Exception ex )
134
      {
135
      android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
136
      unblockEverything();
137
      }
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
  private void removeRotationNow()
143
    {
144
    mRemoveRotation=false;
145
    mNewObject.removeRotationNow();
146

    
147
    boolean solved = mNewObject.isSolved();
148

    
149
    if( solved && !mIsSolved )
150
      {
151
      mInterface.onSolved();
152
      unblockEverything();
153
      doEffectNow( BaseEffect.Type.WIN );
154
      }
155
    else
156
      {
157
      unblockEverything();
158
      }
159

    
160
    mIsSolved = solved;
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  private void removeRotation()
166
    {
167
    mRemoveRotation = true;
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  private void removePatternRotation()
173
    {
174
    mRemovePatternRotation = true;
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  private void removePatternRotationNow()
180
    {
181
    mRemovePatternRotation=false;
182
    mNewObject.removeRotationNow();
183
    mAddActionListener.onActionFinished(mRemoveRotationID);
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  private void addRotationNow()
189
    {
190
    mAddRotation = false;
191
    mAddRotationID = mNewObject.addNewRotation( mAddRotationAxis, mAddRotationRowBitmap,
192
                                                mAddRotationAngle, mAddRotationDuration, this);
193

    
194
    if( mAddRotationID==0 ) // failed to add effect - should never happen
195
      {
196
      unblockEverything();
197
      }
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  private void finishRotationNow()
203
    {
204
    mFinishRotation = false;
205
    blockEverything(BlockController.PLACE_0);
206
    mRotationFinishedID = mNewObject.finishRotationNow(this, mNearestAngle);
207

    
208
    if( mRotationFinishedID==0 ) // failed to add effect - should never happen
209
      {
210
      unblockEverything();
211
      }
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  private void changeObjectNow()
217
    {
218
    mChangeObject = false;
219

    
220
    if ( mNewObject==null || mNewObject.getObjectType()!=mNextObject )
221
      {
222
      blockEverything(BlockController.PLACE_1);
223
      createObjectNow(mNextObject);
224
      doEffectNow( BaseEffect.Type.SIZECHANGE );
225
      }
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  private void recreateObjectNow()
231
    {
232
    mRecreateObject = false;
233

    
234
    if ( mNewObject!=null )
235
      {
236
      blockEverything(BlockController.PLACE_1);
237
      createObjectNow(mNewObject.getObjectType());
238
      doEffectNow( BaseEffect.Type.SIZECHANGE );
239
      }
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  private void scrambleObjectNow()
245
    {
246
    mScrambleObject = false;
247
    mIsSolved       = false;
248
    blockEverything(BlockController.PLACE_3);
249
    doEffectNow( BaseEffect.Type.SCRAMBLE );
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  private void solveObjectNow()
255
    {
256
    mSolveObject = false;
257
    blockEverything(BlockController.PLACE_4);
258
    doEffectNow( BaseEffect.Type.SOLVE );
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  private void solveNow()
264
    {
265
    mSolve = false;
266
    if( mNewObject!=null ) mNewObject.solve();
267
    }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
  private void initializeObjectNow()
272
    {
273
    mInitializeObject = false;
274
    mNewObject.initializeObject(mNextMoves);
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
  private void setTextureMapNow()
280
    {
281
    mSetTextureMap = false;
282

    
283
    if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
  private void resetAllTextureMapsNow()
289
    {
290
    mResetAllTextureMaps = false;
291
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  private void setQuatNow()
297
    {
298
    mSetQuat = false;
299
    mController.setQuat();
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
  void rememberMove(int axis, int row, int angle)
305
    {
306
    mDebug += (" (m "+axis+" "+(1<<row)+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
  void finishRotation(int nearestAngle)
312
    {
313
    mNearestAngle   = nearestAngle;
314
    mFinishRotation = true;
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
  void setTextureMap(int cubit, int face, int newColor)
320
    {
321
    mSetTextureMap = true;
322

    
323
    mCubit    = cubit;
324
    mFace     = face;
325
    mNewColor = newColor;
326
    }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

    
330
  void setQuatOnNextRender()
331
    {
332
    mSetQuat = true;
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
  void setMove(float xmove, float ymove)
338
    {
339
    mMoveX = xmove;
340
    mMoveY = ymove;
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344
// INTERNAL API
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
  public int getNumScrambles()
348
    {
349
    return mScrambleObjectNum;
350
    }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
  public TwistyObject getOldObject()
355
    {
356
    return mOldObject;
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
  public float getMoveX()
362
    {
363
    return mMoveX;
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  public float getMoveY()
369
    {
370
    return mMoveY;
371
    }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374
// PUBLIC API
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
  public void setScreenSize(int width, int height)
378
    {
379
    if( mNewObject!=null )
380
      {
381
      mNewObject.createTexture();
382
      mNewObject.recomputeScaleFactor(width,height);
383
      }
384
    mScreenWidth = width;
385
    mScreenHeight= height;
386
    }
387

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389

    
390
  public void savePreferences(SharedPreferences.Editor editor)
391
    {
392
    if( mNewObject!=null )
393
      {
394
      mNewObject.savePreferences(editor);
395
      }
396
    }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
  public void restorePreferences(SharedPreferences preferences)
401
    {
402
    mPreferences = preferences;
403
    }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
  public void changeObject(ObjectType object)
408
    {
409
    mChangeObject = true;
410
    mNextObject = object;
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
  public void recreateObject()
416
    {
417
    mRecreateObject = true;
418
    }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

    
422
  public boolean isTouchBlocked()
423
    {
424
    return mTouchBlocked;
425
    }
426

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

    
429
  public boolean isUINotBlocked()
430
    {
431
    return !mUIBlocked;
432
    }
433

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435

    
436
  public void blockEverything(int place)
437
    {
438
    mUIBlocked   = true;
439
    mTouchBlocked= true;
440
    mBlockController.touchBlocked(place);
441
    mBlockController.uiBlocked(place);
442
    }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

    
446
  public void blockTouch(int place)
447
    {
448
    mTouchBlocked= true;
449
    mBlockController.touchBlocked(place);
450
    }
451

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

    
454
  public void unblockEverything()
455
    {
456
    mUIBlocked   = false;
457
    mTouchBlocked= false;
458
    mBlockController.touchUnblocked();
459
    mBlockController.uiUnblocked();
460
    }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

    
464
  public void unblockTouch()
465
    {
466
    mTouchBlocked= false;
467
    mBlockController.touchUnblocked();
468
    }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

    
472
  public void unblockUI()
473
    {
474
    mUIBlocked= false;
475
    mBlockController.uiUnblocked();
476
    }
477

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479

    
480
  public void preRender()
481
    {
482
    if( mSolve                 ) solveNow();
483
    if( mSetQuat               ) setQuatNow();
484
    if( mFinishRotation        ) finishRotationNow();
485
    if( mRemoveRotation        ) removeRotationNow();
486
    if( mRemovePatternRotation ) removePatternRotationNow();
487
    if( mChangeObject          ) changeObjectNow();
488
    if( mRecreateObject        ) recreateObjectNow();
489
    if( mSolveObject           ) solveObjectNow();
490
    if( mScrambleObject        ) scrambleObjectNow();
491
    if( mAddRotation           ) addRotationNow();
492
    if( mInitializeObject      ) initializeObjectNow();
493
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
494
    if( mSetTextureMap         ) setTextureMapNow();
495
    }
496

    
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498

    
499
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
500
    {
501
    mAddRotation = true;
502

    
503
    int basicAngle= mNewObject.getBasicAngle()[axis];
504
    int angle     = bareAngle*(360/basicAngle);
505
    int duration  = Math.abs(angle)*millPreDegree;
506

    
507
    mAddActionListener    = listener;
508
    mAddRotationAxis      = axis;
509
    mAddRotationRowBitmap = rowBitmap;
510
    mAddRotationAngle     = angle;
511
    mAddRotationDuration  = duration;
512

    
513
    if( listener instanceof ScrambleEffect )
514
      {
515
      mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
516
      }
517
    }
518

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520

    
521
  public void initializeObject(int[][] moves)
522
    {
523
    mInitializeObject = true;
524
    mNextMoves = moves;
525
    }
526

    
527
///////////////////////////////////////////////////////////////////////////////////////////////////
528

    
529
  public void scrambleObject(int num)
530
    {
531
    if( !mUIBlocked )
532
      {
533
      mScrambleObject = true;
534
      mScrambleObjectNum = num;
535
      mDebug = "";
536
      mDebugStartTime = System.currentTimeMillis();
537
      }
538
    }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541
// this starts the Solve Effect
542

    
543
  public void solveObject()
544
    {
545
    if( !mUIBlocked )
546
      {
547
      mSolveObject = true;
548
      }
549
    }
550

    
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552
// this only sets the cubits state to solved
553

    
554
  public void solveOnly()
555
    {
556
    mSolve = true;
557
    }
558

    
559
///////////////////////////////////////////////////////////////////////////////////////////////////
560

    
561
  public void resetAllTextureMaps()
562
    {
563
    mResetAllTextureMaps = true;
564
    }
565

    
566
///////////////////////////////////////////////////////////////////////////////////////////////////
567

    
568
  public TwistyObject getObject()
569
    {
570
    return mNewObject;
571
    }
572

    
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574

    
575
  public void effectFinished(final long effectID)
576
    {
577
    if( effectID == mRotationFinishedID )
578
      {
579
      mRotationFinishedID = 0;
580
      removeRotation();
581
      }
582
    else if( effectID == mAddRotationID )
583
      {
584
      mAddRotationID = 0;
585
      mRemoveRotationID = effectID;
586
      removePatternRotation();
587
      }
588
    else
589
      {
590
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
591
        {
592
        if( effectID == mEffectID[i] )
593
          {
594
          if( i!=BaseEffect.Type.WIN.ordinal() ) unblockEverything();
595
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() ) mInterface.onScrambleEffectFinished();
596
          if( i==BaseEffect.Type.WIN.ordinal()      ) mInterface.onWinEffectFinished(mDebug,mScrambleObjectNum);
597
          break;
598
          }
599
        }
600
      }
601
    }
602
  }
(8-8/15)