Project

General

Profile

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

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

1 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.objectlib.main;
21
22 81141862 Leszek Koltunski
import java.lang.ref.WeakReference;
23
24 b3fff6fb Leszek Koltunski
import android.app.Activity;
25 7c111294 Leszek Koltunski
import android.content.SharedPreferences;
26
27 ecf3d6e3 Leszek Koltunski
import org.distorted.library.message.EffectListener;
28 81141862 Leszek Koltunski
29 02d80fe6 Leszek Koltunski
import org.distorted.library.type.Static3D;
30 b3fff6fb Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectLibInterface;
31 7c111294 Leszek Koltunski
import org.distorted.objectlib.effects.BaseEffect;
32
import org.distorted.objectlib.effects.scramble.ScrambleEffect;
33
import org.distorted.objectlib.helpers.BlockController;
34
import org.distorted.objectlib.helpers.MovesFinished;
35 15e5214c Leszek Koltunski
36 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
37
38 ecf3d6e3 Leszek Koltunski
public class ObjectPreRender implements EffectListener
39 7c111294 Leszek Koltunski
  {
40 b3fff6fb Leszek Koltunski
  private final WeakReference<Activity> mAct;
41 15e5214c Leszek Koltunski
  private final ObjectControl mController;
42 7c111294 Leszek Koltunski
  private ObjectType mNextObject;
43
  private TwistyObject mOldObject, mNewObject;
44
  private SharedPreferences mPreferences;
45
  private MovesFinished mAddActionListener;
46
  private final BlockController mBlockController;
47 b3fff6fb Leszek Koltunski
  private final ObjectLibInterface mInterface;
48 7c111294 Leszek Koltunski
  private String mDebug;
49 72d6857c Leszek Koltunski
  private float mMoveX, mMoveY;
50 7c111294 Leszek Koltunski
51
  private boolean mFinishRotation, mRemoveRotation, mRemovePatternRotation, mAddRotation,
52 568d4698 Leszek Koltunski
                  mSetQuat, mChangeObject, mSolveObject, mScrambleObject, mRecreateObject,
53 7c111294 Leszek Koltunski
                  mInitializeObject, mSetTextureMap, mResetAllTextureMaps, mSolve;
54
  private boolean mUIBlocked, mTouchBlocked, mIsSolved;
55
  private long mRotationFinishedID;
56
  private final long[] mEffectID;
57
  private int[][] mNextMoves;
58
  private int mScrambleObjectNum;
59
  private int mAddRotationAxis, mAddRotationRowBitmap, mAddRotationAngle;
60
  private long mAddRotationDuration;
61
  private long mAddRotationID, mRemoveRotationID;
62
  private int mCubit, mFace, mNewColor;
63
  private int mNearestAngle;
64
  private long mDebugStartTime;
65
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68 b3fff6fb Leszek Koltunski
  public ObjectPreRender(Activity act, ObjectControl controller, ObjectLibInterface actioner)
69 7c111294 Leszek Koltunski
    {
70 15e5214c Leszek Koltunski
    mAct = new WeakReference<>(act);
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 568d4698 Leszek Koltunski
    mRecreateObject       = false;
81 7c111294 Leszek Koltunski
    mSolveObject          = false;
82
    mSolve                = false;
83
    mScrambleObject       = false;
84
85
    mOldObject = null;
86
    mNewObject = null;
87
88
    mDebug = "";
89
    mScrambleObjectNum = 0;
90
91
    mEffectID = new long[BaseEffect.Type.LENGTH];
92
93 9276dced Leszek Koltunski
    mBlockController = new BlockController(act,this);
94 7c111294 Leszek Koltunski
    unblockEverything();
95
    }
96
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99 81141862 Leszek Koltunski
  private void createObjectNow(ObjectType object)
100 7c111294 Leszek Koltunski
    {
101
    boolean firstTime = (mNewObject==null);
102
103
    if( mOldObject!=null ) mOldObject.releaseResources();
104
    mOldObject = mNewObject;
105 72d6857c Leszek Koltunski
    Static3D move = new Static3D(mMoveX,mMoveY,0);
106 7c111294 Leszek Koltunski
107 7ba38dd4 Leszek Koltunski
    mNewObject = object.create( mController.getQuat(), move, mAct.get().getResources());
108 7c111294 Leszek Koltunski
109
    if( mNewObject!=null )
110
      {
111 e32d318a Leszek Koltunski
      TwistyObjectNode node = mController.getNode();
112
      if( node!=null ) mNewObject.setObjectRatioNow( 1.0f, node.getScaleFactor() );
113 15e5214c Leszek Koltunski
      mController.setMovement(mNewObject.getMovement());
114 b09dd39b Leszek Koltunski
      if( firstTime && mPreferences!=null ) mNewObject.restorePreferences(mPreferences);
115 7c111294 Leszek Koltunski
      mIsSolved = mNewObject.isSolved();
116
      }
117
    }
118
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
121
122
  private void doEffectNow(BaseEffect.Type type)
123
    {
124
    try
125
      {
126
      int index = type.ordinal();
127 7ba38dd4 Leszek Koltunski
      mEffectID[index] = type.startEffect(this);
128 7c111294 Leszek Koltunski
      }
129
    catch( Exception ex )
130
      {
131
      android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
132
      unblockEverything();
133
      }
134
    }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
138
  private void removeRotationNow()
139
    {
140
    mRemoveRotation=false;
141
    mNewObject.removeRotationNow();
142
143
    boolean solved = mNewObject.isSolved();
144
145
    if( solved && !mIsSolved )
146
      {
147 b3fff6fb Leszek Koltunski
      mInterface.onSolved();
148 7c111294 Leszek Koltunski
      unblockEverything();
149
      doEffectNow( BaseEffect.Type.WIN );
150
      }
151
    else
152
      {
153
      unblockEverything();
154
      }
155
156
    mIsSolved = solved;
157
    }
158
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161
  private void removeRotation()
162
    {
163
    mRemoveRotation = true;
164
    }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168
  private void removePatternRotation()
169
    {
170
    mRemovePatternRotation = true;
171
    }
172
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
175
  private void removePatternRotationNow()
176
    {
177
    mRemovePatternRotation=false;
178
    mNewObject.removeRotationNow();
179
    mAddActionListener.onActionFinished(mRemoveRotationID);
180
    }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184
  private void addRotationNow()
185
    {
186
    mAddRotation = false;
187
    mAddRotationID = mNewObject.addNewRotation( mAddRotationAxis, mAddRotationRowBitmap,
188
                                                mAddRotationAngle, mAddRotationDuration, this);
189
190
    if( mAddRotationID==0 ) // failed to add effect - should never happen
191
      {
192
      unblockEverything();
193
      }
194
    }
195
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198
  private void finishRotationNow()
199
    {
200
    mFinishRotation = false;
201 b78ebd76 Leszek Koltunski
    blockEverything(BlockController.PLACE_0);
202 7c111294 Leszek Koltunski
    mRotationFinishedID = mNewObject.finishRotationNow(this, mNearestAngle);
203
204
    if( mRotationFinishedID==0 ) // failed to add effect - should never happen
205
      {
206
      unblockEverything();
207
      }
208
    }
209
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212
  private void changeObjectNow()
213
    {
214
    mChangeObject = false;
215
216
    if ( mNewObject==null || mNewObject.getObjectType()!=mNextObject )
217
      {
218 b78ebd76 Leszek Koltunski
      blockEverything(BlockController.PLACE_1);
219 81141862 Leszek Koltunski
      createObjectNow(mNextObject);
220 7c111294 Leszek Koltunski
      doEffectNow( BaseEffect.Type.SIZECHANGE );
221
      }
222
    }
223
224 568d4698 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
225
226
  private void recreateObjectNow()
227
    {
228
    mRecreateObject = false;
229
230
    if ( mNewObject!=null )
231
      {
232
      blockEverything(BlockController.PLACE_1);
233
      createObjectNow(mNewObject.getObjectType());
234
      doEffectNow( BaseEffect.Type.SIZECHANGE );
235
      }
236
    }
237
238 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240
  private void scrambleObjectNow()
241
    {
242
    mScrambleObject = false;
243
    mIsSolved       = false;
244 b78ebd76 Leszek Koltunski
    blockEverything(BlockController.PLACE_3);
245 7c111294 Leszek Koltunski
    doEffectNow( BaseEffect.Type.SCRAMBLE );
246
    }
247
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249
250
  private void solveObjectNow()
251
    {
252
    mSolveObject = false;
253 b78ebd76 Leszek Koltunski
    blockEverything(BlockController.PLACE_4);
254 7c111294 Leszek Koltunski
    doEffectNow( BaseEffect.Type.SOLVE );
255
    }
256
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258
259
  private void solveNow()
260
    {
261
    mSolve = false;
262 a57e6870 Leszek Koltunski
    if( mNewObject!=null ) mNewObject.solve();
263 7c111294 Leszek Koltunski
    }
264
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
267
  private void initializeObjectNow()
268
    {
269
    mInitializeObject = false;
270
    mNewObject.initializeObject(mNextMoves);
271
    }
272
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
275
  private void setTextureMapNow()
276
    {
277
    mSetTextureMap = false;
278
279
    if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
280
    }
281
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283
284
  private void resetAllTextureMapsNow()
285
    {
286
    mResetAllTextureMaps = false;
287
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
288
    }
289
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291
292
  private void setQuatNow()
293
    {
294
    mSetQuat = false;
295 15e5214c Leszek Koltunski
    mController.setQuat();
296 7c111294 Leszek Koltunski
    }
297
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299
300 15e5214c Leszek Koltunski
  void rememberMove(int axis, int row, int angle)
301 7c111294 Leszek Koltunski
    {
302
    mDebug += (" (m "+axis+" "+(1<<row)+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
303
    }
304
305 15e5214c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
306
307
  void finishRotation(int nearestAngle)
308
    {
309
    mNearestAngle   = nearestAngle;
310
    mFinishRotation = true;
311
    }
312
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314
315
  void setTextureMap(int cubit, int face, int newColor)
316
    {
317
    mSetTextureMap = true;
318
319
    mCubit    = cubit;
320
    mFace     = face;
321
    mNewColor = newColor;
322
    }
323
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325
326
  void setQuatOnNextRender()
327
    {
328
    mSetQuat = true;
329
    }
330
331 02d80fe6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
332
333 72d6857c Leszek Koltunski
  void setMove(float xmove, float ymove)
334 02d80fe6 Leszek Koltunski
    {
335 72d6857c Leszek Koltunski
    mMoveX = xmove;
336
    mMoveY = ymove;
337 02d80fe6 Leszek Koltunski
    }
338
339 15e5214c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
340
// INTERNAL API
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342
343
  public int getNumScrambles()
344
    {
345
    return mScrambleObjectNum;
346
    }
347
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349
350
  public TwistyObject getOldObject()
351
    {
352
    return mOldObject;
353
    }
354
355 02d80fe6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
356
357 72d6857c Leszek Koltunski
  public float getMoveX()
358 02d80fe6 Leszek Koltunski
    {
359 72d6857c Leszek Koltunski
    return mMoveX;
360
    }
361
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363
364
  public float getMoveY()
365
    {
366
    return mMoveY;
367 02d80fe6 Leszek Koltunski
    }
368
369 15e5214c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
370
// PUBLIC API
371 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
372
373 7ba38dd4 Leszek Koltunski
  public void setScreenSize()
374 7c111294 Leszek Koltunski
    {
375 7ba38dd4 Leszek Koltunski
    if( mNewObject!=null ) mNewObject.createTexture();
376 7c111294 Leszek Koltunski
    }
377
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379
380
  public void savePreferences(SharedPreferences.Editor editor)
381
    {
382
    if( mNewObject!=null )
383
      {
384
      mNewObject.savePreferences(editor);
385
      }
386
    }
387
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389
390
  public void restorePreferences(SharedPreferences preferences)
391
    {
392
    mPreferences = preferences;
393
    }
394
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396
397
  public void changeObject(ObjectType object)
398
    {
399
    mChangeObject = true;
400
    mNextObject = object;
401
    }
402
403 568d4698 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
404
405
  public void recreateObject()
406
    {
407
    mRecreateObject = true;
408
    }
409
410 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
411
412
  public boolean isTouchBlocked()
413
    {
414
    return mTouchBlocked;
415
    }
416
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418
419
  public boolean isUINotBlocked()
420
    {
421
    return !mUIBlocked;
422
    }
423
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425
426
  public void blockEverything(int place)
427
    {
428
    mUIBlocked   = true;
429
    mTouchBlocked= true;
430
    mBlockController.touchBlocked(place);
431
    mBlockController.uiBlocked(place);
432
    }
433
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435
436
  public void blockTouch(int place)
437
    {
438
    mTouchBlocked= true;
439
    mBlockController.touchBlocked(place);
440
    }
441
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443
444
  public void unblockEverything()
445
    {
446
    mUIBlocked   = false;
447
    mTouchBlocked= false;
448
    mBlockController.touchUnblocked();
449
    mBlockController.uiUnblocked();
450
    }
451
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453
454
  public void unblockTouch()
455
    {
456
    mTouchBlocked= false;
457
    mBlockController.touchUnblocked();
458
    }
459
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461
462
  public void unblockUI()
463
    {
464
    mUIBlocked= false;
465
    mBlockController.uiUnblocked();
466
    }
467
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469
470
  public void preRender()
471
    {
472
    if( mSolve                 ) solveNow();
473
    if( mSetQuat               ) setQuatNow();
474
    if( mFinishRotation        ) finishRotationNow();
475
    if( mRemoveRotation        ) removeRotationNow();
476
    if( mRemovePatternRotation ) removePatternRotationNow();
477
    if( mChangeObject          ) changeObjectNow();
478 568d4698 Leszek Koltunski
    if( mRecreateObject        ) recreateObjectNow();
479 7c111294 Leszek Koltunski
    if( mSolveObject           ) solveObjectNow();
480
    if( mScrambleObject        ) scrambleObjectNow();
481
    if( mAddRotation           ) addRotationNow();
482
    if( mInitializeObject      ) initializeObjectNow();
483
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
484
    if( mSetTextureMap         ) setTextureMapNow();
485
    }
486
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488
489 2df35810 Leszek Koltunski
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
490 7c111294 Leszek Koltunski
    {
491
    mAddRotation = true;
492
493 2df35810 Leszek Koltunski
    int basicAngle= mNewObject.getBasicAngle()[axis];
494
    int angle     = bareAngle*(360/basicAngle);
495
    int duration  = Math.abs(angle)*millPreDegree;
496
497 7c111294 Leszek Koltunski
    mAddActionListener    = listener;
498
    mAddRotationAxis      = axis;
499
    mAddRotationRowBitmap = rowBitmap;
500
    mAddRotationAngle     = angle;
501
    mAddRotationDuration  = duration;
502
503
    if( listener instanceof ScrambleEffect )
504
      {
505
      mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
506
      }
507
    }
508
509
///////////////////////////////////////////////////////////////////////////////////////////////////
510
511
  public void initializeObject(int[][] moves)
512
    {
513
    mInitializeObject = true;
514
    mNextMoves = moves;
515
    }
516
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518
519
  public void scrambleObject(int num)
520
    {
521
    if( !mUIBlocked )
522
      {
523
      mScrambleObject = true;
524
      mScrambleObjectNum = num;
525
      mDebug = "";
526
      mDebugStartTime = System.currentTimeMillis();
527
      }
528
    }
529
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531
// this starts the Solve Effect
532
533
  public void solveObject()
534
    {
535
    if( !mUIBlocked )
536
      {
537
      mSolveObject = true;
538
      }
539
    }
540
541 17d623f1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
542
// this only sets the cubits state to solved
543
544
  public void solveOnly()
545
    {
546
    mSolve = true;
547
    }
548
549 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
550
551
  public void resetAllTextureMaps()
552
    {
553
    mResetAllTextureMaps = true;
554
    }
555
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557
558
  public TwistyObject getObject()
559
    {
560
    return mNewObject;
561
    }
562
563 7ba38dd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
564
565
  public TwistyObjectNode getObjectNode()
566
    {
567
    return mController.getNode();
568
    }
569
570 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
571
572
  public void effectFinished(final long effectID)
573
    {
574
    if( effectID == mRotationFinishedID )
575
      {
576
      mRotationFinishedID = 0;
577
      removeRotation();
578
      }
579
    else if( effectID == mAddRotationID )
580
      {
581
      mAddRotationID = 0;
582
      mRemoveRotationID = effectID;
583
      removePatternRotation();
584
      }
585
    else
586
      {
587
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
588
        {
589
        if( effectID == mEffectID[i] )
590
          {
591
          if( i!=BaseEffect.Type.WIN.ordinal() ) unblockEverything();
592 b3fff6fb Leszek Koltunski
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() ) mInterface.onScrambleEffectFinished();
593
          if( i==BaseEffect.Type.WIN.ordinal()      ) mInterface.onWinEffectFinished(mDebug,mScrambleObjectNum);
594 7c111294 Leszek Koltunski
          break;
595
          }
596
        }
597
      }
598
    }
599
  }