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
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
68
|
|
69
|
public ObjectPreRender(Activity act, ObjectControl controller, ObjectLibInterface actioner)
|
70
|
{
|
71
|
mInterface = actioner;
|
72
|
mController = controller;
|
73
|
|
74
|
mFinishRotation = false;
|
75
|
mRemoveRotation = false;
|
76
|
mRemovePatternRotation= false;
|
77
|
mAddRotation = false;
|
78
|
mSetQuat = false;
|
79
|
mChangeObject = false;
|
80
|
mSolveObject = false;
|
81
|
mSolve = false;
|
82
|
mScrambleObject = false;
|
83
|
|
84
|
mOldObject = null;
|
85
|
mNewObject = null;
|
86
|
|
87
|
mDebug = "";
|
88
|
mScrambleObjectNum = 0;
|
89
|
|
90
|
mEffectID = new long[BaseEffect.Type.LENGTH];
|
91
|
|
92
|
mBlockController = new BlockController(act,this);
|
93
|
unblockEverything();
|
94
|
}
|
95
|
|
96
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
97
|
|
98
|
private void createObjectNow(int ordinal, InputStream jsonStream, InputStream meshStream)
|
99
|
{
|
100
|
boolean firstTime = (mNewObject==null);
|
101
|
|
102
|
if( mOldObject!=null ) mOldObject.releaseResources();
|
103
|
mOldObject = mNewObject;
|
104
|
Static3D move = new Static3D(mMoveX,mMoveY,0);
|
105
|
Static4D quat = mController.getQuat();
|
106
|
|
107
|
long time1 = System.currentTimeMillis();
|
108
|
|
109
|
if( jsonStream==null ) mNewObject = ObjectType.create( ordinal, quat, move, meshStream);
|
110
|
else mNewObject = new TwistyJson( jsonStream, quat, move, meshStream);
|
111
|
|
112
|
long time2 = System.currentTimeMillis();
|
113
|
mInterface.onObjectCreated(time2-time1);
|
114
|
|
115
|
if( mNewObject!=null )
|
116
|
{
|
117
|
mNewObject.setLibInterface(mInterface);
|
118
|
TwistyObjectNode node = mController.getNode();
|
119
|
if( node!=null ) mNewObject.setObjectRatioNow( 1.0f, node.getScale(), node.getMinSize() );
|
120
|
mController.setMovement(mNewObject.getMovement());
|
121
|
if( firstTime && mPreferences!=null ) mNewObject.restorePreferences(mPreferences);
|
122
|
mIsSolved = mNewObject.isSolved();
|
123
|
}
|
124
|
}
|
125
|
|
126
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
127
|
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
|
128
|
|
129
|
private void doEffectNow(BaseEffect.Type type)
|
130
|
{
|
131
|
try
|
132
|
{
|
133
|
int index = type.ordinal();
|
134
|
mEffectID[index] = type.startEffect(this);
|
135
|
}
|
136
|
catch( Exception ex )
|
137
|
{
|
138
|
android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
|
139
|
unblockEverything();
|
140
|
}
|
141
|
}
|
142
|
|
143
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144
|
|
145
|
private void removeRotationNow()
|
146
|
{
|
147
|
mRemoveRotation=false;
|
148
|
mNewObject.removeRotationNow();
|
149
|
|
150
|
boolean solved = mNewObject.isSolved();
|
151
|
|
152
|
if( solved && !mIsSolved )
|
153
|
{
|
154
|
mInterface.onSolved();
|
155
|
unblockEverything();
|
156
|
doEffectNow( BaseEffect.Type.WIN );
|
157
|
}
|
158
|
else
|
159
|
{
|
160
|
unblockEverything();
|
161
|
}
|
162
|
|
163
|
mIsSolved = solved;
|
164
|
}
|
165
|
|
166
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
167
|
|
168
|
private void removeRotation()
|
169
|
{
|
170
|
mRemoveRotation = true;
|
171
|
}
|
172
|
|
173
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174
|
|
175
|
private void removePatternRotation()
|
176
|
{
|
177
|
mRemovePatternRotation = true;
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
|
182
|
private void removePatternRotationNow()
|
183
|
{
|
184
|
mRemovePatternRotation=false;
|
185
|
mNewObject.removeRotationNow();
|
186
|
mAddActionListener.onActionFinished(mRemoveRotationID);
|
187
|
}
|
188
|
|
189
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
190
|
|
191
|
private void addRotationNow()
|
192
|
{
|
193
|
mAddRotation = false;
|
194
|
mAddRotationID = mNewObject.addNewRotation( mAddRotationAxis, mAddRotationRowBitmap,
|
195
|
mAddRotationAngle, mAddRotationDuration, this);
|
196
|
|
197
|
if( mAddRotationID==0 ) // failed to add effect - should never happen
|
198
|
{
|
199
|
unblockEverything();
|
200
|
}
|
201
|
}
|
202
|
|
203
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204
|
|
205
|
private void finishRotationNow()
|
206
|
{
|
207
|
mFinishRotation = false;
|
208
|
blockEverything(BlockController.PLACE_0);
|
209
|
mRotationFinishedID = mNewObject.finishRotationNow(this, mNearestAngle);
|
210
|
|
211
|
if( mRotationFinishedID==0 ) // failed to add effect - should never happen
|
212
|
{
|
213
|
unblockEverything();
|
214
|
}
|
215
|
}
|
216
|
|
217
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
218
|
|
219
|
private void changeObjectNow()
|
220
|
{
|
221
|
mChangeObject = false;
|
222
|
blockEverything(BlockController.PLACE_1);
|
223
|
createObjectNow(mOrdinal,mJsonStream,mMeshStream);
|
224
|
doEffectNow( BaseEffect.Type.SIZECHANGE );
|
225
|
}
|
226
|
|
227
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
228
|
|
229
|
private void scrambleObjectNow()
|
230
|
{
|
231
|
mScrambleObject = false;
|
232
|
mIsSolved = false;
|
233
|
blockEverything(BlockController.PLACE_3);
|
234
|
doEffectNow( BaseEffect.Type.SCRAMBLE );
|
235
|
}
|
236
|
|
237
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
238
|
|
239
|
private void solveObjectNow()
|
240
|
{
|
241
|
mSolveObject = false;
|
242
|
blockEverything(BlockController.PLACE_4);
|
243
|
doEffectNow( BaseEffect.Type.SOLVE );
|
244
|
}
|
245
|
|
246
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
247
|
|
248
|
private void solveNow()
|
249
|
{
|
250
|
mSolve = false;
|
251
|
if( mNewObject!=null ) mNewObject.solve();
|
252
|
}
|
253
|
|
254
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
255
|
|
256
|
private void initializeObjectNow()
|
257
|
{
|
258
|
mInitializeObject = false;
|
259
|
mNewObject.initializeObject(mNextMoves);
|
260
|
}
|
261
|
|
262
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
263
|
|
264
|
private void setTextureMapNow()
|
265
|
{
|
266
|
mSetTextureMap = false;
|
267
|
|
268
|
if( mNewObject!=null ) mNewObject.setTextureMap(mCubit,mFace,mNewColor);
|
269
|
}
|
270
|
|
271
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
272
|
|
273
|
private void resetAllTextureMapsNow()
|
274
|
{
|
275
|
mResetAllTextureMaps = false;
|
276
|
if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
|
277
|
}
|
278
|
|
279
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
280
|
|
281
|
private void setQuatNow()
|
282
|
{
|
283
|
mSetQuat = false;
|
284
|
mController.setQuat();
|
285
|
}
|
286
|
|
287
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
288
|
|
289
|
void rememberMove(int axis, int row, int angle)
|
290
|
{
|
291
|
mDebug += (" (m "+axis+" "+(1<<row)+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
|
292
|
}
|
293
|
|
294
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
295
|
|
296
|
void finishRotation(int nearestAngle)
|
297
|
{
|
298
|
mNearestAngle = nearestAngle;
|
299
|
mFinishRotation = true;
|
300
|
}
|
301
|
|
302
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
303
|
|
304
|
void setTextureMap(int cubit, int face, int newColor)
|
305
|
{
|
306
|
mSetTextureMap = true;
|
307
|
|
308
|
mCubit = cubit;
|
309
|
mFace = face;
|
310
|
mNewColor = newColor;
|
311
|
}
|
312
|
|
313
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
314
|
|
315
|
void setQuatOnNextRender()
|
316
|
{
|
317
|
mSetQuat = true;
|
318
|
}
|
319
|
|
320
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
321
|
|
322
|
void setMove(float xmove, float ymove)
|
323
|
{
|
324
|
mMoveX = xmove;
|
325
|
mMoveY = ymove;
|
326
|
}
|
327
|
|
328
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
329
|
// INTERNAL API
|
330
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
331
|
|
332
|
public int getNumScrambles()
|
333
|
{
|
334
|
return mScrambleObjectNum;
|
335
|
}
|
336
|
|
337
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
338
|
|
339
|
public TwistyObject getOldObject()
|
340
|
{
|
341
|
return mOldObject;
|
342
|
}
|
343
|
|
344
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
345
|
|
346
|
public float getMoveX()
|
347
|
{
|
348
|
return mMoveX;
|
349
|
}
|
350
|
|
351
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
352
|
|
353
|
public float getMoveY()
|
354
|
{
|
355
|
return mMoveY;
|
356
|
}
|
357
|
|
358
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
359
|
|
360
|
public ObjectLibInterface getInterface()
|
361
|
{
|
362
|
return mInterface;
|
363
|
}
|
364
|
|
365
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
366
|
// PUBLIC API
|
367
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
368
|
|
369
|
public void setScreenSize()
|
370
|
{
|
371
|
if( mNewObject!=null ) mNewObject.createTexture();
|
372
|
}
|
373
|
|
374
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
375
|
|
376
|
public void savePreferences(SharedPreferences.Editor editor)
|
377
|
{
|
378
|
if( mNewObject!=null )
|
379
|
{
|
380
|
mNewObject.savePreferences(editor);
|
381
|
}
|
382
|
}
|
383
|
|
384
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
385
|
|
386
|
public void restorePreferences(SharedPreferences preferences)
|
387
|
{
|
388
|
mPreferences = preferences;
|
389
|
}
|
390
|
|
391
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
392
|
|
393
|
public void changeObject(int ordinal, InputStream jsonStream, InputStream meshStream)
|
394
|
{
|
395
|
mChangeObject = true;
|
396
|
mOrdinal = ordinal;
|
397
|
mJsonStream = jsonStream;
|
398
|
mMeshStream = meshStream;
|
399
|
}
|
400
|
|
401
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
402
|
|
403
|
public boolean isTouchBlocked()
|
404
|
{
|
405
|
return mTouchBlocked;
|
406
|
}
|
407
|
|
408
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
409
|
|
410
|
public boolean isUINotBlocked()
|
411
|
{
|
412
|
return !mUIBlocked;
|
413
|
}
|
414
|
|
415
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
416
|
|
417
|
public void blockEverything(int place)
|
418
|
{
|
419
|
mUIBlocked = true;
|
420
|
mTouchBlocked= true;
|
421
|
mBlockController.touchBlocked(place);
|
422
|
mBlockController.uiBlocked(place);
|
423
|
}
|
424
|
|
425
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
426
|
|
427
|
public void blockTouch(int place)
|
428
|
{
|
429
|
mTouchBlocked= true;
|
430
|
mBlockController.touchBlocked(place);
|
431
|
}
|
432
|
|
433
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
434
|
|
435
|
public void unblockEverything()
|
436
|
{
|
437
|
mUIBlocked = false;
|
438
|
mTouchBlocked= false;
|
439
|
mBlockController.touchUnblocked();
|
440
|
mBlockController.uiUnblocked();
|
441
|
}
|
442
|
|
443
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
444
|
|
445
|
public void unblockTouch()
|
446
|
{
|
447
|
mTouchBlocked= false;
|
448
|
mBlockController.touchUnblocked();
|
449
|
}
|
450
|
|
451
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
452
|
|
453
|
public void unblockUI()
|
454
|
{
|
455
|
mUIBlocked= false;
|
456
|
mBlockController.uiUnblocked();
|
457
|
}
|
458
|
|
459
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
460
|
|
461
|
public void preRender()
|
462
|
{
|
463
|
if( mSolve ) solveNow();
|
464
|
if( mSetQuat ) setQuatNow();
|
465
|
if( mFinishRotation ) finishRotationNow();
|
466
|
if( mRemoveRotation ) removeRotationNow();
|
467
|
if( mRemovePatternRotation ) removePatternRotationNow();
|
468
|
if( mChangeObject ) changeObjectNow();
|
469
|
if( mSolveObject ) solveObjectNow();
|
470
|
if( mScrambleObject ) scrambleObjectNow();
|
471
|
if( mAddRotation ) addRotationNow();
|
472
|
if( mInitializeObject ) initializeObjectNow();
|
473
|
if( mResetAllTextureMaps ) resetAllTextureMapsNow();
|
474
|
if( mSetTextureMap ) setTextureMapNow();
|
475
|
}
|
476
|
|
477
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
478
|
|
479
|
public void addRotation(MovesFinished listener, int axis, int rowBitmap, int bareAngle, int millPreDegree)
|
480
|
{
|
481
|
mAddRotation = true;
|
482
|
|
483
|
int basicAngle= mNewObject.getBasicAngle()[axis];
|
484
|
int angle = bareAngle*(360/basicAngle);
|
485
|
int duration = Math.abs(angle)*millPreDegree;
|
486
|
|
487
|
mAddActionListener = listener;
|
488
|
mAddRotationAxis = axis;
|
489
|
mAddRotationRowBitmap = rowBitmap;
|
490
|
mAddRotationAngle = angle;
|
491
|
mAddRotationDuration = duration;
|
492
|
|
493
|
if( listener instanceof ScrambleEffect )
|
494
|
{
|
495
|
mDebug += (" (a "+axis+" "+rowBitmap+" "+angle+" "+(System.currentTimeMillis()-mDebugStartTime)+")");
|
496
|
}
|
497
|
}
|
498
|
|
499
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
500
|
|
501
|
public void initializeObject(int[][] moves)
|
502
|
{
|
503
|
mInitializeObject = true;
|
504
|
mNextMoves = moves;
|
505
|
}
|
506
|
|
507
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
508
|
|
509
|
public void scrambleObject(int num)
|
510
|
{
|
511
|
if( !mUIBlocked )
|
512
|
{
|
513
|
mScrambleObject = true;
|
514
|
mScrambleObjectNum = num;
|
515
|
mDebug = "";
|
516
|
mDebugStartTime = System.currentTimeMillis();
|
517
|
}
|
518
|
}
|
519
|
|
520
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
521
|
// this starts the Solve Effect
|
522
|
|
523
|
public void solveObject()
|
524
|
{
|
525
|
if( !mUIBlocked )
|
526
|
{
|
527
|
mSolveObject = true;
|
528
|
}
|
529
|
}
|
530
|
|
531
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
532
|
// this only sets the cubits state to solved
|
533
|
|
534
|
public void solveOnly()
|
535
|
{
|
536
|
mSolve = true;
|
537
|
}
|
538
|
|
539
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
540
|
|
541
|
public void resetAllTextureMaps()
|
542
|
{
|
543
|
mResetAllTextureMaps = true;
|
544
|
}
|
545
|
|
546
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
547
|
|
548
|
public TwistyObject getObject()
|
549
|
{
|
550
|
return mNewObject;
|
551
|
}
|
552
|
|
553
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
554
|
|
555
|
public TwistyObjectNode getObjectNode()
|
556
|
{
|
557
|
return mController.getNode();
|
558
|
}
|
559
|
|
560
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
561
|
|
562
|
public void effectFinished(final long effectID)
|
563
|
{
|
564
|
if( effectID == mRotationFinishedID )
|
565
|
{
|
566
|
mRotationFinishedID = 0;
|
567
|
removeRotation();
|
568
|
}
|
569
|
else if( effectID == mAddRotationID )
|
570
|
{
|
571
|
mAddRotationID = 0;
|
572
|
mRemoveRotationID = effectID;
|
573
|
removePatternRotation();
|
574
|
}
|
575
|
else
|
576
|
{
|
577
|
for(int i=0; i<BaseEffect.Type.LENGTH; i++)
|
578
|
{
|
579
|
if( effectID == mEffectID[i] )
|
580
|
{
|
581
|
if( i!=BaseEffect.Type.WIN.ordinal() ) unblockEverything();
|
582
|
if( i==BaseEffect.Type.SCRAMBLE.ordinal() ) mInterface.onScrambleEffectFinished();
|
583
|
if( i==BaseEffect.Type.WIN.ordinal() ) mInterface.onWinEffectFinished(mDebug,mScrambleObjectNum);
|
584
|
break;
|
585
|
}
|
586
|
}
|
587
|
}
|
588
|
}
|
589
|
}
|