Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikPreRender.java @ 1cd323dd

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.Context;
23
import android.content.SharedPreferences;
24
import android.content.res.Resources;
25

    
26
import org.distorted.objectlib.helpers.ObjectStateActioner;
27
import org.distorted.objectlib.main.TwistyObject;
28
import org.distorted.objectlib.main.ObjectType;
29
import org.distorted.objectlib.effects.BaseEffect;
30
import org.distorted.objectlib.effects.EffectController;
31
import org.distorted.objectlib.effects.scramble.ScrambleEffect;
32
import org.distorted.objectlib.helpers.BlockController;
33
import org.distorted.objectlib.helpers.MovesFinished;
34
import org.distorted.objectlib.helpers.TwistyPreRender;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class RubikPreRender implements EffectController, TwistyPreRender
39
  {
40
  private final RubikSurfaceView mView;
41
  private boolean mFinishRotation, mRemoveRotation, mRemovePatternRotation, mAddRotation,
42
                  mSetQuat, mChangeObject, mSetupObject, mSolveObject, mScrambleObject,
43
                  mInitializeObject, mSetTextureMap, mResetAllTextureMaps, mSolve;
44
  private boolean mUIBlocked, mTouchBlocked;
45
  private boolean mIsSolved;
46
  private ObjectType mNextObject;
47
  private long mRotationFinishedID;
48
  private final long[] mEffectID;
49
  private int mScreenWidth;
50
  private SharedPreferences mPreferences;
51
  private int[][] mNextMoves;
52
  private TwistyObject mOldObject, mNewObject;
53
  private int mScrambleObjectNum;
54
  private int mAddRotationAxis, mAddRotationRowBitmap, mAddRotationAngle;
55
  private long mAddRotationDuration;
56
  private MovesFinished mAddActionListener;
57
  private long mAddRotationID, mRemoveRotationID;
58
  private int mCubit, mFace, mNewColor;
59
  private int mNearestAngle;
60
  private long mDebugStartTime;
61
  private final BlockController mBlockController;
62
  private final ObjectStateActioner mActioner;
63
  private String mDebug;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  RubikPreRender(RubikSurfaceView view, ObjectStateActioner actioner)
68
    {
69
    mView = view;
70
    mActioner = actioner;
71

    
72
    mFinishRotation       = false;
73
    mRemoveRotation       = false;
74
    mRemovePatternRotation= false;
75
    mAddRotation          = false;
76
    mSetQuat              = false;
77
    mChangeObject         = false;
78
    mSetupObject          = false;
79
    mSolveObject          = false;
80
    mSolve                = false;
81
    mScrambleObject       = false;
82

    
83
    mOldObject = null;
84
    mNewObject = null;
85

    
86
    mDebug = "";
87

    
88
    mScreenWidth = 0;
89
    mScrambleObjectNum = 0;
90

    
91
    mEffectID = new long[BaseEffect.Type.LENGTH];
92

    
93
    RubikActivity act = (RubikActivity)mView.getContext();
94
    mBlockController = new BlockController(act);
95
    unblockEverything();
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  private void createObjectNow(ObjectType object, int[][] moves)
101
    {
102
    boolean firstTime = (mNewObject==null);
103

    
104
    if( mOldObject!=null ) mOldObject.releaseResources();
105
    mOldObject = mNewObject;
106

    
107
    Context con = mView.getContext();
108
    Resources res = con.getResources();
109

    
110
    mNewObject = object.create(mView.getQuat(), moves, res, mScreenWidth);
111

    
112
    if( mNewObject!=null )
113
      {
114
      mView.setMovement(mNewObject.getMovement());
115
      if( firstTime ) mNewObject.restorePreferences(mPreferences);
116
      mIsSolved = mNewObject.isSolved();
117
      }
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
122

    
123
  private void doEffectNow(BaseEffect.Type type)
124
    {
125
    try
126
      {
127
      int index = type.ordinal();
128
      mEffectID[index] = type.startEffect(mView.getRenderer().getScreen(),this);
129
      }
130
    catch( Exception ex )
131
      {
132
      android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
133
      unblockEverything();
134
      }
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  private void removeRotationNow()
140
    {
141
    mRemoveRotation=false;
142
    mNewObject.removeRotationNow();
143

    
144
    boolean solved = mNewObject.isSolved();
145

    
146
    if( solved && !mIsSolved )
147
      {
148
      mActioner.onSolved();
149
      unblockEverything();
150
      doEffectNow( BaseEffect.Type.WIN );
151
      }
152
    else
153
      {
154
      unblockEverything();
155
      }
156

    
157
    mIsSolved = solved;
158
    }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
  private void removeRotation()
163
    {
164
    mRemoveRotation = true;
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  private void removePatternRotation()
170
    {
171
    mRemovePatternRotation = true;
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  private void removePatternRotationNow()
177
    {
178
    mRemovePatternRotation=false;
179
    mNewObject.removeRotationNow();
180
    mAddActionListener.onActionFinished(mRemoveRotationID);
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  private void addRotationNow()
186
    {
187
    mAddRotation = false;
188
    mAddRotationID = mNewObject.addNewRotation( mAddRotationAxis, mAddRotationRowBitmap,
189
                                                mAddRotationAngle, mAddRotationDuration, this);
190

    
191
    if( mAddRotationID==0 ) // failed to add effect - should never happen
192
      {
193
      unblockEverything();
194
      }
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  private void finishRotationNow()
200
    {
201
    mFinishRotation = false;
202
    blockEverything(BlockController.RUBIK_PLACE_0);
203
    mRotationFinishedID = mNewObject.finishRotationNow(this, mNearestAngle);
204

    
205
    if( mRotationFinishedID==0 ) // failed to add effect - should never happen
206
      {
207
      unblockEverything();
208
      }
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  private void changeObjectNow()
214
    {
215
    mChangeObject = false;
216

    
217
    if ( mNewObject==null || mNewObject.getObjectType()!=mNextObject )
218
      {
219
      blockEverything(BlockController.RUBIK_PLACE_1);
220
      createObjectNow(mNextObject, null);
221
      doEffectNow( BaseEffect.Type.SIZECHANGE );
222
      }
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  private void setupObjectNow()
228
    {
229
    mSetupObject = false;
230

    
231
    if ( mNewObject==null || mNewObject.getObjectType()!=mNextObject)
232
      {
233
      blockEverything(BlockController.RUBIK_PLACE_2);
234
      createObjectNow(mNextObject, mNextMoves);
235
      doEffectNow( BaseEffect.Type.SIZECHANGE );
236
      }
237
    else
238
      {
239
      mNewObject.initializeObject(mNextMoves);
240
      }
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
  private void scrambleObjectNow()
246
    {
247
    mScrambleObject = false;
248
    mIsSolved       = false;
249
    blockEverything(BlockController.RUBIK_PLACE_3);
250
    doEffectNow( BaseEffect.Type.SCRAMBLE );
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  private void solveObjectNow()
256
    {
257
    mSolveObject = false;
258
    blockEverything(BlockController.RUBIK_PLACE_4);
259
    doEffectNow( BaseEffect.Type.SOLVE );
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
  private void solveNow()
265
    {
266
    mSolve = false;
267
    mNewObject.solve();
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

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

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

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

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

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  private void resetAllTextureMapsNow()
290
    {
291
    mResetAllTextureMaps = false;
292
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
293
    }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

    
297
  private void setQuatNow()
298
    {
299
    mSetQuat = false;
300
    mView.setQuat();
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304
//
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  void rememberMove(int axis, int row, int angle)
308
    {
309
    mDebug += (" (m "+axis+" "+(1<<row)+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  void setScreenSize(int width)
315
    {
316
    if( mNewObject!=null )
317
      {
318
      mNewObject.createTexture();
319
      mNewObject.recomputeScaleFactor(width);
320
      }
321
    mScreenWidth = width;
322
    }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
  void savePreferences(SharedPreferences.Editor editor)
327
    {
328
    if( mNewObject!=null )
329
      {
330
      mNewObject.savePreferences(editor);
331
      }
332
    }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

    
336
  void restorePreferences(SharedPreferences preferences)
337
    {
338
    mPreferences = preferences;
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
  void finishRotation(int nearestAngle)
344
    {
345
    mNearestAngle   = nearestAngle;
346
    mFinishRotation = true;
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
  void changeObject(ObjectType object)
352
    {
353
    mChangeObject = true;
354
    mNextObject = object;
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
  void setupObject(ObjectType object, int[][] moves)
360
    {
361
    mSetupObject= true;
362
    mNextObject = object;
363
    mNextMoves  = moves;
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
  public boolean isTouchBlocked()
380
    {
381
    return mTouchBlocked;
382
    }
383

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

    
386
  public boolean isUINotBlocked()
387
    {
388
    return !mUIBlocked;
389
    }
390

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

    
393
  public void blockEverything(int place)
394
    {
395
    mUIBlocked   = true;
396
    mTouchBlocked= true;
397
    mBlockController.touchBlocked(place);
398
    mBlockController.uiBlocked(place);
399
    }
400

    
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402

    
403
  public void blockTouch(int place)
404
    {
405
    mTouchBlocked= true;
406
    mBlockController.touchBlocked(place);
407
    }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

    
411
  public void unblockEverything()
412
    {
413
    mUIBlocked   = false;
414
    mTouchBlocked= false;
415
    mBlockController.touchUnblocked();
416
    mBlockController.uiUnblocked();
417
    }
418

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

    
421
  public void unblockTouch()
422
    {
423
    mTouchBlocked= false;
424
    mBlockController.touchUnblocked();
425
    }
426

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

    
429
  public void unblockUI()
430
    {
431
    mUIBlocked= false;
432
    mBlockController.uiUnblocked();
433
    }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
  void setQuatOnNextRender()
438
    {
439
    mSetQuat = true;
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
  void preRender()
445
    {
446
    if( mSolve                 ) solveNow();
447
    if( mSetQuat               ) setQuatNow();
448
    if( mFinishRotation        ) finishRotationNow();
449
    if( mRemoveRotation        ) removeRotationNow();
450
    if( mRemovePatternRotation ) removePatternRotationNow();
451
    if( mChangeObject          ) changeObjectNow();
452
    if( mSetupObject           ) setupObjectNow();
453
    if( mSolveObject           ) solveObjectNow();
454
    if( mScrambleObject        ) scrambleObjectNow();
455
    if( mAddRotation           ) addRotationNow();
456
    if( mInitializeObject      ) initializeObjectNow();
457
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
458
    if( mSetTextureMap         ) setTextureMapNow();
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462
// PUBLIC API
463
///////////////////////////////////////////////////////////////////////////////////////////////////
464

    
465
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, long duration)
466
    {
467
    mAddRotation = true;
468

    
469
    mAddActionListener    = listener;
470
    mAddRotationAxis      = axis;
471
    mAddRotationRowBitmap = rowBitmap;
472
    mAddRotationAngle     = angle;
473
    mAddRotationDuration  = duration;
474

    
475
    if( listener instanceof ScrambleEffect )
476
      {
477
      mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
478
      }
479
    }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

    
483
  public void initializeObject(int[][] moves)
484
    {
485
    mInitializeObject = true;
486
    mNextMoves = moves;
487
    }
488

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490

    
491
  public void scrambleObject(int num)
492
    {
493
    if( !mUIBlocked )
494
      {
495
      mScrambleObject = true;
496
      mScrambleObjectNum = num;
497
      mDebug = "";
498
      mDebugStartTime = System.currentTimeMillis();
499
      }
500
    }
501

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503
// this starts the Solve Effect
504

    
505
  public void solveObject()
506
    {
507
    if( !mUIBlocked )
508
      {
509
      mSolveObject = true;
510
      }
511
    }
512

    
513
///////////////////////////////////////////////////////////////////////////////////////////////////
514
// this only sets the cubits state to solved
515

    
516
  public void solve()
517
    {
518
    mSolve = true;
519
    }
520

    
521
///////////////////////////////////////////////////////////////////////////////////////////////////
522

    
523
  public void resetAllTextureMaps()
524
    {
525
    mResetAllTextureMaps = true;
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

    
530
  public TwistyObject getObject()
531
    {
532
    return mNewObject;
533
    }
534

    
535
///////////////////////////////////////////////////////////////////////////////////////////////////
536

    
537
  public TwistyObject getOldObject()
538
    {
539
    return mOldObject;
540
    }
541

    
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543

    
544
  public int getNumScrambles()
545
    {
546
    return mScrambleObjectNum;
547
    }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550

    
551
  public void effectFinished(final long effectID)
552
    {
553
    if( effectID == mRotationFinishedID )
554
      {
555
      mRotationFinishedID = 0;
556
      removeRotation();
557
      }
558
    else if( effectID == mAddRotationID )
559
      {
560
      mAddRotationID = 0;
561
      mRemoveRotationID = effectID;
562
      removePatternRotation();
563
      }
564
    else
565
      {
566
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
567
        {
568
        if( effectID == mEffectID[i] )
569
          {
570
          if( i!=BaseEffect.Type.WIN.ordinal() )
571
            {
572
            unblockEverything();
573
            }
574

    
575
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() )
576
            {
577
            RubikActivity act = (RubikActivity)mView.getContext();
578
            mActioner.onScrambleEffectFinished(act);
579
            }
580

    
581
          if( i==BaseEffect.Type.WIN.ordinal() )
582
            {
583
            RubikActivity act = (RubikActivity)mView.getContext();
584
            mActioner.onWinEffectFinished(act,mDebug,mScrambleObjectNum);
585
            }
586

    
587
          break;
588
          }
589
        }
590
      }
591
    }
592
  }
(3-3/5)