Project

General

Profile

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

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

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

    
115
    if( jsonStream==null ) mNewObject = ObjectType.create( ordinal, quat, move, mScale, meshStream);
116
    else                   mNewObject = new TwistyJson( jsonStream, 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.setMovement(mNewObject.getMovement());
125
      if( firstTime && mPreferences!=null ) mNewObject.restorePreferences(mPreferences);
126
      mIsSolved = mNewObject.isSolved();
127
      }
128
    }
129

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

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

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

    
154
    boolean solved = mNewObject.isSolved();
155

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

    
167
    mIsSolved = solved;
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

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

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

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

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

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

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

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

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

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

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

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

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

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

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

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

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

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

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

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

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

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

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

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

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

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

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

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

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

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

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

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

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

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

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

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

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

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

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

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

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

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

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

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356
// INTERNAL API
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

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

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

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

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

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

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

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

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

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

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393
// PUBLIC API
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395

    
396
  public void setScreenSize()
397
    {
398
    if( mNewObject!=null ) mNewObject.setTexture();
399
    }
400

    
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402

    
403
  public void savePreferences(SharedPreferences.Editor editor)
404
    {
405
    if( mNewObject!=null )
406
      {
407
      mNewObject.savePreferences(editor);
408
      }
409
    }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

    
413
  public void restorePreferences(SharedPreferences preferences)
414
    {
415
    mPreferences = preferences;
416
    }
417

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

    
420
  public void changeObject(int ordinal, InputStream jsonStream, InputStream meshStream)
421
    {
422
    mChangeObject = true;
423
    mOrdinal    = ordinal;
424
    mJsonStream = jsonStream;
425
    mMeshStream = meshStream;
426
    }
427

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429

    
430
  public boolean isTouchBlocked()
431
    {
432
    return mTouchBlocked;
433
    }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
  public boolean isUINotBlocked()
438
    {
439
    return !mUIBlocked;
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
  public void blockEverything(int place)
445
    {
446
    mUIBlocked   = true;
447
    mTouchBlocked= true;
448
    mBlockController.touchBlocked(place);
449
    mBlockController.uiBlocked(place);
450
    }
451

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

    
454
  public void blockTouch(int place)
455
    {
456
    mTouchBlocked= true;
457
    mBlockController.touchBlocked(place);
458
    }
459

    
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461

    
462
  public void unblockEverything()
463
    {
464
    mUIBlocked   = false;
465
    mTouchBlocked= false;
466
    mBlockController.touchUnblocked();
467
    mBlockController.uiUnblocked();
468
    }
469

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

    
472
  public void unblockTouch()
473
    {
474
    mTouchBlocked= false;
475
    mBlockController.touchUnblocked();
476
    }
477

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

    
480
  public void unblockUI()
481
    {
482
    mUIBlocked= false;
483
    mBlockController.uiUnblocked();
484
    }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
  public void preRender()
489
    {
490
    if( mSolve                 ) solveNow();
491
    if( mSetQuat               ) setQuatNow();
492
    if( mFinishRotation        ) finishRotationNow();
493
    if( mRemoveRotation        ) removeRotationNow();
494
    if( mRemovePatternRotation ) removePatternRotationNow();
495
    if( mChangeObject          ) changeObjectNow();
496
    if( mSolveObject           ) solveObjectNow();
497
    if( mScrambleObject        ) scrambleObjectNow();
498
    if( mAddRotation           ) addRotationNow();
499
    if( mInitializeObject      ) initializeObjectNow();
500
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
501
    if( mSetTextureMap         ) setTextureMapNow();
502
    }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
507
    {
508
    int[] angles = mNewObject.getBasicAngle();
509

    
510
    if( angles.length>axis )
511
      {
512
      mAddRotation = true;
513

    
514
      int basicAngle= angles[axis];
515
      int angle     = bareAngle*(360/basicAngle);
516
      int duration  = Math.abs(angle)*millPreDegree;
517

    
518
      mAddActionListener    = listener;
519
      mAddRotationAxis      = axis;
520
      mAddRotationRowBitmap = rowBitmap;
521
      mAddRotationAngle     = angle;
522
      mAddRotationDuration  = duration;
523
      mAddRotationTime      = System.currentTimeMillis();
524

    
525
      if( listener instanceof ScrambleEffect )
526
        {
527
        mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
528
        }
529
      }
530
    }
531

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

    
534
  public void initializeObject(int[][] moves)
535
    {
536
    mInitializeObject = true;
537
    mNextMoves = moves;
538
    }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541

    
542
  public void scrambleObject(int num)
543
    {
544
    if( !mUIBlocked )
545
      {
546
      mScrambleObject = true;
547
      mScrambleObjectNum = num;
548
      mDebug = "";
549
      mDebugStartTime = System.currentTimeMillis();
550
      }
551
    }
552

    
553
///////////////////////////////////////////////////////////////////////////////////////////////////
554
// this starts the Solve Effect
555

    
556
  public void solveObject()
557
    {
558
    if( !mUIBlocked )
559
      {
560
      mSolveObject = true;
561
      }
562
    }
563

    
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565
// this only sets the cubits state to solved
566

    
567
  public void solveOnly()
568
    {
569
    mSolve = true;
570
    }
571

    
572
///////////////////////////////////////////////////////////////////////////////////////////////////
573

    
574
  public void resetAllTextureMaps()
575
    {
576
    mResetAllTextureMaps = true;
577
    }
578

    
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580

    
581
  public TwistyObject getObject()
582
    {
583
    return mNewObject;
584
    }
585

    
586
///////////////////////////////////////////////////////////////////////////////////////////////////
587

    
588
  public TwistyObjectNode getObjectNode()
589
    {
590
    return mController.getNode();
591
    }
592

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594

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