Project

General

Profile

Download (28 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectControl.java @ bcdecc9f

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.main;
11

    
12
import java.io.InputStream;
13

    
14
import android.app.Activity;
15
import android.content.SharedPreferences;
16
import android.util.DisplayMetrics;
17
import android.view.MotionEvent;
18

    
19
import org.distorted.library.main.QuatHelper;
20
import org.distorted.library.type.Static4D;
21

    
22
import org.distorted.objectlib.helpers.BlockController;
23
import org.distorted.objectlib.helpers.MovesFinished;
24
import org.distorted.objectlib.helpers.ObjectLibInterface;
25
import org.distorted.objectlib.touchcontrol.TouchControl;
26
import org.distorted.objectlib.touchcontrol.TouchControlShapeChanging;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

    
30
public class ObjectControl
31
{
32
    public static final int MAX_MOVING_PARTS = 242; // Gigaminx
33
    public static final int MAX_QUATS = 60;         // Gigaminx: 60 quats group.
34

    
35
    public static final int NUM_SPEED_PROBES = 10;
36
    public static final int INVALID_POINTER_ID = -1;
37

    
38
    public static final int MODE_ROTATE  = 0;
39
    public static final int MODE_DRAG    = 1;
40
    public static final int MODE_REPLACE = 2;
41
    public static final int MODE_NOTHING = 3;
42

    
43
    // Moving the finger from the middle of the vertical screen to the right edge will rotate a
44
    // given face by SWIPING_SENSITIVITY/2 degrees.
45
    public final static int SWIPING_SENSITIVITY  = 240;
46
    // Moving the finger by 0.3 of an inch will start a Rotation.
47
    public final static float ROTATION_SENSITIVITY = 0.3f;
48

    
49
    private final Static4D CAMERA_POINT = new Static4D(0, 0, 0, 0);
50

    
51
    private final ObjectLibInterface mInterface;
52
    private final ObjectPreRender mPreRender;
53
    private TouchControl mTouchControl;
54
    private TwistyObjectNode mObjectNode;
55
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
56
    private int mScreenWidth, mScreenHeight, mScalingSize;
57
    private float mMoveX, mMoveY;
58
    private int mLastMode;
59

    
60
    private float mRotAngle, mInitDistance;
61
    private float mStartRotX, mStartRotY;
62
    private float mRotationFactor;
63
    private int mCurrentAxis, mCurrentRow;
64
    private float mCurrentAngle, mCurrRotSpeed;
65
    private final float[] mLastX;
66
    private final float[] mLastY;
67
    private final long[] mLastT;
68
    private int mFirstIndex, mLastIndex;
69
    private final int mDensity;
70

    
71
    private int mPointer1, mPointer2;
72
    private float mX1, mY1, mX2, mY2, mX, mY;
73
    private final boolean mIsAutomatic;
74

    
75
    private boolean mIsLocked, mRemLocked;
76
    private final int[] mBuffer;
77
    private final float[] mAxis;
78
    private int mMeshState, mIconMode;
79
    private boolean mRotateOnCreation;
80
    private final Static4D mQuat,mTemp;
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
    private void addSpeedProbe(float x, float y)
85
      {
86
      long currTime = System.currentTimeMillis();
87
      boolean theSame = mLastIndex==mFirstIndex;
88

    
89
      mLastIndex++;
90
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
91

    
92
      mLastT[mLastIndex] = currTime;
93
      mLastX[mLastIndex] = x;
94
      mLastY[mLastIndex] = y;
95

    
96
      if( mLastIndex==mFirstIndex)
97
        {
98
        mFirstIndex++;
99
        if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
100
        }
101

    
102
      if( theSame )
103
        {
104
        mLastT[mFirstIndex] = currTime;
105
        mLastX[mFirstIndex] = x;
106
        mLastY[mFirstIndex] = y;
107
        }
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
    private void computeCurrentSpeedInInchesPerSecond()
113
      {
114
      long firstTime = mLastT[mFirstIndex];
115
      long lastTime  = mLastT[mLastIndex];
116
      float fX = mLastX[mFirstIndex];
117
      float fY = mLastY[mFirstIndex];
118
      float lX = mLastX[mLastIndex];
119
      float lY = mLastY[mLastIndex];
120

    
121
      long timeDiff = lastTime-firstTime;
122

    
123
      mLastIndex = 0;
124
      mFirstIndex= 0;
125

    
126
      mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX-lX,fY-lY)/timeDiff : 0;
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
    private float retFingerDragDistanceInInches(float xd, float yd)
132
      {
133
      float xDist = mScreenWidth*xd;
134
      float yDist = mScreenHeight*yd;
135
      float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
136

    
137
      return distInPixels/mDensity;
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
    private void replaceMode(boolean down)
143
      {
144
      mBeginningRotation= false;
145

    
146
      if( down )
147
        {
148
        int cubit = mTouchControl.getTouchedCubit();
149
        int face  = mTouchControl.getTouchedCubitFace();
150
        mInterface.onReplaceModeDown(cubit,face);
151
        }
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
    private void setUpDragOrRotate(boolean down, float x, float y)
157
      {
158
      if( mLastMode==MODE_DRAG )
159
        {
160
        mDragging           = true;
161
        mBeginningRotation  = false;
162
        mContinuingRotation = false;
163
        }
164
      else
165
        {
166
        CAMERA_POINT.set2( mObjectNode.getCameraDist() );
167
        Static4D touchPoint = new Static4D(x, y, 0, 0);
168
        Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, mQuat);
169
        Static4D rotatedCamera= QuatHelper.rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
170

    
171
        if( mTouchControl!=null && mTouchControl.objectTouched(rotatedTouchPoint,rotatedCamera) )
172
          {
173
          mDragging           = false;
174
          mContinuingRotation = false;
175

    
176
               if( mLastMode==MODE_ROTATE  ) mBeginningRotation = !mPreRender.isRotationBlocked();
177
          else if( mLastMode==MODE_REPLACE ) replaceMode(down);
178
          }
179
        else
180
          {
181
          mDragging           = (!mIsLocked || mIsAutomatic);
182
          mBeginningRotation  = false;
183
          mContinuingRotation = false;
184
          if( !mDragging ) mInterface.failedToDrag();
185
          }
186
        }
187
      }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
    private void drag(float x, float y)
192
      {
193
      if( mPointer1!=INVALID_POINTER_ID && mPointer2!=INVALID_POINTER_ID)
194
        {
195
        float x2 = (mX2 - mScreenWidth*0.5f)/ mScalingSize;
196
        float y2 = (mScreenHeight*0.5f - mY2)/ mScalingSize;
197

    
198
        float angleNow = getAngle(x,y,x2,y2);
199
        float angleDiff = angleNow-mRotAngle;
200
        float sinA =-(float)Math.sin(angleDiff);
201
        float cosA = (float)Math.cos(angleDiff);
202

    
203
        Static4D dragQuat = QuatHelper.quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
204
        mTemp.set(dragQuat);
205

    
206
        mRotAngle = angleNow;
207

    
208
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
209
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
210
        mInitDistance = distNow;
211
        TwistyObject object = mPreRender.getObject();
212
        if( object!=null )
213
          {
214
          object.setObjectRatio(distQuot, mScalingSize );
215
          float ratio = object.getObjectRatio();
216
          if( mLastMode==MODE_REPLACE ) mTouchControl.setObjectRatio(ratio);
217
          }
218
        }
219
      else
220
        {
221
        Static4D dragQuat = QuatHelper.quatMultiply(QuatHelper.quatFromDrag(mX-x,y-mY), mQuat);
222
        mTemp.set(dragQuat);
223
        }
224

    
225
      mPreRender.setQuatOnNextRender();
226
      mX = x;
227
      mY = y;
228
      }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
    private void finishRotation()
233
      {
234
      TwistyObject object = mPreRender.getObject();
235
      int[][] angles = object.getBasicAngles();
236

    
237
      if( mCurrentAxis<angles.length && mCurrentRow<angles[mCurrentAxis].length )
238
        {
239
        computeCurrentSpeedInInchesPerSecond();
240
        int basic = angles[mCurrentAxis][mCurrentRow];
241
        int angle = object.computeNearestAngle(basic,mCurrentAngle, mCurrRotSpeed);
242
        mPreRender.finishRotation(angle);
243
        mPreRender.rememberMove(mCurrentAxis,mCurrentRow,angle);
244

    
245
        if( angle!=0 )
246
          {
247
          int realAngle = (angle*basic)/360;
248
          mInterface.onFinishRotation(mCurrentAxis,mCurrentRow,realAngle);
249
          }
250

    
251
        mContinuingRotation = false;
252
        mBeginningRotation  = false;
253
        mDragging           = true;
254
        }
255
      }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
    private void continueRotation(float x, float y)
260
      {
261
      float dx = x-mStartRotX;
262
      float dy = y-mStartRotY;
263
      float alpha = dx*mAxis[0] + dy*mAxis[1];
264
      float x2 = dx - alpha*mAxis[0];
265
      float y2 = dy - alpha*mAxis[1];
266

    
267
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
268

    
269
      // we have the length of 1D vector 'angle', now the direction:
270
      float tmp = mAxis[1]==0 ? -mAxis[0]*y2 : mAxis[1]*x2;
271

    
272
      float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
273
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
274
      mPreRender.getObject().continueRotation(mCurrentAngle);
275

    
276
      addSpeedProbe(x2,y2);
277
      }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
    private void beginRotation(float x, float y)
282
      {
283
      TwistyObject object = mPreRender.getObject();
284
      int[] numLayers = object.getNumLayers();
285

    
286
      Static4D touchPoint = new Static4D(x, y, 0, 0);
287
      Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, mQuat);
288
      mTouchControl.newRotation(mBuffer,rotatedTouchPoint,mQuat);
289

    
290
      int currentAxis = mBuffer[0];
291
      int currentRow  = mBuffer[1];
292

    
293
      mTouchControl.getCastedRotAxis(mAxis,mQuat,currentAxis);
294
      mRotationFactor = mTouchControl.returnRotationFactor(numLayers,currentRow);
295

    
296
      if( object.beginNewRotation( currentAxis, currentRow ) )
297
        {
298
        mInterface.onBeginRotation();
299

    
300
        addSpeedProbe(x,y);
301

    
302
        mBeginningRotation = false;
303
        mContinuingRotation= true;
304
        mStartRotX = x;
305
        mStartRotY = y;
306
        mCurrentAxis = currentAxis;
307
        mCurrentRow  = currentRow;
308
        }
309
      }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
    private float getAngle(float x1, float y1, float x2, float y2)
314
      {
315
      return (float) Math.atan2(y1-y2, x1-x2);
316
      }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
    private void prepareDown(MotionEvent event)
321
      {
322
      mPointer1 = event.getPointerId(0);
323
      mX1 = event.getX() - mMoveX;
324
      mY1 = event.getY() + mMoveY;
325
      mPointer2 = INVALID_POINTER_ID;
326
      }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

    
330
    private void prepareMove(MotionEvent event)
331
      {
332
      int index1 = event.findPointerIndex(mPointer1);
333

    
334
      if( index1>=0 )
335
        {
336
        mX1 = event.getX(index1) - mMoveX;
337
        mY1 = event.getY(index1) + mMoveY;
338
        }
339

    
340
      int index2 = event.findPointerIndex(mPointer2);
341

    
342
      if( index2>=0 )
343
        {
344
        mX2 = event.getX(index2) - mMoveX;
345
        mY2 = event.getY(index2) + mMoveY;
346
        }
347
      }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
    private void prepareUp(MotionEvent event)
352
      {
353
      mPointer1 = INVALID_POINTER_ID;
354
      mPointer2 = INVALID_POINTER_ID;
355
      }
356

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

    
359
    private void prepareDown2(MotionEvent event)
360
      {
361
      int index = event.getActionIndex();
362

    
363
      if( mPointer1==INVALID_POINTER_ID )
364
        {
365
        mPointer1 = event.getPointerId(index);
366
        mX1 = event.getX(index) - mMoveX;
367
        mY1 = event.getY(index) + mMoveY;
368
        }
369
      else if( mPointer2==INVALID_POINTER_ID )
370
        {
371
        mPointer2 = event.getPointerId(index);
372
        mX2 = event.getX(index) - mMoveX;
373
        mY2 = event.getY(index) + mMoveY;
374
        }
375
      }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
    private void prepareUp2(MotionEvent event)
380
      {
381
      int index = event.getActionIndex();
382

    
383
           if( index==event.findPointerIndex(mPointer1) ) mPointer1 = INVALID_POINTER_ID;
384
      else if( index==event.findPointerIndex(mPointer2) ) mPointer2 = INVALID_POINTER_ID;
385
      }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

    
389
    private void actionMove(float x1, float y1, float x2, float y2)
390
      {
391
      float pX = mPointer1 != INVALID_POINTER_ID ? x1 : x2;
392
      float pY = mPointer1 != INVALID_POINTER_ID ? y1 : y2;
393

    
394
      float x = (pX - mScreenWidth*0.5f)/ mScalingSize;
395
      float y = (mScreenHeight*0.5f -pY)/ mScalingSize;
396

    
397
      if( mBeginningRotation )
398
        {
399
        if( retFingerDragDistanceInInches(mX-x,mY-y) > ROTATION_SENSITIVITY )
400
          {
401
          beginRotation(x,y);
402
          }
403
        }
404
      else if( mContinuingRotation )
405
        {
406
        continueRotation(x,y);
407
        }
408
      else if( mDragging )
409
        {
410
        drag(x,y);
411
        }
412
      else
413
        {
414
        setUpDragOrRotate(false,x,y);
415
        }
416
      }
417

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

    
420
    private void actionDown(float x, float y)
421
      {
422
      mX = (x -  mScreenWidth*0.5f)/ mScalingSize;
423
      mY = (mScreenHeight*0.5f - y)/ mScalingSize;
424

    
425
      setUpDragOrRotate(true,mX,mY);
426
      }
427

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429

    
430
    private void actionUp()
431
      {
432
      if( mContinuingRotation )
433
        {
434
        finishRotation();
435
        }
436

    
437
      if( mLastMode==MODE_REPLACE ) mInterface.onReplaceModeUp();
438
      }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

    
442
    private void actionDown2(float x1, float y1, float x2, float y2)
443
      {
444
      mRotAngle = getAngle(x1,-y1, x2,-y2);
445
      mInitDistance = -1;
446

    
447
      mX = (x1 - mScreenWidth*0.5f )/ mScalingSize;
448
      mY = (mScreenHeight*0.5f - y1)/ mScalingSize;
449

    
450
      if( mBeginningRotation )
451
        {
452
        mContinuingRotation = false;
453
        mBeginningRotation  = false;
454
        mDragging           = true;
455
        }
456
      else if( mContinuingRotation )
457
        {
458
        finishRotation();
459
        }
460
      }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

    
464
    private void actionUp2(boolean p1isUp, float x1, float y1, boolean p2isUp, float x2, float y2)
465
      {
466
      if( p1isUp )
467
        {
468
        mX = (x2 -  mScreenWidth*0.5f)/ mScalingSize;
469
        mY = (mScreenHeight*0.5f - y2)/ mScalingSize;
470
        }
471
      if( p2isUp )
472
        {
473
        mX = (x1 -  mScreenWidth*0.5f)/ mScalingSize;
474
        mY = (mScreenHeight*0.5f - y1)/ mScalingSize;
475
        }
476
      }
477

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479

    
480
    int getScalingSize()
481
      {
482
      return mScalingSize;
483
      }
484

    
485
///////////////////////////////////////////////////////////////////////////////////////////////////
486

    
487
    void setTouchControl(TwistyObject object)
488
      {
489
      if( mLastMode!=MODE_REPLACE ) mTouchControl = object.getTouchControl();
490
      else                          mTouchControl = new TouchControlShapeChanging(object);
491
      }
492

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494
// INTERNAL API (for AutomaticControl)
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496

    
497
    public ObjectPreRender getPreRender()
498
      {
499
      return mPreRender;
500
      }
501

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

    
504
    public ObjectLibInterface getInterface()
505
      {
506
      return mInterface;
507
      }
508

    
509
///////////////////////////////////////////////////////////////////////////////////////////////////
510
// PUBLIC API
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512

    
513
    public ObjectControl(Activity act, ObjectLibInterface actioner)
514
      {
515
      mIsAutomatic = false;
516

    
517
      mBuffer = new int[2];
518
      mAxis   = new float[2];
519

    
520
      mQuat= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
521
      mTemp= new Static4D(0,0,0,1);
522

    
523
      mCurrRotSpeed= 0.0f;
524
      mLastMode    = -1;
525
      mRotateOnCreation = false;
526

    
527
      mLastX = new float[NUM_SPEED_PROBES];
528
      mLastY = new float[NUM_SPEED_PROBES];
529
      mLastT = new long[NUM_SPEED_PROBES];
530
      mFirstIndex= 0;
531
      mLastIndex = 0;
532
      mMeshState =-1;
533
      mIconMode  =-1;
534

    
535
      DisplayMetrics dm = new DisplayMetrics();
536
      act.getWindowManager().getDefaultDisplay().getMetrics(dm);
537

    
538
      mDensity = dm.densityDpi;
539

    
540
      mPreRender = new ObjectPreRender(act,this,actioner);
541
      mInterface = actioner;
542
      }
543

    
544
///////////////////////////////////////////////////////////////////////////////////////////////////
545

    
546
    public void setRotateOnCreation(boolean rotate)
547
      {
548
      mRotateOnCreation = rotate;
549
      }
550

    
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552

    
553
    public boolean getRotateOnCreation()
554
      {
555
      return mRotateOnCreation;
556
      }
557

    
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559

    
560
    public TwistyObjectNode getNode()
561
      {
562
      return mObjectNode;
563
      }
564

    
565
///////////////////////////////////////////////////////////////////////////////////////////////////
566

    
567
    public void createNode(int width, int height)
568
      {
569
      if( mObjectNode==null ) mObjectNode = new TwistyObjectNode(width,height);
570
      }
571

    
572
///////////////////////////////////////////////////////////////////////////////////////////////////
573

    
574
    public void setScreenSizeAndScaling(int width, int height, int scaling)
575
      {
576
      mScreenWidth = width;
577
      mScreenHeight= height;
578
      mScalingSize = scaling;
579

    
580
      if( mObjectNode!=null ) mObjectNode.setSize(width,height);
581

    
582
      TwistyObject object = mPreRender.getObject();
583

    
584
      if( object!=null )
585
        {
586
        object.setTexture();
587
        object.setNodeSize(mScalingSize);
588
        }
589
      }
590

    
591
///////////////////////////////////////////////////////////////////////////////////////////////////
592

    
593
    public void setObjectMove(int xmove, int ymove)
594
      {
595
      mMoveX = xmove;
596
      mMoveY = ymove;
597

    
598
      mPreRender.setMove(xmove,ymove);
599
      }
600

    
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602

    
603
    public void setObjectScale(float scale)
604
      {
605
      mPreRender.setScale(scale);
606
      }
607

    
608
///////////////////////////////////////////////////////////////////////////////////////////////////
609

    
610
    public void onPause()
611
      {
612
      BlockController.onPause();
613
      }
614

    
615
///////////////////////////////////////////////////////////////////////////////////////////////////
616

    
617
    public void onResume()
618
      {
619
      mPointer1 = INVALID_POINTER_ID;
620
      mPointer2 = INVALID_POINTER_ID;
621

    
622
      unlock();
623

    
624
      BlockController.onResume();
625
      }
626

    
627
///////////////////////////////////////////////////////////////////////////////////////////////////
628

    
629
    public void rotateNow(Static4D quat)
630
      {
631
      mTemp.set(quat);
632
      mQuat.set(mTemp);
633
      }
634

    
635
///////////////////////////////////////////////////////////////////////////////////////////////////
636

    
637
    public void scaleNow(float scale)
638
      {
639
      mPreRender.getObject().setObjectRatioNow(scale,mScalingSize );
640
      }
641

    
642
///////////////////////////////////////////////////////////////////////////////////////////////////
643

    
644
    public void setQuat()
645
      {
646
      mQuat.set(mTemp);
647
      }
648

    
649
///////////////////////////////////////////////////////////////////////////////////////////////////
650

    
651
    public Static4D getQuat()
652
      {
653
      return mQuat;
654
      }
655

    
656
///////////////////////////////////////////////////////////////////////////////////////////////////
657

    
658
    public void preRender()
659
      {
660
      mPreRender.preRender();
661
      }
662

    
663
///////////////////////////////////////////////////////////////////////////////////////////////////
664

    
665
    public void blockTouch(int place)
666
      {
667
      setLock(true);
668
      mPreRender.blockRotation(place);
669
      }
670

    
671
///////////////////////////////////////////////////////////////////////////////////////////////////
672

    
673
    public void unblockRotation()
674
      {
675
      unsetLock();
676
      mPreRender.unblockRotation();
677
      }
678

    
679
///////////////////////////////////////////////////////////////////////////////////////////////////
680

    
681
    public void unblockEverything()
682
      {
683
      mPreRender.unblockEverything();
684
      }
685

    
686
///////////////////////////////////////////////////////////////////////////////////////////////////
687

    
688
    public boolean isScramblingAndSolvingNotBlocked()
689
      {
690
      return mPreRender.isScramblingAndSolvingNotBlocked();
691
      }
692

    
693
///////////////////////////////////////////////////////////////////////////////////////////////////
694

    
695
    public void initializeObject(int[][] moves)
696
      {
697
      mPreRender.initializeObject(moves);
698
      }
699

    
700
///////////////////////////////////////////////////////////////////////////////////////////////////
701

    
702
    public void changeObject(int ordinal, int meshState, int iconMode, InputStream jsonStream, InputStream meshStream)
703
      {
704
      mPreRender.changeObject(ordinal, meshState, iconMode, jsonStream, meshStream);
705
      }
706

    
707
///////////////////////////////////////////////////////////////////////////////////////////////////
708

    
709
    public void changeIfDifferent(int ordinal, String newName, int meshState, int iconMode, InputStream jsonStream, InputStream meshStream)
710
      {
711
      TwistyObject object = mPreRender.getObject();
712
      String oldName = object==null ? "" : object.getShortName();
713

    
714
      if( !oldName.equals(newName) || mMeshState!=meshState || mIconMode!=iconMode )
715
        {
716
        mMeshState = meshState;
717
        mIconMode  = iconMode;
718
        mPreRender.changeObject(ordinal, meshState, iconMode, jsonStream, meshStream);
719
        }
720
      }
721

    
722
///////////////////////////////////////////////////////////////////////////////////////////////////
723
// if one or more fingers currently touch the screen, and we just pressed the 'scramble' button, do
724
// not scramble - otherwise a kind of a cheat is possible where user touches the screen, starts
725
// scrambling, then lifts the finger and the act of lifting screws the scrambling - no further
726
// scrambles take any effect!
727

    
728
    public boolean scrambleObject(int num)
729
      {
730
      if( !mBeginningRotation && !mContinuingRotation )
731
        {
732
        return mPreRender.scrambleObject(num);
733
        }
734
      return false;
735
      }
736

    
737
///////////////////////////////////////////////////////////////////////////////////////////////////
738
// ditto
739

    
740
    public boolean fastScrambleObject(int duration, int num)
741
      {
742
      if( !mBeginningRotation && !mContinuingRotation )
743
        {
744
        return mPreRender.fastScrambleObject(duration,num);
745
        }
746
      return false;
747
      }
748

    
749
///////////////////////////////////////////////////////////////////////////////////////////////////
750

    
751
    public void presentObject(int num, int duration)
752
      {
753
      mPreRender.presentObject(num,duration);
754
      }
755

    
756
///////////////////////////////////////////////////////////////////////////////////////////////////
757

    
758
    public void solveObject()
759
      {
760
      mPreRender.solveObject();
761
      }
762

    
763
///////////////////////////////////////////////////////////////////////////////////////////////////
764

    
765
    public void solveOnly()
766
      {
767
      mPreRender.solveOnly();
768
      }
769

    
770
///////////////////////////////////////////////////////////////////////////////////////////////////
771

    
772
    public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, int duration)
773
      {
774
      mPreRender.addRotation(listener,axis,rowBitmap,angle,duration);
775
      }
776

    
777
///////////////////////////////////////////////////////////////////////////////////////////////////
778

    
779
    public void resetAllTextureMaps()
780
      {
781
      mPreRender.resetAllTextureMaps();
782
      }
783

    
784
///////////////////////////////////////////////////////////////////////////////////////////////////
785

    
786
    public TwistyObject getObject()
787
      {
788
      return mPreRender.getObject();
789
      }
790

    
791
///////////////////////////////////////////////////////////////////////////////////////////////////
792

    
793
    public void savePreferences(SharedPreferences.Editor editor)
794
      {
795
      mPreRender.savePreferences(editor);
796
      }
797

    
798
///////////////////////////////////////////////////////////////////////////////////////////////////
799

    
800
    public void restorePreferences(SharedPreferences preferences)
801
      {
802
      mPreRender.restorePreferences(preferences);
803
      }
804

    
805
///////////////////////////////////////////////////////////////////////////////////////////////////
806

    
807
    public boolean retLocked()
808
      {
809
      return mIsLocked;
810
      }
811

    
812
///////////////////////////////////////////////////////////////////////////////////////////////////
813

    
814
    public void toggleLock()
815
      {
816
      mIsLocked = !mIsLocked;
817
      }
818

    
819
///////////////////////////////////////////////////////////////////////////////////////////////////
820

    
821
    public void unlock()
822
      {
823
      mIsLocked = false;
824
      }
825

    
826
///////////////////////////////////////////////////////////////////////////////////////////////////
827

    
828
    public void setLock(boolean value)
829
      {
830
      mRemLocked = mIsLocked;
831
      mIsLocked = value;
832
      }
833

    
834
///////////////////////////////////////////////////////////////////////////////////////////////////
835

    
836
    public void unsetLock()
837
      {
838
      mIsLocked = mRemLocked;
839
      }
840

    
841
///////////////////////////////////////////////////////////////////////////////////////////////////
842

    
843
    public void setTextureMap(int cubit, int face, int newColor)
844
      {
845
      mPreRender.setTextureMap(cubit,face,newColor);
846
      }
847

    
848
///////////////////////////////////////////////////////////////////////////////////////////////////
849

    
850
    public boolean onTouchEvent(MotionEvent event, int mode)
851
      {
852
      if( mode!=MODE_NOTHING )
853
        {
854
        if( mObjectNode==null ) return true;
855

    
856
        if( mode!=mLastMode )
857
          {
858
          TwistyObject object = getObject();
859

    
860
          if( object!=null )
861
            {
862
            mLastMode = mode;
863
            setTouchControl(object);
864
            }
865
          else return true;
866
          }
867

    
868
        int action = event.getActionMasked();
869

    
870
        switch(action)
871
          {
872
          case MotionEvent.ACTION_DOWN        : prepareDown(event);
873
                                                actionDown(mX1, mY1);
874
                                                break;
875
          case MotionEvent.ACTION_MOVE        : prepareMove(event);
876
                                                actionMove(mX1, mY1, mX2, mY2);
877
                                                break;
878
          case MotionEvent.ACTION_UP          : prepareUp(event);
879
                                                actionUp();
880
                                                break;
881
          case MotionEvent.ACTION_POINTER_DOWN: prepareDown2(event);
882
                                                actionDown2(mX1, mY1, mX2, mY2);
883
                                                break;
884
          case MotionEvent.ACTION_POINTER_UP  : prepareUp2(event);
885
                                                boolean p1isUp = mPointer1==INVALID_POINTER_ID;
886
                                                boolean p2isUp = mPointer2==INVALID_POINTER_ID;
887
                                                actionUp2(p1isUp, mX1, mY1, p2isUp, mX2, mY2);
888
                                                break;
889
          }
890
        }
891

    
892
      return true;
893
      }
894
}
895

    
(3-3/9)