Project

General

Profile

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

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

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 boolean isTouchBlocked()
427
    {
428
    return mTouchBlocked;
429
    }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

    
433
  public boolean isUINotBlocked()
434
    {
435
    return !mUIBlocked;
436
    }
437

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

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

    
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449

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

    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457

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

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

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

    
474
///////////////////////////////////////////////////////////////////////////////////////////////////
475

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

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

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

    
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501

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

    
506
    if( angles.length>axis )
507
      {
508
      mAddRotation = true;
509

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

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

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

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

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

    
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537

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

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
// this starts the Solve Effect
551

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

    
560
///////////////////////////////////////////////////////////////////////////////////////////////////
561
// this only sets the cubits state to solved
562

    
563
  public void solveOnly()
564
    {
565
    mSolve = true;
566
    }
567

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569

    
570
  public void resetAllTextureMaps()
571
    {
572
    mResetAllTextureMaps = true;
573
    }
574

    
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576

    
577
  public TwistyObject getObject()
578
    {
579
    return mNewObject;
580
    }
581

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583

    
584
  public TwistyObjectNode getObjectNode()
585
    {
586
    return mController.getNode();
587
    }
588

    
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590

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