Project

General

Profile

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

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

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