Project

General

Profile

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

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

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.DistortedScreen;
28

    
29
import org.distorted.objectlib.helpers.ObjectLibInterface;
30
import org.distorted.objectlib.effects.BaseEffect;
31
import org.distorted.objectlib.effects.EffectController;
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 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, 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
    mSolveObject          = false;
81
    mSolve                = false;
82
    mScrambleObject       = false;
83

    
84
    mOldObject = null;
85
    mNewObject = null;
86

    
87
    mDebug = "";
88

    
89
    mScreenWidth = 0;
90
    mScrambleObjectNum = 0;
91

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

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

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

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

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

    
107
    mNewObject = object.create( mController.getQuat(), mAct.get().getResources(), mScreenWidth);
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
      DistortedScreen screen = mController.getInterface().getScreen();
126
      mEffectID[index] = type.startEffect(screen,this);
127
      }
128
    catch( Exception ex )
129
      {
130
      android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
131
      unblockEverything();
132
      }
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

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

    
142
    boolean solved = mNewObject.isSolved();
143

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

    
155
    mIsSolved = solved;
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

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

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

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

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

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

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

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

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

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

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

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

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

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  private void solveObjectNow()
236
    {
237
    mSolveObject = false;
238
    blockEverything(BlockController.PLACE_4);
239
    doEffectNow( BaseEffect.Type.SOLVE );
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  private void solveNow()
245
    {
246
    mSolve = false;
247
    mNewObject.solve();
248
    }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
  private void initializeObjectNow()
253
    {
254
    mInitializeObject = false;
255
    mNewObject.initializeObject(mNextMoves);
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  private void setTextureMapNow()
261
    {
262
    mSetTextureMap = false;
263

    
264
    if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
265
    }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
  private void resetAllTextureMapsNow()
270
    {
271
    mResetAllTextureMaps = false;
272
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  private void setQuatNow()
278
    {
279
    mSetQuat = false;
280
    mController.setQuat();
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

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

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

    
292
  void finishRotation(int nearestAngle)
293
    {
294
    mNearestAngle   = nearestAngle;
295
    mFinishRotation = true;
296
    }
297

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

    
300
  void setTextureMap(int cubit, int face, int newColor)
301
    {
302
    mSetTextureMap = true;
303

    
304
    mCubit    = cubit;
305
    mFace     = face;
306
    mNewColor = newColor;
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
  void setQuatOnNextRender()
312
    {
313
    mSetQuat = true;
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317
// INTERNAL API
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  public int getNumScrambles()
321
    {
322
    return mScrambleObjectNum;
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  public TwistyObject getOldObject()
328
    {
329
    return mOldObject;
330
    }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333
// PUBLIC API
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

    
336
  public void setScreenSize(int width)
337
    {
338
    if( mNewObject!=null )
339
      {
340
      mNewObject.createTexture();
341
      mNewObject.recomputeScaleFactor(width);
342
      }
343
    mScreenWidth = width;
344
    }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
  public void savePreferences(SharedPreferences.Editor editor)
349
    {
350
    if( mNewObject!=null )
351
      {
352
      mNewObject.savePreferences(editor);
353
      }
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
  public void restorePreferences(SharedPreferences preferences)
359
    {
360
    mPreferences = preferences;
361
    }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
  public void changeObject(ObjectType object)
366
    {
367
    mChangeObject = true;
368
    mNextObject = object;
369
    }
370

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

    
373
  public boolean isTouchBlocked()
374
    {
375
    return mTouchBlocked;
376
    }
377

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

    
380
  public boolean isUINotBlocked()
381
    {
382
    return !mUIBlocked;
383
    }
384

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

    
387
  public void blockEverything(int place)
388
    {
389
    mUIBlocked   = true;
390
    mTouchBlocked= true;
391
    mBlockController.touchBlocked(place);
392
    mBlockController.uiBlocked(place);
393
    }
394

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

    
397
  public void blockTouch(int place)
398
    {
399
    mTouchBlocked= true;
400
    mBlockController.touchBlocked(place);
401
    }
402

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

    
405
  public void unblockEverything()
406
    {
407
    mUIBlocked   = false;
408
    mTouchBlocked= false;
409
    mBlockController.touchUnblocked();
410
    mBlockController.uiUnblocked();
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
  public void unblockTouch()
416
    {
417
    mTouchBlocked= false;
418
    mBlockController.touchUnblocked();
419
    }
420

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422

    
423
  public void unblockUI()
424
    {
425
    mUIBlocked= false;
426
    mBlockController.uiUnblocked();
427
    }
428

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430

    
431
  public void preRender()
432
    {
433
    if( mSolve                 ) solveNow();
434
    if( mSetQuat               ) setQuatNow();
435
    if( mFinishRotation        ) finishRotationNow();
436
    if( mRemoveRotation        ) removeRotationNow();
437
    if( mRemovePatternRotation ) removePatternRotationNow();
438
    if( mChangeObject          ) changeObjectNow();
439
    if( mSolveObject           ) solveObjectNow();
440
    if( mScrambleObject        ) scrambleObjectNow();
441
    if( mAddRotation           ) addRotationNow();
442
    if( mInitializeObject      ) initializeObjectNow();
443
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
444
    if( mSetTextureMap         ) setTextureMapNow();
445
    }
446

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448

    
449
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
450
    {
451
    mAddRotation = true;
452

    
453
    int basicAngle= mNewObject.getBasicAngle()[axis];
454
    int angle     = bareAngle*(360/basicAngle);
455
    int duration  = Math.abs(angle)*millPreDegree;
456

    
457
    mAddActionListener    = listener;
458
    mAddRotationAxis      = axis;
459
    mAddRotationRowBitmap = rowBitmap;
460
    mAddRotationAngle     = angle;
461
    mAddRotationDuration  = duration;
462

    
463
    if( listener instanceof ScrambleEffect )
464
      {
465
      mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
466
      }
467
    }
468

    
469
///////////////////////////////////////////////////////////////////////////////////////////////////
470

    
471
  public void initializeObject(int[][] moves)
472
    {
473
    mInitializeObject = true;
474
    mNextMoves = moves;
475
    }
476

    
477
///////////////////////////////////////////////////////////////////////////////////////////////////
478

    
479
  public void scrambleObject(int num)
480
    {
481
    if( !mUIBlocked )
482
      {
483
      mScrambleObject = true;
484
      mScrambleObjectNum = num;
485
      mDebug = "";
486
      mDebugStartTime = System.currentTimeMillis();
487
      }
488
    }
489

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491
// this starts the Solve Effect
492

    
493
  public void solveObject()
494
    {
495
    if( !mUIBlocked )
496
      {
497
      mSolveObject = true;
498
      }
499
    }
500

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502
// this only sets the cubits state to solved
503

    
504
  public void solveOnly()
505
    {
506
    mSolve = true;
507
    }
508

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

    
511
  public void resetAllTextureMaps()
512
    {
513
    mResetAllTextureMaps = true;
514
    }
515

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

    
518
  public TwistyObject getObject()
519
    {
520
    return mNewObject;
521
    }
522

    
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524

    
525
  public void effectFinished(final long effectID)
526
    {
527
    if( effectID == mRotationFinishedID )
528
      {
529
      mRotationFinishedID = 0;
530
      removeRotation();
531
      }
532
    else if( effectID == mAddRotationID )
533
      {
534
      mAddRotationID = 0;
535
      mRemoveRotationID = effectID;
536
      removePatternRotation();
537
      }
538
    else
539
      {
540
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
541
        {
542
        if( effectID == mEffectID[i] )
543
          {
544
          if( i!=BaseEffect.Type.WIN.ordinal() ) unblockEverything();
545
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() ) mInterface.onScrambleEffectFinished();
546
          if( i==BaseEffect.Type.WIN.ordinal()      ) mInterface.onWinEffectFinished(mDebug,mScrambleObjectNum);
547
          break;
548
          }
549
        }
550
      }
551
    }
552
  }
(8-8/15)