Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectPreRender.java @ 11fa413d

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

    
68
  // debugging only
69
  private long mAddRotationTime;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

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

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

    
88
    mOldObject = null;
89
    mNewObject = null;
90

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

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

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

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

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

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

    
112
    long time1 = System.currentTimeMillis();
113

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

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

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

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

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

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

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

    
155
    boolean solved = mNewObject.isSolved();
156

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

    
168
    mIsSolved = solved;
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

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

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

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

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

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

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

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

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

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

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

    
219
      mInterface.reportProblem(error);
220
      unblockEverything();
221
      }
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

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

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

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

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

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

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

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

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

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

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

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

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

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

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

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

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

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

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

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

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

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

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

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

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

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

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

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

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

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

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

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

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

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357
// INTERNAL API
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

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

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

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

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

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

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

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

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

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

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394
// PUBLIC API
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

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

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

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

    
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413

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

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

    
424
  public boolean isTouchBlocked()
425
    {
426
    return mTouchBlocked;
427
    }
428

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

    
431
  public boolean isUINotBlocked()
432
    {
433
    return !mUIBlocked;
434
    }
435

    
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437

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

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

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

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

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

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

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

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

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

    
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481

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

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

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

    
504
    if( angles.length>axis )
505
      {
506
      mAddRotation = true;
507

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

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

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

    
526
///////////////////////////////////////////////////////////////////////////////////////////////////
527

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

    
534
///////////////////////////////////////////////////////////////////////////////////////////////////
535

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

    
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548
// this starts the Solve Effect
549

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

    
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559
// this only sets the cubits state to solved
560

    
561
  public void solveOnly()
562
    {
563
    mSolve = true;
564
    }
565

    
566
///////////////////////////////////////////////////////////////////////////////////////////////////
567

    
568
  public void resetAllTextureMaps()
569
    {
570
    mResetAllTextureMaps = true;
571
    }
572

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

    
575
  public TwistyObject getObject()
576
    {
577
    return mNewObject;
578
    }
579

    
580
///////////////////////////////////////////////////////////////////////////////////////////////////
581

    
582
  public TwistyObjectNode getObjectNode()
583
    {
584
    return mController.getNode();
585
    }
586

    
587
///////////////////////////////////////////////////////////////////////////////////////////////////
588

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