Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectPreRender.java @ b3fff6fb

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

    
26
import org.distorted.library.main.DistortedScreen;
27
import org.distorted.objectlib.helpers.ObjectLibInterface;
28
import org.distorted.objectlib.effects.BaseEffect;
29
import org.distorted.objectlib.effects.EffectController;
30
import org.distorted.objectlib.effects.scramble.ScrambleEffect;
31
import org.distorted.objectlib.helpers.BlockController;
32
import org.distorted.objectlib.helpers.MovesFinished;
33

    
34
import java.lang.ref.WeakReference;
35

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

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

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

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  public ObjectPreRender(Activity act, ObjectControl controller, ObjectLibInterface actioner)
69
    {
70
    mAct = new WeakReference<>(act);
71
    mInterface = actioner;
72
    mController = controller;
73

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

    
85
    mOldObject = null;
86
    mNewObject = null;
87

    
88
    mDebug = "";
89

    
90
    mScreenWidth = 0;
91
    mScrambleObjectNum = 0;
92

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

    
95
    mBlockController = new BlockController(act,this);
96
    unblockEverything();
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

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

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

    
108
    Activity tact = mAct.get();
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
      DistortedScreen screen = mController.getInterface().getScreen();
130
      mEffectID[index] = type.startEffect(screen,this);
131
      }
132
    catch( Exception ex )
133
      {
134
      android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
135
      unblockEverything();
136
      }
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

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

    
146
    boolean solved = mNewObject.isSolved();
147

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

    
159
    mIsSolved = solved;
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

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

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

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

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

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

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

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

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

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

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

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

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

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

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

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

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

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

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

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

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

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

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

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

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

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

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

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

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

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

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  private void setQuatNow()
300
    {
301
    mSetQuat = false;
302
    mController.setQuat();
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 finishRotation(int nearestAngle)
315
    {
316
    mNearestAngle   = nearestAngle;
317
    mFinishRotation = true;
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

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

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

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

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

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339
// INTERNAL API
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

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

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

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

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355
// PUBLIC API
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

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

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

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

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

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

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

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

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

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

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

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

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

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

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

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

    
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427

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

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435

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

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

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

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

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

    
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461

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

    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480

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

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

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

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

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502

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

    
509
///////////////////////////////////////////////////////////////////////////////////////////////////
510

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

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

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

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

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

    
541
///////////////////////////////////////////////////////////////////////////////////////////////////
542

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

    
548
///////////////////////////////////////////////////////////////////////////////////////////////////
549

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

    
555
///////////////////////////////////////////////////////////////////////////////////////////////////
556

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