Project

General

Profile

Download (17.9 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / main / RubikPostRender.java @ 1f894991

1 8becce57 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 1f9772f3 Leszek Koltunski
package org.distorted.main;
21 8becce57 Leszek Koltunski
22
import android.content.SharedPreferences;
23
import android.os.Bundle;
24
25 1f9772f3 Leszek Koltunski
import org.distorted.dialogs.RubikDialogNewRecord;
26
import org.distorted.dialogs.RubikDialogSolved;
27
import org.distorted.effects.BaseEffect;
28 8becce57 Leszek Koltunski
import org.distorted.library.message.EffectListener;
29 1f9772f3 Leszek Koltunski
import org.distorted.objects.RubikObject;
30
import org.distorted.objects.RubikObjectList;
31 8becce57 Leszek Koltunski
import org.distorted.scores.RubikScores;
32 1f9772f3 Leszek Koltunski
import org.distorted.states.RubikState;
33
import org.distorted.states.RubikStateSolving;
34 8becce57 Leszek Koltunski
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
37
public class RubikPostRender implements EffectListener
38
  {
39
  public interface ActionFinishedListener
40
    {
41
    void onActionFinished(long effectID);
42
    }
43
44
  private RubikSurfaceView mView;
45 d12bb11b Leszek Koltunski
  private boolean mFinishRotation, mRemoveRotation, mRemovePatternRotation, mAddRotation,
46 2e8ec627 Leszek Koltunski
                  mSetQuatCurrent, mSetQuatAccumulated, mChangeObject, mSetupObject, mSolveObject,
47 1f9772f3 Leszek Koltunski
                  mScrambleObject, mInitializeObject, mSetTextureMap, mResetAllTextureMaps;
48 a42e25a6 Leszek Koltunski
  private boolean mCanRotate, mCanPlay;
49 8becce57 Leszek Koltunski
  private boolean mIsSolved;
50
  private RubikObjectList mNextObject;
51
  private int mNextSize;
52
  private long mRotationFinishedID;
53
  private long[] mEffectID;
54
  private boolean mIsNewRecord;
55
  private long mNewRecord;
56
  private int mScreenWidth, mScreenHeight;
57
  private SharedPreferences mPreferences;
58 a31d25de Leszek Koltunski
  private int[][] mNextMoves;
59 8becce57 Leszek Koltunski
  private RubikObject mOldObject, mNewObject;
60
  private int mScrambleObjectNum;
61
  private int mAddRotationAxis, mAddRotationRowBitmap, mAddRotationAngle;
62
  private long mAddRotationDuration;
63
  private ActionFinishedListener mAddActionListener;
64 d12bb11b Leszek Koltunski
  private long mAddRotationID, mRemoveRotationID;
65 1f9772f3 Leszek Koltunski
  private int mCubit, mFace, mNewColor;
66 8becce57 Leszek Koltunski
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69
  RubikPostRender(RubikSurfaceView view)
70
    {
71
    mView = view;
72
73 d12bb11b Leszek Koltunski
    mFinishRotation       = false;
74
    mRemoveRotation       = false;
75
    mRemovePatternRotation= false;
76
    mAddRotation          = false;
77
    mSetQuatCurrent       = false;
78
    mSetQuatAccumulated   = false;
79
    mChangeObject         = false;
80 2e8ec627 Leszek Koltunski
    mSetupObject          = false;
81 d12bb11b Leszek Koltunski
    mSolveObject          = false;
82
    mScrambleObject       = false;
83 8becce57 Leszek Koltunski
84 a42e25a6 Leszek Koltunski
    mCanRotate = true;
85
    mCanPlay   = true;
86 8becce57 Leszek Koltunski
87
    mOldObject = null;
88
    mNewObject = null;
89
90
    mScreenWidth = mScreenHeight = 0;
91
    mScrambleObjectNum = 0;
92
93
    mEffectID = new long[BaseEffect.Type.LENGTH];
94
    }
95
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
98 a31d25de Leszek Koltunski
  private void createObjectNow(RubikObjectList object, int size, int[][] moves)
99 8becce57 Leszek Koltunski
    {
100
    boolean firstTime = (mNewObject==null);
101
102
    if( mOldObject!=null ) mOldObject.releaseResources();
103
    mOldObject = mNewObject;
104
105
    mNewObject = object.create(size, mView.getQuatCurrent(), mView.getQuatAccumulated(), moves);
106
107 b6d0c697 Leszek Koltunski
    if( mNewObject!=null )
108 8becce57 Leszek Koltunski
      {
109 b6d0c697 Leszek Koltunski
      mNewObject.createTexture();
110
      mView.setMovement(object.getObjectMovementClass());
111 8becce57 Leszek Koltunski
112 b6d0c697 Leszek Koltunski
      if( firstTime ) mNewObject.restorePreferences(mPreferences);
113
114
      if( mScreenWidth!=0 )
115
        {
116
        mNewObject.recomputeScaleFactor(mScreenWidth, mScreenHeight);
117
        }
118
119
      mIsSolved = mNewObject.isSolved();
120
      }
121 8becce57 Leszek Koltunski
    }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
125
126
  private void doEffectNow(BaseEffect.Type type)
127
    {
128
    int index = type.ordinal();
129
130
    try
131
      {
132
      mEffectID[index] = type.startEffect(mView.getRenderer().getScreen(),this);
133
      }
134
    catch( Exception ex )
135
      {
136
      android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
137
138 a42e25a6 Leszek Koltunski
      mCanPlay   = true;
139 8becce57 Leszek Koltunski
      mCanRotate = true;
140
      }
141
    }
142
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
145
  private void removeRotationNow()
146
    {
147
    mRemoveRotation=false;
148
    mNewObject.removeRotationNow();
149
150
    boolean solved = mNewObject.isSolved();
151
152
    if( solved && !mIsSolved )
153
      {
154
      if( RubikState.getCurrentState()==RubikState.SOLV )
155
        {
156
        RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
157 1f894991 Leszek Koltunski
        mNewRecord = solving.getRecord();
158 8becce57 Leszek Koltunski
159
        if( mNewRecord< 0 )
160
          {
161
          mNewRecord = -mNewRecord;
162
          mIsNewRecord = false;
163
          }
164
        else
165
          {
166
          mIsNewRecord = true;
167
          }
168
        }
169
170
      mCanRotate = false;
171 a42e25a6 Leszek Koltunski
      mCanPlay = false;
172 8becce57 Leszek Koltunski
      doEffectNow( BaseEffect.Type.WIN );
173
      }
174
    else
175
      {
176
      mCanRotate = true;
177 a42e25a6 Leszek Koltunski
      mCanPlay = true;
178 8becce57 Leszek Koltunski
      }
179
180
    mIsSolved = solved;
181
    }
182
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
185
  private void removeRotation()
186
    {
187
    mRemoveRotation = true;
188
    }
189
190 d12bb11b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
191
192
  private void removePatternRotation()
193
    {
194
    mRemovePatternRotation = true;
195
    }
196
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
199
  private void removePatternRotationNow()
200
    {
201
    mRemovePatternRotation=false;
202
    mNewObject.removeRotationNow();
203
    mAddActionListener.onActionFinished(mRemoveRotationID);
204
    }
205
206 8becce57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208
  private void addRotationNow()
209
    {
210
    mAddRotation = false;
211
    mAddRotationID = mNewObject.addNewRotation( mAddRotationAxis, mAddRotationRowBitmap,
212
                                                mAddRotationAngle, mAddRotationDuration, this);
213
    }
214
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
  private void finishRotationNow()
218
    {
219
    mFinishRotation = false;
220
    mCanRotate      = false;
221 a42e25a6 Leszek Koltunski
    mCanPlay        = false;
222 8becce57 Leszek Koltunski
    mRotationFinishedID = mNewObject.finishRotationNow(this);
223
224
    if( mRotationFinishedID==0 ) // failed to add effect - should never happen
225
      {
226
      mCanRotate = true;
227 a42e25a6 Leszek Koltunski
      mCanPlay   = true;
228 8becce57 Leszek Koltunski
      }
229
    }
230
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232
233
  private void changeObjectNow()
234
    {
235
    mChangeObject = false;
236
237 2e8ec627 Leszek Koltunski
    if ( mNewObject==null || mNewObject.getObjectList()!=mNextObject || mNewObject.getSize()!=mNextSize)
238
      {
239
      mCanRotate= false;
240
      mCanPlay  = false;
241
      createObjectNow(mNextObject, mNextSize, null);
242
      doEffectNow( BaseEffect.Type.SIZECHANGE );
243
      }
244
    }
245
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
248
  private void setupObjectNow()
249
    {
250
    mSetupObject = false;
251
252 8becce57 Leszek Koltunski
    if ( mNewObject==null || mNewObject.getObjectList()!=mNextObject || mNewObject.getSize()!=mNextSize)
253
      {
254 a42e25a6 Leszek Koltunski
      mCanRotate= false;
255
      mCanPlay  = false;
256 8becce57 Leszek Koltunski
      createObjectNow(mNextObject, mNextSize, mNextMoves);
257
      doEffectNow( BaseEffect.Type.SIZECHANGE );
258
      }
259
    else
260
      {
261
      mNewObject.initializeObject(mNextMoves);
262
      }
263
    }
264
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
267
  private void scrambleObjectNow()
268
    {
269
    mScrambleObject = false;
270
    mCanRotate      = false;
271 a42e25a6 Leszek Koltunski
    mCanPlay        = false;
272 8becce57 Leszek Koltunski
    mIsSolved       = false;
273
    RubikScores.getInstance().incrementNumPlays();
274
    doEffectNow( BaseEffect.Type.SCRAMBLE );
275
    }
276
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278
279
  private void solveObjectNow()
280
    {
281 a42e25a6 Leszek Koltunski
    mSolveObject = false;
282
    mCanRotate   = false;
283
    mCanPlay     = false;
284 8becce57 Leszek Koltunski
    doEffectNow( BaseEffect.Type.SOLVE );
285
    }
286
287 d12bb11b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
288
289
  private void initializeObjectNow()
290
    {
291
    mInitializeObject = false;
292
    mNewObject.initializeObject(mNextMoves);
293
    }
294
295 1f9772f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
296
297
  private void setTextureMapNow()
298
    {
299
    mSetTextureMap = false;
300
301 b6d0c697 Leszek Koltunski
    if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
302 1f9772f3 Leszek Koltunski
    }
303
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305
306
  private void resetAllTextureMapsNow()
307
    {
308
    mResetAllTextureMaps = false;
309
310 b6d0c697 Leszek Koltunski
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
311 1f9772f3 Leszek Koltunski
    }
312
313 8becce57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
314
315
  private void setQuatCurrentNow()
316
    {
317
    mSetQuatCurrent = false;
318
    mView.setQuatCurrent();
319
    }
320
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322
323
  private void setQuatAccumulatedNow()
324
    {
325
    mSetQuatAccumulated = false;
326
    mView.setQuatAccumulated();
327
    }
328
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330
//
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332
333
  void setScreenSize(int width, int height)
334
    {
335
    if( mNewObject!=null )
336
      {
337
      mNewObject.createTexture();
338
      mNewObject.recomputeScaleFactor(width,height);
339
      }
340
341
    mScreenHeight = height;
342
    mScreenWidth  = width;
343
    }
344
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346
347
  void savePreferences(SharedPreferences.Editor editor)
348
    {
349
    if( mNewObject!=null )
350
      {
351
      mNewObject.savePreferences(editor);
352
      }
353
    }
354
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356
357
  void restorePreferences(SharedPreferences preferences)
358
    {
359
    mPreferences = preferences;
360
    }
361
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363
364
  void finishRotation()
365
    {
366
    mFinishRotation = true;
367
    }
368
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370
371 2e8ec627 Leszek Koltunski
  void changeObject(RubikObjectList object, int size)
372 8becce57 Leszek Koltunski
    {
373
    if( size>0 )
374
      {
375
      mChangeObject = true;
376
      mNextObject = object;
377
      mNextSize   = size;
378 2e8ec627 Leszek Koltunski
      }
379
    }
380
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382
383
  void setupObject(RubikObjectList object, int size, int[][] moves)
384
    {
385
    if( size>0 )
386
      {
387
      mSetupObject= true;
388
      mNextObject = object;
389
      mNextSize   = size;
390 8becce57 Leszek Koltunski
      mNextMoves  = moves;
391
      }
392
    }
393
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395
396 85b09df4 Leszek Koltunski
  void setTextureMap(int cubit, int face, int newColor)
397 8becce57 Leszek Koltunski
    {
398 85b09df4 Leszek Koltunski
    mSetTextureMap = true;
399 8becce57 Leszek Koltunski
400 85b09df4 Leszek Koltunski
    mCubit    = cubit;
401
    mFace     = face;
402
    mNewColor = newColor;
403 8becce57 Leszek Koltunski
    }
404
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406
407
  boolean canRotate()
408
    {
409
    return mCanRotate;
410
    }
411
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413
414 a42e25a6 Leszek Koltunski
  public boolean canPlay()
415 8becce57 Leszek Koltunski
    {
416 a42e25a6 Leszek Koltunski
    return mCanPlay;
417 8becce57 Leszek Koltunski
    }
418
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420
421
  void setQuatCurrentOnNextRender()
422
    {
423
    mSetQuatCurrent = true;
424
    }
425
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427
428
  void setQuatAccumulatedOnNextRender()
429
    {
430
    mSetQuatAccumulated = true;
431
    }
432
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434
435
  void postRender()
436
    {
437 d12bb11b Leszek Koltunski
    if( mSetQuatCurrent        ) setQuatCurrentNow();
438
    if( mSetQuatAccumulated    ) setQuatAccumulatedNow();
439
    if( mFinishRotation        ) finishRotationNow();
440
    if( mRemoveRotation        ) removeRotationNow();
441
    if( mRemovePatternRotation ) removePatternRotationNow();
442
    if( mChangeObject          ) changeObjectNow();
443 2e8ec627 Leszek Koltunski
    if( mSetupObject           ) setupObjectNow();
444 d12bb11b Leszek Koltunski
    if( mSolveObject           ) solveObjectNow();
445
    if( mScrambleObject        ) scrambleObjectNow();
446
    if( mAddRotation           ) addRotationNow();
447
    if( mInitializeObject      ) initializeObjectNow();
448 1f9772f3 Leszek Koltunski
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
449
    if( mSetTextureMap         ) setTextureMapNow();
450 8becce57 Leszek Koltunski
    }
451
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453
// PUBLIC API
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455
456
  public void addRotation(ActionFinishedListener listener, int axis, int rowBitmap, int angle, long duration)
457
    {
458
    mAddRotation = true;
459
460
    mAddActionListener    = listener;
461
    mAddRotationAxis      = axis;
462
    mAddRotationRowBitmap = rowBitmap;
463
    mAddRotationAngle     = angle;
464
    mAddRotationDuration  = duration;
465
    }
466
467 d12bb11b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
468
469
  public void initializeObject(int[][] moves)
470
    {
471
    mInitializeObject = true;
472
    mNextMoves = moves;
473
    }
474
475 1f9772f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
476
477 85b09df4 Leszek Koltunski
  public void scrambleObject(int num)
478 1f9772f3 Leszek Koltunski
    {
479 85b09df4 Leszek Koltunski
    if( mCanPlay )
480
      {
481
      mScrambleObject = true;
482
      mScrambleObjectNum = num;
483
      }
484
    }
485 1f9772f3 Leszek Koltunski
486 85b09df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
487
488
  public void solveObject()
489
    {
490
    if( mCanPlay )
491
      {
492
      mSolveObject = true;
493
      }
494 1f9772f3 Leszek Koltunski
    }
495
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497
498
  public void resetAllTextureMaps()
499
    {
500
    mResetAllTextureMaps = true;
501
    }
502
503 8becce57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
504
505
  public RubikObject getObject()
506
    {
507
    return mNewObject;
508
    }
509
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511
512
  public RubikObject getOldObject()
513
    {
514
    return mOldObject;
515
    }
516
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518
519
  public int getNumScrambles()
520
    {
521
    return mScrambleObjectNum;
522
    }
523
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525
526
  public void effectFinished(final long effectID)
527
    {
528
    if( effectID == mRotationFinishedID )
529
      {
530
      mRotationFinishedID = 0;
531
      removeRotation();
532
      }
533
    else if( effectID == mAddRotationID )
534
      {
535
      mAddRotationID = 0;
536 d12bb11b Leszek Koltunski
      mRemoveRotationID = effectID;
537
      removePatternRotation();
538 8becce57 Leszek Koltunski
      }
539
    else
540
      {
541
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
542
        {
543
        if( effectID == mEffectID[i] )
544
          {
545 a42e25a6 Leszek Koltunski
          mCanRotate = true;
546
          mCanPlay   = true;
547 8becce57 Leszek Koltunski
548
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() )
549
            {
550
            final RubikActivity act = (RubikActivity)mView.getContext();
551
552
            act.runOnUiThread(new Runnable()
553
              {
554
              @Override
555
              public void run()
556
                {
557 807d82b7 Leszek Koltunski
                RubikState.switchState( act, RubikState.READ);
558 8becce57 Leszek Koltunski
                }
559
              });
560
            }
561
562
          if( i==BaseEffect.Type.WIN.ordinal() )
563
            {
564
            if( RubikState.getCurrentState()==RubikState.SOLV )
565
              {
566
              final RubikActivity act = (RubikActivity)mView.getContext();
567
              Bundle bundle = new Bundle();
568
              bundle.putLong("time", mNewRecord );
569
570
              if( mIsNewRecord )
571
                {
572
                RubikDialogNewRecord dialog = new RubikDialogNewRecord();
573
                dialog.setArguments(bundle);
574
                dialog.show( act.getSupportFragmentManager(), null);
575
                }
576
              else
577
                {
578
                RubikDialogSolved dialog = new RubikDialogSolved();
579
                dialog.setArguments(bundle);
580
                dialog.show( act.getSupportFragmentManager(), null);
581
                }
582 807d82b7 Leszek Koltunski
583
              act.runOnUiThread(new Runnable()
584
                {
585
                @Override
586
                public void run()
587
                  {
588
                  RubikState.switchState( act, RubikState.DONE);
589
                  }
590
                });
591 8becce57 Leszek Koltunski
              }
592
            }
593
594
          break;
595
          }
596
        }
597
      }
598
    }
599
  }