Project

General

Profile

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

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

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, mRecreateObject,
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
    mRecreateObject       = false;
83
    mSolveObject          = false;
84
    mSolve                = false;
85
    mScrambleObject       = false;
86

    
87
    mOldObject = null;
88
    mNewObject = null;
89

    
90
    mDebug = "";
91

    
92
    mScreenWidth = 0;
93
    mScrambleObjectNum = 0;
94

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

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

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

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

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

    
111
    mNewObject = object.create( mController.getQuat(), move, mAct.get().getResources(), 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
      DistortedFramebuffer frame = mController.getInterface().getFramebuffer();
130
      mEffectID[index] = type.startEffect(frame,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);
223
      doEffectNow( BaseEffect.Type.SIZECHANGE );
224
      }
225
    }
226

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

    
229
  private void recreateObjectNow()
230
    {
231
    mRecreateObject = false;
232

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

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

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

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  private void solveObjectNow()
254
    {
255
    mSolveObject = false;
256
    blockEverything(BlockController.PLACE_4);
257
    doEffectNow( BaseEffect.Type.SOLVE );
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
  private void solveNow()
263
    {
264
    mSolve = false;
265
    if( mNewObject!=null ) mNewObject.solve();
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  private void initializeObjectNow()
271
    {
272
    mInitializeObject = false;
273
    mNewObject.initializeObject(mNextMoves);
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
  private void setTextureMapNow()
279
    {
280
    mSetTextureMap = false;
281

    
282
    if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
  private void resetAllTextureMapsNow()
288
    {
289
    mResetAllTextureMaps = false;
290
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
291
    }
292

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

    
295
  private void setQuatNow()
296
    {
297
    mSetQuat = false;
298
    mController.setQuat();
299
    }
300

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

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

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  void finishRotation(int nearestAngle)
311
    {
312
    mNearestAngle   = nearestAngle;
313
    mFinishRotation = true;
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
  void setTextureMap(int cubit, int face, int newColor)
319
    {
320
    mSetTextureMap = true;
321

    
322
    mCubit    = cubit;
323
    mFace     = face;
324
    mNewColor = newColor;
325
    }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
  void setQuatOnNextRender()
330
    {
331
    mSetQuat = true;
332
    }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

    
336
  void setMove(float xmove, float ymove)
337
    {
338
    mMoveX = xmove;
339
    mMoveY = ymove;
340
    }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343
// INTERNAL API
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
  public int getNumScrambles()
347
    {
348
    return mScrambleObjectNum;
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  public TwistyObject getOldObject()
354
    {
355
    return mOldObject;
356
    }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

    
360
  public float getMoveX()
361
    {
362
    return mMoveX;
363
    }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

    
367
  public float getMoveY()
368
    {
369
    return mMoveY;
370
    }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373
// PUBLIC API
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

    
376
  public void setScreenSize(int width)
377
    {
378
    if( mNewObject!=null )
379
      {
380
      mNewObject.createTexture();
381
      mNewObject.recomputeScaleFactor(width);
382
      }
383
    mScreenWidth = width;
384
    }
385

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

    
388
  public void savePreferences(SharedPreferences.Editor editor)
389
    {
390
    if( mNewObject!=null )
391
      {
392
      mNewObject.savePreferences(editor);
393
      }
394
    }
395

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

    
398
  public void restorePreferences(SharedPreferences preferences)
399
    {
400
    mPreferences = preferences;
401
    }
402

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

    
405
  public void changeObject(ObjectType object)
406
    {
407
    mChangeObject = true;
408
    mNextObject = object;
409
    }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

    
413
  public void recreateObject()
414
    {
415
    mRecreateObject = true;
416
    }
417

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

    
420
  public boolean isTouchBlocked()
421
    {
422
    return mTouchBlocked;
423
    }
424

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

    
427
  public boolean isUINotBlocked()
428
    {
429
    return !mUIBlocked;
430
    }
431

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

    
434
  public void blockEverything(int place)
435
    {
436
    mUIBlocked   = true;
437
    mTouchBlocked= true;
438
    mBlockController.touchBlocked(place);
439
    mBlockController.uiBlocked(place);
440
    }
441

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

    
444
  public void blockTouch(int place)
445
    {
446
    mTouchBlocked= true;
447
    mBlockController.touchBlocked(place);
448
    }
449

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

    
452
  public void unblockEverything()
453
    {
454
    mUIBlocked   = false;
455
    mTouchBlocked= false;
456
    mBlockController.touchUnblocked();
457
    mBlockController.uiUnblocked();
458
    }
459

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

    
462
  public void unblockTouch()
463
    {
464
    mTouchBlocked= false;
465
    mBlockController.touchUnblocked();
466
    }
467

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

    
470
  public void unblockUI()
471
    {
472
    mUIBlocked= false;
473
    mBlockController.uiUnblocked();
474
    }
475

    
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477

    
478
  public void preRender()
479
    {
480
    if( mSolve                 ) solveNow();
481
    if( mSetQuat               ) setQuatNow();
482
    if( mFinishRotation        ) finishRotationNow();
483
    if( mRemoveRotation        ) removeRotationNow();
484
    if( mRemovePatternRotation ) removePatternRotationNow();
485
    if( mChangeObject          ) changeObjectNow();
486
    if( mRecreateObject        ) recreateObjectNow();
487
    if( mSolveObject           ) solveObjectNow();
488
    if( mScrambleObject        ) scrambleObjectNow();
489
    if( mAddRotation           ) addRotationNow();
490
    if( mInitializeObject      ) initializeObjectNow();
491
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
492
    if( mSetTextureMap         ) setTextureMapNow();
493
    }
494

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496

    
497
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
498
    {
499
    mAddRotation = true;
500

    
501
    int basicAngle= mNewObject.getBasicAngle()[axis];
502
    int angle     = bareAngle*(360/basicAngle);
503
    int duration  = Math.abs(angle)*millPreDegree;
504

    
505
    mAddActionListener    = listener;
506
    mAddRotationAxis      = axis;
507
    mAddRotationRowBitmap = rowBitmap;
508
    mAddRotationAngle     = angle;
509
    mAddRotationDuration  = duration;
510

    
511
    if( listener instanceof ScrambleEffect )
512
      {
513
      mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
514
      }
515
    }
516

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

    
519
  public void initializeObject(int[][] moves)
520
    {
521
    mInitializeObject = true;
522
    mNextMoves = moves;
523
    }
524

    
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526

    
527
  public void scrambleObject(int num)
528
    {
529
    if( !mUIBlocked )
530
      {
531
      mScrambleObject = true;
532
      mScrambleObjectNum = num;
533
      mDebug = "";
534
      mDebugStartTime = System.currentTimeMillis();
535
      }
536
    }
537

    
538
///////////////////////////////////////////////////////////////////////////////////////////////////
539
// this starts the Solve Effect
540

    
541
  public void solveObject()
542
    {
543
    if( !mUIBlocked )
544
      {
545
      mSolveObject = true;
546
      }
547
    }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
// this only sets the cubits state to solved
551

    
552
  public void solveOnly()
553
    {
554
    mSolve = true;
555
    }
556

    
557
///////////////////////////////////////////////////////////////////////////////////////////////////
558

    
559
  public void resetAllTextureMaps()
560
    {
561
    mResetAllTextureMaps = true;
562
    }
563

    
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565

    
566
  public TwistyObject getObject()
567
    {
568
    return mNewObject;
569
    }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572

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