Project

General

Profile

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

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

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 java.lang.ref.WeakReference;
23

    
24
import android.app.Activity;
25
import android.content.SharedPreferences;
26

    
27
import org.distorted.library.message.EffectListener;
28

    
29
import org.distorted.library.type.Static3D;
30
import org.distorted.objectlib.helpers.ObjectLibInterface;
31
import org.distorted.objectlib.effects.BaseEffect;
32
import org.distorted.objectlib.effects.scramble.ScrambleEffect;
33
import org.distorted.objectlib.helpers.BlockController;
34
import org.distorted.objectlib.helpers.MovesFinished;
35

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

    
38
public class ObjectPreRender implements EffectListener
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
  private float mMoveX, mMoveY;
50

    
51
  private boolean mFinishRotation, mRemoveRotation, mRemovePatternRotation, mAddRotation,
52
                  mSetQuat, mChangeObject, mSolveObject, mScrambleObject, mRecreateObject,
53
                  mInitializeObject, mSetTextureMap, mResetAllTextureMaps, mSolve;
54
  private boolean mUIBlocked, mTouchBlocked, mIsSolved;
55
  private long mRotationFinishedID;
56
  private final long[] mEffectID;
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
    mRecreateObject       = false;
81
    mSolveObject          = false;
82
    mSolve                = false;
83
    mScrambleObject       = false;
84

    
85
    mOldObject = null;
86
    mNewObject = null;
87

    
88
    mDebug = "";
89
    mScrambleObjectNum = 0;
90

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

    
93
    mBlockController = new BlockController(act,this);
94
    unblockEverything();
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  private void createObjectNow(ObjectType object)
100
    {
101
    boolean firstTime = (mNewObject==null);
102

    
103
    if( mOldObject!=null ) mOldObject.releaseResources();
104
    mOldObject = mNewObject;
105
    Static3D move = new Static3D(mMoveX,mMoveY,0);
106

    
107
    mNewObject = object.create( mController.getQuat(), move, mAct.get().getResources());
108

    
109
    if( mNewObject!=null )
110
      {
111
      TwistyObjectNode node = mController.getNode();
112
      if( node!=null ) mNewObject.setObjectRatioNow( 1.0f, node.getScaleFactor() );
113
      mController.setMovement(mNewObject.getMovement());
114
      if( firstTime && mPreferences!=null ) mNewObject.restorePreferences(mPreferences);
115
      mIsSolved = mNewObject.isSolved();
116
      }
117
    }
118

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

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

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

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

    
143
    boolean solved = mNewObject.isSolved();
144

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

    
156
    mIsSolved = solved;
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

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

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

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

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

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

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

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

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

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

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

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

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

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  private void recreateObjectNow()
227
    {
228
    mRecreateObject = false;
229

    
230
    if ( mNewObject!=null )
231
      {
232
      blockEverything(BlockController.PLACE_1);
233
      createObjectNow(mNewObject.getObjectType());
234
      doEffectNow( BaseEffect.Type.SIZECHANGE );
235
      }
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  private void scrambleObjectNow()
241
    {
242
    mScrambleObject = false;
243
    mIsSolved       = false;
244
    blockEverything(BlockController.PLACE_3);
245
    doEffectNow( BaseEffect.Type.SCRAMBLE );
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  private void solveObjectNow()
251
    {
252
    mSolveObject = false;
253
    blockEverything(BlockController.PLACE_4);
254
    doEffectNow( BaseEffect.Type.SOLVE );
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  private void solveNow()
260
    {
261
    mSolve = false;
262
    if( mNewObject!=null ) mNewObject.solve();
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  private void initializeObjectNow()
268
    {
269
    mInitializeObject = false;
270
    mNewObject.initializeObject(mNextMoves);
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
  private void setTextureMapNow()
276
    {
277
    mSetTextureMap = false;
278

    
279
    if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  private void resetAllTextureMapsNow()
285
    {
286
    mResetAllTextureMaps = false;
287
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
  private void setQuatNow()
293
    {
294
    mSetQuat = false;
295
    mController.setQuat();
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  void rememberMove(int axis, int row, int angle)
301
    {
302
    mDebug += (" (m "+axis+" "+(1<<row)+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  void finishRotation(int nearestAngle)
308
    {
309
    mNearestAngle   = nearestAngle;
310
    mFinishRotation = true;
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
  void setTextureMap(int cubit, int face, int newColor)
316
    {
317
    mSetTextureMap = true;
318

    
319
    mCubit    = cubit;
320
    mFace     = face;
321
    mNewColor = newColor;
322
    }
323

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

    
326
  void setQuatOnNextRender()
327
    {
328
    mSetQuat = true;
329
    }
330

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

    
333
  void setMove(float xmove, float ymove)
334
    {
335
    mMoveX = xmove;
336
    mMoveY = ymove;
337
    }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340
// INTERNAL API
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

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

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

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

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
  public float getMoveX()
358
    {
359
    return mMoveX;
360
    }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
  public float getMoveY()
365
    {
366
    return mMoveY;
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370
// PUBLIC API
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
  public void setScreenSize()
374
    {
375
    if( mNewObject!=null ) mNewObject.createTexture();
376
    }
377

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

    
380
  public void savePreferences(SharedPreferences.Editor editor)
381
    {
382
    if( mNewObject!=null )
383
      {
384
      mNewObject.savePreferences(editor);
385
      }
386
    }
387

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389

    
390
  public void restorePreferences(SharedPreferences preferences)
391
    {
392
    mPreferences = preferences;
393
    }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
  public void changeObject(ObjectType object)
398
    {
399
    mChangeObject = true;
400
    mNextObject = object;
401
    }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

    
405
  public void recreateObject()
406
    {
407
    mRecreateObject = true;
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
  public boolean isTouchBlocked()
413
    {
414
    return mTouchBlocked;
415
    }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
  public boolean isUINotBlocked()
420
    {
421
    return !mUIBlocked;
422
    }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
  public void blockEverything(int place)
427
    {
428
    mUIBlocked   = true;
429
    mTouchBlocked= true;
430
    mBlockController.touchBlocked(place);
431
    mBlockController.uiBlocked(place);
432
    }
433

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

    
436
  public void blockTouch(int place)
437
    {
438
    mTouchBlocked= true;
439
    mBlockController.touchBlocked(place);
440
    }
441

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

    
444
  public void unblockEverything()
445
    {
446
    mUIBlocked   = false;
447
    mTouchBlocked= false;
448
    mBlockController.touchUnblocked();
449
    mBlockController.uiUnblocked();
450
    }
451

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

    
454
  public void unblockTouch()
455
    {
456
    mTouchBlocked= false;
457
    mBlockController.touchUnblocked();
458
    }
459

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

    
462
  public void unblockUI()
463
    {
464
    mUIBlocked= false;
465
    mBlockController.uiUnblocked();
466
    }
467

    
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469

    
470
  public void preRender()
471
    {
472
    if( mSolve                 ) solveNow();
473
    if( mSetQuat               ) setQuatNow();
474
    if( mFinishRotation        ) finishRotationNow();
475
    if( mRemoveRotation        ) removeRotationNow();
476
    if( mRemovePatternRotation ) removePatternRotationNow();
477
    if( mChangeObject          ) changeObjectNow();
478
    if( mRecreateObject        ) recreateObjectNow();
479
    if( mSolveObject           ) solveObjectNow();
480
    if( mScrambleObject        ) scrambleObjectNow();
481
    if( mAddRotation           ) addRotationNow();
482
    if( mInitializeObject      ) initializeObjectNow();
483
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
484
    if( mSetTextureMap         ) setTextureMapNow();
485
    }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488

    
489
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
490
    {
491
    mAddRotation = true;
492

    
493
    int basicAngle= mNewObject.getBasicAngle()[axis];
494
    int angle     = bareAngle*(360/basicAngle);
495
    int duration  = Math.abs(angle)*millPreDegree;
496

    
497
    mAddActionListener    = listener;
498
    mAddRotationAxis      = axis;
499
    mAddRotationRowBitmap = rowBitmap;
500
    mAddRotationAngle     = angle;
501
    mAddRotationDuration  = duration;
502

    
503
    if( listener instanceof ScrambleEffect )
504
      {
505
      mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
506
      }
507
    }
508

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

    
511
  public void initializeObject(int[][] moves)
512
    {
513
    mInitializeObject = true;
514
    mNextMoves = moves;
515
    }
516

    
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518

    
519
  public void scrambleObject(int num)
520
    {
521
    if( !mUIBlocked )
522
      {
523
      mScrambleObject = true;
524
      mScrambleObjectNum = num;
525
      mDebug = "";
526
      mDebugStartTime = System.currentTimeMillis();
527
      }
528
    }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531
// this starts the Solve Effect
532

    
533
  public void solveObject()
534
    {
535
    if( !mUIBlocked )
536
      {
537
      mSolveObject = true;
538
      }
539
    }
540

    
541
///////////////////////////////////////////////////////////////////////////////////////////////////
542
// this only sets the cubits state to solved
543

    
544
  public void solveOnly()
545
    {
546
    mSolve = true;
547
    }
548

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

    
551
  public void resetAllTextureMaps()
552
    {
553
    mResetAllTextureMaps = true;
554
    }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557

    
558
  public TwistyObject getObject()
559
    {
560
    return mNewObject;
561
    }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564

    
565
  public TwistyObjectNode getObjectNode()
566
    {
567
    return mController.getNode();
568
    }
569

    
570
///////////////////////////////////////////////////////////////////////////////////////////////////
571

    
572
  public void effectFinished(final long effectID)
573
    {
574
    if( effectID == mRotationFinishedID )
575
      {
576
      mRotationFinishedID = 0;
577
      removeRotation();
578
      }
579
    else if( effectID == mAddRotationID )
580
      {
581
      mAddRotationID = 0;
582
      mRemoveRotationID = effectID;
583
      removePatternRotation();
584
      }
585
    else
586
      {
587
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
588
        {
589
        if( effectID == mEffectID[i] )
590
          {
591
          if( i!=BaseEffect.Type.WIN.ordinal() ) unblockEverything();
592
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() ) mInterface.onScrambleEffectFinished();
593
          if( i==BaseEffect.Type.WIN.ordinal()      ) mInterface.onWinEffectFinished(mDebug,mScrambleObjectNum);
594
          break;
595
          }
596
        }
597
      }
598
    }
599
  }
(9-9/17)