Project

General

Profile

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

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

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 android.content.SharedPreferences;
23
import android.content.res.Resources;
24

    
25
import org.distorted.objectlib.helpers.ObjectStateActioner;
26
import org.distorted.objectlib.effects.BaseEffect;
27
import org.distorted.objectlib.effects.EffectController;
28
import org.distorted.objectlib.effects.scramble.ScrambleEffect;
29
import org.distorted.objectlib.helpers.BlockController;
30
import org.distorted.objectlib.helpers.MovesFinished;
31
import org.distorted.objectlib.helpers.TwistyActivity;
32

    
33
import java.lang.ref.WeakReference;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
public class ObjectPreRender implements EffectController
38
  {
39
  private final WeakReference<TwistyActivity> mAct;
40
  private final ObjectControl mController;
41
  private ObjectType mNextObject;
42
  private TwistyObject mOldObject, mNewObject;
43
  private SharedPreferences mPreferences;
44
  private MovesFinished mAddActionListener;
45
  private final BlockController mBlockController;
46
  private final ObjectStateActioner mActioner;
47
  private String mDebug;
48

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

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  public ObjectPreRender(TwistyActivity act, ObjectControl controller, ObjectStateActioner actioner)
68
    {
69
    mAct = new WeakReference<>(act);
70
    mActioner = actioner;
71
    mController = controller;
72

    
73
    mFinishRotation       = false;
74
    mRemoveRotation       = false;
75
    mRemovePatternRotation= false;
76
    mAddRotation          = false;
77
    mSetQuat              = false;
78
    mChangeObject         = false;
79
    mSetupObject          = false;
80
    mSolveObject          = false;
81
    mSolve                = false;
82
    mScrambleObject       = false;
83

    
84
    mOldObject = null;
85
    mNewObject = null;
86

    
87
    mDebug = "";
88

    
89
    mScreenWidth = 0;
90
    mScrambleObjectNum = 0;
91

    
92
    mEffectID = new long[BaseEffect.Type.LENGTH];
93

    
94
    mBlockController = new BlockController(act);
95
    unblockEverything();
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  private void createObjectNow(ObjectType object, int[][] moves)
101
    {
102
    boolean firstTime = (mNewObject==null);
103

    
104
    if( mOldObject!=null ) mOldObject.releaseResources();
105
    mOldObject = mNewObject;
106

    
107
    TwistyActivity tact = mAct.get();
108

    
109
    Resources res = tact.getResources();
110

    
111
    mNewObject = object.create( mController.getQuat(), moves, res, mScreenWidth);
112

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

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

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  private void removeRotationNow()
141
    {
142
    mRemoveRotation=false;
143
    mNewObject.removeRotationNow();
144

    
145
    boolean solved = mNewObject.isSolved();
146

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

    
158
    mIsSolved = solved;
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  private void removeRotation()
164
    {
165
    mRemoveRotation = true;
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  private void removePatternRotation()
171
    {
172
    mRemovePatternRotation = true;
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

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

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

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

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

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

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

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

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  private void changeObjectNow()
215
    {
216
    mChangeObject = false;
217

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

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  private void setupObjectNow()
229
    {
230
    mSetupObject = false;
231

    
232
    if ( mNewObject==null || mNewObject.getObjectType()!=mNextObject)
233
      {
234
      blockEverything(BlockController.PLACE_2);
235
      createObjectNow(mNextObject, mNextMoves);
236
      doEffectNow( BaseEffect.Type.SIZECHANGE );
237
      }
238
    else
239
      {
240
      mNewObject.initializeObject(mNextMoves);
241
      }
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

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

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

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

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  private void solveNow()
266
    {
267
    mSolve = false;
268
    mNewObject.solve();
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

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

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  private void setTextureMapNow()
282
    {
283
    mSetTextureMap = false;
284

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

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

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

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

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

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

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

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

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

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

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

    
325
    mCubit    = cubit;
326
    mFace     = face;
327
    mNewColor = newColor;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
  void setQuatOnNextRender()
333
    {
334
    mSetQuat = true;
335
    }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338
// INTERNAL API
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340
// this only sets the cubits state to solved
341

    
342
  public void solve()
343
    {
344
    mSolve = true;
345
    }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

    
349
  public int getNumScrambles()
350
    {
351
    return mScrambleObjectNum;
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
  public TwistyObject getOldObject()
357
    {
358
    return mOldObject;
359
    }
360

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362
// PUBLIC API
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
  public void setScreenSize(int width)
366
    {
367
    if( mNewObject!=null )
368
      {
369
      mNewObject.createTexture();
370
      mNewObject.recomputeScaleFactor(width);
371
      }
372
    mScreenWidth = width;
373
    }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
  public void savePreferences(SharedPreferences.Editor editor)
378
    {
379
    if( mNewObject!=null )
380
      {
381
      mNewObject.savePreferences(editor);
382
      }
383
    }
384

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

    
387
  public void restorePreferences(SharedPreferences preferences)
388
    {
389
    mPreferences = preferences;
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
  public void changeObject(ObjectType object)
395
    {
396
    mChangeObject = true;
397
    mNextObject = object;
398
    }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
  public void setupObject(ObjectType object, int[][] moves)
403
    {
404
    mSetupObject= true;
405
    mNextObject = object;
406
    mNextMoves  = moves;
407
    }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

    
411
  public boolean isTouchBlocked()
412
    {
413
    return mTouchBlocked;
414
    }
415

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

    
418
  public boolean isUINotBlocked()
419
    {
420
    return !mUIBlocked;
421
    }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

    
425
  public void blockEverything(int place)
426
    {
427
    mUIBlocked   = true;
428
    mTouchBlocked= true;
429
    mBlockController.touchBlocked(place);
430
    mBlockController.uiBlocked(place);
431
    }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

    
435
  public void blockTouch(int place)
436
    {
437
    mTouchBlocked= true;
438
    mBlockController.touchBlocked(place);
439
    }
440

    
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442

    
443
  public void unblockEverything()
444
    {
445
    mUIBlocked   = false;
446
    mTouchBlocked= false;
447
    mBlockController.touchUnblocked();
448
    mBlockController.uiUnblocked();
449
    }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
  public void unblockTouch()
454
    {
455
    mTouchBlocked= false;
456
    mBlockController.touchUnblocked();
457
    }
458

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460

    
461
  public void unblockUI()
462
    {
463
    mUIBlocked= false;
464
    mBlockController.uiUnblocked();
465
    }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

    
469
  public void preRender()
470
    {
471
    if( mSolve                 ) solveNow();
472
    if( mSetQuat               ) setQuatNow();
473
    if( mFinishRotation        ) finishRotationNow();
474
    if( mRemoveRotation        ) removeRotationNow();
475
    if( mRemovePatternRotation ) removePatternRotationNow();
476
    if( mChangeObject          ) changeObjectNow();
477
    if( mSetupObject           ) setupObjectNow();
478
    if( mSolveObject           ) solveObjectNow();
479
    if( mScrambleObject        ) scrambleObjectNow();
480
    if( mAddRotation           ) addRotationNow();
481
    if( mInitializeObject      ) initializeObjectNow();
482
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
483
    if( mSetTextureMap         ) setTextureMapNow();
484
    }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, long duration)
489
    {
490
    mAddRotation = true;
491

    
492
    mAddActionListener    = listener;
493
    mAddRotationAxis      = axis;
494
    mAddRotationRowBitmap = rowBitmap;
495
    mAddRotationAngle     = angle;
496
    mAddRotationDuration  = duration;
497

    
498
    if( listener instanceof ScrambleEffect )
499
      {
500
      mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
501
      }
502
    }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
  public void initializeObject(int[][] moves)
507
    {
508
    mInitializeObject = true;
509
    mNextMoves = moves;
510
    }
511

    
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513

    
514
  public void scrambleObject(int num)
515
    {
516
    if( !mUIBlocked )
517
      {
518
      mScrambleObject = true;
519
      mScrambleObjectNum = num;
520
      mDebug = "";
521
      mDebugStartTime = System.currentTimeMillis();
522
      }
523
    }
524

    
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526
// this starts the Solve Effect
527

    
528
  public void solveObject()
529
    {
530
    if( !mUIBlocked )
531
      {
532
      mSolveObject = true;
533
      }
534
    }
535

    
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537

    
538
  public void resetAllTextureMaps()
539
    {
540
    mResetAllTextureMaps = true;
541
    }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544

    
545
  public TwistyObject getObject()
546
    {
547
    return mNewObject;
548
    }
549

    
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551

    
552
  public void effectFinished(final long effectID)
553
    {
554
    if( effectID == mRotationFinishedID )
555
      {
556
      mRotationFinishedID = 0;
557
      removeRotation();
558
      }
559
    else if( effectID == mAddRotationID )
560
      {
561
      mAddRotationID = 0;
562
      mRemoveRotationID = effectID;
563
      removePatternRotation();
564
      }
565
    else
566
      {
567
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
568
        {
569
        if( effectID == mEffectID[i] )
570
          {
571
          if( i!=BaseEffect.Type.WIN.ordinal() ) unblockEverything();
572
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() ) mActioner.onScrambleEffectFinished(mAct.get());
573
          if( i==BaseEffect.Type.WIN.ordinal()      ) mActioner.onWinEffectFinished(mAct.get(),mDebug,mScrambleObjectNum);
574
          break;
575
          }
576
        }
577
      }
578
    }
579
  }
(8-8/15)