Project

General

Profile

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

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

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.io.InputStream;
23

    
24
import android.app.Activity;
25
import android.content.SharedPreferences;
26

    
27
import org.distorted.library.message.EffectListener;
28
import org.distorted.library.type.Static3D;
29

    
30
import org.distorted.library.type.Static4D;
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 ObjectControl mController;
42
  private InputStream mJsonStream, mMeshStream;
43
  private int mOrdinal;
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
  private float mScale;
52

    
53
  private boolean mFinishRotation, mRemoveRotation, mRemovePatternRotation, mAddRotation,
54
                  mSetQuat, mChangeObject, mSolveObject, mScrambleObject,
55
                  mInitializeObject, mSetTextureMap, mResetAllTextureMaps, mSolve;
56
  private boolean mUIBlocked, mTouchBlocked, mIsSolved;
57
  private long mRotationFinishedID;
58
  private final long[] mEffectID;
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 mScrambleStartTime, mScrambleEndTime;
67
  private int mMeshState, mIconMode;
68

    
69
  // debugging only
70
  private long mAddRotationTime;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  public ObjectPreRender(Activity act, ObjectControl controller, ObjectLibInterface actioner)
75
    {
76
    mInterface = actioner;
77
    mController = controller;
78

    
79
    mFinishRotation       = false;
80
    mRemoveRotation       = false;
81
    mRemovePatternRotation= false;
82
    mAddRotation          = false;
83
    mSetQuat              = false;
84
    mChangeObject         = false;
85
    mSolveObject          = false;
86
    mSolve                = false;
87
    mScrambleObject       = false;
88

    
89
    mOldObject = null;
90
    mNewObject = null;
91

    
92
    mDebug = "";
93
    mScrambleObjectNum = 0;
94
    mScale = 1.0f;
95

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

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  private void createObjectNow(int ordinal, int meshState, int iconMode, InputStream jsonStream, InputStream meshStream)
105
    {
106
    if( mOldObject!=null ) mOldObject.releaseResources();
107
    mOldObject = mNewObject;
108
    Static3D move = new Static3D(mMoveX,mMoveY,0);
109
    Static4D quat = mController.getQuat();
110

    
111
    long time1 = System.currentTimeMillis();
112

    
113
    if( jsonStream==null ) mNewObject = ObjectType.create( ordinal, meshState, iconMode, quat, move, mScale, meshStream);
114
    else                   mNewObject = new TwistyJson( jsonStream, meshState, iconMode, quat, move, mScale, meshStream);
115

    
116
    long time2 = System.currentTimeMillis();
117
    mInterface.onObjectCreated(time2-time1);
118

    
119
    if( mNewObject!=null )
120
      {
121
      mNewObject.setLibInterface(mInterface);
122
      mController.setTouchControl(mNewObject);
123
      TwistyObjectNode node = mController.getNode();
124
      if( node!=null ) mNewObject.setObjectRatioNow(mScale, node.getMinSize() );
125

    
126
      if( mPreferences!=null )
127
        {
128
        mNewObject.restorePreferences(mPreferences);
129
        mPreferences = null;
130
        }
131

    
132
      mIsSolved = mNewObject.isSolved();
133
      }
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
138

    
139
  private void doEffectNow(BaseEffect.Type type)
140
    {
141
    try
142
      {
143
      int index = type.ordinal();
144
      mEffectID[index] = type.startEffect(this);
145
      }
146
    catch( Exception ex )
147
      {
148
      android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
149
      unblockEverything();
150
      }
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  private void removeRotationNow()
156
    {
157
    mRemoveRotation=false;
158
    mNewObject.removeRotationNow();
159

    
160
    boolean solved = mNewObject.isSolved();
161

    
162
    if( solved && !mIsSolved )
163
      {
164
      mInterface.onSolved();
165
      unblockEverything();
166
      doEffectNow( BaseEffect.Type.WIN );
167
      }
168
    else
169
      {
170
      unblockEverything();
171
      }
172

    
173
    mIsSolved = solved;
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  private void removeRotation()
179
    {
180
    mRemoveRotation = true;
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  private void removePatternRotation()
186
    {
187
    mRemovePatternRotation = true;
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  private void removePatternRotationNow()
193
    {
194
    mRemovePatternRotation=false;
195
    mNewObject.removeRotationNow();
196
    mAddActionListener.onActionFinished(mRemoveRotationID);
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  private void addRotationNow()
202
    {
203
    mAddRotation = false;
204

    
205
    if( mNewObject.mNumAxis > mAddRotationAxis )
206
      {
207
      mAddRotationID = mNewObject.addNewRotation( mAddRotationAxis, mAddRotationRowBitmap,
208
                                                  mAddRotationAngle, mAddRotationDuration, this);
209

    
210
      if( mAddRotationID==0 ) // failed to add effect - should never happen
211
        {
212
        unblockEverything();
213
        }
214
      }
215
    else // should never happen but Firebase says it sometimes does
216
      {
217
      long timeNow = System.currentTimeMillis();
218
      Class<? extends MovesFinished> clazz = mAddActionListener.getClass();
219
      String name = clazz.getSimpleName();
220

    
221
      String error = "time now: "+timeNow+" add time: "+mAddRotationTime+" axis="+mAddRotationAxis+
222
                      "object: "+mNewObject.getShortName()+" "+name;
223

    
224
      mInterface.reportProblem(error,true);
225
      unblockEverything();
226
      }
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  private void finishRotationNow()
232
    {
233
    mFinishRotation = false;
234
    blockEverything(BlockController.PLACE_0);
235
    mRotationFinishedID = mNewObject.finishRotationNow(this, mNearestAngle);
236

    
237
    if( mRotationFinishedID==0 ) // failed to add effect - should never happen
238
      {
239
      unblockEverything();
240
      }
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
  private void changeObjectNow()
246
    {
247
    mChangeObject = false;
248
    blockEverything(BlockController.PLACE_1);
249
    createObjectNow(mOrdinal,mMeshState,mIconMode,mJsonStream,mMeshStream);
250
    doEffectNow( BaseEffect.Type.SIZECHANGE );
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  private void scrambleObjectNow()
256
    {
257
    mScrambleObject = false;
258
    mIsSolved       = false;
259
    blockEverything(BlockController.PLACE_3);
260
    doEffectNow( BaseEffect.Type.SCRAMBLE );
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  private void solveObjectNow()
266
    {
267
    mSolveObject = false;
268
    blockEverything(BlockController.PLACE_4);
269
    doEffectNow( BaseEffect.Type.SOLVE );
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
  private void solveNow()
275
    {
276
    mSolve = false;
277
    if( mNewObject!=null ) mNewObject.solve();
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  private void initializeObjectNow()
283
    {
284
    mInitializeObject = false;
285
    mNewObject.initializeObject(mNextMoves);
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  private void setTextureMapNow()
291
    {
292
    mSetTextureMap = false;
293

    
294
    if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  private void resetAllTextureMapsNow()
300
    {
301
    mResetAllTextureMaps = false;
302
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  private void setQuatNow()
308
    {
309
    mSetQuat = false;
310
    mController.setQuat();
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
  private int computeRowFromBitmap(int rowBitmap)
316
    {
317
    int index = 0;
318

    
319
    while(index<32)
320
      {
321
      if( (rowBitmap&0x1) != 0 ) return index;
322
      rowBitmap>>=1;
323
      index++;
324
      }
325

    
326
    return 0;
327
    }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
  void rememberMove(int axis, int row, int angle)
332
    {
333
    mDebug += ("(m "+axis+" "+(1<<row)+" "+angle+" "+(System.currentTimeMillis()-mScrambleEndTime)+")");
334
    }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
  void finishRotation(int nearestAngle)
339
    {
340
    mNearestAngle   = nearestAngle;
341
    mFinishRotation = true;
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
  void setTextureMap(int cubit, int face, int newColor)
347
    {
348
    mSetTextureMap = true;
349

    
350
    mCubit    = cubit;
351
    mFace     = face;
352
    mNewColor = newColor;
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
  void setQuatOnNextRender()
358
    {
359
    mSetQuat = true;
360
    }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
  void setMove(float xmove, float ymove)
365
    {
366
    mMoveX = xmove;
367
    mMoveY = ymove;
368
    }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

    
372
  void setScale(float scale)
373
    {
374
    mScale = scale;
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378
// INTERNAL API
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
  public int getNumScrambles()
382
    {
383
    return mScrambleObjectNum;
384
    }
385

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

    
388
  public TwistyObject getOldObject()
389
    {
390
    return mOldObject;
391
    }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
  public float getMoveX()
396
    {
397
    return mMoveX;
398
    }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
  public float getMoveY()
403
    {
404
    return mMoveY;
405
    }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
  public ObjectLibInterface getInterface()
410
    {
411
    return mInterface;
412
    }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415
// PUBLIC API
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

    
418
  public void savePreferences(SharedPreferences.Editor editor)
419
    {
420
    if( mNewObject!=null )
421
      {
422
      mNewObject.savePreferences(editor);
423
      }
424
    }
425

    
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427

    
428
  public void restorePreferences(SharedPreferences preferences)
429
    {
430
    mPreferences = preferences;
431
    }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

    
435
  public void changeObject(int ordinal, int meshState, int iconMode, InputStream jsonStream, InputStream meshStream)
436
    {
437
    mChangeObject = true;
438
    mOrdinal    = ordinal;
439
    mMeshState  = meshState;
440
    mIconMode   = iconMode;
441
    mJsonStream = jsonStream;
442
    mMeshStream = meshStream;
443
    }
444

    
445
///////////////////////////////////////////////////////////////////////////////////////////////////
446

    
447
  public void setDefaultRotation(int numFaces)
448
    {
449
    if( mController!=null && mController.getRotateOnCreation() )
450
      {
451
      switch(numFaces)
452
        {
453
        case  4: mController.rotateNow(ShapeTetrahedron.DEFAULT_ROT ); break;
454
        case  6: mController.rotateNow(ShapeHexahedron.DEFAULT_ROT  ); break;
455
        case  8: mController.rotateNow(ShapeOctahedron.DEFAULT_ROT  ); break;
456
        case 12: mController.rotateNow(ShapeDodecahedron.DEFAULT_ROT); break;
457
        }
458
      }
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

    
463
  public boolean isTouchBlocked()
464
    {
465
    return mTouchBlocked;
466
    }
467

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

    
470
  public boolean isUINotBlocked()
471
    {
472
    return !mUIBlocked;
473
    }
474

    
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476

    
477
  public void blockEverything(int place)
478
    {
479
    mUIBlocked   = true;
480
    mTouchBlocked= true;
481
    mBlockController.touchBlocked(place);
482
    mBlockController.uiBlocked(place);
483
    }
484

    
485
///////////////////////////////////////////////////////////////////////////////////////////////////
486

    
487
  public void blockTouch(int place)
488
    {
489
    mTouchBlocked= true;
490
    mBlockController.touchBlocked(place);
491
    }
492

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494

    
495
  public void unblockEverything()
496
    {
497
    mUIBlocked   = false;
498
    mTouchBlocked= false;
499
    mBlockController.touchUnblocked();
500
    mBlockController.uiUnblocked();
501
    }
502

    
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504

    
505
  public void unblockTouch()
506
    {
507
    mTouchBlocked= false;
508
    mBlockController.touchUnblocked();
509
    }
510

    
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512

    
513
  public void unblockUI()
514
    {
515
    mUIBlocked= false;
516
    mBlockController.uiUnblocked();
517
    }
518

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520

    
521
  public void preRender()
522
    {
523
    if( mSolve                 ) solveNow();
524
    if( mSetQuat               ) setQuatNow();
525
    if( mFinishRotation        ) finishRotationNow();
526
    if( mRemoveRotation        ) removeRotationNow();
527
    if( mRemovePatternRotation ) removePatternRotationNow();
528
    if( mChangeObject          ) changeObjectNow();
529
    if( mSolveObject           ) solveObjectNow();
530
    if( mScrambleObject        ) scrambleObjectNow();
531
    if( mAddRotation           ) addRotationNow();
532
    if( mInitializeObject      ) initializeObjectNow();
533
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
534
    if( mSetTextureMap         ) setTextureMapNow();
535
    }
536

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538

    
539
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
540
    {
541
    int[][] basicAngles = mNewObject.getBasicAngles();
542

    
543
    if( axis<basicAngles.length )
544
      {
545
      mAddRotation = true;
546
      int row = computeRowFromBitmap(rowBitmap);
547

    
548
      if( row<basicAngles[axis].length )
549
        {
550
        int basicAngle= basicAngles[axis][row];
551
        int angle     = bareAngle*(360/basicAngle);
552
        int duration  = Math.abs(angle)*millPreDegree;
553

    
554
        mAddActionListener    = listener;
555
        mAddRotationAxis      = axis;
556
        mAddRotationRowBitmap = rowBitmap;
557
        mAddRotationAngle     = angle;
558
        mAddRotationDuration  = duration;
559
        mAddRotationTime      = System.currentTimeMillis();
560

    
561
        if( listener instanceof ScrambleEffect )
562
          {
563
          mDebug += ("(a "+axis+" "+rowBitmap+" "+angle+" "+(mAddRotationTime-mScrambleStartTime)+")");
564
          }
565
        }
566
      }
567
    }
568

    
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570

    
571
  public void initializeObject(int[][] moves)
572
    {
573
    mInitializeObject = true;
574
    mNextMoves = moves;
575
    }
576

    
577
///////////////////////////////////////////////////////////////////////////////////////////////////
578

    
579
  public void scrambleObject(int num)
580
    {
581
    if( !mUIBlocked )
582
      {
583
      mScrambleObject = true;
584
      mScrambleObjectNum = num;
585
      mDebug = "";
586
      mScrambleStartTime = System.currentTimeMillis();
587
      }
588
    }
589

    
590
///////////////////////////////////////////////////////////////////////////////////////////////////
591
// this starts the Solve Effect
592

    
593
  public void solveObject()
594
    {
595
    if( !mUIBlocked )
596
      {
597
      mSolveObject = true;
598
      }
599
    }
600

    
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602
// this only sets the cubits state to solved
603

    
604
  public void solveOnly()
605
    {
606
    mSolve = true;
607
    }
608

    
609
///////////////////////////////////////////////////////////////////////////////////////////////////
610

    
611
  public void resetAllTextureMaps()
612
    {
613
    mResetAllTextureMaps = true;
614
    }
615

    
616
///////////////////////////////////////////////////////////////////////////////////////////////////
617

    
618
  public TwistyObject getObject()
619
    {
620
    return mNewObject;
621
    }
622

    
623
///////////////////////////////////////////////////////////////////////////////////////////////////
624

    
625
  public TwistyObjectNode getObjectNode()
626
    {
627
    return mController.getNode();
628
    }
629

    
630
///////////////////////////////////////////////////////////////////////////////////////////////////
631

    
632
  public void effectFinished(final long effectID)
633
    {
634
    if( effectID == mRotationFinishedID )
635
      {
636
      mRotationFinishedID = 0;
637
      removeRotation();
638
      }
639
    else if( effectID == mAddRotationID )
640
      {
641
      mAddRotationID = 0;
642
      mRemoveRotationID = effectID;
643
      removePatternRotation();
644
      }
645
    else
646
      {
647
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
648
        {
649
        if( effectID == mEffectID[i] )
650
          {
651
          if( i!=BaseEffect.Type.WIN.ordinal()      )
652
            {
653
            unblockEverything();
654
            }
655
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() )
656
            {
657
            mScrambleEndTime = System.currentTimeMillis();
658
            mInterface.onScrambleEffectFinished();
659
            }
660
          if( i==BaseEffect.Type.WIN.ordinal()      )
661
            {
662
            mInterface.onWinEffectFinished(mScrambleStartTime,mScrambleEndTime-mScrambleStartTime,mDebug,mScrambleObjectNum);
663
            }
664
          break;
665
          }
666
        }
667
      }
668
    }
669
  }
(3-3/12)