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.objects;
|
21
|
|
22
|
import android.content.SharedPreferences;
|
23
|
import android.content.res.Resources;
|
24
|
import android.graphics.Bitmap;
|
25
|
import android.graphics.Canvas;
|
26
|
import android.graphics.Paint;
|
27
|
|
28
|
import com.google.firebase.crashlytics.FirebaseCrashlytics;
|
29
|
|
30
|
import org.distorted.library.effect.Effect;
|
31
|
import org.distorted.library.effect.MatrixEffectMove;
|
32
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
33
|
import org.distorted.library.effect.MatrixEffectScale;
|
34
|
import org.distorted.library.effect.VertexEffectQuaternion;
|
35
|
import org.distorted.library.effect.VertexEffectRotate;
|
36
|
import org.distorted.library.main.DistortedEffects;
|
37
|
import org.distorted.library.main.DistortedLibrary;
|
38
|
import org.distorted.library.main.DistortedNode;
|
39
|
import org.distorted.library.main.DistortedTexture;
|
40
|
import org.distorted.library.mesh.MeshBase;
|
41
|
import org.distorted.library.mesh.MeshFile;
|
42
|
import org.distorted.library.mesh.MeshJoined;
|
43
|
import org.distorted.library.mesh.MeshSquare;
|
44
|
import org.distorted.library.message.EffectListener;
|
45
|
import org.distorted.library.type.Dynamic1D;
|
46
|
import org.distorted.library.type.Static1D;
|
47
|
import org.distorted.library.type.Static3D;
|
48
|
import org.distorted.library.type.Static4D;
|
49
|
import org.distorted.main.BuildConfig;
|
50
|
|
51
|
import java.io.DataInputStream;
|
52
|
import java.io.IOException;
|
53
|
import java.io.InputStream;
|
54
|
import java.util.Random;
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
|
58
|
public abstract class TwistyObject extends DistortedNode
|
59
|
{
|
60
|
static final int COLOR_YELLOW = 0xffffff00;
|
61
|
static final int COLOR_WHITE = 0xffffffff;
|
62
|
static final int COLOR_BLUE = 0xff0000ff;
|
63
|
static final int COLOR_GREEN = 0xff00ff00;
|
64
|
static final int COLOR_RED = 0xffff0000;
|
65
|
static final int COLOR_BROWN = 0xffb5651d;
|
66
|
static final int COLOR_PINK = 0xffff90ff;
|
67
|
static final int COLOR_VIOLET = 0xff7700bb;
|
68
|
static final int COLOR_BLACK = 0xff000000;
|
69
|
|
70
|
static final int TEXTURE_HEIGHT = 256;
|
71
|
static final int NUM_STICKERS_IN_ROW = 4;
|
72
|
|
73
|
static final float SQ2 = (float)Math.sqrt(2);
|
74
|
static final float SQ3 = (float)Math.sqrt(3);
|
75
|
static final float SQ6 = (float)Math.sqrt(6);
|
76
|
|
77
|
private static final float NODE_RATIO = 1.40f;
|
78
|
private static final float MAX_SIZE_CHANGE = 1.35f;
|
79
|
private static final float MIN_SIZE_CHANGE = 0.8f;
|
80
|
|
81
|
private static boolean mCreateFromDMesh = true;
|
82
|
|
83
|
private static final Static3D CENTER = new Static3D(0,0,0);
|
84
|
private static final int POST_ROTATION_MILLISEC = 500;
|
85
|
|
86
|
final Static3D[] ROTATION_AXIS;
|
87
|
final Static4D[] QUATS;
|
88
|
final Cubit[] CUBITS;
|
89
|
final int NUM_FACES;
|
90
|
final int NUM_TEXTURES;
|
91
|
final int NUM_CUBIT_FACES;
|
92
|
final int NUM_AXIS;
|
93
|
final int NUM_CUBITS;
|
94
|
final float[] CUTS;
|
95
|
final int NUM_CUTS;
|
96
|
|
97
|
private static float mInitScreenRatio;
|
98
|
private static float mObjectScreenRatio = 1.0f;
|
99
|
|
100
|
private final int mNodeSize;
|
101
|
private int mRotRowBitmap;
|
102
|
private int mRotAxis;
|
103
|
private Static3D[] mOrigPos;
|
104
|
private Static3D mNodeScale;
|
105
|
private Static4D mQuat;
|
106
|
private int mNumLayers, mRealSize;
|
107
|
private ObjectList mList;
|
108
|
private MeshBase mMesh;
|
109
|
private DistortedEffects mEffects;
|
110
|
private VertexEffectRotate mRotateEffect;
|
111
|
private Dynamic1D mRotationAngle;
|
112
|
private Static3D mRotationAxis;
|
113
|
private Static3D mObjectScale;
|
114
|
private int[] mQuatDebug;
|
115
|
private int mNumTexRows, mNumTexCols;
|
116
|
|
117
|
float[] mRowChances;
|
118
|
Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
|
119
|
DistortedTexture mTexture;
|
120
|
MatrixEffectScale mScaleEffect;
|
121
|
MatrixEffectQuaternion mQuatEffect;
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
TwistyObject(int numLayers, int realSize, int fov, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
|
126
|
DistortedEffects nodeEffects, int[][] moves, ObjectList list, Resources res, int screenWidth)
|
127
|
{
|
128
|
super(nodeTexture,nodeEffects,nodeMesh);
|
129
|
|
130
|
mNodeSize = screenWidth;
|
131
|
|
132
|
resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
|
133
|
|
134
|
mNumLayers = numLayers;
|
135
|
mRealSize = realSize;
|
136
|
mList = list;
|
137
|
mOrigPos = getCubitPositions(mNumLayers);
|
138
|
|
139
|
QUATS = getQuats();
|
140
|
NUM_CUBITS = mOrigPos.length;
|
141
|
ROTATION_AXIS = getRotationAxis();
|
142
|
NUM_AXIS = ROTATION_AXIS.length;
|
143
|
mInitScreenRatio = getScreenRatio();
|
144
|
NUM_FACES = getNumFaces();
|
145
|
NUM_CUBIT_FACES = getNumCubitFaces();
|
146
|
NUM_TEXTURES = getNumStickerTypes()*NUM_FACES;
|
147
|
CUTS = getCuts(mNumLayers);
|
148
|
NUM_CUTS = CUTS.length;
|
149
|
|
150
|
mQuatDebug = new int[NUM_CUBITS];
|
151
|
|
152
|
if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
|
153
|
if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
|
154
|
|
155
|
mNodeScale= new Static3D(1,NODE_RATIO,1);
|
156
|
mQuat = quat;
|
157
|
|
158
|
mRowChances = getRowChances();
|
159
|
|
160
|
mRotationAngle= new Dynamic1D();
|
161
|
mRotationAxis = new Static3D(1,0,0);
|
162
|
mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
|
163
|
|
164
|
mRotationAngleStatic = new Static1D(0);
|
165
|
mRotationAngleMiddle = new Static1D(0);
|
166
|
mRotationAngleFinal = new Static1D(0);
|
167
|
|
168
|
float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
|
169
|
mObjectScale = new Static3D(scale,scale,scale);
|
170
|
mScaleEffect = new MatrixEffectScale(mObjectScale);
|
171
|
mQuatEffect = new MatrixEffectQuaternion(quat, CENTER);
|
172
|
|
173
|
MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
|
174
|
nodeEffects.apply(nodeScaleEffect);
|
175
|
|
176
|
mNumTexCols = NUM_STICKERS_IN_ROW;
|
177
|
mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
|
178
|
|
179
|
if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
|
180
|
|
181
|
CUBITS = new Cubit[NUM_CUBITS];
|
182
|
createMeshAndCubits(list,res);
|
183
|
|
184
|
mTexture = new DistortedTexture();
|
185
|
mEffects = new DistortedEffects();
|
186
|
|
187
|
int num_quats = QUATS.length;
|
188
|
for(int q=0; q<num_quats; q++)
|
189
|
{
|
190
|
VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
|
191
|
vq.setMeshAssociation(0,q);
|
192
|
mEffects.apply(vq);
|
193
|
}
|
194
|
|
195
|
mEffects.apply(mRotateEffect);
|
196
|
mEffects.apply(mQuatEffect);
|
197
|
mEffects.apply(mScaleEffect);
|
198
|
|
199
|
// Now postprocessed effects (the glow when you solve an object) require component centers. In
|
200
|
// order for the effect to be in front of the object, we need to set the center to be behind it.
|
201
|
getMesh().setComponentCenter(0,0,0,-0.1f);
|
202
|
|
203
|
attach( new DistortedNode(mTexture,mEffects,mMesh) );
|
204
|
|
205
|
setupPosition(moves);
|
206
|
|
207
|
setProjection(fov, 0.1f);
|
208
|
}
|
209
|
|
210
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211
|
|
212
|
private void createMeshAndCubits(ObjectList list, Resources res)
|
213
|
{
|
214
|
if( mCreateFromDMesh )
|
215
|
{
|
216
|
int sizeIndex = ObjectList.getSizeIndex(list.ordinal(),mNumLayers);
|
217
|
int resourceID= list.getResourceIDs()[sizeIndex];
|
218
|
|
219
|
InputStream is = res.openRawResource(resourceID);
|
220
|
DataInputStream dos = new DataInputStream(is);
|
221
|
mMesh = new MeshFile(dos);
|
222
|
|
223
|
try
|
224
|
{
|
225
|
is.close();
|
226
|
}
|
227
|
catch(IOException e)
|
228
|
{
|
229
|
android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
|
230
|
}
|
231
|
|
232
|
for(int i=0; i<NUM_CUBITS; i++)
|
233
|
{
|
234
|
CUBITS[i] = new Cubit(this,mOrigPos[i]);
|
235
|
mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
|
236
|
}
|
237
|
|
238
|
if( shouldResetTextureMaps() ) resetAllTextureMaps();
|
239
|
}
|
240
|
else
|
241
|
{
|
242
|
MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
|
243
|
|
244
|
for(int i=0; i<NUM_CUBITS; i++)
|
245
|
{
|
246
|
CUBITS[i] = new Cubit(this,mOrigPos[i]);
|
247
|
cubitMesh[i] = createCubitMesh(i);
|
248
|
cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
|
249
|
cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
|
250
|
}
|
251
|
|
252
|
mMesh = new MeshJoined(cubitMesh);
|
253
|
resetAllTextureMaps();
|
254
|
}
|
255
|
}
|
256
|
|
257
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
258
|
|
259
|
public void setObjectRatio(float sizeChange)
|
260
|
{
|
261
|
mObjectScreenRatio *= (1.0f+sizeChange)/2;
|
262
|
|
263
|
if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
|
264
|
if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
|
265
|
|
266
|
float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
|
267
|
mObjectScale.set(scale,scale,scale);
|
268
|
}
|
269
|
|
270
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
271
|
|
272
|
static float getObjectRatio()
|
273
|
{
|
274
|
return mObjectScreenRatio*mInitScreenRatio;
|
275
|
}
|
276
|
|
277
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
278
|
|
279
|
int computeRow(float x, float y, float z, int rotIndex)
|
280
|
{
|
281
|
Static3D axis = ROTATION_AXIS[rotIndex];
|
282
|
float tmp = x*axis.get0() + y*axis.get1() + z*axis.get2();
|
283
|
|
284
|
for(int i=0; i<NUM_CUTS; i++)
|
285
|
{
|
286
|
if( tmp<CUTS[i] ) return i;
|
287
|
}
|
288
|
|
289
|
return NUM_CUTS;
|
290
|
}
|
291
|
|
292
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
293
|
|
294
|
private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
|
295
|
{
|
296
|
int cubitRow = (int)(CUBITS[cubit].mRotationRow[axis]+0.5f);
|
297
|
return ((1<<cubitRow)&rowBitmap)!=0;
|
298
|
}
|
299
|
|
300
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
301
|
// note the minus in front of the sin() - we rotate counterclockwise
|
302
|
// when looking towards the direction where the axis increases in values.
|
303
|
|
304
|
private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
|
305
|
{
|
306
|
Static3D axis = ROTATION_AXIS[axisIndex];
|
307
|
|
308
|
while( angleInDegrees<0 ) angleInDegrees += 360;
|
309
|
angleInDegrees %= 360;
|
310
|
|
311
|
float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
|
312
|
float sinA =-(float)Math.sqrt(1-cosA*cosA);
|
313
|
|
314
|
return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
|
315
|
}
|
316
|
|
317
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
318
|
|
319
|
private synchronized void setupPosition(int[][] moves)
|
320
|
{
|
321
|
if( moves!=null )
|
322
|
{
|
323
|
Static4D quat;
|
324
|
int index, axis, rowBitmap, angle;
|
325
|
int corr = (360/getBasicAngle());
|
326
|
|
327
|
for(int[] move: moves)
|
328
|
{
|
329
|
axis = move[0];
|
330
|
rowBitmap= move[1];
|
331
|
angle = move[2]*corr;
|
332
|
quat = makeQuaternion(axis,angle);
|
333
|
|
334
|
for(int j=0; j<NUM_CUBITS; j++)
|
335
|
if( belongsToRotation(j,axis,rowBitmap) )
|
336
|
{
|
337
|
index = CUBITS[j].removeRotationNow(quat);
|
338
|
mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
|
339
|
}
|
340
|
}
|
341
|
}
|
342
|
}
|
343
|
|
344
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
345
|
|
346
|
int getCubitFaceColorIndex(int cubit, int face)
|
347
|
{
|
348
|
Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
|
349
|
|
350
|
int x = (int)(texMap.get0()/texMap.get2());
|
351
|
int y = (int)(texMap.get1()/texMap.get3());
|
352
|
|
353
|
return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
|
354
|
}
|
355
|
|
356
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
357
|
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
|
358
|
|
359
|
void clampPos(Static3D pos)
|
360
|
{
|
361
|
float currError, minError = Float.MAX_VALUE;
|
362
|
int minErrorIndex= -1;
|
363
|
float x = pos.get0();
|
364
|
float y = pos.get1();
|
365
|
float z = pos.get2();
|
366
|
float xo,yo,zo;
|
367
|
|
368
|
for(int i=0; i<NUM_CUBITS; i++)
|
369
|
{
|
370
|
xo = mOrigPos[i].get0();
|
371
|
yo = mOrigPos[i].get1();
|
372
|
zo = mOrigPos[i].get2();
|
373
|
|
374
|
currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
|
375
|
|
376
|
if( currError<minError )
|
377
|
{
|
378
|
minError = currError;
|
379
|
minErrorIndex = i;
|
380
|
}
|
381
|
}
|
382
|
|
383
|
pos.set( mOrigPos[minErrorIndex] );
|
384
|
}
|
385
|
|
386
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
387
|
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
|
388
|
|
389
|
public void createTexture()
|
390
|
{
|
391
|
Bitmap bitmap;
|
392
|
|
393
|
Paint paint = new Paint();
|
394
|
bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
|
395
|
Canvas canvas = new Canvas(bitmap);
|
396
|
|
397
|
paint.setAntiAlias(true);
|
398
|
paint.setTextAlign(Paint.Align.CENTER);
|
399
|
paint.setStyle(Paint.Style.FILL);
|
400
|
|
401
|
paint.setColor(COLOR_BLACK);
|
402
|
canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
|
403
|
|
404
|
int tex = 0;
|
405
|
|
406
|
for(int row=0; row<mNumTexRows; row++)
|
407
|
for(int col=0; col<mNumTexCols; col++)
|
408
|
{
|
409
|
if( tex>=NUM_TEXTURES ) break;
|
410
|
createFaceTexture(canvas, paint, tex, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT);
|
411
|
tex++;
|
412
|
}
|
413
|
|
414
|
if( !mTexture.setTexture(bitmap) )
|
415
|
{
|
416
|
int max = DistortedLibrary.getMaxTextureSize();
|
417
|
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
|
418
|
crashlytics.log("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
|
419
|
}
|
420
|
}
|
421
|
|
422
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
423
|
|
424
|
public int getNumLayers()
|
425
|
{
|
426
|
return mNumLayers;
|
427
|
}
|
428
|
|
429
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
430
|
|
431
|
public void continueRotation(float angleInDegrees)
|
432
|
{
|
433
|
mRotationAngleStatic.set0(angleInDegrees);
|
434
|
}
|
435
|
|
436
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
437
|
|
438
|
public Static4D getRotationQuat()
|
439
|
{
|
440
|
return mQuat;
|
441
|
}
|
442
|
|
443
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
444
|
|
445
|
public void recomputeScaleFactor(int scrWidth)
|
446
|
{
|
447
|
mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
|
448
|
}
|
449
|
|
450
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
451
|
|
452
|
public void savePreferences(SharedPreferences.Editor editor)
|
453
|
{
|
454
|
for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
|
455
|
}
|
456
|
|
457
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
458
|
|
459
|
public synchronized void restorePreferences(SharedPreferences preferences)
|
460
|
{
|
461
|
boolean error = false;
|
462
|
|
463
|
for(int i=0; i<NUM_CUBITS; i++)
|
464
|
{
|
465
|
mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
|
466
|
|
467
|
if( mQuatDebug[i]>=0 && mQuatDebug[i]<QUATS.length)
|
468
|
{
|
469
|
CUBITS[i].modifyCurrentPosition(QUATS[mQuatDebug[i]]);
|
470
|
mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
|
471
|
}
|
472
|
else
|
473
|
{
|
474
|
error = true;
|
475
|
}
|
476
|
}
|
477
|
|
478
|
if( error )
|
479
|
{
|
480
|
for(int i=0; i<NUM_CUBITS; i++)
|
481
|
{
|
482
|
CUBITS[i].solve();
|
483
|
mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
|
484
|
}
|
485
|
recordQuatsState("Failed to restorePreferences");
|
486
|
}
|
487
|
}
|
488
|
|
489
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
490
|
|
491
|
public void recordQuatsState(String message)
|
492
|
{
|
493
|
StringBuilder quats = new StringBuilder();
|
494
|
|
495
|
for(int j=0; j<NUM_CUBITS; j++)
|
496
|
{
|
497
|
quats.append(mQuatDebug[j]);
|
498
|
quats.append(" ");
|
499
|
}
|
500
|
|
501
|
if( BuildConfig.DEBUG )
|
502
|
{
|
503
|
android.util.Log.e("quats" , quats.toString());
|
504
|
android.util.Log.e("object", mList.name()+"_"+mNumLayers);
|
505
|
}
|
506
|
else
|
507
|
{
|
508
|
Exception ex = new Exception(message);
|
509
|
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
|
510
|
crashlytics.setCustomKey("quats" , quats.toString());
|
511
|
crashlytics.setCustomKey("object", mList.name()+"_"+mNumLayers );
|
512
|
crashlytics.recordException(ex);
|
513
|
}
|
514
|
}
|
515
|
|
516
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
517
|
|
518
|
public void releaseResources()
|
519
|
{
|
520
|
mTexture.markForDeletion();
|
521
|
}
|
522
|
|
523
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
524
|
|
525
|
public void apply(Effect effect, int position)
|
526
|
{
|
527
|
mEffects.apply(effect, position);
|
528
|
}
|
529
|
|
530
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
531
|
|
532
|
public void remove(long effectID)
|
533
|
{
|
534
|
mEffects.abortById(effectID);
|
535
|
}
|
536
|
|
537
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
538
|
|
539
|
public synchronized void solve()
|
540
|
{
|
541
|
for(int i=0; i<NUM_CUBITS; i++)
|
542
|
{
|
543
|
CUBITS[i].solve();
|
544
|
mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
|
545
|
}
|
546
|
}
|
547
|
|
548
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
549
|
|
550
|
public void resetAllTextureMaps()
|
551
|
{
|
552
|
final float ratioW = 1.0f/mNumTexCols;
|
553
|
final float ratioH = 1.0f/mNumTexRows;
|
554
|
int color, row, col;
|
555
|
|
556
|
for(int cubit=0; cubit<NUM_CUBITS; cubit++)
|
557
|
{
|
558
|
final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
|
559
|
|
560
|
for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
|
561
|
{
|
562
|
color = getFaceColor(cubit,cubitface,mNumLayers);
|
563
|
row = (mNumTexRows-1) - color/mNumTexCols;
|
564
|
col = color%mNumTexCols;
|
565
|
maps[cubitface] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
|
566
|
}
|
567
|
|
568
|
mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
|
569
|
}
|
570
|
}
|
571
|
|
572
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
573
|
|
574
|
public void setTextureMap(int cubit, int face, int newColor)
|
575
|
{
|
576
|
final float ratioW = 1.0f/mNumTexCols;
|
577
|
final float ratioH = 1.0f/mNumTexRows;
|
578
|
final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
|
579
|
int row = (mNumTexRows-1) - newColor/mNumTexCols;
|
580
|
int col = newColor%mNumTexCols;
|
581
|
|
582
|
maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
|
583
|
mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
|
584
|
}
|
585
|
|
586
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
587
|
|
588
|
public synchronized void beginNewRotation(int axis, int row )
|
589
|
{
|
590
|
if( axis<0 || axis>=ROTATION_AXIS.length )
|
591
|
{
|
592
|
android.util.Log.e("object", "invalid rotation axis: "+axis);
|
593
|
return;
|
594
|
}
|
595
|
if( row<0 || row>=mNumLayers )
|
596
|
{
|
597
|
android.util.Log.e("object", "invalid rotation row: "+row);
|
598
|
return;
|
599
|
}
|
600
|
|
601
|
mRotAxis = axis;
|
602
|
mRotRowBitmap= (1<<row);
|
603
|
mRotationAngleStatic.set0(0.0f);
|
604
|
mRotationAxis.set( ROTATION_AXIS[axis] );
|
605
|
mRotationAngle.add(mRotationAngleStatic);
|
606
|
mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
|
607
|
}
|
608
|
|
609
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
610
|
|
611
|
public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
|
612
|
{
|
613
|
mRotAxis = axis;
|
614
|
mRotRowBitmap= rowBitmap;
|
615
|
|
616
|
mRotationAngleStatic.set0(0.0f);
|
617
|
mRotationAxis.set( ROTATION_AXIS[axis] );
|
618
|
mRotationAngle.setDuration(durationMillis);
|
619
|
mRotationAngle.resetToBeginning();
|
620
|
mRotationAngle.add(new Static1D(0));
|
621
|
mRotationAngle.add(new Static1D(angle));
|
622
|
mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
|
623
|
mRotateEffect.notifyWhenFinished(listener);
|
624
|
|
625
|
return mRotateEffect.getID();
|
626
|
}
|
627
|
|
628
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
629
|
|
630
|
public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
|
631
|
{
|
632
|
float angle = getAngle();
|
633
|
mRotationAngleStatic.set0(angle);
|
634
|
mRotationAngleFinal.set0(nearestAngleInDegrees);
|
635
|
mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
|
636
|
|
637
|
mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
|
638
|
mRotationAngle.resetToBeginning();
|
639
|
mRotationAngle.removeAll();
|
640
|
mRotationAngle.add(mRotationAngleStatic);
|
641
|
mRotationAngle.add(mRotationAngleMiddle);
|
642
|
mRotationAngle.add(mRotationAngleFinal);
|
643
|
mRotateEffect.notifyWhenFinished(listener);
|
644
|
|
645
|
return mRotateEffect.getID();
|
646
|
}
|
647
|
|
648
|
|
649
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
650
|
|
651
|
private float getAngle()
|
652
|
{
|
653
|
int pointNum = mRotationAngle.getNumPoints();
|
654
|
|
655
|
if( pointNum>=1 )
|
656
|
{
|
657
|
return mRotationAngle.getPoint(pointNum-1).get0();
|
658
|
}
|
659
|
else
|
660
|
{
|
661
|
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
|
662
|
crashlytics.log("points in RotationAngle: "+pointNum);
|
663
|
return 0;
|
664
|
}
|
665
|
}
|
666
|
|
667
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
668
|
|
669
|
public synchronized void removeRotationNow()
|
670
|
{
|
671
|
float angle = getAngle();
|
672
|
double nearestAngleInRadians = angle*Math.PI/180;
|
673
|
float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
|
674
|
float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
|
675
|
float axisX = ROTATION_AXIS[mRotAxis].get0();
|
676
|
float axisY = ROTATION_AXIS[mRotAxis].get1();
|
677
|
float axisZ = ROTATION_AXIS[mRotAxis].get2();
|
678
|
Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
|
679
|
|
680
|
mRotationAngle.removeAll();
|
681
|
mRotationAngleStatic.set0(0);
|
682
|
|
683
|
for(int i=0; i<NUM_CUBITS; i++)
|
684
|
if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
|
685
|
{
|
686
|
int index = CUBITS[i].removeRotationNow(quat);
|
687
|
mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
|
688
|
}
|
689
|
}
|
690
|
|
691
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
692
|
|
693
|
public void initializeObject(int[][] moves)
|
694
|
{
|
695
|
solve();
|
696
|
setupPosition(moves);
|
697
|
}
|
698
|
|
699
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
700
|
|
701
|
public int getCubit(float[] point3D)
|
702
|
{
|
703
|
float dist, minDist = Float.MAX_VALUE;
|
704
|
int currentBest=-1;
|
705
|
float multiplier = returnMultiplier();
|
706
|
|
707
|
point3D[0] *= multiplier;
|
708
|
point3D[1] *= multiplier;
|
709
|
point3D[2] *= multiplier;
|
710
|
|
711
|
for(int i=0; i<NUM_CUBITS; i++)
|
712
|
{
|
713
|
dist = CUBITS[i].getDistSquared(point3D);
|
714
|
if( dist<minDist )
|
715
|
{
|
716
|
minDist = dist;
|
717
|
currentBest = i;
|
718
|
}
|
719
|
}
|
720
|
|
721
|
return currentBest;
|
722
|
}
|
723
|
|
724
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
725
|
|
726
|
public int computeNearestAngle(float angle, float speed)
|
727
|
{
|
728
|
final int NEAREST = 360/getBasicAngle();
|
729
|
|
730
|
int tmp = (int)((angle+NEAREST/2)/NEAREST);
|
731
|
if( angle< -(NEAREST*0.5) ) tmp-=1;
|
732
|
|
733
|
if( tmp!=0 ) return NEAREST*tmp;
|
734
|
|
735
|
return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
|
736
|
}
|
737
|
|
738
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
739
|
|
740
|
public int getNodeSize()
|
741
|
{
|
742
|
return mNodeSize;
|
743
|
}
|
744
|
|
745
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
746
|
|
747
|
public ObjectList getObjectList()
|
748
|
{
|
749
|
return mList;
|
750
|
}
|
751
|
|
752
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
753
|
|
754
|
abstract float getScreenRatio();
|
755
|
abstract Static3D[] getCubitPositions(int numLayers);
|
756
|
abstract Static4D[] getQuats();
|
757
|
abstract int getNumFaces();
|
758
|
abstract int getNumStickerTypes();
|
759
|
abstract int getNumCubitFaces();
|
760
|
abstract MeshBase createCubitMesh(int cubit);
|
761
|
abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top);
|
762
|
abstract int getFaceColor(int cubit, int cubitface, int numLayers);
|
763
|
abstract float returnMultiplier();
|
764
|
abstract float[] getRowChances();
|
765
|
abstract float[] getCuts(int numLayers);
|
766
|
abstract boolean shouldResetTextureMaps();
|
767
|
|
768
|
public abstract boolean isSolved();
|
769
|
public abstract Static3D[] getRotationAxis();
|
770
|
public abstract int getBasicAngle();
|
771
|
public abstract String retObjectString();
|
772
|
public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
|
773
|
public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
|
774
|
public abstract int getObjectName(int numLayers);
|
775
|
public abstract int getInventor(int numLayers);
|
776
|
public abstract int getComplexity(int numLayers);
|
777
|
}
|