Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectPreRender.java @ 0eff8c76

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
    boolean firstTime = (mNewObject==null);
107

    
108
    if( mOldObject!=null ) mOldObject.releaseResources();
109
    mOldObject = mNewObject;
110
    Static3D move = new Static3D(mMoveX,mMoveY,0);
111
    Static4D quat = mController.getQuat();
112

    
113
    long time1 = System.currentTimeMillis();
114

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

    
118
    long time2 = System.currentTimeMillis();
119
    mInterface.onObjectCreated(time2-time1);
120

    
121
    if( mNewObject!=null )
122
      {
123
      mNewObject.setLibInterface(mInterface);
124
      mController.setTouchControl(mNewObject);
125
      TwistyObjectNode node = mController.getNode();
126
      if( node!=null ) mNewObject.setObjectRatioNow(mScale, node.getMinSize() );
127
      if( firstTime && mPreferences!=null ) mNewObject.restorePreferences(mPreferences);
128
      mIsSolved = mNewObject.isSolved();
129
      }
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
134

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

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  private void removeRotationNow()
152
    {
153
    mRemoveRotation=false;
154
    mNewObject.removeRotationNow();
155

    
156
    boolean solved = mNewObject.isSolved();
157

    
158
    if( solved && !mIsSolved )
159
      {
160
      mInterface.onSolved();
161
      unblockEverything();
162
      doEffectNow( BaseEffect.Type.WIN );
163
      }
164
    else
165
      {
166
      unblockEverything();
167
      }
168

    
169
    mIsSolved = solved;
170
    }
171

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

    
174
  private void removeRotation()
175
    {
176
    mRemoveRotation = true;
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  private void removePatternRotation()
182
    {
183
    mRemovePatternRotation = true;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  private void removePatternRotationNow()
189
    {
190
    mRemovePatternRotation=false;
191
    mNewObject.removeRotationNow();
192
    mAddActionListener.onActionFinished(mRemoveRotationID);
193
    }
194

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

    
197
  private void addRotationNow()
198
    {
199
    mAddRotation = false;
200

    
201
    if( mNewObject.mNumAxis > mAddRotationAxis )
202
      {
203
      mAddRotationID = mNewObject.addNewRotation( mAddRotationAxis, mAddRotationRowBitmap,
204
                                                  mAddRotationAngle, mAddRotationDuration, this);
205

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

    
217
      String error = "time now: "+timeNow+" add time: "+mAddRotationTime+" axis="+mAddRotationAxis+
218
                      "object: "+mNewObject.getShortName()+" "+name;
219

    
220
      mInterface.reportProblem(error,true);
221
      unblockEverything();
222
      }
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  private void finishRotationNow()
228
    {
229
    mFinishRotation = false;
230
    blockEverything(BlockController.PLACE_0);
231
    mRotationFinishedID = mNewObject.finishRotationNow(this, mNearestAngle);
232

    
233
    if( mRotationFinishedID==0 ) // failed to add effect - should never happen
234
      {
235
      unblockEverything();
236
      }
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  private void changeObjectNow()
242
    {
243
    mChangeObject = false;
244
    blockEverything(BlockController.PLACE_1);
245
    createObjectNow(mOrdinal,mMeshState,mIconMode,mJsonStream,mMeshStream);
246
    doEffectNow( BaseEffect.Type.SIZECHANGE );
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
  private void scrambleObjectNow()
252
    {
253
    mScrambleObject = false;
254
    mIsSolved       = false;
255
    blockEverything(BlockController.PLACE_3);
256
    doEffectNow( BaseEffect.Type.SCRAMBLE );
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  private void solveObjectNow()
262
    {
263
    mSolveObject = false;
264
    blockEverything(BlockController.PLACE_4);
265
    doEffectNow( BaseEffect.Type.SOLVE );
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  private void solveNow()
271
    {
272
    mSolve = false;
273
    if( mNewObject!=null ) mNewObject.solve();
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
  private void initializeObjectNow()
279
    {
280
    mInitializeObject = false;
281
    mNewObject.initializeObject(mNextMoves);
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  private void setTextureMapNow()
287
    {
288
    mSetTextureMap = false;
289

    
290
    if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
  private void resetAllTextureMapsNow()
296
    {
297
    mResetAllTextureMaps = false;
298
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
  private void setQuatNow()
304
    {
305
    mSetQuat = false;
306
    mController.setQuat();
307
    }
308

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

    
311
  private int computeRowFromBitmap(int rowBitmap)
312
    {
313
    int index = 0;
314

    
315
    while(index<32)
316
      {
317
      if( (rowBitmap&0x1) != 0 ) return index;
318
      rowBitmap>>=1;
319
      index++;
320
      }
321

    
322
    return 0;
323
    }
324

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

    
327
  void rememberMove(int axis, int row, int angle)
328
    {
329
    mDebug += ("(m "+axis+" "+(1<<row)+" "+angle+" "+(System.currentTimeMillis()-mScrambleEndTime)+")");
330
    }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
  void finishRotation(int nearestAngle)
335
    {
336
    mNearestAngle   = nearestAngle;
337
    mFinishRotation = true;
338
    }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

    
342
  void setTextureMap(int cubit, int face, int newColor)
343
    {
344
    mSetTextureMap = true;
345

    
346
    mCubit    = cubit;
347
    mFace     = face;
348
    mNewColor = newColor;
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  void setQuatOnNextRender()
354
    {
355
    mSetQuat = true;
356
    }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

    
360
  void setMove(float xmove, float ymove)
361
    {
362
    mMoveX = xmove;
363
    mMoveY = ymove;
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  void setScale(float scale)
369
    {
370
    mScale = scale;
371
    }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374
// INTERNAL API
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
  public int getNumScrambles()
378
    {
379
    return mScrambleObjectNum;
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
  public TwistyObject getOldObject()
385
    {
386
    return mOldObject;
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
  public float getMoveX()
392
    {
393
    return mMoveX;
394
    }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  public float getMoveY()
399
    {
400
    return mMoveY;
401
    }
402

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

    
405
  public ObjectLibInterface getInterface()
406
    {
407
    return mInterface;
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411
// PUBLIC API
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413

    
414
  public void savePreferences(SharedPreferences.Editor editor)
415
    {
416
    if( mNewObject!=null )
417
      {
418
      mNewObject.savePreferences(editor);
419
      }
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
  public void restorePreferences(SharedPreferences preferences)
425
    {
426
    mPreferences = preferences;
427
    }
428

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

    
431
  public void changeObject(int ordinal, int meshState, int iconMode, InputStream jsonStream, InputStream meshStream)
432
    {
433
    mChangeObject = true;
434
    mOrdinal    = ordinal;
435
    mMeshState  = meshState;
436
    mIconMode   = iconMode;
437
    mJsonStream = jsonStream;
438
    mMeshStream = meshStream;
439
    }
440

    
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442

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

    
457
///////////////////////////////////////////////////////////////////////////////////////////////////
458

    
459
  public boolean isTouchBlocked()
460
    {
461
    return mTouchBlocked;
462
    }
463

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

    
466
  public boolean isUINotBlocked()
467
    {
468
    return !mUIBlocked;
469
    }
470

    
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472

    
473
  public void blockEverything(int place)
474
    {
475
    mUIBlocked   = true;
476
    mTouchBlocked= true;
477
    mBlockController.touchBlocked(place);
478
    mBlockController.uiBlocked(place);
479
    }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

    
483
  public void blockTouch(int place)
484
    {
485
    mTouchBlocked= true;
486
    mBlockController.touchBlocked(place);
487
    }
488

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490

    
491
  public void unblockEverything()
492
    {
493
    mUIBlocked   = false;
494
    mTouchBlocked= false;
495
    mBlockController.touchUnblocked();
496
    mBlockController.uiUnblocked();
497
    }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500

    
501
  public void unblockTouch()
502
    {
503
    mTouchBlocked= false;
504
    mBlockController.touchUnblocked();
505
    }
506

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508

    
509
  public void unblockUI()
510
    {
511
    mUIBlocked= false;
512
    mBlockController.uiUnblocked();
513
    }
514

    
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516

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

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

    
535
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
536
    {
537
    int[][] basicAngles = mNewObject.getBasicAngles();
538

    
539
    if( axis<basicAngles.length )
540
      {
541
      mAddRotation = true;
542
      int row = computeRowFromBitmap(rowBitmap);
543

    
544
      if( row<basicAngles[axis].length )
545
        {
546
        int basicAngle= basicAngles[axis][row];
547
        int angle     = bareAngle*(360/basicAngle);
548
        int duration  = Math.abs(angle)*millPreDegree;
549

    
550
        mAddActionListener    = listener;
551
        mAddRotationAxis      = axis;
552
        mAddRotationRowBitmap = rowBitmap;
553
        mAddRotationAngle     = angle;
554
        mAddRotationDuration  = duration;
555
        mAddRotationTime      = System.currentTimeMillis();
556

    
557
        if( listener instanceof ScrambleEffect )
558
          {
559
          mDebug += ("(a "+axis+" "+rowBitmap+" "+angle+" "+(mAddRotationTime-mScrambleStartTime)+")");
560
          }
561
        }
562
      }
563
    }
564

    
565
///////////////////////////////////////////////////////////////////////////////////////////////////
566

    
567
  public void initializeObject(int[][] moves)
568
    {
569
    mInitializeObject = true;
570
    mNextMoves = moves;
571
    }
572

    
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574

    
575
  public void scrambleObject(int num)
576
    {
577
    if( !mUIBlocked )
578
      {
579
      mScrambleObject = true;
580
      mScrambleObjectNum = num;
581
      mDebug = "";
582
      mScrambleStartTime = System.currentTimeMillis();
583
      }
584
    }
585

    
586
///////////////////////////////////////////////////////////////////////////////////////////////////
587
// this starts the Solve Effect
588

    
589
  public void solveObject()
590
    {
591
    if( !mUIBlocked )
592
      {
593
      mSolveObject = true;
594
      }
595
    }
596

    
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598
// this only sets the cubits state to solved
599

    
600
  public void solveOnly()
601
    {
602
    mSolve = true;
603
    }
604

    
605
///////////////////////////////////////////////////////////////////////////////////////////////////
606

    
607
  public void resetAllTextureMaps()
608
    {
609
    mResetAllTextureMaps = true;
610
    }
611

    
612
///////////////////////////////////////////////////////////////////////////////////////////////////
613

    
614
  public TwistyObject getObject()
615
    {
616
    return mNewObject;
617
    }
618

    
619
///////////////////////////////////////////////////////////////////////////////////////////////////
620

    
621
  public TwistyObjectNode getObjectNode()
622
    {
623
    return mController.getNode();
624
    }
625

    
626
///////////////////////////////////////////////////////////////////////////////////////////////////
627

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