Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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.main;
21

    
22
import android.content.SharedPreferences;
23
import android.os.Bundle;
24

    
25
import org.distorted.dialogs.RubikDialogNewRecord;
26
import org.distorted.dialogs.RubikDialogSolved;
27
import org.distorted.effects.BaseEffect;
28
import org.distorted.library.message.EffectListener;
29
import org.distorted.objects.RubikObject;
30
import org.distorted.objects.RubikObjectList;
31
import org.distorted.scores.RubikScores;
32
import org.distorted.states.RubikState;
33
import org.distorted.states.RubikStateSolving;
34

    
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
  private boolean mFinishRotation, mRemoveRotation, mRemovePatternRotation, mAddRotation,
46
                  mSetQuatCurrent, mSetQuatAccumulated, mChangeObject, mSolveObject,
47
                  mScrambleObject, mInitializeObject, mSetTextureMap, mResetAllTextureMaps;
48
  private boolean mCanRotate, mCanPlay;
49
  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
  private int[][] mNextMoves;
59
  private RubikObject mOldObject, mNewObject;
60
  private int mScrambleObjectNum;
61
  private int mAddRotationAxis, mAddRotationRowBitmap, mAddRotationAngle;
62
  private long mAddRotationDuration;
63
  private ActionFinishedListener mAddActionListener;
64
  private long mAddRotationID, mRemoveRotationID;
65
  private int mCubit, mFace, mNewColor;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  RubikPostRender(RubikSurfaceView view)
70
    {
71
    mView = view;
72

    
73
    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

    
83
    mCanRotate = true;
84
    mCanPlay   = true;
85

    
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
  private void createObjectNow(RubikObjectList object, int size, int[][] moves)
98
    {
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
    if( mNewObject!=null )
107
      {
108
      mNewObject.createTexture();
109
      mView.setMovement(object.getObjectMovementClass());
110

    
111
      if( firstTime ) mNewObject.restorePreferences(mPreferences);
112

    
113
      if( mScreenWidth!=0 )
114
        {
115
        mNewObject.recomputeScaleFactor(mScreenWidth, mScreenHeight);
116
        }
117

    
118
      mIsSolved = mNewObject.isSolved();
119
      }
120
    }
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
      mCanPlay   = true;
138
      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
      mCanPlay = false;
171
      doEffectNow( BaseEffect.Type.WIN );
172
      }
173
    else
174
      {
175
      mCanRotate = true;
176
      mCanPlay = true;
177
      }
178

    
179
    mIsSolved = solved;
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  private void removeRotation()
185
    {
186
    mRemoveRotation = true;
187
    }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
    mCanPlay        = false;
221
    mRotationFinishedID = mNewObject.finishRotationNow(this);
222

    
223
    if( mRotationFinishedID==0 ) // failed to add effect - should never happen
224
      {
225
      mCanRotate = true;
226
      mCanPlay   = true;
227
      }
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
      mCanRotate= false;
239
      mCanPlay  = false;
240
      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
    mCanPlay        = false;
256
    mIsSolved       = false;
257
    RubikScores.getInstance().incrementNumPlays();
258
    doEffectNow( BaseEffect.Type.SCRAMBLE );
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  private void solveObjectNow()
264
    {
265
    mSolveObject = false;
266
    mCanRotate   = false;
267
    mCanPlay     = false;
268
    doEffectNow( BaseEffect.Type.SOLVE );
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  private void initializeObjectNow()
274
    {
275
    mInitializeObject = false;
276
    mNewObject.initializeObject(mNextMoves);
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  private void setTextureMapNow()
282
    {
283
    mSetTextureMap = false;
284

    
285
    if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  private void resetAllTextureMapsNow()
291
    {
292
    mResetAllTextureMaps = false;
293

    
294
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
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
  void changeObject(RubikObjectList object, int size, int[][] moves)
356
    {
357
    if( size>0 )
358
      {
359
      mChangeObject = true;
360
      mNextObject = object;
361
      mNextSize   = size;
362
      mNextMoves  = moves;
363
      }
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  void setTextureMap(int cubit, int face, int newColor)
369
    {
370
    mSetTextureMap = true;
371

    
372
    mCubit    = cubit;
373
    mFace     = face;
374
    mNewColor = newColor;
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
  boolean canRotate()
380
    {
381
    return mCanRotate;
382
    }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

    
386
  public boolean canPlay()
387
    {
388
    return mCanPlay;
389
    }
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
    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
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
420
    if( mSetTextureMap         ) setTextureMapNow();
421
    }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
439

    
440
  public void initializeObject(int[][] moves)
441
    {
442
    mInitializeObject = true;
443
    mNextMoves = moves;
444
    }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

    
448
  public void scrambleObject(int num)
449
    {
450
    if( mCanPlay )
451
      {
452
      mScrambleObject = true;
453
      mScrambleObjectNum = num;
454
      }
455
    }
456

    
457
///////////////////////////////////////////////////////////////////////////////////////////////////
458

    
459
  public void solveObject()
460
    {
461
    if( mCanPlay )
462
      {
463
      mSolveObject = true;
464
      }
465
    }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

    
469
  public void resetAllTextureMaps()
470
    {
471
    mResetAllTextureMaps = true;
472
    }
473

    
474
///////////////////////////////////////////////////////////////////////////////////////////////////
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
      mRemoveRotationID = effectID;
508
      removePatternRotation();
509
      }
510
    else
511
      {
512
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
513
        {
514
        if( effectID == mEffectID[i] )
515
          {
516
          mCanRotate = true;
517
          mCanPlay   = true;
518

    
519
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() )
520
            {
521
            final RubikActivity act = (RubikActivity)mView.getContext();
522
            RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
523
            solving.resetUpperText();
524

    
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
  }
(2-2/4)