Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectPreRender.java @ 8c069e23

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

    
52
  private boolean mFinishRotation, mRemoveRotation, mRemovePatternRotation, mAddRotation,
53
                  mSetQuat, mChangeObject, mSolveObject, mScrambleObject,
54
                  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
  // debugging only
68
  private long mAddRotationTime;
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

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

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

    
87
    mOldObject = null;
88
    mNewObject = null;
89

    
90
    mDebug = "";
91
    mScrambleObjectNum = 0;
92

    
93
    mEffectID = new long[BaseEffect.Type.LENGTH];
94

    
95
    mBlockController = new BlockController(act,this);
96
    unblockEverything();
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  private void createObjectNow(int ordinal, InputStream jsonStream, InputStream meshStream)
102
    {
103
    boolean firstTime = (mNewObject==null);
104

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

    
110
    long time1 = System.currentTimeMillis();
111

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

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

    
118
    if( mNewObject!=null )
119
      {
120
      mNewObject.setLibInterface(mInterface);
121
      TwistyObjectNode node = mController.getNode();
122
      if( node!=null ) mNewObject.setObjectRatioNow( 1.0f, node.getScale(), node.getMinSize() );
123
      mController.setMovement(mNewObject.getMovement());
124
      if( firstTime && mPreferences!=null ) mNewObject.restorePreferences(mPreferences);
125
      mIsSolved = mNewObject.isSolved();
126
      }
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
131

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  private void removeRotationNow()
149
    {
150
    mRemoveRotation=false;
151
    mNewObject.removeRotationNow();
152

    
153
    boolean solved = mNewObject.isSolved();
154

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

    
166
    mIsSolved = solved;
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  private void removeRotation()
172
    {
173
    mRemoveRotation = true;
174
    }
175

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

    
178
  private void removePatternRotation()
179
    {
180
    mRemovePatternRotation = true;
181
    }
182

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

    
185
  private void removePatternRotationNow()
186
    {
187
    mRemovePatternRotation=false;
188
    mNewObject.removeRotationNow();
189
    mAddActionListener.onActionFinished(mRemoveRotationID);
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  private void addRotationNow()
195
    {
196
    mAddRotation = false;
197

    
198
    if( mNewObject.NUM_AXIS>mAddRotationAxis )
199
      {
200
      mAddRotationID = mNewObject.addNewRotation( mAddRotationAxis, mAddRotationRowBitmap,
201
                                                  mAddRotationAngle, mAddRotationDuration, this);
202

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

    
214
      String error = "time now: "+timeNow+" add time: "+mAddRotationTime+" axis="+mAddRotationAxis+
215
                      "object: "+mNewObject.getShortName()+" "+name;
216

    
217
      mInterface.reportProblem(error);
218
      unblockEverything();
219
      }
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  private void finishRotationNow()
225
    {
226
    mFinishRotation = false;
227
    blockEverything(BlockController.PLACE_0);
228
    mRotationFinishedID = mNewObject.finishRotationNow(this, mNearestAngle);
229

    
230
    if( mRotationFinishedID==0 ) // failed to add effect - should never happen
231
      {
232
      unblockEverything();
233
      }
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  private void changeObjectNow()
239
    {
240
    mChangeObject = false;
241
    blockEverything(BlockController.PLACE_1);
242
    createObjectNow(mOrdinal,mJsonStream,mMeshStream);
243
    doEffectNow( BaseEffect.Type.SIZECHANGE );
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

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

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  private void solveObjectNow()
259
    {
260
    mSolveObject = false;
261
    blockEverything(BlockController.PLACE_4);
262
    doEffectNow( BaseEffect.Type.SOLVE );
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  private void solveNow()
268
    {
269
    mSolve = false;
270
    if( mNewObject!=null ) mNewObject.solve();
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
  private void initializeObjectNow()
276
    {
277
    mInitializeObject = false;
278
    mNewObject.initializeObject(mNextMoves);
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  private void setTextureMapNow()
284
    {
285
    mSetTextureMap = false;
286

    
287
    if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
  private void resetAllTextureMapsNow()
293
    {
294
    mResetAllTextureMaps = false;
295
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  private void setQuatNow()
301
    {
302
    mSetQuat = false;
303
    mController.setQuat();
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
  void rememberMove(int axis, int row, int angle)
309
    {
310
    mDebug += (" (m "+axis+" "+(1<<row)+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
311
    }
312

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

    
315
  void finishRotation(int nearestAngle)
316
    {
317
    mNearestAngle   = nearestAngle;
318
    mFinishRotation = true;
319
    }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

    
323
  void setTextureMap(int cubit, int face, int newColor)
324
    {
325
    mSetTextureMap = true;
326

    
327
    mCubit    = cubit;
328
    mFace     = face;
329
    mNewColor = newColor;
330
    }
331

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

    
334
  void setQuatOnNextRender()
335
    {
336
    mSetQuat = true;
337
    }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
  void setMove(float xmove, float ymove)
342
    {
343
    mMoveX = xmove;
344
    mMoveY = ymove;
345
    }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348
// INTERNAL API
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
  public int getNumScrambles()
352
    {
353
    return mScrambleObjectNum;
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
  public TwistyObject getOldObject()
359
    {
360
    return mOldObject;
361
    }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
  public float getMoveX()
366
    {
367
    return mMoveX;
368
    }
369

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

    
372
  public float getMoveY()
373
    {
374
    return mMoveY;
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
  public ObjectLibInterface getInterface()
380
    {
381
    return mInterface;
382
    }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385
// PUBLIC API
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

    
388
  public void setScreenSize()
389
    {
390
    if( mNewObject!=null ) mNewObject.createTexture();
391
    }
392

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

    
395
  public void savePreferences(SharedPreferences.Editor editor)
396
    {
397
    if( mNewObject!=null )
398
      {
399
      mNewObject.savePreferences(editor);
400
      }
401
    }
402

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

    
405
  public void restorePreferences(SharedPreferences preferences)
406
    {
407
    mPreferences = preferences;
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
  public void changeObject(int ordinal, InputStream jsonStream, InputStream meshStream)
413
    {
414
    mChangeObject = true;
415
    mOrdinal    = ordinal;
416
    mJsonStream = jsonStream;
417
    mMeshStream = meshStream;
418
    }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

    
422
  public boolean isTouchBlocked()
423
    {
424
    return mTouchBlocked;
425
    }
426

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

    
429
  public boolean isUINotBlocked()
430
    {
431
    return !mUIBlocked;
432
    }
433

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435

    
436
  public void blockEverything(int place)
437
    {
438
    mUIBlocked   = true;
439
    mTouchBlocked= true;
440
    mBlockController.touchBlocked(place);
441
    mBlockController.uiBlocked(place);
442
    }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

    
446
  public void blockTouch(int place)
447
    {
448
    mTouchBlocked= true;
449
    mBlockController.touchBlocked(place);
450
    }
451

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

    
454
  public void unblockEverything()
455
    {
456
    mUIBlocked   = false;
457
    mTouchBlocked= false;
458
    mBlockController.touchUnblocked();
459
    mBlockController.uiUnblocked();
460
    }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

    
464
  public void unblockTouch()
465
    {
466
    mTouchBlocked= false;
467
    mBlockController.touchUnblocked();
468
    }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

    
472
  public void unblockUI()
473
    {
474
    mUIBlocked= false;
475
    mBlockController.uiUnblocked();
476
    }
477

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479

    
480
  public void preRender()
481
    {
482
    if( mSolve                 ) solveNow();
483
    if( mSetQuat               ) setQuatNow();
484
    if( mFinishRotation        ) finishRotationNow();
485
    if( mRemoveRotation        ) removeRotationNow();
486
    if( mRemovePatternRotation ) removePatternRotationNow();
487
    if( mChangeObject          ) changeObjectNow();
488
    if( mSolveObject           ) solveObjectNow();
489
    if( mScrambleObject        ) scrambleObjectNow();
490
    if( mAddRotation           ) addRotationNow();
491
    if( mInitializeObject      ) initializeObjectNow();
492
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
493
    if( mSetTextureMap         ) setTextureMapNow();
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
499
    {
500
    int[] angles = mNewObject.getBasicAngle();
501

    
502
    if( angles.length>axis )
503
      {
504
      mAddRotation = true;
505

    
506
      int basicAngle= angles[axis];
507
      int angle     = bareAngle*(360/basicAngle);
508
      int duration  = Math.abs(angle)*millPreDegree;
509

    
510
      mAddActionListener    = listener;
511
      mAddRotationAxis      = axis;
512
      mAddRotationRowBitmap = rowBitmap;
513
      mAddRotationAngle     = angle;
514
      mAddRotationDuration  = duration;
515
      mAddRotationTime      = System.currentTimeMillis();
516

    
517
      if( listener instanceof ScrambleEffect )
518
        {
519
        mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
520
        }
521
      }
522
    }
523

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525

    
526
  public void initializeObject(int[][] moves)
527
    {
528
    mInitializeObject = true;
529
    mNextMoves = moves;
530
    }
531

    
532
///////////////////////////////////////////////////////////////////////////////////////////////////
533

    
534
  public void scrambleObject(int num)
535
    {
536
    if( !mUIBlocked )
537
      {
538
      mScrambleObject = true;
539
      mScrambleObjectNum = num;
540
      mDebug = "";
541
      mDebugStartTime = System.currentTimeMillis();
542
      }
543
    }
544

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546
// this starts the Solve Effect
547

    
548
  public void solveObject()
549
    {
550
    if( !mUIBlocked )
551
      {
552
      mSolveObject = true;
553
      }
554
    }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557
// this only sets the cubits state to solved
558

    
559
  public void solveOnly()
560
    {
561
    mSolve = true;
562
    }
563

    
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565

    
566
  public void resetAllTextureMaps()
567
    {
568
    mResetAllTextureMaps = true;
569
    }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572

    
573
  public TwistyObject getObject()
574
    {
575
    return mNewObject;
576
    }
577

    
578
///////////////////////////////////////////////////////////////////////////////////////////////////
579

    
580
  public TwistyObjectNode getObjectNode()
581
    {
582
    return mController.getNode();
583
    }
584

    
585
///////////////////////////////////////////////////////////////////////////////////////////////////
586

    
587
  public void effectFinished(final long effectID)
588
    {
589
    if( effectID == mRotationFinishedID )
590
      {
591
      mRotationFinishedID = 0;
592
      removeRotation();
593
      }
594
    else if( effectID == mAddRotationID )
595
      {
596
      mAddRotationID = 0;
597
      mRemoveRotationID = effectID;
598
      removePatternRotation();
599
      }
600
    else
601
      {
602
      for(int i=0; i<BaseEffect.Type.LENGTH; i++)
603
        {
604
        if( effectID == mEffectID[i] )
605
          {
606
          if( i!=BaseEffect.Type.WIN.ordinal() ) unblockEverything();
607
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() ) mInterface.onScrambleEffectFinished();
608
          if( i==BaseEffect.Type.WIN.ordinal()      ) mInterface.onWinEffectFinished(mDebug,mScrambleObjectNum);
609
          break;
610
          }
611
        }
612
      }
613
    }
614
  }
(9-9/18)