Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ 28b54fe3

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