Project

General

Profile

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

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

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