Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ 4c0a6d97

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objects;
21

    
22
import android.content.SharedPreferences;
23
import android.content.res.Resources;
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27

    
28
import com.google.firebase.crashlytics.FirebaseCrashlytics;
29

    
30
import org.distorted.library.effect.Effect;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectQuaternion;
35
import org.distorted.library.effect.VertexEffectRotate;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.main.DistortedNode;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.mesh.MeshBase;
41
import org.distorted.library.mesh.MeshFile;
42
import org.distorted.library.mesh.MeshJoined;
43
import org.distorted.library.mesh.MeshSquare;
44
import org.distorted.library.message.EffectListener;
45
import org.distorted.library.type.Dynamic1D;
46
import org.distorted.library.type.Static1D;
47
import org.distorted.library.type.Static3D;
48
import org.distorted.library.type.Static4D;
49
import org.distorted.main.BuildConfig;
50

    
51
import java.io.DataInputStream;
52
import java.io.IOException;
53
import java.io.InputStream;
54
import java.util.Random;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
public abstract class TwistyObject extends DistortedNode
59
  {
60
  static final int COLOR_YELLOW = 0xffffff00;
61
  static final int COLOR_WHITE  = 0xffffffff;
62
  static final int COLOR_BLUE   = 0xff0000ff;
63
  static final int COLOR_GREEN  = 0xff00bb00;
64
  static final int COLOR_RED    = 0xff990000;
65
  static final int COLOR_ORANGE = 0xffff6200;
66
  static final int COLOR_GREY   = 0xff727c7b;
67
  static final int COLOR_VIOLET = 0xff7700bb;
68
  static final int COLOR_BLACK  = 0xff000000;
69

    
70
  static final int TEXTURE_HEIGHT = 256;
71
  static final int NUM_STICKERS_IN_ROW = 4;
72

    
73
  static final float SQ2 = (float)Math.sqrt(2);
74
  static final float SQ3 = (float)Math.sqrt(3);
75
  static final float SQ5 = (float)Math.sqrt(5);
76
  static final float SQ6 = (float)Math.sqrt(6);
77

    
78
  private static final float NODE_RATIO = 1.40f;
79
  private static final float MAX_SIZE_CHANGE = 1.35f;
80
  private static final float MIN_SIZE_CHANGE = 0.75f;
81

    
82
  private static final boolean mCreateFromDMesh = false;
83

    
84
  private static final Static3D CENTER = new Static3D(0,0,0);
85
  private static final int POST_ROTATION_MILLISEC = 500;
86

    
87
  final Static3D[] ROTATION_AXIS;
88
  final Static4D[] QUATS;
89
  final Cubit[] CUBITS;
90
  final int NUM_FACES;
91
  final int NUM_TEXTURES;
92
  final int NUM_CUBIT_FACES;
93
  final int NUM_AXIS;
94
  final int NUM_CUBITS;
95
  final float[] CUTS;
96
  final int NUM_CUTS;
97

    
98
  private static float mInitScreenRatio;
99
  private static float mObjectScreenRatio = 1.0f;
100

    
101
  private final int mNodeSize;
102
  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 final float mCameraDist;
114
  private int mNumTexRows, mNumTexCols;
115
  private int mRotRowBitmap;
116
  private int mRotAxis;
117
  private MeshBase mMesh;
118

    
119
  float[] mRowChances;
120
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
121
  DistortedTexture mTexture;
122
  MatrixEffectScale mScaleEffect;
123
  MatrixEffectQuaternion mQuatEffect;
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  TwistyObject(int numLayers, int realSize, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
128
               DistortedEffects nodeEffects, int[][] moves, ObjectList list, Resources res, int screenWidth)
129
    {
130
    super(nodeTexture,nodeEffects,nodeMesh);
131

    
132
    mNodeSize = screenWidth;
133

    
134
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
135

    
136
    mNumLayers = numLayers;
137
    mRealSize = realSize;
138
    mList = list;
139
    mOrigPos = getCubitPositions(mNumLayers);
140

    
141
    QUATS = getQuats();
142
    NUM_CUBITS  = mOrigPos.length;
143
    ROTATION_AXIS = getRotationAxis();
144
    NUM_AXIS = ROTATION_AXIS.length;
145
    mInitScreenRatio = getScreenRatio();
146
    NUM_FACES = getNumFaces();
147
    NUM_CUBIT_FACES = getNumCubitFaces();
148
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACES;
149
    CUTS = getCuts(mNumLayers);
150
    NUM_CUTS = CUTS.length;
151

    
152
    mQuatDebug = new int[NUM_CUBITS];
153

    
154
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
155
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
156

    
157
    mNodeScale= new Static3D(1,NODE_RATIO,1);
158
    mQuat = quat;
159

    
160
    mRowChances = getRowChances(mNumLayers);
161

    
162
    mRotationAngle= new Dynamic1D();
163
    mRotationAxis = new Static3D(1,0,0);
164
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
165

    
166
    mRotationAngleStatic = new Static1D(0);
167
    mRotationAngleMiddle = new Static1D(0);
168
    mRotationAngleFinal  = new Static1D(0);
169

    
170
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
171
    mObjectScale = new Static3D(scale,scale,scale);
172
    mScaleEffect = new MatrixEffectScale(mObjectScale);
173
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
174

    
175
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
176
    nodeEffects.apply(nodeScaleEffect);
177

    
178
    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
    CUBITS = new Cubit[NUM_CUBITS];
184
    createMeshAndCubits(list,res);
185

    
186
    mTexture = new DistortedTexture();
187
    mEffects = new DistortedEffects();
188

    
189
    int num_quats = QUATS.length;
190
    for(int q=0; q<num_quats; q++)
191
      {
192
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
193
      vq.setMeshAssociation(0,q);
194
      mEffects.apply(vq);
195
      }
196

    
197
    mEffects.apply(mRotateEffect);
198
    mEffects.apply(mQuatEffect);
199
    mEffects.apply(mScaleEffect);
200

    
201
    // Now postprocessed effects (the glow when you solve an object) require component centers. In
202
    // order for the effect to be in front of the object, we need to set the center to be behind it.
203
    getMesh().setComponentCenter(0,0,0,-0.1f);
204

    
205
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
206

    
207
    setupPosition(moves);
208

    
209
    float fov = list.getFOV();
210
    double halfFOV = fov * (Math.PI/360);
211
    mCameraDist = 0.5f*NODE_RATIO / (float)Math.tan(halfFOV);
212

    
213
    setProjection( fov, 0.1f);
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  private void createMeshAndCubits(ObjectList list, Resources res)
219
    {
220
    if( mCreateFromDMesh )
221
      {
222
      int sizeIndex = ObjectList.getSizeIndex(list.ordinal(),mNumLayers);
223
      int resourceID= list.getResourceIDs()[sizeIndex];
224

    
225
      InputStream is = res.openRawResource(resourceID);
226
      DataInputStream dos = new DataInputStream(is);
227
      mMesh = new MeshFile(dos);
228

    
229
      try
230
        {
231
        is.close();
232
        }
233
      catch(IOException e)
234
        {
235
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
236
        }
237

    
238
      for(int i=0; i<NUM_CUBITS; i++)
239
        {
240
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
241
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
242
        }
243

    
244
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
245
      }
246
    else
247
      {
248
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
249

    
250
      for(int i=0; i<NUM_CUBITS; i++)
251
        {
252
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
253
        cubitMesh[i] = createCubitMesh(i,mNumLayers);
254
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
255
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
256
        }
257

    
258
      mMesh = new MeshJoined(cubitMesh);
259
      resetAllTextureMaps();
260
      }
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  public void setObjectRatio(float sizeChange)
266
    {
267
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
268

    
269
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
270
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
271

    
272
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
273
    mObjectScale.set(scale,scale,scale);
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
  static float getObjectRatio()
279
    {
280
    return mObjectScreenRatio*mInitScreenRatio;
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
  int computeRow(float x, float y, float z, int rotIndex)
286
    {
287
    Static3D axis = ROTATION_AXIS[rotIndex];
288
    float tmp = x*axis.get0() + y*axis.get1() + z*axis.get2();
289

    
290
    for(int i=0; i<NUM_CUTS; i++)
291
      {
292
      if( tmp<CUTS[i] ) return i;
293
      }
294

    
295
    return NUM_CUTS;
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  private boolean wasRotateApplied()
301
    {
302
    return mEffects.exists(mRotateEffect.getID());
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
308
    {
309
    int cubitRow = (int)(CUBITS[cubit].mRotationRow[axis]+0.5f);
310
    return ((1<<cubitRow)&rowBitmap)!=0;
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314
// note the minus in front of the sin() - we rotate counterclockwise
315
// when looking towards the direction where the axis increases in values.
316

    
317
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
318
    {
319
    Static3D axis = ROTATION_AXIS[axisIndex];
320

    
321
    while( angleInDegrees<0 ) angleInDegrees += 360;
322
    angleInDegrees %= 360;
323
    
324
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
325
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
326

    
327
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
  private synchronized void setupPosition(int[][] moves)
333
    {
334
    if( moves!=null )
335
      {
336
      Static4D quat;
337
      int index, axis, rowBitmap, angle;
338
      int corr = (360/getBasicAngle());
339

    
340
      for(int[] move: moves)
341
        {
342
        axis     = move[0];
343
        rowBitmap= move[1];
344
        angle    = move[2]*corr;
345
        quat     = makeQuaternion(axis,angle);
346

    
347
        for(int j=0; j<NUM_CUBITS; j++)
348
          if( belongsToRotation(j,axis,rowBitmap) )
349
            {
350
            index = CUBITS[j].removeRotationNow(quat);
351
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
352
            }
353
        }
354
      }
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
  int getCubitFaceColorIndex(int cubit, int face)
360
    {
361
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
362

    
363
    int x = (int)(texMap.get0()/texMap.get2());
364
    int y = (int)(texMap.get1()/texMap.get3());
365

    
366
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
371

    
372
  void clampPos(Static3D pos)
373
    {
374
    float currError, minError = Float.MAX_VALUE;
375
    int minErrorIndex= -1;
376
    float x = pos.get0();
377
    float y = pos.get1();
378
    float z = pos.get2();
379
    float xo,yo,zo;
380

    
381
    for(int i=0; i<NUM_CUBITS; i++)
382
      {
383
      xo = mOrigPos[i].get0();
384
      yo = mOrigPos[i].get1();
385
      zo = mOrigPos[i].get2();
386

    
387
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
388

    
389
      if( currError<minError )
390
        {
391
        minError = currError;
392
        minErrorIndex = i;
393
        }
394
      }
395

    
396
    pos.set( mOrigPos[minErrorIndex] );
397
    }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
401

    
402
  public void createTexture()
403
    {
404
    Bitmap bitmap;
405

    
406
    Paint paint = new Paint();
407
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
408
    Canvas canvas = new Canvas(bitmap);
409

    
410
    paint.setAntiAlias(true);
411
    paint.setTextAlign(Paint.Align.CENTER);
412
    paint.setStyle(Paint.Style.FILL);
413

    
414
    paint.setColor(COLOR_BLACK);
415
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
416

    
417
    int tex = 0;
418

    
419
    for(int row=0; row<mNumTexRows; row++)
420
      for(int col=0; col<mNumTexCols; col++)
421
        {
422
        if( tex>=NUM_TEXTURES ) break;
423
        createFaceTexture(canvas, paint, tex, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT);
424
        tex++;
425
        }
426

    
427
    if( !mTexture.setTexture(bitmap) )
428
      {
429
      int max = DistortedLibrary.getMaxTextureSize();
430
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
431
      crashlytics.log("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
432
      }
433
    }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
  public int getNumLayers()
438
    {
439
    return mNumLayers;
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
  public void continueRotation(float angleInDegrees)
445
    {
446
    mRotationAngleStatic.set0(angleInDegrees);
447
    }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
  public Static4D getRotationQuat()
452
      {
453
      return mQuat;
454
      }
455

    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457

    
458
  public void recomputeScaleFactor(int scrWidth)
459
    {
460
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
461
    }
462

    
463
///////////////////////////////////////////////////////////////////////////////////////////////////
464

    
465
  public void savePreferences(SharedPreferences.Editor editor)
466
    {
467
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
468
    }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

    
472
  public synchronized void restorePreferences(SharedPreferences preferences)
473
    {
474
    boolean error = false;
475

    
476
    for(int i=0; i<NUM_CUBITS; i++)
477
      {
478
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
479

    
480
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<QUATS.length)
481
        {
482
        CUBITS[i].modifyCurrentPosition(QUATS[mQuatDebug[i]]);
483
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
484
        }
485
      else
486
        {
487
        error = true;
488
        }
489
      }
490

    
491
    if( error )
492
      {
493
      for(int i=0; i<NUM_CUBITS; i++)
494
        {
495
        CUBITS[i].solve();
496
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
497
        }
498
      recordQuatsState("Failed to restorePreferences");
499
      }
500
    }
501

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

    
504
  public void recordQuatsState(String message)
505
    {
506
    StringBuilder quats = new StringBuilder();
507

    
508
    for(int j=0; j<NUM_CUBITS; j++)
509
      {
510
      quats.append(mQuatDebug[j]);
511
      quats.append(" ");
512
      }
513

    
514
    if( BuildConfig.DEBUG )
515
      {
516
      android.util.Log.e("quats" , quats.toString());
517
      android.util.Log.e("object", mList.name()+"_"+mNumLayers);
518
      }
519
    else
520
      {
521
      Exception ex = new Exception(message);
522
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
523
      crashlytics.setCustomKey("quats" , quats.toString());
524
      crashlytics.setCustomKey("object", mList.name()+"_"+mNumLayers );
525
      crashlytics.recordException(ex);
526
      }
527
    }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530

    
531
  public void releaseResources()
532
    {
533
    mTexture.markForDeletion();
534
    mMesh.markForDeletion();
535
    mEffects.markForDeletion();
536

    
537
    for(int j=0; j<NUM_CUBITS; j++)
538
      {
539
      CUBITS[j].releaseResources();
540
      }
541
    }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544

    
545
  public void apply(Effect effect, int position)
546
    {
547
    mEffects.apply(effect, position);
548
    }
549

    
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551

    
552
  public void remove(long effectID)
553
    {
554
    mEffects.abortById(effectID);
555
    }
556

    
557
///////////////////////////////////////////////////////////////////////////////////////////////////
558

    
559
  public synchronized void solve()
560
    {
561
    for(int i=0; i<NUM_CUBITS; i++)
562
      {
563
      CUBITS[i].solve();
564
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
565
      }
566
    }
567

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569

    
570
  public void resetAllTextureMaps()
571
    {
572
    final float ratioW = 1.0f/mNumTexCols;
573
    final float ratioH = 1.0f/mNumTexRows;
574
    int color, row, col;
575

    
576
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
577
      {
578
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
579

    
580
      for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
581
        {
582
        color = getFaceColor(cubit,cubitface,mNumLayers);
583
        row = (mNumTexRows-1) - color/mNumTexCols;
584
        col = color%mNumTexCols;
585
        maps[cubitface] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
586
        }
587

    
588
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
589
      }
590
    }
591

    
592
///////////////////////////////////////////////////////////////////////////////////////////////////
593

    
594
  public void setTextureMap(int cubit, int face, int newColor)
595
    {
596
    final float ratioW = 1.0f/mNumTexCols;
597
    final float ratioH = 1.0f/mNumTexRows;
598
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
599
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
600
    int col = newColor%mNumTexCols;
601

    
602
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
603
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
604
    }
605

    
606
///////////////////////////////////////////////////////////////////////////////////////////////////
607

    
608
  public synchronized void beginNewRotation(int axis, int row )
609
    {
610
    if( axis<0 || axis>=ROTATION_AXIS.length )
611
      {
612
      android.util.Log.e("object", "invalid rotation axis: "+axis);
613
      return;
614
      }
615
    if( row<0 || row>=mNumLayers )
616
      {
617
      android.util.Log.e("object", "invalid rotation row: "+row);
618
      return;
619
      }
620

    
621
    mRotAxis     = axis;
622
    mRotRowBitmap= (1<<row);
623
    mRotationAngleStatic.set0(0.0f);
624
    mRotationAxis.set( ROTATION_AXIS[axis] );
625
    mRotationAngle.add(mRotationAngleStatic);
626
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
627
    }
628

    
629
///////////////////////////////////////////////////////////////////////////////////////////////////
630

    
631
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
632
    {
633
    if( wasRotateApplied() )
634
      {
635
      mRotAxis     = axis;
636
      mRotRowBitmap= rowBitmap;
637

    
638
      mRotationAngleStatic.set0(0.0f);
639
      mRotationAxis.set( ROTATION_AXIS[axis] );
640
      mRotationAngle.setDuration(durationMillis);
641
      mRotationAngle.resetToBeginning();
642
      mRotationAngle.add(new Static1D(0));
643
      mRotationAngle.add(new Static1D(angle));
644
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
645
      mRotateEffect.notifyWhenFinished(listener);
646

    
647
      return mRotateEffect.getID();
648
      }
649

    
650
    return 0;
651
    }
652

    
653
///////////////////////////////////////////////////////////////////////////////////////////////////
654

    
655
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
656
    {
657
    if( wasRotateApplied() )
658
      {
659
      float angle = getAngle();
660
      mRotationAngleStatic.set0(angle);
661
      mRotationAngleFinal.set0(nearestAngleInDegrees);
662
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
663

    
664
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
665
      mRotationAngle.resetToBeginning();
666
      mRotationAngle.removeAll();
667
      mRotationAngle.add(mRotationAngleStatic);
668
      mRotationAngle.add(mRotationAngleMiddle);
669
      mRotationAngle.add(mRotationAngleFinal);
670
      mRotateEffect.notifyWhenFinished(listener);
671

    
672
      return mRotateEffect.getID();
673
      }
674

    
675
    return 0;
676
    }
677

    
678
///////////////////////////////////////////////////////////////////////////////////////////////////
679

    
680
  private float getAngle()
681
    {
682
    int pointNum = mRotationAngle.getNumPoints();
683

    
684
    if( pointNum>=1 )
685
      {
686
      return mRotationAngle.getPoint(pointNum-1).get0();
687
      }
688
    else
689
      {
690
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
691
      crashlytics.log("points in RotationAngle: "+pointNum);
692
      return 0;
693
      }
694
    }
695

    
696
///////////////////////////////////////////////////////////////////////////////////////////////////
697

    
698
  public synchronized void removeRotationNow()
699
    {
700
    float angle = getAngle();
701
    double nearestAngleInRadians = angle*Math.PI/180;
702
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
703
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
704
    float axisX = ROTATION_AXIS[mRotAxis].get0();
705
    float axisY = ROTATION_AXIS[mRotAxis].get1();
706
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
707
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
708

    
709
    mRotationAngle.removeAll();
710
    mRotationAngleStatic.set0(0);
711

    
712
    for(int i=0; i<NUM_CUBITS; i++)
713
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
714
        {
715
        int index = CUBITS[i].removeRotationNow(quat);
716
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
717
        }
718
    }
719

    
720
///////////////////////////////////////////////////////////////////////////////////////////////////
721

    
722
  public void initializeObject(int[][] moves)
723
    {
724
    solve();
725
    setupPosition(moves);
726
    }
727

    
728
///////////////////////////////////////////////////////////////////////////////////////////////////
729

    
730
  public int getCubit(float[] point3D)
731
    {
732
    float dist, minDist = Float.MAX_VALUE;
733
    int currentBest=-1;
734
    float multiplier = returnMultiplier();
735

    
736
    point3D[0] *= multiplier;
737
    point3D[1] *= multiplier;
738
    point3D[2] *= multiplier;
739

    
740
    for(int i=0; i<NUM_CUBITS; i++)
741
      {
742
      dist = CUBITS[i].getDistSquared(point3D);
743
      if( dist<minDist )
744
        {
745
        minDist = dist;
746
        currentBest = i;
747
        }
748
      }
749

    
750
    return currentBest;
751
    }
752

    
753
///////////////////////////////////////////////////////////////////////////////////////////////////
754

    
755
  public int computeNearestAngle(float angle, float speed)
756
    {
757
    final int NEAREST = 360/getBasicAngle();
758

    
759
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
760
    if( angle< -(NEAREST*0.5) ) tmp-=1;
761

    
762
    if( tmp!=0 ) return NEAREST*tmp;
763

    
764
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
765
    }
766

    
767
///////////////////////////////////////////////////////////////////////////////////////////////////
768

    
769
  public float getCameraDist()
770
    {
771
    return mCameraDist;
772
    }
773

    
774
///////////////////////////////////////////////////////////////////////////////////////////////////
775

    
776
  public int getNodeSize()
777
    {
778
    return mNodeSize;
779
    }
780

    
781
///////////////////////////////////////////////////////////////////////////////////////////////////
782

    
783
  public ObjectList getObjectList()
784
    {
785
    return mList;
786
    }
787

    
788
///////////////////////////////////////////////////////////////////////////////////////////////////
789

    
790
  abstract float getScreenRatio();
791
  abstract Static3D[] getCubitPositions(int numLayers);
792
  abstract Static4D[] getQuats();
793
  abstract int getNumFaces();
794
  abstract int getNumStickerTypes(int numLayers);
795
  abstract int getNumCubitFaces();
796
  abstract MeshBase createCubitMesh(int cubit, int numLayers);
797
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top);
798
  abstract int getFaceColor(int cubit, int cubitface, int numLayers);
799
  abstract float returnMultiplier();
800
  abstract float[] getRowChances(int numLayers);
801
  abstract float[] getCuts(int numLayers);
802
  abstract boolean shouldResetTextureMaps();
803

    
804
  public abstract boolean isSolved();
805
  public abstract Static3D[] getRotationAxis();
806
  public abstract int getBasicAngle();
807
  public abstract String retObjectString();
808
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
809
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
810
  public abstract int getObjectName(int numLayers);
811
  public abstract int getInventor(int numLayers);
812
  public abstract int getComplexity(int numLayers);
813
  }
(28-28/32)