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 savePreferences(SharedPreferences.Editor editor)
|
397
|
{
|
398
|
if( mNewObject!=null )
|
399
|
{
|
400
|
mNewObject.savePreferences(editor);
|
401
|
}
|
402
|
}
|
403
|
|
404
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
405
|
|
406
|
public void restorePreferences(SharedPreferences preferences)
|
407
|
{
|
408
|
mPreferences = preferences;
|
409
|
}
|
410
|
|
411
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
412
|
|
413
|
public void changeObject(int ordinal, InputStream jsonStream, InputStream meshStream)
|
414
|
{
|
415
|
mChangeObject = true;
|
416
|
mOrdinal = ordinal;
|
417
|
mJsonStream = jsonStream;
|
418
|
mMeshStream = meshStream;
|
419
|
}
|
420
|
|
421
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
422
|
|
423
|
public boolean isTouchBlocked()
|
424
|
{
|
425
|
return mTouchBlocked;
|
426
|
}
|
427
|
|
428
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
429
|
|
430
|
public boolean isUINotBlocked()
|
431
|
{
|
432
|
return !mUIBlocked;
|
433
|
}
|
434
|
|
435
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
436
|
|
437
|
public void blockEverything(int place)
|
438
|
{
|
439
|
mUIBlocked = true;
|
440
|
mTouchBlocked= true;
|
441
|
mBlockController.touchBlocked(place);
|
442
|
mBlockController.uiBlocked(place);
|
443
|
}
|
444
|
|
445
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
446
|
|
447
|
public void blockTouch(int place)
|
448
|
{
|
449
|
mTouchBlocked= true;
|
450
|
mBlockController.touchBlocked(place);
|
451
|
}
|
452
|
|
453
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
454
|
|
455
|
public void unblockEverything()
|
456
|
{
|
457
|
mUIBlocked = false;
|
458
|
mTouchBlocked= false;
|
459
|
mBlockController.touchUnblocked();
|
460
|
mBlockController.uiUnblocked();
|
461
|
}
|
462
|
|
463
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
464
|
|
465
|
public void unblockTouch()
|
466
|
{
|
467
|
mTouchBlocked= false;
|
468
|
mBlockController.touchUnblocked();
|
469
|
}
|
470
|
|
471
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
472
|
|
473
|
public void unblockUI()
|
474
|
{
|
475
|
mUIBlocked= false;
|
476
|
mBlockController.uiUnblocked();
|
477
|
}
|
478
|
|
479
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
480
|
|
481
|
public void preRender()
|
482
|
{
|
483
|
if( mSolve ) solveNow();
|
484
|
if( mSetQuat ) setQuatNow();
|
485
|
if( mFinishRotation ) finishRotationNow();
|
486
|
if( mRemoveRotation ) removeRotationNow();
|
487
|
if( mRemovePatternRotation ) removePatternRotationNow();
|
488
|
if( mChangeObject ) changeObjectNow();
|
489
|
if( mSolveObject ) solveObjectNow();
|
490
|
if( mScrambleObject ) scrambleObjectNow();
|
491
|
if( mAddRotation ) addRotationNow();
|
492
|
if( mInitializeObject ) initializeObjectNow();
|
493
|
if( mResetAllTextureMaps ) resetAllTextureMapsNow();
|
494
|
if( mSetTextureMap ) setTextureMapNow();
|
495
|
}
|
496
|
|
497
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
498
|
|
499
|
public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
|
500
|
{
|
501
|
int[] angles = mNewObject.getBasicAngle();
|
502
|
|
503
|
if( angles.length>axis )
|
504
|
{
|
505
|
mAddRotation = true;
|
506
|
|
507
|
int basicAngle= angles[axis];
|
508
|
int angle = bareAngle*(360/basicAngle);
|
509
|
int duration = Math.abs(angle)*millPreDegree;
|
510
|
|
511
|
mAddActionListener = listener;
|
512
|
mAddRotationAxis = axis;
|
513
|
mAddRotationRowBitmap = rowBitmap;
|
514
|
mAddRotationAngle = angle;
|
515
|
mAddRotationDuration = duration;
|
516
|
mAddRotationTime = System.currentTimeMillis();
|
517
|
|
518
|
if( listener instanceof ScrambleEffect )
|
519
|
{
|
520
|
mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
|
521
|
}
|
522
|
}
|
523
|
}
|
524
|
|
525
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
526
|
|
527
|
public void initializeObject(int[][] moves)
|
528
|
{
|
529
|
mInitializeObject = true;
|
530
|
mNextMoves = moves;
|
531
|
}
|
532
|
|
533
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
534
|
|
535
|
public void scrambleObject(int num)
|
536
|
{
|
537
|
if( !mUIBlocked )
|
538
|
{
|
539
|
mScrambleObject = true;
|
540
|
mScrambleObjectNum = num;
|
541
|
mDebug = "";
|
542
|
mDebugStartTime = System.currentTimeMillis();
|
543
|
}
|
544
|
}
|
545
|
|
546
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
547
|
// this starts the Solve Effect
|
548
|
|
549
|
public void solveObject()
|
550
|
{
|
551
|
if( !mUIBlocked )
|
552
|
{
|
553
|
mSolveObject = true;
|
554
|
}
|
555
|
}
|
556
|
|
557
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
558
|
// this only sets the cubits state to solved
|
559
|
|
560
|
public void solveOnly()
|
561
|
{
|
562
|
mSolve = true;
|
563
|
}
|
564
|
|
565
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
566
|
|
567
|
public void resetAllTextureMaps()
|
568
|
{
|
569
|
mResetAllTextureMaps = true;
|
570
|
}
|
571
|
|
572
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
573
|
|
574
|
public TwistyObject getObject()
|
575
|
{
|
576
|
return mNewObject;
|
577
|
}
|
578
|
|
579
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
580
|
|
581
|
public TwistyObjectNode getObjectNode()
|
582
|
{
|
583
|
return mController.getNode();
|
584
|
}
|
585
|
|
586
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
587
|
|
588
|
public void effectFinished(final long effectID)
|
589
|
{
|
590
|
if( effectID == mRotationFinishedID )
|
591
|
{
|
592
|
mRotationFinishedID = 0;
|
593
|
removeRotation();
|
594
|
}
|
595
|
else if( effectID == mAddRotationID )
|
596
|
{
|
597
|
mAddRotationID = 0;
|
598
|
mRemoveRotationID = effectID;
|
599
|
removePatternRotation();
|
600
|
}
|
601
|
else
|
602
|
{
|
603
|
for(int i=0; i<BaseEffect.Type.LENGTH; i++)
|
604
|
{
|
605
|
if( effectID == mEffectID[i] )
|
606
|
{
|
607
|
if( i!=BaseEffect.Type.WIN.ordinal() ) unblockEverything();
|
608
|
if( i==BaseEffect.Type.SCRAMBLE.ordinal() ) mInterface.onScrambleEffectFinished();
|
609
|
if( i==BaseEffect.Type.WIN.ordinal() ) mInterface.onWinEffectFinished(mDebug,mScrambleObjectNum);
|
610
|
break;
|
611
|
}
|
612
|
}
|
613
|
}
|
614
|
}
|
615
|
}
|