Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ 36b9ee93

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