Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectPreRender.java @ 02d80fe6

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
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 Static3D mMove;
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
    mMove = new Static3D(0,0,0);
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

    
110
    mNewObject = object.create( mController.getQuat(), mController.getMove(), 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
      DistortedScreen screen = mController.getInterface().getScreen();
129
      mEffectID[index] = type.startEffect(screen,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(Static3D move)
322
    {
323
    mMove = move;
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327
// INTERNAL API
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

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

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

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

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

    
344
  public Static3D getMove()
345
    {
346
    return mMove;
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350
// PUBLIC API
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  public void setScreenSize(int width)
354
    {
355
    if( mNewObject!=null )
356
      {
357
      mNewObject.createTexture();
358
      mNewObject.recomputeScaleFactor(width);
359
      }
360
    mScreenWidth = width;
361
    }
362

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

    
365
  public void savePreferences(SharedPreferences.Editor editor)
366
    {
367
    if( mNewObject!=null )
368
      {
369
      mNewObject.savePreferences(editor);
370
      }
371
    }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374

    
375
  public void restorePreferences(SharedPreferences preferences)
376
    {
377
    mPreferences = preferences;
378
    }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

    
382
  public void changeObject(ObjectType object)
383
    {
384
    mChangeObject = true;
385
    mNextObject = object;
386
    }
387

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

    
390
  public boolean isTouchBlocked()
391
    {
392
    return mTouchBlocked;
393
    }
394

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

    
397
  public boolean isUINotBlocked()
398
    {
399
    return !mUIBlocked;
400
    }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

    
404
  public void blockEverything(int place)
405
    {
406
    mUIBlocked   = true;
407
    mTouchBlocked= true;
408
    mBlockController.touchBlocked(place);
409
    mBlockController.uiBlocked(place);
410
    }
411

    
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413

    
414
  public void blockTouch(int place)
415
    {
416
    mTouchBlocked= true;
417
    mBlockController.touchBlocked(place);
418
    }
419

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

    
422
  public void unblockEverything()
423
    {
424
    mUIBlocked   = false;
425
    mTouchBlocked= false;
426
    mBlockController.touchUnblocked();
427
    mBlockController.uiUnblocked();
428
    }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

    
432
  public void unblockTouch()
433
    {
434
    mTouchBlocked= false;
435
    mBlockController.touchUnblocked();
436
    }
437

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

    
440
  public void unblockUI()
441
    {
442
    mUIBlocked= false;
443
    mBlockController.uiUnblocked();
444
    }
445

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

    
448
  public void preRender()
449
    {
450
    if( mSolve                 ) solveNow();
451
    if( mSetQuat               ) setQuatNow();
452
    if( mFinishRotation        ) finishRotationNow();
453
    if( mRemoveRotation        ) removeRotationNow();
454
    if( mRemovePatternRotation ) removePatternRotationNow();
455
    if( mChangeObject          ) changeObjectNow();
456
    if( mSolveObject           ) solveObjectNow();
457
    if( mScrambleObject        ) scrambleObjectNow();
458
    if( mAddRotation           ) addRotationNow();
459
    if( mInitializeObject      ) initializeObjectNow();
460
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
461
    if( mSetTextureMap         ) setTextureMapNow();
462
    }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

    
466
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
467
    {
468
    mAddRotation = true;
469

    
470
    int basicAngle= mNewObject.getBasicAngle()[axis];
471
    int angle     = bareAngle*(360/basicAngle);
472
    int duration  = Math.abs(angle)*millPreDegree;
473

    
474
    mAddActionListener    = listener;
475
    mAddRotationAxis      = axis;
476
    mAddRotationRowBitmap = rowBitmap;
477
    mAddRotationAngle     = angle;
478
    mAddRotationDuration  = duration;
479

    
480
    if( listener instanceof ScrambleEffect )
481
      {
482
      mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
483
      }
484
    }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
  public void initializeObject(int[][] moves)
489
    {
490
    mInitializeObject = true;
491
    mNextMoves = moves;
492
    }
493

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

    
496
  public void scrambleObject(int num)
497
    {
498
    if( !mUIBlocked )
499
      {
500
      mScrambleObject = true;
501
      mScrambleObjectNum = num;
502
      mDebug = "";
503
      mDebugStartTime = System.currentTimeMillis();
504
      }
505
    }
506

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508
// this starts the Solve Effect
509

    
510
  public void solveObject()
511
    {
512
    if( !mUIBlocked )
513
      {
514
      mSolveObject = true;
515
      }
516
    }
517

    
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519
// this only sets the cubits state to solved
520

    
521
  public void solveOnly()
522
    {
523
    mSolve = true;
524
    }
525

    
526
///////////////////////////////////////////////////////////////////////////////////////////////////
527

    
528
  public void resetAllTextureMaps()
529
    {
530
    mResetAllTextureMaps = true;
531
    }
532

    
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534

    
535
  public TwistyObject getObject()
536
    {
537
    return mNewObject;
538
    }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541

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