Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectPreRender.java @ 7ba38dd4

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
      mController.setMovement(mNewObject.getMovement());
112
      if( firstTime && mPreferences!=null ) mNewObject.restorePreferences(mPreferences);
113
      mIsSolved = mNewObject.isSolved();
114
      }
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
119

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

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  private void removeRotationNow()
137
    {
138
    mRemoveRotation=false;
139
    mNewObject.removeRotationNow();
140

    
141
    boolean solved = mNewObject.isSolved();
142

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

    
154
    mIsSolved = solved;
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  private void removeRotation()
160
    {
161
    mRemoveRotation = true;
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  private void removePatternRotation()
167
    {
168
    mRemovePatternRotation = true;
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

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

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

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

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

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

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

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

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  private void changeObjectNow()
211
    {
212
    mChangeObject = false;
213

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

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  private void recreateObjectNow()
225
    {
226
    mRecreateObject = false;
227

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

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

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

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

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

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

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

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

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

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

    
273
  private void setTextureMapNow()
274
    {
275
    mSetTextureMap = false;
276

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

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

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

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

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

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

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

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

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

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

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

    
317
    mCubit    = cubit;
318
    mFace     = face;
319
    mNewColor = newColor;
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  void setQuatOnNextRender()
325
    {
326
    mSetQuat = true;
327
    }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
  void setMove(float xmove, float ymove)
332
    {
333
    mMoveX = xmove;
334
    mMoveY = ymove;
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

    
355
  public float getMoveX()
356
    {
357
    return mMoveX;
358
    }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

    
362
  public float getMoveY()
363
    {
364
    return mMoveY;
365
    }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368
// PUBLIC API
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

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

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

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

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

    
388
  public void restorePreferences(SharedPreferences preferences)
389
    {
390
    mPreferences = preferences;
391
    }
392

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

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

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

    
403
  public void recreateObject()
404
    {
405
    mRecreateObject = true;
406
    }
407

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

    
410
  public boolean isTouchBlocked()
411
    {
412
    return mTouchBlocked;
413
    }
414

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

    
417
  public boolean isUINotBlocked()
418
    {
419
    return !mUIBlocked;
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

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

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

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

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

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

    
450
///////////////////////////////////////////////////////////////////////////////////////////////////
451

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

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

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

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

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

    
485
///////////////////////////////////////////////////////////////////////////////////////////////////
486

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

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

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

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

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508

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

    
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516

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

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529
// this starts the Solve Effect
530

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

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540
// this only sets the cubits state to solved
541

    
542
  public void solveOnly()
543
    {
544
    mSolve = true;
545
    }
546

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

    
549
  public void resetAllTextureMaps()
550
    {
551
    mResetAllTextureMaps = true;
552
    }
553

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

    
556
  public TwistyObject getObject()
557
    {
558
    return mNewObject;
559
    }
560

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562

    
563
  public TwistyObjectNode getObjectNode()
564
    {
565
    return mController.getNode();
566
    }
567

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569

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