Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectPreRender.java @ 17d623f1

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

    
22
import android.content.SharedPreferences;
23
import android.content.res.Resources;
24

    
25
import org.distorted.objectlib.helpers.ObjectStateActioner;
26
import org.distorted.objectlib.effects.BaseEffect;
27
import org.distorted.objectlib.effects.EffectController;
28
import org.distorted.objectlib.effects.scramble.ScrambleEffect;
29
import org.distorted.objectlib.helpers.BlockController;
30
import org.distorted.objectlib.helpers.MovesFinished;
31
import org.distorted.objectlib.helpers.TwistyActivity;
32

    
33
import java.lang.ref.WeakReference;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
public class ObjectPreRender implements EffectController
38
  {
39
  private final WeakReference<TwistyActivity> mAct;
40
  private final ObjectControl mController;
41
  private ObjectType mNextObject;
42
  private TwistyObject mOldObject, mNewObject;
43
  private SharedPreferences mPreferences;
44
  private MovesFinished mAddActionListener;
45
  private final BlockController mBlockController;
46
  private final ObjectStateActioner mActioner;
47
  private String mDebug;
48

    
49
  private boolean mFinishRotation, mRemoveRotation, mRemovePatternRotation, mAddRotation,
50
                  mSetQuat, mChangeObject, mSetupObject, mSolveObject, mScrambleObject,
51
                  mInitializeObject, mSetTextureMap, mResetAllTextureMaps, mSolve;
52
  private boolean mUIBlocked, mTouchBlocked, mIsSolved;
53
  private long mRotationFinishedID;
54
  private final long[] mEffectID;
55
  private int mScreenWidth;
56
  private int[][] mNextMoves;
57
  private int mScrambleObjectNum;
58
  private int mAddRotationAxis, mAddRotationRowBitmap, mAddRotationAngle;
59
  private long mAddRotationDuration;
60
  private long mAddRotationID, mRemoveRotationID;
61
  private int mCubit, mFace, mNewColor;
62
  private int mNearestAngle;
63
  private long mDebugStartTime;
64

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

    
67
  public ObjectPreRender(TwistyActivity act, ObjectControl controller, ObjectStateActioner actioner)
68
    {
69
    mAct = new WeakReference<>(act);
70
    mActioner = actioner;
71
    mController = controller;
72

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

    
84
    mOldObject = null;
85
    mNewObject = null;
86

    
87
    mDebug = "";
88

    
89
    mScreenWidth = 0;
90
    mScrambleObjectNum = 0;
91

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

    
94
    mBlockController = new BlockController(act,this);
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
    TwistyActivity tact = mAct.get();
108

    
109
    Resources res = tact.getResources();
110

    
111
    mNewObject = object.create( mController.getQuat(), moves, res, mScreenWidth);
112

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

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

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

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

    
145
    boolean solved = mNewObject.isSolved();
146

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

    
158
    mIsSolved = solved;
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

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

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

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

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

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

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

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

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

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

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

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

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

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

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

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

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

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

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  private void solveNow()
266
    {
267
    mSolve = false;
268
    mNewObject.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
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

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

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

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

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  void finishRotation(int nearestAngle)
314
    {
315
    mNearestAngle   = nearestAngle;
316
    mFinishRotation = true;
317
    }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
  void setTextureMap(int cubit, int face, int newColor)
322
    {
323
    mSetTextureMap = true;
324

    
325
    mCubit    = cubit;
326
    mFace     = face;
327
    mNewColor = newColor;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
  void setQuatOnNextRender()
333
    {
334
    mSetQuat = true;
335
    }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338
// INTERNAL API
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
  public int getNumScrambles()
342
    {
343
    return mScrambleObjectNum;
344
    }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
  public TwistyObject getOldObject()
349
    {
350
    return mOldObject;
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354
// PUBLIC API
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
  public void setScreenSize(int width)
358
    {
359
    if( mNewObject!=null )
360
      {
361
      mNewObject.createTexture();
362
      mNewObject.recomputeScaleFactor(width);
363
      }
364
    mScreenWidth = width;
365
    }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

    
369
  public void savePreferences(SharedPreferences.Editor editor)
370
    {
371
    if( mNewObject!=null )
372
      {
373
      mNewObject.savePreferences(editor);
374
      }
375
    }
376

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

    
379
  public void restorePreferences(SharedPreferences preferences)
380
    {
381
    mPreferences = preferences;
382
    }
383

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

    
386
  public void changeObject(ObjectType object)
387
    {
388
    mChangeObject = true;
389
    mNextObject = object;
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
  public void setupObject(ObjectType object, int[][] moves)
395
    {
396
    mSetupObject= true;
397
    mNextObject = object;
398
    mNextMoves  = moves;
399
    }
400

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

    
403
  public boolean isTouchBlocked()
404
    {
405
    return mTouchBlocked;
406
    }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

    
410
  public boolean isUINotBlocked()
411
    {
412
    return !mUIBlocked;
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
  public void blockEverything(int place)
418
    {
419
    mUIBlocked   = true;
420
    mTouchBlocked= true;
421
    mBlockController.touchBlocked(place);
422
    mBlockController.uiBlocked(place);
423
    }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

    
427
  public void blockTouch(int place)
428
    {
429
    mTouchBlocked= true;
430
    mBlockController.touchBlocked(place);
431
    }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

    
435
  public void unblockEverything()
436
    {
437
    mUIBlocked   = false;
438
    mTouchBlocked= false;
439
    mBlockController.touchUnblocked();
440
    mBlockController.uiUnblocked();
441
    }
442

    
443
///////////////////////////////////////////////////////////////////////////////////////////////////
444

    
445
  public void unblockTouch()
446
    {
447
    mTouchBlocked= false;
448
    mBlockController.touchUnblocked();
449
    }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
  public void unblockUI()
454
    {
455
    mUIBlocked= false;
456
    mBlockController.uiUnblocked();
457
    }
458

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460

    
461
  public void preRender()
462
    {
463
    if( mSolve                 ) solveNow();
464
    if( mSetQuat               ) setQuatNow();
465
    if( mFinishRotation        ) finishRotationNow();
466
    if( mRemoveRotation        ) removeRotationNow();
467
    if( mRemovePatternRotation ) removePatternRotationNow();
468
    if( mChangeObject          ) changeObjectNow();
469
    if( mSetupObject           ) setupObjectNow();
470
    if( mSolveObject           ) solveObjectNow();
471
    if( mScrambleObject        ) scrambleObjectNow();
472
    if( mAddRotation           ) addRotationNow();
473
    if( mInitializeObject      ) initializeObjectNow();
474
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
475
    if( mSetTextureMap         ) setTextureMapNow();
476
    }
477

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479

    
480
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
481
    {
482
    mAddRotation = true;
483

    
484
    int basicAngle= mNewObject.getBasicAngle()[axis];
485
    int angle     = bareAngle*(360/basicAngle);
486
    int duration  = Math.abs(angle)*millPreDegree;
487

    
488
    mAddActionListener    = listener;
489
    mAddRotationAxis      = axis;
490
    mAddRotationRowBitmap = rowBitmap;
491
    mAddRotationAngle     = angle;
492
    mAddRotationDuration  = duration;
493

    
494
    if( listener instanceof ScrambleEffect )
495
      {
496
      mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
497
      }
498
    }
499

    
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501

    
502
  public void initializeObject(int[][] moves)
503
    {
504
    mInitializeObject = true;
505
    mNextMoves = moves;
506
    }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509

    
510
  public void scrambleObject(int num)
511
    {
512
    if( !mUIBlocked )
513
      {
514
      mScrambleObject = true;
515
      mScrambleObjectNum = num;
516
      mDebug = "";
517
      mDebugStartTime = System.currentTimeMillis();
518
      }
519
    }
520

    
521
///////////////////////////////////////////////////////////////////////////////////////////////////
522
// this starts the Solve Effect
523

    
524
  public void solveObject()
525
    {
526
    if( !mUIBlocked )
527
      {
528
      mSolveObject = true;
529
      }
530
    }
531

    
532
///////////////////////////////////////////////////////////////////////////////////////////////////
533
// this only sets the cubits state to solved
534

    
535
  public void solveOnly()
536
    {
537
    mSolve = true;
538
    }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541

    
542
  public void resetAllTextureMaps()
543
    {
544
    mResetAllTextureMaps = true;
545
    }
546

    
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548

    
549
  public TwistyObject getObject()
550
    {
551
    return mNewObject;
552
    }
553

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555

    
556
  public void effectFinished(final long effectID)
557
    {
558
    if( effectID == mRotationFinishedID )
559
      {
560
      mRotationFinishedID = 0;
561
      removeRotation();
562
      }
563
    else if( effectID == mAddRotationID )
564
      {
565
      mAddRotationID = 0;
566
      mRemoveRotationID = effectID;
567
      removePatternRotation();
568
      }
569
    else
570
      {
571
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
572
        {
573
        if( effectID == mEffectID[i] )
574
          {
575
          if( i!=BaseEffect.Type.WIN.ordinal() ) unblockEverything();
576
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() ) mActioner.onScrambleEffectFinished(mAct.get());
577
          if( i==BaseEffect.Type.WIN.ordinal()      ) mActioner.onWinEffectFinished(mAct.get(),mDebug,mScrambleObjectNum);
578
          break;
579
          }
580
        }
581
      }
582
    }
583
  }
(8-8/15)