Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikPostRender.java @ b6d0c697

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