Project

General

Profile

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

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

1 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 4c87f159 Leszek Koltunski
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.objectlib.main;
11
12 b3fff6fb Leszek Koltunski
import android.app.Activity;
13 7c111294 Leszek Koltunski
import android.content.SharedPreferences;
14
15 ecf3d6e3 Leszek Koltunski
import org.distorted.library.message.EffectListener;
16 02d80fe6 Leszek Koltunski
import org.distorted.library.type.Static3D;
17 82eb152a Leszek Koltunski
18 e1a86bf2 Leszek Koltunski
import org.distorted.library.type.Static4D;
19 b3fff6fb Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectLibInterface;
20 7c111294 Leszek Koltunski
import org.distorted.objectlib.effects.BaseEffect;
21
import org.distorted.objectlib.effects.scramble.ScrambleEffect;
22
import org.distorted.objectlib.helpers.BlockController;
23
import org.distorted.objectlib.helpers.MovesFinished;
24 b31249d6 Leszek Koltunski
import org.distorted.objectlib.shape.ShapeDodecahedron;
25
import org.distorted.objectlib.shape.ShapeHexahedron;
26
import org.distorted.objectlib.shape.ShapeOctahedron;
27
import org.distorted.objectlib.shape.ShapeTetrahedron;
28 15e5214c Leszek Koltunski
29 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
30
31 ecf3d6e3 Leszek Koltunski
public class ObjectPreRender implements EffectListener
32 7c111294 Leszek Koltunski
  {
33 e8d8c12a Leszek Koltunski
  private static final int MAX_SLOW_SCRAMBLE = 50;
34
35 15e5214c Leszek Koltunski
  private final ObjectControl mController;
36 cf93ea4e Leszek Koltunski
  private InitAssets mAsset;
37 e1a86bf2 Leszek Koltunski
  private int mOrdinal;
38 7c111294 Leszek Koltunski
  private TwistyObject mOldObject, mNewObject;
39
  private SharedPreferences mPreferences;
40
  private MovesFinished mAddActionListener;
41
  private final BlockController mBlockController;
42 b3fff6fb Leszek Koltunski
  private final ObjectLibInterface mInterface;
43 7c111294 Leszek Koltunski
  private String mDebug;
44 72d6857c Leszek Koltunski
  private float mMoveX, mMoveY;
45 64c209f5 Leszek Koltunski
  private float mScale;
46 7c111294 Leszek Koltunski
47
  private boolean mFinishRotation, mRemoveRotation, mRemovePatternRotation, mAddRotation,
48 4c2c0f44 Leszek Koltunski
                  mSetQuat, mChangeObject, mSolveObject, mScrambleObject, mFastScrambleObject,
49
                  mPresentObject,mInitializeObject, mSetTextureMap, mResetAllTextureMaps, mSolve,
50
                  mApplyScrambles, mResetTextureEffect;
51 7205c655 Leszek Koltunski
  private boolean mScramblingAndSolvingBlocked, mRotationBlocked, mIsSolved;
52 7c111294 Leszek Koltunski
  private long mRotationFinishedID;
53
  private final long[] mEffectID;
54
  private int[][] mNextMoves;
55 f7e30f36 Leszek Koltunski
  private int mScrambleObjectNum, mScrambleObjectDuration;
56 7c111294 Leszek Koltunski
  private int mAddRotationAxis, mAddRotationRowBitmap, mAddRotationAngle;
57
  private long mAddRotationDuration;
58 4c2c0f44 Leszek Koltunski
  private int mPresentDuration, mRestickerDuration;
59 7c111294 Leszek Koltunski
  private long mAddRotationID, mRemoveRotationID;
60
  private int mCubit, mFace, mNewColor;
61
  private int mNearestAngle;
62 adb3a093 Leszek Koltunski
  private long mScrambleStartTime, mScrambleEndTime;
63 3bf19410 Leszek Koltunski
  private int mMeshState, mIconMode;
64 7c111294 Leszek Koltunski
65 8c069e23 Leszek Koltunski
  // debugging only
66
  private long mAddRotationTime;
67
68 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70 b3fff6fb Leszek Koltunski
  public ObjectPreRender(Activity act, ObjectControl controller, ObjectLibInterface actioner)
71 7c111294 Leszek Koltunski
    {
72 b3fff6fb Leszek Koltunski
    mInterface = actioner;
73 15e5214c Leszek Koltunski
    mController = controller;
74 7c111294 Leszek Koltunski
75
    mFinishRotation       = false;
76
    mRemoveRotation       = false;
77
    mRemovePatternRotation= false;
78
    mAddRotation          = false;
79
    mSetQuat              = false;
80
    mChangeObject         = false;
81
    mSolveObject          = false;
82
    mSolve                = false;
83
    mScrambleObject       = false;
84 186bc982 Leszek Koltunski
    mFastScrambleObject   = false;
85 826d293e Leszek Koltunski
    mPresentObject        = false;
86 4c2c0f44 Leszek Koltunski
    mResetTextureEffect   = false;
87 7c111294 Leszek Koltunski
88
    mOldObject = null;
89
    mNewObject = null;
90
91
    mDebug = "";
92
    mScrambleObjectNum = 0;
93 64c209f5 Leszek Koltunski
    mScale = 1.0f;
94 7c111294 Leszek Koltunski
95
    mEffectID = new long[BaseEffect.Type.LENGTH];
96
97 9276dced Leszek Koltunski
    mBlockController = new BlockController(act,this);
98 7c111294 Leszek Koltunski
    unblockEverything();
99
    }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103 cf93ea4e Leszek Koltunski
  private void createObjectNow(int ordinal, int meshState, int iconMode, InitAssets assets)
104 7c111294 Leszek Koltunski
    {
105 3ce95490 Leszek Koltunski
    long time1 = System.currentTimeMillis();
106 72d6857c Leszek Koltunski
    Static3D move = new Static3D(mMoveX,mMoveY,0);
107 e1a86bf2 Leszek Koltunski
    Static4D quat = mController.getQuat();
108 3ce95490 Leszek Koltunski
    TwistyObject tmp;
109
    boolean error = false;
110 7c111294 Leszek Koltunski
111 cf93ea4e Leszek Koltunski
    if( assets==null || assets.noJsonStream() )
112 3ce95490 Leszek Koltunski
      {
113 cf93ea4e Leszek Koltunski
      tmp = ObjectType.create( ordinal, meshState, iconMode, quat, move, mScale, assets);
114 3ce95490 Leszek Koltunski
      }
115
    else
116
      {
117 cf93ea4e Leszek Koltunski
      tmp = new TwistyJson( meshState, iconMode, quat, move, mScale, assets);
118 3ce95490 Leszek Koltunski
      error = tmp.getError();
119
      }
120 594bbce0 Leszek Koltunski
121 3ce95490 Leszek Koltunski
    if( error )
122
      {
123
      String errorString = tmp.getErrorString();
124
      mInterface.reportJSONError(errorString,ordinal);
125
      }
126
    else
127 7c111294 Leszek Koltunski
      {
128 3ce95490 Leszek Koltunski
      if( mOldObject!=null ) mOldObject.releaseResources();
129
      mOldObject = mNewObject;
130
      mNewObject = tmp;
131
132
      long time2 = System.currentTimeMillis();
133
      mInterface.onObjectCreated(time2-time1);
134 9a694085 Leszek Koltunski
135 3ce95490 Leszek Koltunski
      if( mNewObject!=null )
136 9a694085 Leszek Koltunski
        {
137 3ce95490 Leszek Koltunski
        mNewObject.setLibInterface(mInterface);
138
        mController.setTouchControl(mNewObject);
139 30bd2f96 Leszek Koltunski
        mNewObject.setObjectRatioNow(mScale, mController.getScalingSize() );
140 3ce95490 Leszek Koltunski
141
        if( mPreferences!=null )
142
          {
143
          mNewObject.restorePreferences(mPreferences);
144
          mPreferences = null;
145
          }
146
147
        mIsSolved = mNewObject.isSolved();
148 9a694085 Leszek Koltunski
        }
149 7c111294 Leszek Koltunski
      }
150
    }
151
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
154
155 b1370e3b Leszek Koltunski
  private void doEffectNow(BaseEffect.Type type, int duration)
156 7c111294 Leszek Koltunski
    {
157
    try
158
      {
159
      int index = type.ordinal();
160 b1370e3b Leszek Koltunski
      mEffectID[index] = type.startEffect(this,duration);
161 7c111294 Leszek Koltunski
      }
162
    catch( Exception ex )
163
      {
164
      android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
165
      unblockEverything();
166
      }
167
    }
168
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171
  private void removeRotationNow()
172
    {
173
    mRemoveRotation=false;
174
    mNewObject.removeRotationNow();
175
176
    boolean solved = mNewObject.isSolved();
177
178
    if( solved && !mIsSolved )
179
      {
180 b3fff6fb Leszek Koltunski
      mInterface.onSolved();
181 7c111294 Leszek Koltunski
      unblockEverything();
182 b1370e3b Leszek Koltunski
      int duration = BaseEffect.Type.WIN.getDuration();
183
      doEffectNow( BaseEffect.Type.WIN, duration );
184 7c111294 Leszek Koltunski
      }
185
    else
186
      {
187
      unblockEverything();
188
      }
189
190
    mIsSolved = solved;
191
    }
192
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194
195
  private void removeRotation()
196
    {
197
    mRemoveRotation = true;
198
    }
199
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
202
  private void removePatternRotation()
203
    {
204
    mRemovePatternRotation = true;
205
    }
206
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
209
  private void removePatternRotationNow()
210
    {
211
    mRemovePatternRotation=false;
212
    mNewObject.removeRotationNow();
213
    mAddActionListener.onActionFinished(mRemoveRotationID);
214
    }
215
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217
218
  private void addRotationNow()
219
    {
220
    mAddRotation = false;
221
222 d85de775 Leszek Koltunski
    if( mNewObject.getNumAxis() > mAddRotationAxis )
223 8c069e23 Leszek Koltunski
      {
224
      mAddRotationID = mNewObject.addNewRotation( mAddRotationAxis, mAddRotationRowBitmap,
225
                                                  mAddRotationAngle, mAddRotationDuration, this);
226
227 605f319b Leszek Koltunski
      // failed to add effect (previous rotation hasn't been removed yet)
228
      if( mAddRotationID==0 )
229
        {
230
        mAddActionListener.onActionFinished(0);
231
        }
232
      // the rotation we have just added has forced a finish() of an ongoing
233
      // manual rotation. So the effect that's going to finish is not the manual
234
      // rotation (we have just removed it) but the rotation we have just added
235
      // here - so set mRotationFinishedID to 0
236
      else if( mAddRotationID<0 )
237
        {
238
        mAddRotationID = -mAddRotationID;
239
        mRotationFinishedID = 0;
240 8c069e23 Leszek Koltunski
        }
241
      }
242
    else // should never happen but Firebase says it sometimes does
243 7c111294 Leszek Koltunski
      {
244 8c069e23 Leszek Koltunski
      long timeNow = System.currentTimeMillis();
245
      Class<? extends MovesFinished> clazz = mAddActionListener.getClass();
246
      String name = clazz.getSimpleName();
247
248
      String error = "time now: "+timeNow+" add time: "+mAddRotationTime+" axis="+mAddRotationAxis+
249
                      "object: "+mNewObject.getShortName()+" "+name;
250
251 32c1697e Leszek Koltunski
      mInterface.reportProblem(error,true);
252 7c111294 Leszek Koltunski
      unblockEverything();
253
      }
254
    }
255
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257
258
  private void finishRotationNow()
259
    {
260
    mFinishRotation = false;
261 b78ebd76 Leszek Koltunski
    blockEverything(BlockController.PLACE_0);
262 7c111294 Leszek Koltunski
    mRotationFinishedID = mNewObject.finishRotationNow(this, mNearestAngle);
263
264
    if( mRotationFinishedID==0 ) // failed to add effect - should never happen
265
      {
266
      unblockEverything();
267
      }
268
    }
269
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
272
  private void changeObjectNow()
273
    {
274
    mChangeObject = false;
275 e1a86bf2 Leszek Koltunski
    blockEverything(BlockController.PLACE_1);
276 cf93ea4e Leszek Koltunski
    createObjectNow(mOrdinal,mMeshState,mIconMode,mAsset);
277 b1370e3b Leszek Koltunski
    int duration = BaseEffect.Type.SIZECHANGE.getDuration();
278
    doEffectNow( BaseEffect.Type.SIZECHANGE, duration );
279 568d4698 Leszek Koltunski
    }
280
281 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
282
283
  private void scrambleObjectNow()
284
    {
285
    mScrambleObject = false;
286
    mIsSolved       = false;
287 b78ebd76 Leszek Koltunski
    blockEverything(BlockController.PLACE_3);
288 e8d8c12a Leszek Koltunski
289
    if( mScrambleObjectNum<MAX_SLOW_SCRAMBLE )
290
      {
291
      int duration = BaseEffect.Type.SCRAMBLE.getDuration();
292
      doEffectNow( BaseEffect.Type.SCRAMBLE, duration );
293
      }
294
    else
295
      {
296
      int duration = BaseEffect.Type.FAST_SCRAMBLE.getDuration();
297
      doEffectNow( BaseEffect.Type.FAST_SCRAMBLE, duration );
298
      }
299 7c111294 Leszek Koltunski
    }
300
301 186bc982 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
302
303
  private void fastScrambleObjectNow()
304
    {
305
    mFastScrambleObject = false;
306
    mIsSolved           = false;
307
    blockEverything(BlockController.PLACE_5);
308 f7e30f36 Leszek Koltunski
    doEffectNow( BaseEffect.Type.FAST_SCRAMBLE, mScrambleObjectDuration );
309 186bc982 Leszek Koltunski
    }
310
311 4c2c0f44 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
312
313
  private void resetTextureNow()
314
    {
315
    mResetTextureEffect = false;
316
    doEffectNow( BaseEffect.Type.RESTICKER, mRestickerDuration );
317
    }
318
319 826d293e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
320
321
  private void presentObjectNow()
322
    {
323
    mPresentObject = false;
324 b1370e3b Leszek Koltunski
    doEffectNow( BaseEffect.Type.PRESENT, mPresentDuration );
325 826d293e Leszek Koltunski
    }
326
327 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
328
329
  private void solveObjectNow()
330
    {
331
    mSolveObject = false;
332 b78ebd76 Leszek Koltunski
    blockEverything(BlockController.PLACE_4);
333 b1370e3b Leszek Koltunski
    int duration = BaseEffect.Type.SOLVE.getDuration();
334
    doEffectNow( BaseEffect.Type.SOLVE, duration );
335 7c111294 Leszek Koltunski
    }
336
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338
339
  private void solveNow()
340
    {
341
    mSolve = false;
342 a57e6870 Leszek Koltunski
    if( mNewObject!=null ) mNewObject.solve();
343 7c111294 Leszek Koltunski
    }
344
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346
347
  private void initializeObjectNow()
348
    {
349
    mInitializeObject = false;
350
    mNewObject.initializeObject(mNextMoves);
351
    }
352
353 d356eecc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
354
355
  private void applyScramblesNow()
356
    {
357
    mApplyScrambles = false;
358
    mNewObject.applyScrambles(mNextMoves);
359
    }
360
361 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
362
363
  private void setTextureMapNow()
364
    {
365
    mSetTextureMap = false;
366
367
    if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
368
    }
369
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371
372
  private void resetAllTextureMapsNow()
373
    {
374
    mResetAllTextureMaps = false;
375
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
376
    }
377
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379
380
  private void setQuatNow()
381
    {
382
    mSetQuat = false;
383 15e5214c Leszek Koltunski
    mController.setQuat();
384 7c111294 Leszek Koltunski
    }
385
386 beee90ab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
387
388
  private int computeRowFromBitmap(int rowBitmap)
389
    {
390
    int index = 0;
391
392
    while(index<32)
393
      {
394
      if( (rowBitmap&0x1) != 0 ) return index;
395
      rowBitmap>>=1;
396
      index++;
397
      }
398
399
    return 0;
400
    }
401
402 6b301f56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
403
404
  private void blockEverything(int place)
405
    {
406 7205c655 Leszek Koltunski
    mScramblingAndSolvingBlocked = true;
407
    mRotationBlocked = true;
408
    mBlockController.rotationBlocked(place);
409
    mBlockController.scramblingAndSolvingBlocked(place);
410 6b301f56 Leszek Koltunski
    }
411
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413 060b445e Leszek Koltunski
// called from ObjectControl; this from app when we switch the screen to READ post-scrambling.
414
// The point: we only unblock having completed the screen switch so that it is impossible to
415
// click the solve button then.
416 6b301f56 Leszek Koltunski
417 060b445e Leszek Koltunski
  void unblockEverything()
418 6b301f56 Leszek Koltunski
    {
419 7205c655 Leszek Koltunski
    mScramblingAndSolvingBlocked = false;
420
    mRotationBlocked = false;
421
    mBlockController.rotationUnblocked();
422
    mBlockController.scramblingAndSolvingUnblocked();
423 6b301f56 Leszek Koltunski
    }
424
425 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
426
427 15e5214c Leszek Koltunski
  void rememberMove(int axis, int row, int angle)
428 7c111294 Leszek Koltunski
    {
429 93743a22 Leszek Koltunski
    mDebug += (mNewObject==null ? "[null]" : mNewObject.reportState() );
430
    mDebug += ("(m "+axis+" "+(1<<row)+" "+angle+" "+(System.currentTimeMillis()-mScrambleEndTime))+")";
431 7c111294 Leszek Koltunski
    }
432
433 15e5214c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
434
435
  void finishRotation(int nearestAngle)
436
    {
437
    mNearestAngle   = nearestAngle;
438
    mFinishRotation = true;
439
    }
440
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442
443
  void setTextureMap(int cubit, int face, int newColor)
444
    {
445
    mSetTextureMap = true;
446
447
    mCubit    = cubit;
448
    mFace     = face;
449
    mNewColor = newColor;
450
    }
451
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453
454
  void setQuatOnNextRender()
455
    {
456
    mSetQuat = true;
457
    }
458
459 02d80fe6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
460
461 72d6857c Leszek Koltunski
  void setMove(float xmove, float ymove)
462 02d80fe6 Leszek Koltunski
    {
463 72d6857c Leszek Koltunski
    mMoveX = xmove;
464
    mMoveY = ymove;
465 02d80fe6 Leszek Koltunski
    }
466
467 64c209f5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
468
469
  void setScale(float scale)
470
    {
471
    mScale = scale;
472
    }
473
474 15e5214c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
475
// INTERNAL API
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477
478
  public int getNumScrambles()
479
    {
480
    return mScrambleObjectNum;
481
    }
482
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484
485
  public TwistyObject getOldObject()
486
    {
487
    return mOldObject;
488
    }
489
490 02d80fe6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
491
492 72d6857c Leszek Koltunski
  public float getMoveX()
493 02d80fe6 Leszek Koltunski
    {
494 72d6857c Leszek Koltunski
    return mMoveX;
495
    }
496
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498
499
  public float getMoveY()
500
    {
501
    return mMoveY;
502 02d80fe6 Leszek Koltunski
    }
503
504 d887aa16 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
505
506
  public ObjectLibInterface getInterface()
507
    {
508
    return mInterface;
509
    }
510
511 15e5214c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
512
// PUBLIC API
513 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
514
515
  public void savePreferences(SharedPreferences.Editor editor)
516
    {
517
    if( mNewObject!=null )
518
      {
519
      mNewObject.savePreferences(editor);
520
      }
521
    }
522
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524
525
  public void restorePreferences(SharedPreferences preferences)
526
    {
527
    mPreferences = preferences;
528
    }
529
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531
532 cf93ea4e Leszek Koltunski
  public void changeObject(int ordinal, int meshState, int iconMode, InitAssets asset)
533 7c111294 Leszek Koltunski
    {
534
    mChangeObject = true;
535 e1a86bf2 Leszek Koltunski
    mOrdinal    = ordinal;
536 c3a033e9 Leszek Koltunski
    mMeshState  = meshState;
537 3bf19410 Leszek Koltunski
    mIconMode   = iconMode;
538 cf93ea4e Leszek Koltunski
    mAsset      = asset;
539 568d4698 Leszek Koltunski
    }
540
541 4a389a4e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
542
543
  public void setDefaultRotation(int numFaces)
544
    {
545 66959ff9 Leszek Koltunski
    if( mController!=null && mController.getRotateOnCreation() )
546 4a389a4e Leszek Koltunski
      {
547
      switch(numFaces)
548
        {
549
        case  4: mController.rotateNow(ShapeTetrahedron.DEFAULT_ROT ); break;
550
        case  6: mController.rotateNow(ShapeHexahedron.DEFAULT_ROT  ); break;
551
        case  8: mController.rotateNow(ShapeOctahedron.DEFAULT_ROT  ); break;
552
        case 12: mController.rotateNow(ShapeDodecahedron.DEFAULT_ROT); break;
553
        }
554
      }
555
    }
556
557 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
558
559 7205c655 Leszek Koltunski
  public boolean isRotationBlocked()
560 7c111294 Leszek Koltunski
    {
561 7205c655 Leszek Koltunski
    return mRotationBlocked;
562 7c111294 Leszek Koltunski
    }
563
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565
566 7205c655 Leszek Koltunski
  public boolean isScramblingAndSolvingNotBlocked()
567 7c111294 Leszek Koltunski
    {
568 7205c655 Leszek Koltunski
    return !mScramblingAndSolvingBlocked;
569 7c111294 Leszek Koltunski
    }
570
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572
573 7205c655 Leszek Koltunski
  public void blockRotation(int place)
574 7c111294 Leszek Koltunski
    {
575 7205c655 Leszek Koltunski
    mRotationBlocked = true;
576
    mBlockController.rotationBlocked(place);
577 7c111294 Leszek Koltunski
    }
578
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580
581 7205c655 Leszek Koltunski
  public void unblockRotation()
582 7c111294 Leszek Koltunski
    {
583 7205c655 Leszek Koltunski
    mRotationBlocked = false;
584
    mBlockController.rotationUnblocked();
585 7c111294 Leszek Koltunski
    }
586
587
///////////////////////////////////////////////////////////////////////////////////////////////////
588
589 7205c655 Leszek Koltunski
  public void unblockScramblingAndSolving()
590 7c111294 Leszek Koltunski
    {
591 7205c655 Leszek Koltunski
    mScramblingAndSolvingBlocked = false;
592
    mBlockController.scramblingAndSolvingUnblocked();
593 7c111294 Leszek Koltunski
    }
594
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596
597
  public void preRender()
598
    {
599
    if( mSolve                 ) solveNow();
600
    if( mSetQuat               ) setQuatNow();
601
    if( mFinishRotation        ) finishRotationNow();
602
    if( mRemoveRotation        ) removeRotationNow();
603
    if( mRemovePatternRotation ) removePatternRotationNow();
604
    if( mChangeObject          ) changeObjectNow();
605
    if( mSolveObject           ) solveObjectNow();
606
    if( mScrambleObject        ) scrambleObjectNow();
607 186bc982 Leszek Koltunski
    if( mFastScrambleObject    ) fastScrambleObjectNow();
608 826d293e Leszek Koltunski
    if( mPresentObject         ) presentObjectNow();
609 7c111294 Leszek Koltunski
    if( mAddRotation           ) addRotationNow();
610
    if( mInitializeObject      ) initializeObjectNow();
611 d356eecc Leszek Koltunski
    if( mApplyScrambles        ) applyScramblesNow();
612 7c111294 Leszek Koltunski
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
613
    if( mSetTextureMap         ) setTextureMapNow();
614 4c2c0f44 Leszek Koltunski
    if( mResetTextureEffect    ) resetTextureNow();
615 7c111294 Leszek Koltunski
    }
616
617
///////////////////////////////////////////////////////////////////////////////////////////////////
618
619 2df35810 Leszek Koltunski
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
620 7c111294 Leszek Koltunski
    {
621 3a0a23bf Leszek Koltunski
    int[][] basicAngles = mNewObject==null ? null : mNewObject.getBasicAngles();
622
    int length = basicAngles==null ? 0 : basicAngles.length;
623 7c111294 Leszek Koltunski
624 3a0a23bf Leszek Koltunski
    if( axis<length )
625 7c111294 Leszek Koltunski
      {
626 0eff8c76 Leszek Koltunski
      int row = computeRowFromBitmap(rowBitmap);
627 a1bcb301 Leszek Koltunski
628 0eff8c76 Leszek Koltunski
      if( row<basicAngles[axis].length )
629 a1bcb301 Leszek Koltunski
        {
630 0eff8c76 Leszek Koltunski
        int basicAngle= basicAngles[axis][row];
631
        int angle     = bareAngle*(360/basicAngle);
632
        int duration  = Math.abs(angle)*millPreDegree;
633
634
        mAddActionListener    = listener;
635
        mAddRotationAxis      = axis;
636
        mAddRotationRowBitmap = rowBitmap;
637
        mAddRotationAngle     = angle;
638
        mAddRotationDuration  = duration;
639
        mAddRotationTime      = System.currentTimeMillis();
640 605f319b Leszek Koltunski
        mAddRotation          = true;
641 0eff8c76 Leszek Koltunski
642
        if( listener instanceof ScrambleEffect )
643
          {
644 11d8426d Leszek Koltunski
          mDebug += (mNewObject==null ? "null" : mNewObject.reportState() );
645 93743a22 Leszek Koltunski
          mDebug += ("(a "+axis+" "+rowBitmap+" "+angle+" "+(mAddRotationTime-mScrambleStartTime))+")";
646 0eff8c76 Leszek Koltunski
          }
647 a1bcb301 Leszek Koltunski
        }
648 7c111294 Leszek Koltunski
      }
649
    }
650
651
///////////////////////////////////////////////////////////////////////////////////////////////////
652
653
  public void initializeObject(int[][] moves)
654
    {
655
    mInitializeObject = true;
656
    mNextMoves = moves;
657
    }
658
659 d356eecc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
660
661
  public void applyScrambles(int[][] moves)
662
    {
663
    mApplyScrambles = true;
664
    mNextMoves = moves;
665
    }
666
667 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
668
669 36f6390b Leszek Koltunski
  public boolean scrambleObject(int num)
670 7c111294 Leszek Koltunski
    {
671 7205c655 Leszek Koltunski
    if( !mScramblingAndSolvingBlocked )
672 7c111294 Leszek Koltunski
      {
673
      mScrambleObject = true;
674
      mScrambleObjectNum = num;
675
      mDebug = "";
676 adb3a093 Leszek Koltunski
      mScrambleStartTime = System.currentTimeMillis();
677 36f6390b Leszek Koltunski
      return true;
678 7c111294 Leszek Koltunski
      }
679 36f6390b Leszek Koltunski
    return false;
680 7c111294 Leszek Koltunski
    }
681
682 186bc982 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
683
684 36f6390b Leszek Koltunski
  public boolean fastScrambleObject(int duration, int num)
685 186bc982 Leszek Koltunski
    {
686 7205c655 Leszek Koltunski
    if( !mScramblingAndSolvingBlocked )
687 186bc982 Leszek Koltunski
      {
688
      mFastScrambleObject = true;
689
      mScrambleObjectNum = num;
690 f7e30f36 Leszek Koltunski
      mScrambleObjectDuration = duration;
691 186bc982 Leszek Koltunski
      mDebug = "";
692
      mScrambleStartTime = System.currentTimeMillis();
693 36f6390b Leszek Koltunski
      return true;
694 186bc982 Leszek Koltunski
      }
695 36f6390b Leszek Koltunski
    return false;
696 186bc982 Leszek Koltunski
    }
697
698 826d293e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
699
700 b1370e3b Leszek Koltunski
  public void presentObject(int num, int duration)
701 826d293e Leszek Koltunski
    {
702
    mScrambleObjectNum = num;
703 b1370e3b Leszek Koltunski
    mPresentDuration = duration;
704 826d293e Leszek Koltunski
    mPresentObject = true;
705
    }
706
707 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
708
// this starts the Solve Effect
709
710
  public void solveObject()
711
    {
712 7205c655 Leszek Koltunski
    if( !mScramblingAndSolvingBlocked )
713 7c111294 Leszek Koltunski
      {
714
      mSolveObject = true;
715
      }
716
    }
717
718 17d623f1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
719
// this only sets the cubits state to solved
720
721
  public void solveOnly()
722
    {
723
    mSolve = true;
724
    }
725
726 4c2c0f44 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
727
728
  public void resetTextureMapsEffect(int duration)
729
    {
730
    mRestickerDuration = duration;
731
    mResetTextureEffect = true;
732
    }
733
734 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
735
736
  public void resetAllTextureMaps()
737
    {
738
    mResetAllTextureMaps = true;
739
    }
740
741
///////////////////////////////////////////////////////////////////////////////////////////////////
742
743
  public TwistyObject getObject()
744
    {
745
    return mNewObject;
746
    }
747
748 7ba38dd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
749
750
  public TwistyObjectNode getObjectNode()
751
    {
752
    return mController.getNode();
753
    }
754
755 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
756
757
  public void effectFinished(final long effectID)
758
    {
759
    if( effectID == mRotationFinishedID )
760
      {
761
      mRotationFinishedID = 0;
762
      removeRotation();
763
      }
764
    else if( effectID == mAddRotationID )
765
      {
766
      mAddRotationID = 0;
767
      mRemoveRotationID = effectID;
768
      removePatternRotation();
769
      }
770
    else
771
      {
772
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
773
        {
774
        if( effectID == mEffectID[i] )
775
          {
776 adb3a093 Leszek Koltunski
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() )
777
            {
778
            mScrambleEndTime = System.currentTimeMillis();
779
            mInterface.onScrambleEffectFinished();
780
            }
781 060b445e Leszek Koltunski
          else if( i==BaseEffect.Type.WIN.ordinal()      )
782 adb3a093 Leszek Koltunski
            {
783
            mInterface.onWinEffectFinished(mScrambleStartTime,mScrambleEndTime-mScrambleStartTime,mDebug,mScrambleObjectNum);
784
            }
785 060b445e Leszek Koltunski
          else
786
            {
787
            unblockEverything();
788
            }
789
790 7c111294 Leszek Koltunski
          break;
791
          }
792
        }
793
      }
794
    }
795
  }