Project

General

Profile

Download (25.4 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ 752f6ce9

1 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 1f9772f3 Leszek Koltunski
package org.distorted.objects;
21 fdec60a3 Leszek Koltunski
22 27a70eae Leszek Koltunski
import android.content.SharedPreferences;
23 ccf9fec5 Leszek Koltunski
import android.content.res.Resources;
24 411c6285 Leszek Koltunski
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27 27a70eae Leszek Koltunski
28 27e6c301 Leszek Koltunski
import com.google.firebase.crashlytics.FirebaseCrashlytics;
29
30 27a70eae Leszek Koltunski
import org.distorted.library.effect.Effect;
31 19f0f767 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
32 27a70eae Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34 10585385 Leszek Koltunski
import org.distorted.library.effect.VertexEffectQuaternion;
35 27e6c301 Leszek Koltunski
import org.distorted.library.effect.VertexEffectRotate;
36 27a70eae Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
37 c7e23561 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
38 27a70eae Leszek Koltunski
import org.distorted.library.main.DistortedNode;
39
import org.distorted.library.main.DistortedTexture;
40 b32444ee Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
41 ccf9fec5 Leszek Koltunski
import org.distorted.library.mesh.MeshFile;
42 19f0f767 Leszek Koltunski
import org.distorted.library.mesh.MeshJoined;
43 efa8aa48 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
44 27a70eae Leszek Koltunski
import org.distorted.library.message.EffectListener;
45 27e6c301 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
46 27a70eae Leszek Koltunski
import org.distorted.library.type.Static1D;
47
import org.distorted.library.type.Static3D;
48
import org.distorted.library.type.Static4D;
49 25445dcf Leszek Koltunski
import org.distorted.main.BuildConfig;
50 4f9f99a2 Leszek Koltunski
51 ccf9fec5 Leszek Koltunski
import java.io.DataInputStream;
52
import java.io.IOException;
53
import java.io.InputStream;
54 7c969a6d Leszek Koltunski
import java.util.Random;
55 ccf9fec5 Leszek Koltunski
56 0333d81e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 9c2f0c91 Leszek Koltunski
public abstract class TwistyObject extends DistortedNode
59 fdec60a3 Leszek Koltunski
  {
60 ece1b58d Leszek Koltunski
  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 5581ba2b Leszek Koltunski
  static final int COLOR_PINK   = 0xffff90ff;
67
  static final int COLOR_VIOLET = 0xff7700bb;
68 ee526fe0 Leszek Koltunski
  static final int COLOR_BLACK  = 0xff000000;
69 ece1b58d Leszek Koltunski
70 b89898c5 Leszek Koltunski
  static final int TEXTURE_HEIGHT = 256;
71 ae755eda Leszek Koltunski
  static final int NUM_STICKERS_IN_ROW = 4;
72 b89898c5 Leszek Koltunski
73 3f3ff476 Leszek Koltunski
  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 ee526fe0 Leszek Koltunski
  private static final float NODE_RATIO = 1.40f;
78
  private static final float MAX_SIZE_CHANGE = 1.35f;
79 c7b00dfb Leszek Koltunski
  private static final float MIN_SIZE_CHANGE = 0.8f;
80
81 d73de3db Leszek Koltunski
  private static boolean mCreateFromDMesh = true;
82 19f0f767 Leszek Koltunski
83 8cccfb10 Leszek Koltunski
  private static final Static3D CENTER = new Static3D(0,0,0);
84 27e6c301 Leszek Koltunski
  private static final int POST_ROTATION_MILLISEC = 500;
85
86 efef689c Leszek Koltunski
  final Static3D[] ROTATION_AXIS;
87 98904e45 Leszek Koltunski
  final Static4D[] QUATS;
88 6b6504fe Leszek Koltunski
  final Cubit[] CUBITS;
89 470820a7 Leszek Koltunski
  final int NUM_FACES;
90 eab9d8f8 Leszek Koltunski
  final int NUM_TEXTURES;
91 8f53e513 Leszek Koltunski
  final int NUM_CUBIT_FACES;
92 1ebc4767 Leszek Koltunski
  final int NUM_AXIS;
93 6b6504fe Leszek Koltunski
  final int NUM_CUBITS;
94 a97e02b7 Leszek Koltunski
  final float[] CUTS;
95
  final int NUM_CUTS;
96 27a70eae Leszek Koltunski
97 b30695c6 Leszek Koltunski
  private static float mInitScreenRatio;
98
  private static float mObjectScreenRatio = 1.0f;
99 f0fa83ae Leszek Koltunski
100 5b893eee Leszek Koltunski
  private final int mNodeSize;
101 9224ffd2 Leszek Koltunski
  private int mRotRowBitmap;
102 efef689c Leszek Koltunski
  private int mRotAxis;
103 49f67f9b Leszek Koltunski
  private Static3D[] mOrigPos;
104 fb6a40c8 Leszek Koltunski
  private Static3D mNodeScale;
105 4da7d87a Leszek Koltunski
  private Static4D mQuat;
106 d99f3a48 Leszek Koltunski
  private int mNumLayers, mRealSize;
107 9c2f0c91 Leszek Koltunski
  private ObjectList mList;
108 470820a7 Leszek Koltunski
  private MeshBase mMesh;
109
  private DistortedEffects mEffects;
110 27e6c301 Leszek Koltunski
  private VertexEffectRotate mRotateEffect;
111
  private Dynamic1D mRotationAngle;
112
  private Static3D mRotationAxis;
113 c7b00dfb Leszek Koltunski
  private Static3D mObjectScale;
114 a15078bb Leszek Koltunski
  private int[] mQuatDebug;
115 ae755eda Leszek Koltunski
  private int mNumTexRows, mNumTexCols;
116 27a70eae Leszek Koltunski
117 7c969a6d Leszek Koltunski
  float[] mRowChances;
118 27a70eae Leszek Koltunski
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
119
  DistortedTexture mTexture;
120
  MatrixEffectScale mScaleEffect;
121 4da7d87a Leszek Koltunski
  MatrixEffectQuaternion mQuatEffect;
122 27a70eae Leszek Koltunski
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124 fdec60a3 Leszek Koltunski
125 d99f3a48 Leszek Koltunski
  TwistyObject(int numLayers, int realSize, int fov, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
126 9c2f0c91 Leszek Koltunski
               DistortedEffects nodeEffects, int[][] moves, ObjectList list, Resources res, int screenWidth)
127 fdec60a3 Leszek Koltunski
    {
128 411c6285 Leszek Koltunski
    super(nodeTexture,nodeEffects,nodeMesh);
129 fdec60a3 Leszek Koltunski
130 5b893eee Leszek Koltunski
    mNodeSize = screenWidth;
131
132 c7b00dfb Leszek Koltunski
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
133 d41742f7 Leszek Koltunski
134 d99f3a48 Leszek Koltunski
    mNumLayers = numLayers;
135
    mRealSize = realSize;
136 aa171dee Leszek Koltunski
    mList = list;
137 d99f3a48 Leszek Koltunski
    mOrigPos = getCubitPositions(mNumLayers);
138 10a2e360 Leszek Koltunski
139 98904e45 Leszek Koltunski
    QUATS = getQuats();
140 49f67f9b Leszek Koltunski
    NUM_CUBITS  = mOrigPos.length;
141 efef689c Leszek Koltunski
    ROTATION_AXIS = getRotationAxis();
142 1ebc4767 Leszek Koltunski
    NUM_AXIS = ROTATION_AXIS.length;
143 b30695c6 Leszek Koltunski
    mInitScreenRatio = getScreenRatio();
144 470820a7 Leszek Koltunski
    NUM_FACES = getNumFaces();
145 8f53e513 Leszek Koltunski
    NUM_CUBIT_FACES = getNumCubitFaces();
146 eab9d8f8 Leszek Koltunski
    NUM_TEXTURES = getNumStickerTypes()*NUM_FACES;
147 d99f3a48 Leszek Koltunski
    CUTS = getCuts(mNumLayers);
148 a97e02b7 Leszek Koltunski
    NUM_CUTS = CUTS.length;
149 a10ada2a Leszek Koltunski
150 a15078bb Leszek Koltunski
    mQuatDebug = new int[NUM_CUBITS];
151
152 b30695c6 Leszek Koltunski
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
153
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
154
155 c7b00dfb Leszek Koltunski
    mNodeScale= new Static3D(1,NODE_RATIO,1);
156 4da7d87a Leszek Koltunski
    mQuat = quat;
157 e844c116 Leszek Koltunski
158 7c969a6d Leszek Koltunski
    mRowChances = getRowChances();
159
160 27e6c301 Leszek Koltunski
    mRotationAngle= new Dynamic1D();
161
    mRotationAxis = new Static3D(1,0,0);
162 8cccfb10 Leszek Koltunski
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
163 27e6c301 Leszek Koltunski
164 27a70eae Leszek Koltunski
    mRotationAngleStatic = new Static1D(0);
165
    mRotationAngleMiddle = new Static1D(0);
166
    mRotationAngleFinal  = new Static1D(0);
167
168 d99f3a48 Leszek Koltunski
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
169 19f0f767 Leszek Koltunski
    mObjectScale = new Static3D(scale,scale,scale);
170 c7b00dfb Leszek Koltunski
    mScaleEffect = new MatrixEffectScale(mObjectScale);
171 4da7d87a Leszek Koltunski
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
172 27a70eae Leszek Koltunski
173
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
174 411c6285 Leszek Koltunski
    nodeEffects.apply(nodeScaleEffect);
175 a10ada2a Leszek Koltunski
176 ae755eda Leszek Koltunski
    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 6b6504fe Leszek Koltunski
    CUBITS = new Cubit[NUM_CUBITS];
182 19f0f767 Leszek Koltunski
    createMeshAndCubits(list,res);
183 7381193e Leszek Koltunski
184 19f0f767 Leszek Koltunski
    mTexture = new DistortedTexture();
185 470820a7 Leszek Koltunski
    mEffects = new DistortedEffects();
186 10585385 Leszek Koltunski
187 98904e45 Leszek Koltunski
    int num_quats = QUATS.length;
188 10585385 Leszek Koltunski
    for(int q=0; q<num_quats; q++)
189
      {
190 98904e45 Leszek Koltunski
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
191 10585385 Leszek Koltunski
      vq.setMeshAssociation(0,q);
192
      mEffects.apply(vq);
193
      }
194
195 27e6c301 Leszek Koltunski
    mEffects.apply(mRotateEffect);
196 4da7d87a Leszek Koltunski
    mEffects.apply(mQuatEffect);
197 470820a7 Leszek Koltunski
    mEffects.apply(mScaleEffect);
198
199 dfbb340a Leszek Koltunski
    // Now postprocessed effects (the glow when you solve an object) require component centers. In
200 b376bfd7 Leszek Koltunski
    // order for the effect to be in front of the object, we need to set the center to be behind it.
201 dfbb340a Leszek Koltunski
    getMesh().setComponentCenter(0,0,0,-0.1f);
202
203 470820a7 Leszek Koltunski
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
204
205 aa171dee Leszek Koltunski
    setupPosition(moves);
206
207 4888e97c Leszek Koltunski
    setProjection(fov, 0.1f);
208 27a70eae Leszek Koltunski
    }
209
210 19f0f767 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212 9c2f0c91 Leszek Koltunski
  private void createMeshAndCubits(ObjectList list, Resources res)
213 19f0f767 Leszek Koltunski
    {
214
    if( mCreateFromDMesh )
215
      {
216 d99f3a48 Leszek Koltunski
      int sizeIndex = ObjectList.getSizeIndex(list.ordinal(),mNumLayers);
217 19f0f767 Leszek Koltunski
      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 6b6504fe Leszek Koltunski
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
235
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
236 19f0f767 Leszek Koltunski
        }
237 eaee1ddc Leszek Koltunski
238
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
239 19f0f767 Leszek Koltunski
      }
240
    else
241
      {
242
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
243
244
      for(int i=0; i<NUM_CUBITS; i++)
245
        {
246 6b6504fe Leszek Koltunski
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
247 19f0f767 Leszek Koltunski
        cubitMesh[i] = createCubitMesh(i);
248
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
249 6b6504fe Leszek Koltunski
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
250 19f0f767 Leszek Koltunski
        }
251
252
      mMesh = new MeshJoined(cubitMesh);
253
      resetAllTextureMaps();
254
      }
255
    }
256
257 c7b00dfb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
258
259
  public void setObjectRatio(float sizeChange)
260
    {
261
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
262
263 b30695c6 Leszek Koltunski
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
264
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
265 c7b00dfb Leszek Koltunski
266 d99f3a48 Leszek Koltunski
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
267 c7b00dfb Leszek Koltunski
    mObjectScale.set(scale,scale,scale);
268
    }
269
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
272
  static float getObjectRatio()
273
    {
274 b30695c6 Leszek Koltunski
    return mObjectScreenRatio*mInitScreenRatio;
275 c7b00dfb Leszek Koltunski
    }
276
277 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
278
279 a97e02b7 Leszek Koltunski
  int computeRow(float x, float y, float z, int rotIndex)
280 e844c116 Leszek Koltunski
    {
281 a97e02b7 Leszek Koltunski
    Static3D axis = ROTATION_AXIS[rotIndex];
282
    float tmp = x*axis.get0() + y*axis.get1() + z*axis.get2();
283 e844c116 Leszek Koltunski
284 a97e02b7 Leszek Koltunski
    for(int i=0; i<NUM_CUTS; i++)
285 e844c116 Leszek Koltunski
      {
286 a97e02b7 Leszek Koltunski
      if( tmp<CUTS[i] ) return i;
287 e844c116 Leszek Koltunski
      }
288
289 a97e02b7 Leszek Koltunski
    return NUM_CUTS;
290 e844c116 Leszek Koltunski
    }
291
292 efef689c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
293
294 9224ffd2 Leszek Koltunski
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
295 efef689c Leszek Koltunski
    {
296 6b6504fe Leszek Koltunski
    int cubitRow = (int)(CUBITS[cubit].mRotationRow[axis]+0.5f);
297 9224ffd2 Leszek Koltunski
    return ((1<<cubitRow)&rowBitmap)!=0;
298 66cbdd21 Leszek Koltunski
    }
299
300 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
301 a31d25de Leszek Koltunski
// note the minus in front of the sin() - we rotate counterclockwise
302
// when looking towards the direction where the axis increases in values.
303 aa171dee Leszek Koltunski
304 a31d25de Leszek Koltunski
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
305 aa171dee Leszek Koltunski
    {
306 a31d25de Leszek Koltunski
    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 8bbac3c2 Leszek Koltunski
  private synchronized void setupPosition(int[][] moves)
320 a31d25de Leszek Koltunski
    {
321
    if( moves!=null )
322
      {
323
      Static4D quat;
324 818431ed Leszek Koltunski
      int index, axis, rowBitmap, angle;
325 a31d25de Leszek Koltunski
      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 6b6504fe Leszek Koltunski
            index = CUBITS[j].removeRotationNow(quat);
338
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
339 a31d25de Leszek Koltunski
            }
340
        }
341
      }
342 aa171dee Leszek Koltunski
    }
343
344 fa0f7a56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
345
346
  int getCubitFaceColorIndex(int cubit, int face)
347
    {
348 470820a7 Leszek Koltunski
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
349 064ccc31 Leszek Koltunski
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 fa0f7a56 Leszek Koltunski
    }
355
356 49f67f9b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 411c6285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
387 ae755eda Leszek Koltunski
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
388 411c6285 Leszek Koltunski
389
  public void createTexture()
390
    {
391
    Bitmap bitmap;
392
393
    Paint paint = new Paint();
394 ae755eda Leszek Koltunski
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
395 411c6285 Leszek Koltunski
    Canvas canvas = new Canvas(bitmap);
396
397
    paint.setAntiAlias(true);
398
    paint.setTextAlign(Paint.Align.CENTER);
399
    paint.setStyle(Paint.Style.FILL);
400
401 ee526fe0 Leszek Koltunski
    paint.setColor(COLOR_BLACK);
402 ae755eda Leszek Koltunski
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
403 411c6285 Leszek Koltunski
404 ae755eda Leszek Koltunski
    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 411c6285 Leszek Koltunski
414 c7e23561 Leszek Koltunski
    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 411c6285 Leszek Koltunski
    }
421
422 dd73fdab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
423
424 d99f3a48 Leszek Koltunski
  public int getNumLayers()
425 fdec60a3 Leszek Koltunski
    {
426 d99f3a48 Leszek Koltunski
    return mNumLayers;
427 fdec60a3 Leszek Koltunski
    }
428
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430
431 27a70eae Leszek Koltunski
  public void continueRotation(float angleInDegrees)
432 fdec60a3 Leszek Koltunski
    {
433 27a70eae Leszek Koltunski
    mRotationAngleStatic.set0(angleInDegrees);
434 fdec60a3 Leszek Koltunski
    }
435
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437
438 27a70eae Leszek Koltunski
  public Static4D getRotationQuat()
439
      {
440 4da7d87a Leszek Koltunski
      return mQuat;
441 27a70eae Leszek Koltunski
      }
442
443
///////////////////////////////////////////////////////////////////////////////////////////////////
444
445 f18e8fae Leszek Koltunski
  public void recomputeScaleFactor(int scrWidth)
446 fdec60a3 Leszek Koltunski
    {
447 3717a94e Leszek Koltunski
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
448 fdec60a3 Leszek Koltunski
    }
449 27a70eae Leszek Koltunski
450
///////////////////////////////////////////////////////////////////////////////////////////////////
451
452 a10ada2a Leszek Koltunski
  public void savePreferences(SharedPreferences.Editor editor)
453
    {
454 6b6504fe Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
455 a10ada2a Leszek Koltunski
    }
456 f16ff19d Leszek Koltunski
457 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
458 27a70eae Leszek Koltunski
459 8bbac3c2 Leszek Koltunski
  public synchronized void restorePreferences(SharedPreferences preferences)
460 a10ada2a Leszek Koltunski
    {
461 fc3c5170 Leszek Koltunski
    boolean error = false;
462
463 2fcad75d Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++)
464
      {
465 a15078bb Leszek Koltunski
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
466 1d6c1eea Leszek Koltunski
467 fc3c5170 Leszek Koltunski
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<QUATS.length)
468 1d6c1eea Leszek Koltunski
        {
469 fc3c5170 Leszek Koltunski
        CUBITS[i].modifyCurrentPosition(QUATS[mQuatDebug[i]]);
470
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
471
        }
472
      else
473
        {
474
        error = true;
475 1d6c1eea Leszek Koltunski
        }
476 fc3c5170 Leszek Koltunski
      }
477 1d6c1eea Leszek Koltunski
478 fc3c5170 Leszek Koltunski
    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 a15078bb Leszek Koltunski
      }
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 2fcad75d Leszek Koltunski
      }
500 a15078bb Leszek Koltunski
501 25445dcf Leszek Koltunski
    if( BuildConfig.DEBUG )
502
      {
503 2d9d9d62 Leszek Koltunski
      android.util.Log.e("quats" , quats.toString());
504 25445dcf Leszek Koltunski
      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 a10ada2a Leszek Koltunski
    }
515 27a70eae Leszek Koltunski
516 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
517
518
  public void releaseResources()
519
    {
520
    mTexture.markForDeletion();
521
    }
522
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524
525
  public void apply(Effect effect, int position)
526
    {
527 8cccfb10 Leszek Koltunski
    mEffects.apply(effect, position);
528 a10ada2a Leszek Koltunski
    }
529
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531
532
  public void remove(long effectID)
533
    {
534 8cccfb10 Leszek Koltunski
    mEffects.abortById(effectID);
535 a10ada2a Leszek Koltunski
    }
536 74686c71 Leszek Koltunski
537 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
538
539 8bbac3c2 Leszek Koltunski
  public synchronized void solve()
540 a10ada2a Leszek Koltunski
    {
541 98904e45 Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++)
542
      {
543 6b6504fe Leszek Koltunski
      CUBITS[i].solve();
544
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
545 a10ada2a Leszek Koltunski
      }
546
    }
547
548 1f9772f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
549
550
  public void resetAllTextureMaps()
551
    {
552 ae755eda Leszek Koltunski
    final float ratioW = 1.0f/mNumTexCols;
553
    final float ratioH = 1.0f/mNumTexRows;
554
    int color, row, col;
555 380162cb Leszek Koltunski
556 ad73edd5 Leszek Koltunski
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
557 1f9772f3 Leszek Koltunski
      {
558 8f53e513 Leszek Koltunski
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
559 ad73edd5 Leszek Koltunski
560 f6d06256 Leszek Koltunski
      for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
561 ad73edd5 Leszek Koltunski
        {
562 d99f3a48 Leszek Koltunski
        color = getFaceColor(cubit,cubitface,mNumLayers);
563 ae755eda Leszek Koltunski
        row = (mNumTexRows-1) - color/mNumTexCols;
564
        col = color%mNumTexCols;
565
        maps[cubitface] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
566 ad73edd5 Leszek Koltunski
        }
567
568 8f53e513 Leszek Koltunski
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
569 1f9772f3 Leszek Koltunski
      }
570
    }
571
572
///////////////////////////////////////////////////////////////////////////////////////////////////
573
574
  public void setTextureMap(int cubit, int face, int newColor)
575
    {
576 064ccc31 Leszek Koltunski
    final float ratioW = 1.0f/mNumTexCols;
577
    final float ratioH = 1.0f/mNumTexRows;
578 8f53e513 Leszek Koltunski
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
579 064ccc31 Leszek Koltunski
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
580
    int col = newColor%mNumTexCols;
581 1f9772f3 Leszek Koltunski
582 064ccc31 Leszek Koltunski
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
583 8f53e513 Leszek Koltunski
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
584 1f9772f3 Leszek Koltunski
    }
585
586 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
587
588 8bbac3c2 Leszek Koltunski
  public synchronized void beginNewRotation(int axis, int row )
589 a10ada2a Leszek Koltunski
    {
590 9cd7695f Leszek Koltunski
    if( axis<0 || axis>=ROTATION_AXIS.length )
591
      {
592
      android.util.Log.e("object", "invalid rotation axis: "+axis);
593
      return;
594
      }
595 d99f3a48 Leszek Koltunski
    if( row<0 || row>=mNumLayers )
596 9cd7695f Leszek Koltunski
      {
597
      android.util.Log.e("object", "invalid rotation row: "+row);
598
      return;
599
      }
600
601 27e6c301 Leszek Koltunski
    mRotAxis     = axis;
602
    mRotRowBitmap= (1<<row);
603 a10ada2a Leszek Koltunski
    mRotationAngleStatic.set0(0.0f);
604 27e6c301 Leszek Koltunski
    mRotationAxis.set( ROTATION_AXIS[axis] );
605
    mRotationAngle.add(mRotationAngleStatic);
606 9c2f0c91 Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
607 27e6c301 Leszek Koltunski
    }
608 a10ada2a Leszek Koltunski
609
///////////////////////////////////////////////////////////////////////////////////////////////////
610
611 8bbac3c2 Leszek Koltunski
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
612 27e6c301 Leszek Koltunski
    {
613
    mRotAxis     = axis;
614
    mRotRowBitmap= rowBitmap;
615 a10ada2a Leszek Koltunski
616 27e6c301 Leszek Koltunski
    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 9c2f0c91 Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
623 27e6c301 Leszek Koltunski
    mRotateEffect.notifyWhenFinished(listener);
624
625
    return mRotateEffect.getID();
626
    }
627 a10ada2a Leszek Koltunski
628 27e6c301 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
629 a10ada2a Leszek Koltunski
630 168b6b56 Leszek Koltunski
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
631 27e6c301 Leszek Koltunski
    {
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 001cc0e4 Leszek Koltunski
648 a10ada2a Leszek Koltunski
649 001cc0e4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
650
651 27e6c301 Leszek Koltunski
  private float getAngle()
652 001cc0e4 Leszek Koltunski
    {
653 27e6c301 Leszek Koltunski
    int pointNum = mRotationAngle.getNumPoints();
654 001cc0e4 Leszek Koltunski
655 27e6c301 Leszek Koltunski
    if( pointNum>=1 )
656 001cc0e4 Leszek Koltunski
      {
657 27e6c301 Leszek Koltunski
      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 001cc0e4 Leszek Koltunski
      }
665
    }
666
667 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
668
669 8bbac3c2 Leszek Koltunski
  public synchronized void removeRotationNow()
670 168b6b56 Leszek Koltunski
    {
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 6b6504fe Leszek Koltunski
        int index = CUBITS[i].removeRotationNow(quat);
687
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
688 168b6b56 Leszek Koltunski
        }
689
    }
690 a10ada2a Leszek Koltunski
691 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
692
693 a31d25de Leszek Koltunski
  public void initializeObject(int[][] moves)
694 aa171dee Leszek Koltunski
    {
695
    solve();
696
    setupPosition(moves);
697
    }
698
699 9621255f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
700
701
  public int getCubit(float[] point3D)
702
    {
703 418aa554 Leszek Koltunski
    float dist, minDist = Float.MAX_VALUE;
704 9621255f Leszek Koltunski
    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 6b6504fe Leszek Koltunski
      dist = CUBITS[i].getDistSquared(point3D);
714 9621255f Leszek Koltunski
      if( dist<minDist )
715
        {
716
        minDist = dist;
717
        currentBest = i;
718
        }
719
      }
720
721
    return currentBest;
722
    }
723
724 0e5ad27c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
725
726 168b6b56 Leszek Koltunski
  public int computeNearestAngle(float angle, float speed)
727 0e5ad27c Leszek Koltunski
    {
728
    final int NEAREST = 360/getBasicAngle();
729
730 4c864c68 Leszek Koltunski
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
731
    if( angle< -(NEAREST*0.5) ) tmp-=1;
732 168b6b56 Leszek Koltunski
733 4c864c68 Leszek Koltunski
    if( tmp!=0 ) return NEAREST*tmp;
734 168b6b56 Leszek Koltunski
735 c7b00dfb Leszek Koltunski
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
736 0e5ad27c Leszek Koltunski
    }
737
738 5b893eee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
739
740
  public int getNodeSize()
741
    {
742
    return mNodeSize;
743
    }
744
745 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
746
747 9c2f0c91 Leszek Koltunski
  public ObjectList getObjectList()
748 aa171dee Leszek Koltunski
    {
749
    return mList;
750
    }
751
752 10a2e360 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
753
754 f0fa83ae Leszek Koltunski
  abstract float getScreenRatio();
755 ae755eda Leszek Koltunski
  abstract Static3D[] getCubitPositions(int numLayers);
756 10585385 Leszek Koltunski
  abstract Static4D[] getQuats();
757 411c6285 Leszek Koltunski
  abstract int getNumFaces();
758 eab9d8f8 Leszek Koltunski
  abstract int getNumStickerTypes();
759 8f53e513 Leszek Koltunski
  abstract int getNumCubitFaces();
760 14bd7976 Leszek Koltunski
  abstract MeshBase createCubitMesh(int cubit);
761 ae755eda Leszek Koltunski
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top);
762
  abstract int getFaceColor(int cubit, int cubitface, int numLayers);
763 fb377dae Leszek Koltunski
  abstract float returnMultiplier();
764 7c969a6d Leszek Koltunski
  abstract float[] getRowChances();
765 ae755eda Leszek Koltunski
  abstract float[] getCuts(int numLayers);
766 eaee1ddc Leszek Koltunski
  abstract boolean shouldResetTextureMaps();
767 7c969a6d Leszek Koltunski
768 6b6504fe Leszek Koltunski
  public abstract boolean isSolved();
769 12ad3fca Leszek Koltunski
  public abstract Static3D[] getRotationAxis();
770 e844c116 Leszek Koltunski
  public abstract int getBasicAngle();
771 20931cf6 Leszek Koltunski
  public abstract String retObjectString();
772 7c969a6d Leszek Koltunski
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
773
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
774 6fd4a72c Leszek Koltunski
  public abstract int getObjectName(int numLayers);
775
  public abstract int getInventor(int numLayers);
776
  public abstract int getComplexity(int numLayers);
777 fdec60a3 Leszek Koltunski
  }