Project

General

Profile

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

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

1 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 81141862 Leszek Koltunski
import java.lang.ref.WeakReference;
23
24 b3fff6fb Leszek Koltunski
import android.app.Activity;
25 7c111294 Leszek Koltunski
import android.content.SharedPreferences;
26
27 dd00d051 Leszek Koltunski
import org.distorted.library.main.DistortedFramebuffer;
28 ecf3d6e3 Leszek Koltunski
import org.distorted.library.message.EffectListener;
29 81141862 Leszek Koltunski
30 02d80fe6 Leszek Koltunski
import org.distorted.library.type.Static3D;
31 b3fff6fb Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectLibInterface;
32 7c111294 Leszek Koltunski
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 15e5214c Leszek Koltunski
37 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39 ecf3d6e3 Leszek Koltunski
public class ObjectPreRender implements EffectListener
40 7c111294 Leszek Koltunski
  {
41 b3fff6fb Leszek Koltunski
  private final WeakReference<Activity> mAct;
42 15e5214c Leszek Koltunski
  private final ObjectControl mController;
43 7c111294 Leszek Koltunski
  private ObjectType mNextObject;
44
  private TwistyObject mOldObject, mNewObject;
45
  private SharedPreferences mPreferences;
46
  private MovesFinished mAddActionListener;
47
  private final BlockController mBlockController;
48 b3fff6fb Leszek Koltunski
  private final ObjectLibInterface mInterface;
49 7c111294 Leszek Koltunski
  private String mDebug;
50 72d6857c Leszek Koltunski
  private float mMoveX, mMoveY;
51 7c111294 Leszek Koltunski
52
  private boolean mFinishRotation, mRemoveRotation, mRemovePatternRotation, mAddRotation,
53 568d4698 Leszek Koltunski
                  mSetQuat, mChangeObject, mSolveObject, mScrambleObject, mRecreateObject,
54 7c111294 Leszek Koltunski
                  mInitializeObject, mSetTextureMap, mResetAllTextureMaps, mSolve;
55
  private boolean mUIBlocked, mTouchBlocked, mIsSolved;
56
  private long mRotationFinishedID;
57
  private final long[] mEffectID;
58 e7daa161 Leszek Koltunski
  private int mScreenWidth, mScreenHeight;
59 7c111294 Leszek Koltunski
  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 b3fff6fb Leszek Koltunski
  public ObjectPreRender(Activity act, ObjectControl controller, ObjectLibInterface actioner)
71 7c111294 Leszek Koltunski
    {
72 15e5214c Leszek Koltunski
    mAct = new WeakReference<>(act);
73 b3fff6fb Leszek Koltunski
    mInterface = actioner;
74 15e5214c Leszek Koltunski
    mController = controller;
75 7c111294 Leszek Koltunski
76
    mFinishRotation       = false;
77
    mRemoveRotation       = false;
78
    mRemovePatternRotation= false;
79
    mAddRotation          = false;
80
    mSetQuat              = false;
81
    mChangeObject         = false;
82 568d4698 Leszek Koltunski
    mRecreateObject       = false;
83 7c111294 Leszek Koltunski
    mSolveObject          = false;
84
    mSolve                = false;
85
    mScrambleObject       = false;
86
87
    mOldObject = null;
88
    mNewObject = null;
89
90
    mDebug = "";
91
92 e7daa161 Leszek Koltunski
    mScreenWidth  = 0;
93
    mScreenHeight = 0;
94 7c111294 Leszek Koltunski
    mScrambleObjectNum = 0;
95
96
    mEffectID = new long[BaseEffect.Type.LENGTH];
97
98 9276dced Leszek Koltunski
    mBlockController = new BlockController(act,this);
99 7c111294 Leszek Koltunski
    unblockEverything();
100
    }
101
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104 81141862 Leszek Koltunski
  private void createObjectNow(ObjectType object)
105 7c111294 Leszek Koltunski
    {
106
    boolean firstTime = (mNewObject==null);
107
108
    if( mOldObject!=null ) mOldObject.releaseResources();
109
    mOldObject = mNewObject;
110 72d6857c Leszek Koltunski
    Static3D move = new Static3D(mMoveX,mMoveY,0);
111 7c111294 Leszek Koltunski
112 e7daa161 Leszek Koltunski
    mNewObject = object.create( mController.getQuat(), move, mAct.get().getResources(), mScreenWidth, mScreenHeight);
113 7c111294 Leszek Koltunski
114
    if( mNewObject!=null )
115
      {
116 15e5214c Leszek Koltunski
      mController.setMovement(mNewObject.getMovement());
117 b09dd39b Leszek Koltunski
      if( firstTime && mPreferences!=null ) mNewObject.restorePreferences(mPreferences);
118 7c111294 Leszek Koltunski
      mIsSolved = mNewObject.isSolved();
119
      }
120
    }
121
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
124
125
  private void doEffectNow(BaseEffect.Type type)
126
    {
127
    try
128
      {
129
      int index = type.ordinal();
130 dd00d051 Leszek Koltunski
      DistortedFramebuffer frame = mController.getInterface().getFramebuffer();
131
      mEffectID[index] = type.startEffect(frame,this);
132 7c111294 Leszek Koltunski
      }
133
    catch( Exception ex )
134
      {
135
      android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
136
      unblockEverything();
137
      }
138
    }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142
  private void removeRotationNow()
143
    {
144
    mRemoveRotation=false;
145
    mNewObject.removeRotationNow();
146
147
    boolean solved = mNewObject.isSolved();
148
149
    if( solved && !mIsSolved )
150
      {
151 b3fff6fb Leszek Koltunski
      mInterface.onSolved();
152 7c111294 Leszek Koltunski
      unblockEverything();
153
      doEffectNow( BaseEffect.Type.WIN );
154
      }
155
    else
156
      {
157
      unblockEverything();
158
      }
159
160
    mIsSolved = solved;
161
    }
162
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
165
  private void removeRotation()
166
    {
167
    mRemoveRotation = true;
168
    }
169
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172
  private void removePatternRotation()
173
    {
174
    mRemovePatternRotation = true;
175
    }
176
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
179
  private void removePatternRotationNow()
180
    {
181
    mRemovePatternRotation=false;
182
    mNewObject.removeRotationNow();
183
    mAddActionListener.onActionFinished(mRemoveRotationID);
184
    }
185
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
188
  private void addRotationNow()
189
    {
190
    mAddRotation = false;
191
    mAddRotationID = mNewObject.addNewRotation( mAddRotationAxis, mAddRotationRowBitmap,
192
                                                mAddRotationAngle, mAddRotationDuration, this);
193
194
    if( mAddRotationID==0 ) // failed to add effect - should never happen
195
      {
196
      unblockEverything();
197
      }
198
    }
199
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
202
  private void finishRotationNow()
203
    {
204
    mFinishRotation = false;
205 b78ebd76 Leszek Koltunski
    blockEverything(BlockController.PLACE_0);
206 7c111294 Leszek Koltunski
    mRotationFinishedID = mNewObject.finishRotationNow(this, mNearestAngle);
207
208
    if( mRotationFinishedID==0 ) // failed to add effect - should never happen
209
      {
210
      unblockEverything();
211
      }
212
    }
213
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
216
  private void changeObjectNow()
217
    {
218
    mChangeObject = false;
219
220
    if ( mNewObject==null || mNewObject.getObjectType()!=mNextObject )
221
      {
222 b78ebd76 Leszek Koltunski
      blockEverything(BlockController.PLACE_1);
223 81141862 Leszek Koltunski
      createObjectNow(mNextObject);
224 7c111294 Leszek Koltunski
      doEffectNow( BaseEffect.Type.SIZECHANGE );
225
      }
226
    }
227
228 568d4698 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
229
230
  private void recreateObjectNow()
231
    {
232
    mRecreateObject = false;
233
234
    if ( mNewObject!=null )
235
      {
236
      blockEverything(BlockController.PLACE_1);
237
      createObjectNow(mNewObject.getObjectType());
238
      doEffectNow( BaseEffect.Type.SIZECHANGE );
239
      }
240
    }
241
242 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
243
244
  private void scrambleObjectNow()
245
    {
246
    mScrambleObject = false;
247
    mIsSolved       = false;
248 b78ebd76 Leszek Koltunski
    blockEverything(BlockController.PLACE_3);
249 7c111294 Leszek Koltunski
    doEffectNow( BaseEffect.Type.SCRAMBLE );
250
    }
251
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253
254
  private void solveObjectNow()
255
    {
256
    mSolveObject = false;
257 b78ebd76 Leszek Koltunski
    blockEverything(BlockController.PLACE_4);
258 7c111294 Leszek Koltunski
    doEffectNow( BaseEffect.Type.SOLVE );
259
    }
260
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
263
  private void solveNow()
264
    {
265
    mSolve = false;
266 a57e6870 Leszek Koltunski
    if( mNewObject!=null ) mNewObject.solve();
267 7c111294 Leszek Koltunski
    }
268
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270
271
  private void initializeObjectNow()
272
    {
273
    mInitializeObject = false;
274
    mNewObject.initializeObject(mNextMoves);
275
    }
276
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278
279
  private void setTextureMapNow()
280
    {
281
    mSetTextureMap = false;
282
283
    if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
284
    }
285
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287
288
  private void resetAllTextureMapsNow()
289
    {
290
    mResetAllTextureMaps = false;
291
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
292
    }
293
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
296
  private void setQuatNow()
297
    {
298
    mSetQuat = false;
299 15e5214c Leszek Koltunski
    mController.setQuat();
300 7c111294 Leszek Koltunski
    }
301
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303
304 15e5214c Leszek Koltunski
  void rememberMove(int axis, int row, int angle)
305 7c111294 Leszek Koltunski
    {
306
    mDebug += (" (m "+axis+" "+(1<<row)+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
307
    }
308
309 15e5214c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
310
311
  void finishRotation(int nearestAngle)
312
    {
313
    mNearestAngle   = nearestAngle;
314
    mFinishRotation = true;
315
    }
316
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318
319
  void setTextureMap(int cubit, int face, int newColor)
320
    {
321
    mSetTextureMap = true;
322
323
    mCubit    = cubit;
324
    mFace     = face;
325
    mNewColor = newColor;
326
    }
327
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329
330
  void setQuatOnNextRender()
331
    {
332
    mSetQuat = true;
333
    }
334
335 02d80fe6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
336
337 72d6857c Leszek Koltunski
  void setMove(float xmove, float ymove)
338 02d80fe6 Leszek Koltunski
    {
339 72d6857c Leszek Koltunski
    mMoveX = xmove;
340
    mMoveY = ymove;
341 02d80fe6 Leszek Koltunski
    }
342
343 15e5214c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
344
// INTERNAL API
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346
347
  public int getNumScrambles()
348
    {
349
    return mScrambleObjectNum;
350
    }
351
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353
354
  public TwistyObject getOldObject()
355
    {
356
    return mOldObject;
357
    }
358
359 02d80fe6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
360
361 72d6857c Leszek Koltunski
  public float getMoveX()
362 02d80fe6 Leszek Koltunski
    {
363 72d6857c Leszek Koltunski
    return mMoveX;
364
    }
365
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367
368
  public float getMoveY()
369
    {
370
    return mMoveY;
371 02d80fe6 Leszek Koltunski
    }
372
373 15e5214c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
374
// PUBLIC API
375 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
376
377 e7daa161 Leszek Koltunski
  public void setScreenSize(int width, int height)
378 7c111294 Leszek Koltunski
    {
379
    if( mNewObject!=null )
380
      {
381
      mNewObject.createTexture();
382 e7daa161 Leszek Koltunski
      mNewObject.recomputeScaleFactor(width,height);
383 7c111294 Leszek Koltunski
      }
384
    mScreenWidth = width;
385 e7daa161 Leszek Koltunski
    mScreenHeight= height;
386 7c111294 Leszek Koltunski
    }
387
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389
390
  public void savePreferences(SharedPreferences.Editor editor)
391
    {
392
    if( mNewObject!=null )
393
      {
394
      mNewObject.savePreferences(editor);
395
      }
396
    }
397
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399
400
  public void restorePreferences(SharedPreferences preferences)
401
    {
402
    mPreferences = preferences;
403
    }
404
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406
407
  public void changeObject(ObjectType object)
408
    {
409
    mChangeObject = true;
410
    mNextObject = object;
411
    }
412
413 568d4698 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
414
415
  public void recreateObject()
416
    {
417
    mRecreateObject = true;
418
    }
419
420 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
421
422
  public boolean isTouchBlocked()
423
    {
424
    return mTouchBlocked;
425
    }
426
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428
429
  public boolean isUINotBlocked()
430
    {
431
    return !mUIBlocked;
432
    }
433
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435
436
  public void blockEverything(int place)
437
    {
438
    mUIBlocked   = true;
439
    mTouchBlocked= true;
440
    mBlockController.touchBlocked(place);
441
    mBlockController.uiBlocked(place);
442
    }
443
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445
446
  public void blockTouch(int place)
447
    {
448
    mTouchBlocked= true;
449
    mBlockController.touchBlocked(place);
450
    }
451
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453
454
  public void unblockEverything()
455
    {
456
    mUIBlocked   = false;
457
    mTouchBlocked= false;
458
    mBlockController.touchUnblocked();
459
    mBlockController.uiUnblocked();
460
    }
461
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463
464
  public void unblockTouch()
465
    {
466
    mTouchBlocked= false;
467
    mBlockController.touchUnblocked();
468
    }
469
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471
472
  public void unblockUI()
473
    {
474
    mUIBlocked= false;
475
    mBlockController.uiUnblocked();
476
    }
477
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479
480
  public void preRender()
481
    {
482
    if( mSolve                 ) solveNow();
483
    if( mSetQuat               ) setQuatNow();
484
    if( mFinishRotation        ) finishRotationNow();
485
    if( mRemoveRotation        ) removeRotationNow();
486
    if( mRemovePatternRotation ) removePatternRotationNow();
487
    if( mChangeObject          ) changeObjectNow();
488 568d4698 Leszek Koltunski
    if( mRecreateObject        ) recreateObjectNow();
489 7c111294 Leszek Koltunski
    if( mSolveObject           ) solveObjectNow();
490
    if( mScrambleObject        ) scrambleObjectNow();
491
    if( mAddRotation           ) addRotationNow();
492
    if( mInitializeObject      ) initializeObjectNow();
493
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
494
    if( mSetTextureMap         ) setTextureMapNow();
495
    }
496
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498
499 2df35810 Leszek Koltunski
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
500 7c111294 Leszek Koltunski
    {
501
    mAddRotation = true;
502
503 2df35810 Leszek Koltunski
    int basicAngle= mNewObject.getBasicAngle()[axis];
504
    int angle     = bareAngle*(360/basicAngle);
505
    int duration  = Math.abs(angle)*millPreDegree;
506
507 7c111294 Leszek Koltunski
    mAddActionListener    = listener;
508
    mAddRotationAxis      = axis;
509
    mAddRotationRowBitmap = rowBitmap;
510
    mAddRotationAngle     = angle;
511
    mAddRotationDuration  = duration;
512
513
    if( listener instanceof ScrambleEffect )
514
      {
515
      mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
516
      }
517
    }
518
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520
521
  public void initializeObject(int[][] moves)
522
    {
523
    mInitializeObject = true;
524
    mNextMoves = moves;
525
    }
526
527
///////////////////////////////////////////////////////////////////////////////////////////////////
528
529
  public void scrambleObject(int num)
530
    {
531
    if( !mUIBlocked )
532
      {
533
      mScrambleObject = true;
534
      mScrambleObjectNum = num;
535
      mDebug = "";
536
      mDebugStartTime = System.currentTimeMillis();
537
      }
538
    }
539
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541
// this starts the Solve Effect
542
543
  public void solveObject()
544
    {
545
    if( !mUIBlocked )
546
      {
547
      mSolveObject = true;
548
      }
549
    }
550
551 17d623f1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
552
// this only sets the cubits state to solved
553
554
  public void solveOnly()
555
    {
556
    mSolve = true;
557
    }
558
559 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
560
561
  public void resetAllTextureMaps()
562
    {
563
    mResetAllTextureMaps = true;
564
    }
565
566
///////////////////////////////////////////////////////////////////////////////////////////////////
567
568
  public TwistyObject getObject()
569
    {
570
    return mNewObject;
571
    }
572
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574
575
  public void effectFinished(final long effectID)
576
    {
577
    if( effectID == mRotationFinishedID )
578
      {
579
      mRotationFinishedID = 0;
580
      removeRotation();
581
      }
582
    else if( effectID == mAddRotationID )
583
      {
584
      mAddRotationID = 0;
585
      mRemoveRotationID = effectID;
586
      removePatternRotation();
587
      }
588
    else
589
      {
590
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
591
        {
592
        if( effectID == mEffectID[i] )
593
          {
594
          if( i!=BaseEffect.Type.WIN.ordinal() ) unblockEverything();
595 b3fff6fb Leszek Koltunski
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() ) mInterface.onScrambleEffectFinished();
596
          if( i==BaseEffect.Type.WIN.ordinal()      ) mInterface.onWinEffectFinished(mDebug,mScrambleObjectNum);
597 7c111294 Leszek Koltunski
          break;
598
          }
599
        }
600
      }
601
    }
602
  }