Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectPreRender.java @ 092e257b

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