Project

General

Profile

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

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

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.main.DistortedFramebuffer;
28
import org.distorted.library.message.EffectListener;
29

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

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

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

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

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

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

    
86
    mOldObject = null;
87
    mNewObject = null;
88

    
89
    mDebug = "";
90

    
91
    mScreenWidth = 0;
92
    mScrambleObjectNum = 0;
93

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

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

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  private void createObjectNow(ObjectType object)
103
    {
104
    boolean firstTime = (mNewObject==null);
105

    
106
    if( mOldObject!=null ) mOldObject.releaseResources();
107
    mOldObject = mNewObject;
108
    Static3D move = new Static3D(mMoveX,mMoveY,0);
109

    
110
    mNewObject = object.create( mController.getQuat(), move, mAct.get().getResources(), mScreenWidth);
111

    
112
    if( mNewObject!=null )
113
      {
114
      mController.setMovement(mNewObject.getMovement());
115
      if( firstTime && mPreferences!=null ) 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
      DistortedFramebuffer frame = mController.getInterface().getFramebuffer();
129
      mEffectID[index] = type.startEffect(frame,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
      mInterface.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);
222
      doEffectNow( BaseEffect.Type.SIZECHANGE );
223
      }
224
    }
225

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

    
228
  private void scrambleObjectNow()
229
    {
230
    mScrambleObject = false;
231
    mIsSolved       = false;
232
    blockEverything(BlockController.PLACE_3);
233
    doEffectNow( BaseEffect.Type.SCRAMBLE );
234
    }
235

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

    
238
  private void solveObjectNow()
239
    {
240
    mSolveObject = false;
241
    blockEverything(BlockController.PLACE_4);
242
    doEffectNow( BaseEffect.Type.SOLVE );
243
    }
244

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

    
247
  private void solveNow()
248
    {
249
    mSolve = false;
250
    mNewObject.solve();
251
    }
252

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

    
255
  private void initializeObjectNow()
256
    {
257
    mInitializeObject = false;
258
    mNewObject.initializeObject(mNextMoves);
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  private void setTextureMapNow()
264
    {
265
    mSetTextureMap = false;
266

    
267
    if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
268
    }
269

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

    
272
  private void resetAllTextureMapsNow()
273
    {
274
    mResetAllTextureMaps = false;
275
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
276
    }
277

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

    
280
  private void setQuatNow()
281
    {
282
    mSetQuat = false;
283
    mController.setQuat();
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
  void rememberMove(int axis, int row, int angle)
289
    {
290
    mDebug += (" (m "+axis+" "+(1<<row)+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
  void finishRotation(int nearestAngle)
296
    {
297
    mNearestAngle   = nearestAngle;
298
    mFinishRotation = true;
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
  void setTextureMap(int cubit, int face, int newColor)
304
    {
305
    mSetTextureMap = true;
306

    
307
    mCubit    = cubit;
308
    mFace     = face;
309
    mNewColor = newColor;
310
    }
311

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

    
314
  void setQuatOnNextRender()
315
    {
316
    mSetQuat = true;
317
    }
318

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

    
321
  void setMove(float xmove, float ymove)
322
    {
323
    mMoveX = xmove;
324
    mMoveY = ymove;
325
    }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328
// INTERNAL API
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
  public int getNumScrambles()
332
    {
333
    return mScrambleObjectNum;
334
    }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
  public TwistyObject getOldObject()
339
    {
340
    return mOldObject;
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
  public float getMoveX()
346
    {
347
    return mMoveX;
348
    }
349

    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351

    
352
  public float getMoveY()
353
    {
354
    return mMoveY;
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358
// PUBLIC API
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

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

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
  public void savePreferences(SharedPreferences.Editor editor)
374
    {
375
    if( mNewObject!=null )
376
      {
377
      mNewObject.savePreferences(editor);
378
      }
379
    }
380

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

    
383
  public void restorePreferences(SharedPreferences preferences)
384
    {
385
    mPreferences = preferences;
386
    }
387

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

    
390
  public void changeObject(ObjectType object)
391
    {
392
    mChangeObject = true;
393
    mNextObject = object;
394
    }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  public boolean isTouchBlocked()
399
    {
400
    return mTouchBlocked;
401
    }
402

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

    
405
  public boolean isUINotBlocked()
406
    {
407
    return !mUIBlocked;
408
    }
409

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

    
412
  public void blockEverything(int place)
413
    {
414
    mUIBlocked   = true;
415
    mTouchBlocked= true;
416
    mBlockController.touchBlocked(place);
417
    mBlockController.uiBlocked(place);
418
    }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

    
422
  public void blockTouch(int place)
423
    {
424
    mTouchBlocked= true;
425
    mBlockController.touchBlocked(place);
426
    }
427

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429

    
430
  public void unblockEverything()
431
    {
432
    mUIBlocked   = false;
433
    mTouchBlocked= false;
434
    mBlockController.touchUnblocked();
435
    mBlockController.uiUnblocked();
436
    }
437

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

    
440
  public void unblockTouch()
441
    {
442
    mTouchBlocked= false;
443
    mBlockController.touchUnblocked();
444
    }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

    
448
  public void unblockUI()
449
    {
450
    mUIBlocked= false;
451
    mBlockController.uiUnblocked();
452
    }
453

    
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455

    
456
  public void preRender()
457
    {
458
    if( mSolve                 ) solveNow();
459
    if( mSetQuat               ) setQuatNow();
460
    if( mFinishRotation        ) finishRotationNow();
461
    if( mRemoveRotation        ) removeRotationNow();
462
    if( mRemovePatternRotation ) removePatternRotationNow();
463
    if( mChangeObject          ) changeObjectNow();
464
    if( mSolveObject           ) solveObjectNow();
465
    if( mScrambleObject        ) scrambleObjectNow();
466
    if( mAddRotation           ) addRotationNow();
467
    if( mInitializeObject      ) initializeObjectNow();
468
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
469
    if( mSetTextureMap         ) setTextureMapNow();
470
    }
471

    
472
///////////////////////////////////////////////////////////////////////////////////////////////////
473

    
474
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
475
    {
476
    mAddRotation = true;
477

    
478
    int basicAngle= mNewObject.getBasicAngle()[axis];
479
    int angle     = bareAngle*(360/basicAngle);
480
    int duration  = Math.abs(angle)*millPreDegree;
481

    
482
    mAddActionListener    = listener;
483
    mAddRotationAxis      = axis;
484
    mAddRotationRowBitmap = rowBitmap;
485
    mAddRotationAngle     = angle;
486
    mAddRotationDuration  = duration;
487

    
488
    if( listener instanceof ScrambleEffect )
489
      {
490
      mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
491
      }
492
    }
493

    
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495

    
496
  public void initializeObject(int[][] moves)
497
    {
498
    mInitializeObject = true;
499
    mNextMoves = moves;
500
    }
501

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

    
504
  public void scrambleObject(int num)
505
    {
506
    if( !mUIBlocked )
507
      {
508
      mScrambleObject = true;
509
      mScrambleObjectNum = num;
510
      mDebug = "";
511
      mDebugStartTime = System.currentTimeMillis();
512
      }
513
    }
514

    
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516
// this starts the Solve Effect
517

    
518
  public void solveObject()
519
    {
520
    if( !mUIBlocked )
521
      {
522
      mSolveObject = true;
523
      }
524
    }
525

    
526
///////////////////////////////////////////////////////////////////////////////////////////////////
527
// this only sets the cubits state to solved
528

    
529
  public void solveOnly()
530
    {
531
    mSolve = true;
532
    }
533

    
534
///////////////////////////////////////////////////////////////////////////////////////////////////
535

    
536
  public void resetAllTextureMaps()
537
    {
538
    mResetAllTextureMaps = true;
539
    }
540

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

    
543
  public TwistyObject getObject()
544
    {
545
    return mNewObject;
546
    }
547

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

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