Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectPreRender.java @ 4a389a4e

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 mDebugStartTime;
67
  private int mMeshState;
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, 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, quat, move, mScale, meshStream);
116
    else                   mNewObject = new TwistyJson( jsonStream, meshState, 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.NUM_AXIS>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,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
  void rememberMove(int axis, int row, int angle)
312
    {
313
    mDebug += (" (m "+axis+" "+(1<<row)+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
  void finishRotation(int nearestAngle)
319
    {
320
    mNearestAngle   = nearestAngle;
321
    mFinishRotation = true;
322
    }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
  void setTextureMap(int cubit, int face, int newColor)
327
    {
328
    mSetTextureMap = true;
329

    
330
    mCubit    = cubit;
331
    mFace     = face;
332
    mNewColor = newColor;
333
    }
334

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

    
337
  void setQuatOnNextRender()
338
    {
339
    mSetQuat = true;
340
    }
341

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

    
344
  void setMove(float xmove, float ymove)
345
    {
346
    mMoveX = xmove;
347
    mMoveY = ymove;
348
    }
349

    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351

    
352
  void setScale(float scale)
353
    {
354
    mScale = scale;
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358
// INTERNAL API
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
  public int getNumScrambles()
362
    {
363
    return mScrambleObjectNum;
364
    }
365

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

    
368
  public TwistyObject getOldObject()
369
    {
370
    return mOldObject;
371
    }
372

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

    
375
  public float getMoveX()
376
    {
377
    return mMoveX;
378
    }
379

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

    
382
  public float getMoveY()
383
    {
384
    return mMoveY;
385
    }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

    
389
  public ObjectLibInterface getInterface()
390
    {
391
    return mInterface;
392
    }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395
// PUBLIC API
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  public void savePreferences(SharedPreferences.Editor editor)
399
    {
400
    if( mNewObject!=null )
401
      {
402
      mNewObject.savePreferences(editor);
403
      }
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
  public void restorePreferences(SharedPreferences preferences)
409
    {
410
    mPreferences = preferences;
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
  public void changeObject(int ordinal, int meshState, InputStream jsonStream, InputStream meshStream)
416
    {
417
    mChangeObject = true;
418
    mOrdinal    = ordinal;
419
    mMeshState  = meshState;
420
    mJsonStream = jsonStream;
421
    mMeshStream = meshStream;
422
    }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
  public void setDefaultRotation(int numFaces)
427
    {
428
    if( mController.getRotateOnCreation() )
429
      {
430
      switch(numFaces)
431
        {
432
        case  4: mController.rotateNow(ShapeTetrahedron.DEFAULT_ROT ); break;
433
        case  6: mController.rotateNow(ShapeHexahedron.DEFAULT_ROT  ); break;
434
        case  8: mController.rotateNow(ShapeOctahedron.DEFAULT_ROT  ); break;
435
        case 12: mController.rotateNow(ShapeDodecahedron.DEFAULT_ROT); break;
436
        }
437
      }
438
    }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

    
442
  public boolean isTouchBlocked()
443
    {
444
    return mTouchBlocked;
445
    }
446

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448

    
449
  public boolean isUINotBlocked()
450
    {
451
    return !mUIBlocked;
452
    }
453

    
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455

    
456
  public void blockEverything(int place)
457
    {
458
    mUIBlocked   = true;
459
    mTouchBlocked= true;
460
    mBlockController.touchBlocked(place);
461
    mBlockController.uiBlocked(place);
462
    }
463

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

    
466
  public void blockTouch(int place)
467
    {
468
    mTouchBlocked= true;
469
    mBlockController.touchBlocked(place);
470
    }
471

    
472
///////////////////////////////////////////////////////////////////////////////////////////////////
473

    
474
  public void unblockEverything()
475
    {
476
    mUIBlocked   = false;
477
    mTouchBlocked= false;
478
    mBlockController.touchUnblocked();
479
    mBlockController.uiUnblocked();
480
    }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
  public void unblockTouch()
485
    {
486
    mTouchBlocked= false;
487
    mBlockController.touchUnblocked();
488
    }
489

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491

    
492
  public void unblockUI()
493
    {
494
    mUIBlocked= false;
495
    mBlockController.uiUnblocked();
496
    }
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

    
500
  public void preRender()
501
    {
502
    if( mSolve                 ) solveNow();
503
    if( mSetQuat               ) setQuatNow();
504
    if( mFinishRotation        ) finishRotationNow();
505
    if( mRemoveRotation        ) removeRotationNow();
506
    if( mRemovePatternRotation ) removePatternRotationNow();
507
    if( mChangeObject          ) changeObjectNow();
508
    if( mSolveObject           ) solveObjectNow();
509
    if( mScrambleObject        ) scrambleObjectNow();
510
    if( mAddRotation           ) addRotationNow();
511
    if( mInitializeObject      ) initializeObjectNow();
512
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
513
    if( mSetTextureMap         ) setTextureMapNow();
514
    }
515

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

    
518
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
519
    {
520
    int[] angles = mNewObject.getBasicAngle();
521

    
522
    if( angles.length>axis )
523
      {
524
      mAddRotation = true;
525

    
526
      int basicAngle= angles[axis];
527
      int angle     = bareAngle*(360/basicAngle);
528
      int duration  = Math.abs(angle)*millPreDegree;
529

    
530
      mAddActionListener    = listener;
531
      mAddRotationAxis      = axis;
532
      mAddRotationRowBitmap = rowBitmap;
533
      mAddRotationAngle     = angle;
534
      mAddRotationDuration  = duration;
535
      mAddRotationTime      = System.currentTimeMillis();
536

    
537
      if( listener instanceof ScrambleEffect )
538
        {
539
        mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
540
        }
541
      }
542
    }
543

    
544
///////////////////////////////////////////////////////////////////////////////////////////////////
545

    
546
  public void initializeObject(int[][] moves)
547
    {
548
    mInitializeObject = true;
549
    mNextMoves = moves;
550
    }
551

    
552
///////////////////////////////////////////////////////////////////////////////////////////////////
553

    
554
  public void scrambleObject(int num)
555
    {
556
    if( !mUIBlocked )
557
      {
558
      mScrambleObject = true;
559
      mScrambleObjectNum = num;
560
      mDebug = "";
561
      mDebugStartTime = System.currentTimeMillis();
562
      }
563
    }
564

    
565
///////////////////////////////////////////////////////////////////////////////////////////////////
566
// this starts the Solve Effect
567

    
568
  public void solveObject()
569
    {
570
    if( !mUIBlocked )
571
      {
572
      mSolveObject = true;
573
      }
574
    }
575

    
576
///////////////////////////////////////////////////////////////////////////////////////////////////
577
// this only sets the cubits state to solved
578

    
579
  public void solveOnly()
580
    {
581
    mSolve = true;
582
    }
583

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585

    
586
  public void resetAllTextureMaps()
587
    {
588
    mResetAllTextureMaps = true;
589
    }
590

    
591
///////////////////////////////////////////////////////////////////////////////////////////////////
592

    
593
  public TwistyObject getObject()
594
    {
595
    return mNewObject;
596
    }
597

    
598
///////////////////////////////////////////////////////////////////////////////////////////////////
599

    
600
  public TwistyObjectNode getObjectNode()
601
    {
602
    return mController.getNode();
603
    }
604

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

    
607
  public void effectFinished(final long effectID)
608
    {
609
    if( effectID == mRotationFinishedID )
610
      {
611
      mRotationFinishedID = 0;
612
      removeRotation();
613
      }
614
    else if( effectID == mAddRotationID )
615
      {
616
      mAddRotationID = 0;
617
      mRemoveRotationID = effectID;
618
      removePatternRotation();
619
      }
620
    else
621
      {
622
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
623
        {
624
        if( effectID == mEffectID[i] )
625
          {
626
          if( i!=BaseEffect.Type.WIN.ordinal() ) unblockEverything();
627
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() ) mInterface.onScrambleEffectFinished();
628
          if( i==BaseEffect.Type.WIN.ordinal()      ) mInterface.onWinEffectFinished(mDebug,mScrambleObjectNum);
629
          break;
630
          }
631
        }
632
      }
633
    }
634
  }
(4-4/13)