Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.objectlib.main;
21

    
22
import java.io.InputStream;
23

    
24
import android.app.Activity;
25
import android.content.SharedPreferences;
26
import android.util.DisplayMetrics;
27
import android.view.MotionEvent;
28

    
29
import org.distorted.library.main.QuatHelper;
30
import org.distorted.library.type.Static4D;
31

    
32
import org.distorted.objectlib.helpers.BlockController;
33
import org.distorted.objectlib.helpers.MovesFinished;
34
import org.distorted.objectlib.helpers.ObjectLibInterface;
35
import org.distorted.objectlib.touchcontrol.TouchControl;
36
import org.distorted.objectlib.touchcontrol.TouchControlShapeChanging;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
public class ObjectControl
41
{
42
    public static final int MAX_MOVING_PARTS = 242; // Gigaminx
43
    public static final int MAX_QUATS = 60;         // Gigaminx: 60 quats group.
44

    
45
    public static final int NUM_SPEED_PROBES = 10;
46
    public static final int INVALID_POINTER_ID = -1;
47

    
48
    public static final int MODE_ROTATE  = 0;
49
    public static final int MODE_DRAG    = 1;
50
    public static final int MODE_REPLACE = 2;
51

    
52
    // Moving the finger from the middle of the vertical screen to the right edge will rotate a
53
    // given face by SWIPING_SENSITIVITY/2 degrees.
54
    public final static int SWIPING_SENSITIVITY  = 240;
55
    // Moving the finger by 0.3 of an inch will start a Rotation.
56
    public final static float ROTATION_SENSITIVITY = 0.3f;
57

    
58
    private final Static4D CAMERA_POINT = new Static4D(0, 0, 0, 0);
59

    
60
    private final ObjectLibInterface mInterface;
61
    private final ObjectPreRender mPreRender;
62
    private TouchControl mTouchControl, mTouchControlBackup;
63
    private TwistyObjectNode mObjectNode;
64
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
65
    private int mScreenWidth, mScreenHeight, mScreenMin;
66
    private float mMoveX, mMoveY;
67
    private int mLastMode;
68

    
69
    private float mRotAngle, mInitDistance;
70
    private float mStartRotX, mStartRotY;
71
    private float mRotationFactor;
72
    private int mCurrentAxis, mCurrentRow;
73
    private float mCurrentAngle, mCurrRotSpeed;
74
    private final float[] mLastX;
75
    private final float[] mLastY;
76
    private final long[] mLastT;
77
    private int mFirstIndex, mLastIndex;
78
    private final int mDensity;
79

    
80
    private int mPointer1, mPointer2;
81
    private float mX1, mY1, mX2, mY2, mX, mY;
82
    private final boolean mIsAutomatic;
83

    
84
    private boolean mIsLocked, mRemLocked;
85
    private final int[] mBuffer;
86
    private final float[] mAxis;
87
    private int mMeshState, mIconMode;
88
    private boolean mRotateOnCreation;
89

    
90
    private static final Static4D mQuat= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
91
    private static final Static4D mTemp= new Static4D(0,0,0,1);
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
    private void addSpeedProbe(float x, float y)
96
      {
97
      long currTime = System.currentTimeMillis();
98
      boolean theSame = mLastIndex==mFirstIndex;
99

    
100
      mLastIndex++;
101
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
102

    
103
      mLastT[mLastIndex] = currTime;
104
      mLastX[mLastIndex] = x;
105
      mLastY[mLastIndex] = y;
106

    
107
      if( mLastIndex==mFirstIndex)
108
        {
109
        mFirstIndex++;
110
        if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
111
        }
112

    
113
      if( theSame )
114
        {
115
        mLastT[mFirstIndex] = currTime;
116
        mLastX[mFirstIndex] = x;
117
        mLastY[mFirstIndex] = y;
118
        }
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
    private void computeCurrentSpeedInInchesPerSecond()
124
      {
125
      long firstTime = mLastT[mFirstIndex];
126
      long lastTime  = mLastT[mLastIndex];
127
      float fX = mLastX[mFirstIndex];
128
      float fY = mLastY[mFirstIndex];
129
      float lX = mLastX[mLastIndex];
130
      float lY = mLastY[mLastIndex];
131

    
132
      long timeDiff = lastTime-firstTime;
133

    
134
      mLastIndex = 0;
135
      mFirstIndex= 0;
136

    
137
      mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX-lX,fY-lY)/timeDiff : 0;
138
      }
139

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

    
142
    private float retFingerDragDistanceInInches(float xd, float yd)
143
      {
144
      float xDist = mScreenWidth*xd;
145
      float yDist = mScreenHeight*yd;
146
      float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
147

    
148
      return distInPixels/mDensity;
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
    private void replaceMode(boolean down)
154
      {
155
      mBeginningRotation= false;
156

    
157
      if( down )
158
        {
159
        int cubit = mTouchControl.getTouchedCubit();
160
        int face  = mTouchControl.getTouchedCubitFace();
161
        mInterface.onReplaceModeDown(cubit,face);
162
        }
163
      }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
    private void setUpDragOrRotate(boolean down, float x, float y)
168
      {
169
      if( mLastMode==MODE_DRAG )
170
        {
171
        mDragging           = true;
172
        mBeginningRotation  = false;
173
        mContinuingRotation = false;
174
        }
175
      else
176
        {
177
        CAMERA_POINT.set2( mObjectNode.getCameraDist() );
178
        Static4D touchPoint = new Static4D(x, y, 0, 0);
179
        Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, mQuat);
180
        Static4D rotatedCamera= QuatHelper.rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
181

    
182
        if( mTouchControl!=null && mTouchControl.objectTouched(rotatedTouchPoint,rotatedCamera) )
183
          {
184
          mDragging           = false;
185
          mContinuingRotation = false;
186

    
187
               if( mLastMode==MODE_ROTATE  ) mBeginningRotation = !mPreRender.isTouchBlocked();
188
          else if( mLastMode==MODE_REPLACE ) replaceMode(down);
189
          }
190
        else
191
          {
192
          mDragging           = (!mIsLocked || mIsAutomatic);
193
          mBeginningRotation  = false;
194
          mContinuingRotation = false;
195
          if( !mDragging ) mInterface.failedToDrag();
196
          }
197
        }
198
      }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
    private void drag(float x, float y)
203
      {
204
      if( mPointer1!=INVALID_POINTER_ID && mPointer2!=INVALID_POINTER_ID)
205
        {
206
        float x2 = (mX2 - mScreenWidth*0.5f)/mScreenMin;
207
        float y2 = (mScreenHeight*0.5f - mY2)/mScreenMin;
208

    
209
        float angleNow = getAngle(x,y,x2,y2);
210
        float angleDiff = angleNow-mRotAngle;
211
        float sinA =-(float)Math.sin(angleDiff);
212
        float cosA = (float)Math.cos(angleDiff);
213

    
214
        Static4D dragQuat = QuatHelper.quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
215
        mTemp.set(dragQuat);
216

    
217
        mRotAngle = angleNow;
218

    
219
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
220
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
221
        mInitDistance = distNow;
222
        TwistyObject object = mPreRender.getObject();
223
        if( object!=null ) object.setObjectRatio(distQuot, mObjectNode.getMinSize() );
224
        }
225
      else
226
        {
227
        Static4D dragQuat = QuatHelper.quatMultiply(QuatHelper.quatFromDrag(mX-x,y-mY), mQuat);
228
        mTemp.set(dragQuat);
229
        }
230

    
231
      mPreRender.setQuatOnNextRender();
232
      mX = x;
233
      mY = y;
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    private void finishRotation()
239
      {
240
      TwistyObject object = mPreRender.getObject();
241
      int[][] angles = object.getBasicAngles();
242

    
243
      if( mCurrentAxis<angles.length && mCurrentRow<angles[mCurrentAxis].length )
244
        {
245
        computeCurrentSpeedInInchesPerSecond();
246
        int basic = angles[mCurrentAxis][mCurrentRow];
247
        int angle = object.computeNearestAngle(basic,mCurrentAngle, mCurrRotSpeed);
248
        mPreRender.finishRotation(angle);
249
        mPreRender.rememberMove(mCurrentAxis,mCurrentRow,angle);
250

    
251
        if( angle!=0 )
252
          {
253
          int realAngle = (angle*basic)/360;
254
          mInterface.onFinishRotation(mCurrentAxis,mCurrentRow,realAngle);
255
          }
256

    
257
        mContinuingRotation = false;
258
        mBeginningRotation  = false;
259
        mDragging           = true;
260
        }
261
      }
262

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

    
265
    private void continueRotation(float x, float y)
266
      {
267
      float dx = x-mStartRotX;
268
      float dy = y-mStartRotY;
269
      float alpha = dx*mAxis[0] + dy*mAxis[1];
270
      float x2 = dx - alpha*mAxis[0];
271
      float y2 = dy - alpha*mAxis[1];
272

    
273
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
274

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

    
278
      float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
279
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
280
      mPreRender.getObject().continueRotation(mCurrentAngle);
281

    
282
      addSpeedProbe(x2,y2);
283
      }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
    private void beginRotation(float x, float y)
288
      {
289
      mStartRotX = x;
290
      mStartRotY = y;
291

    
292
      TwistyObject object = mPreRender.getObject();
293
      int[] numLayers = object.getNumLayers();
294

    
295
      Static4D touchPoint = new Static4D(x, y, 0, 0);
296
      Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, mQuat);
297
      mTouchControl.newRotation(mBuffer,rotatedTouchPoint,mQuat);
298

    
299
      mCurrentAxis = mBuffer[0];
300
      mCurrentRow  = mBuffer[1];
301

    
302
      mTouchControl.getCastedRotAxis(mAxis,mQuat,mCurrentAxis);
303
      mRotationFactor = mTouchControl.returnRotationFactor(numLayers,mCurrentRow);
304

    
305
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
306

    
307
      mInterface.onBeginRotation();
308

    
309
      addSpeedProbe(x,y);
310

    
311
      mBeginningRotation = false;
312
      mContinuingRotation= true;
313
      }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
    private float getAngle(float x1, float y1, float x2, float y2)
318
      {
319
      return (float) Math.atan2(y1-y2, x1-x2);
320
      }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
    private void prepareDown(MotionEvent event)
325
      {
326
      mPointer1 = event.getPointerId(0);
327
      mX1 = event.getX() - mMoveX;
328
      mY1 = event.getY() + mMoveY;
329
      mPointer2 = INVALID_POINTER_ID;
330
      }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
    private void prepareMove(MotionEvent event)
335
      {
336
      int index1 = event.findPointerIndex(mPointer1);
337

    
338
      if( index1>=0 )
339
        {
340
        mX1 = event.getX(index1) - mMoveX;
341
        mY1 = event.getY(index1) + mMoveY;
342
        }
343

    
344
      int index2 = event.findPointerIndex(mPointer2);
345

    
346
      if( index2>=0 )
347
        {
348
        mX2 = event.getX(index2) - mMoveX;
349
        mY2 = event.getY(index2) + mMoveY;
350
        }
351
      }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
    private void prepareUp(MotionEvent event)
356
      {
357
      mPointer1 = INVALID_POINTER_ID;
358
      mPointer2 = INVALID_POINTER_ID;
359
      }
360

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

    
363
    private void prepareDown2(MotionEvent event)
364
      {
365
      int index = event.getActionIndex();
366

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

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

    
383
    private void prepareUp2(MotionEvent event)
384
      {
385
      int index = event.getActionIndex();
386

    
387
           if( index==event.findPointerIndex(mPointer1) ) mPointer1 = INVALID_POINTER_ID;
388
      else if( index==event.findPointerIndex(mPointer2) ) mPointer2 = INVALID_POINTER_ID;
389
      }
390

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

    
393
    private void actionMove(float x1, float y1, float x2, float y2)
394
      {
395
      float pX = mPointer1 != INVALID_POINTER_ID ? x1 : x2;
396
      float pY = mPointer1 != INVALID_POINTER_ID ? y1 : y2;
397

    
398
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
399
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
400

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

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
    private void actionDown(float x, float y)
425
      {
426
      mX = (x -  mScreenWidth*0.5f)/mScreenMin;
427
      mY = (mScreenHeight*0.5f - y)/mScreenMin;
428

    
429
      setUpDragOrRotate(true,mX,mY);
430
      }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
    private void actionUp()
435
      {
436
      if( mContinuingRotation )
437
        {
438
        finishRotation();
439
        }
440

    
441
      if( mLastMode==MODE_REPLACE ) mInterface.onReplaceModeUp();
442
      }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

    
446
    private void actionDown2(float x1, float y1, float x2, float y2)
447
      {
448
      mRotAngle = getAngle(x1,-y1, x2,-y2);
449
      mInitDistance = -1;
450

    
451
      mX = (x1 - mScreenWidth*0.5f )/mScreenMin;
452
      mY = (mScreenHeight*0.5f - y1)/mScreenMin;
453

    
454
      if( mBeginningRotation )
455
        {
456
        mContinuingRotation = false;
457
        mBeginningRotation  = false;
458
        mDragging           = true;
459
        }
460
      else if( mContinuingRotation )
461
        {
462
        finishRotation();
463
        }
464
      }
465

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

    
468
    private void actionUp2(boolean p1isUp, float x1, float y1, boolean p2isUp, float x2, float y2)
469
      {
470
      if( p1isUp )
471
        {
472
        mX = (x2 -  mScreenWidth*0.5f)/mScreenMin;
473
        mY = (mScreenHeight*0.5f - y2)/mScreenMin;
474
        }
475
      if( p2isUp )
476
        {
477
        mX = (x1 -  mScreenWidth*0.5f)/mScreenMin;
478
        mY = (mScreenHeight*0.5f - y1)/mScreenMin;
479
        }
480
      }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
    private void switchTouchControl(int oldMode, int newMode)
485
      {
486
      if( newMode==MODE_REPLACE )
487
        {
488
        if( mTouchControlBackup!=null )
489
          {
490
          TouchControl tmp = mTouchControlBackup;
491
          mTouchControlBackup = mTouchControl;
492
          mTouchControl = tmp;
493
          }
494
        else
495
          {
496
          mTouchControlBackup = mTouchControl;
497
          TwistyObject object = getObject();
498
          mTouchControl = new TouchControlShapeChanging(object);
499
          float ratio = object.getObjectRatio();
500
          mTouchControl.setObjectRatio(ratio);
501
          }
502
        }
503
      if( oldMode==MODE_REPLACE )
504
        {
505
        if( mTouchControlBackup!=null )
506
          {
507
          TouchControl tmp = mTouchControlBackup;
508
          mTouchControlBackup = mTouchControl;
509
          mTouchControl = tmp;
510
          }
511
        else
512
          {
513
          mTouchControlBackup = mTouchControl;
514
          TwistyObject object = getObject();
515
          mTouchControl = object.getTouchControl();
516
          float ratio = object.getObjectRatio();
517
          mTouchControl.setObjectRatio(ratio);
518
          }
519
        }
520
      }
521

    
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523

    
524
    void setTouchControl(TwistyObject object)
525
      {
526
      if( mLastMode!=MODE_REPLACE )  mTouchControl = object.getTouchControl();
527
      else                           mTouchControl = new TouchControlShapeChanging(object);
528
      }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531
// INTERNAL API (for AutomaticControl)
532
///////////////////////////////////////////////////////////////////////////////////////////////////
533

    
534
    public ObjectPreRender getPreRender()
535
      {
536
      return mPreRender;
537
      }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

    
541
    public ObjectLibInterface getInterface()
542
      {
543
      return mInterface;
544
      }
545

    
546
///////////////////////////////////////////////////////////////////////////////////////////////////
547
// PUBLIC API
548
///////////////////////////////////////////////////////////////////////////////////////////////////
549

    
550
    public ObjectControl(Activity act, ObjectLibInterface actioner)
551
      {
552
      mIsAutomatic = false;
553

    
554
      mBuffer = new int[2];
555
      mAxis   = new float[2];
556

    
557
      mCurrRotSpeed= 0.0f;
558
      mLastMode    = -1;
559
      mRotateOnCreation = false;
560

    
561
      mLastX = new float[NUM_SPEED_PROBES];
562
      mLastY = new float[NUM_SPEED_PROBES];
563
      mLastT = new long[NUM_SPEED_PROBES];
564
      mFirstIndex= 0;
565
      mLastIndex = 0;
566
      mMeshState =-1;
567
      mIconMode  =-1;
568

    
569
      DisplayMetrics dm = new DisplayMetrics();
570
      act.getWindowManager().getDefaultDisplay().getMetrics(dm);
571

    
572
      mDensity = dm.densityDpi;
573

    
574
      mPreRender = new ObjectPreRender(act,this,actioner);
575
      mInterface = actioner;
576
      }
577

    
578
///////////////////////////////////////////////////////////////////////////////////////////////////
579

    
580
    public void setRotateOnCreation(boolean rotate)
581
      {
582
      mRotateOnCreation = rotate;
583
      }
584

    
585
///////////////////////////////////////////////////////////////////////////////////////////////////
586

    
587
    public boolean getRotateOnCreation()
588
      {
589
      return mRotateOnCreation;
590
      }
591

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

    
594
    public TwistyObjectNode getNode()
595
      {
596
      return mObjectNode;
597
      }
598

    
599
///////////////////////////////////////////////////////////////////////////////////////////////////
600

    
601
    public void createNode(int width, int height)
602
      {
603
      if( mObjectNode==null ) mObjectNode = new TwistyObjectNode(width,height);
604
      }
605

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

    
608
    public void setScreenSize(int width, int height)
609
      {
610
      mScreenWidth = width;
611
      mScreenHeight= height;
612
      mScreenMin   = Math.min(width,height);
613

    
614
      if( mObjectNode!=null ) mObjectNode.setSize(width,height);
615

    
616
      TwistyObject object = mPreRender.getObject();
617

    
618
      if( object!=null )
619
        {
620
        object.setTexture();
621
        object.setNodeSize(mScreenMin);
622
        }
623
      }
624

    
625
///////////////////////////////////////////////////////////////////////////////////////////////////
626

    
627
    public void setObjectMove(int xmove, int ymove)
628
      {
629
      mMoveX = xmove;
630
      mMoveY = ymove;
631

    
632
      mPreRender.setMove(xmove,ymove);
633
      }
634

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

    
637
    public void setObjectScale(float scale)
638
      {
639
      mPreRender.setScale(scale);
640
      }
641

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

    
644
    public void onPause()
645
      {
646
      BlockController.onPause();
647
      }
648

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

    
651
    public void onResume()
652
      {
653
      mPointer1 = INVALID_POINTER_ID;
654
      mPointer2 = INVALID_POINTER_ID;
655

    
656
      unlock();
657

    
658
      BlockController.onResume();
659
      }
660

    
661
///////////////////////////////////////////////////////////////////////////////////////////////////
662

    
663
    public void rotateNow(Static4D quat)
664
      {
665
      mTemp.set(quat);
666
      mQuat.set(mTemp);
667
      }
668

    
669
///////////////////////////////////////////////////////////////////////////////////////////////////
670

    
671
    public void scaleNow(float scale)
672
      {
673
      mPreRender.getObject().setObjectRatioNow(scale,mObjectNode.getMinSize() );
674
      }
675

    
676
///////////////////////////////////////////////////////////////////////////////////////////////////
677

    
678
    public void setQuat()
679
      {
680
      mQuat.set(mTemp);
681
      }
682

    
683
///////////////////////////////////////////////////////////////////////////////////////////////////
684

    
685
    public Static4D getQuat()
686
      {
687
      return mQuat;
688
      }
689

    
690
///////////////////////////////////////////////////////////////////////////////////////////////////
691

    
692
    public void preRender()
693
      {
694
      mPreRender.preRender();
695
      }
696

    
697
///////////////////////////////////////////////////////////////////////////////////////////////////
698

    
699
    public void blockEverything(int place)
700
      {
701
      setLock(true);
702
      mPreRender.blockEverything(place);
703
      }
704

    
705
///////////////////////////////////////////////////////////////////////////////////////////////////
706

    
707
    public void blockTouch(int place)
708
      {
709
      setLock(true);
710
      mPreRender.blockTouch(place);
711
      }
712

    
713
///////////////////////////////////////////////////////////////////////////////////////////////////
714

    
715
    public void unblockEverything()
716
      {
717
      unsetLock();
718
      mPreRender.unblockEverything();
719
      }
720

    
721
///////////////////////////////////////////////////////////////////////////////////////////////////
722

    
723
    public void unblockTouch()
724
      {
725
      unsetLock();
726
      mPreRender.unblockTouch();
727
      }
728

    
729
///////////////////////////////////////////////////////////////////////////////////////////////////
730

    
731
    public void unblockUI()
732
      {
733
      mPreRender.unblockUI();
734
      }
735

    
736
///////////////////////////////////////////////////////////////////////////////////////////////////
737

    
738
    public boolean isTouchBlocked()
739
      {
740
      return mPreRender.isTouchBlocked();
741
      }
742

    
743
///////////////////////////////////////////////////////////////////////////////////////////////////
744

    
745
    public boolean isUINotBlocked()
746
      {
747
      return mPreRender.isUINotBlocked();
748
      }
749

    
750
///////////////////////////////////////////////////////////////////////////////////////////////////
751

    
752
    public void initializeObject(int[][] moves)
753
      {
754
      mPreRender.initializeObject(moves);
755
      }
756

    
757
///////////////////////////////////////////////////////////////////////////////////////////////////
758

    
759
    public void changeObject(int ordinal, int meshState, int iconMode, InputStream jsonStream, InputStream meshStream)
760
      {
761
      mPreRender.changeObject(ordinal, meshState, iconMode, jsonStream, meshStream);
762
      }
763

    
764
///////////////////////////////////////////////////////////////////////////////////////////////////
765

    
766
    public void changeIfDifferent(int ordinal, String oldName, int meshState, int iconMode, InputStream jsonStream, InputStream meshStream)
767
      {
768
      TwistyObject object = mPreRender.getObject();
769
      String newName = object==null ? "" : object.getShortName();
770

    
771
      if( !oldName.equals(newName) || mMeshState!=meshState || mIconMode!=iconMode )
772
        {
773
        mMeshState = meshState;
774
        mIconMode  = iconMode;
775
        mPreRender.changeObject(ordinal, meshState, iconMode, jsonStream, meshStream);
776
        }
777
      }
778

    
779
///////////////////////////////////////////////////////////////////////////////////////////////////
780
// if one or more fingers currently touch the screen, and we just pressed the 'scramble' button, do
781
// not scramble - otherwise a kind of a cheat is possible where user touches the screen, starts
782
// scrambling, then lifts the finger and the act of lifting screws the scrambling - no further
783
// scrambles take any effect!
784

    
785
    public void scrambleObject(int num)
786
      {
787
      if( !mBeginningRotation && !mContinuingRotation )
788
        {
789
        mPreRender.scrambleObject(num);
790
        }
791
      }
792

    
793
///////////////////////////////////////////////////////////////////////////////////////////////////
794

    
795
    public void solveObject()
796
      {
797
      mPreRender.solveObject();
798
      }
799

    
800
///////////////////////////////////////////////////////////////////////////////////////////////////
801

    
802
    public void solveOnly()
803
      {
804
      mPreRender.solveOnly();
805
      }
806

    
807
///////////////////////////////////////////////////////////////////////////////////////////////////
808

    
809
    public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, int duration)
810
      {
811
      mPreRender.addRotation(listener,axis,rowBitmap,angle,duration);
812
      }
813

    
814
///////////////////////////////////////////////////////////////////////////////////////////////////
815

    
816
    public void resetAllTextureMaps()
817
      {
818
      mPreRender.resetAllTextureMaps();
819
      }
820

    
821
///////////////////////////////////////////////////////////////////////////////////////////////////
822

    
823
    public TwistyObject getObject()
824
      {
825
      return mPreRender.getObject();
826
      }
827

    
828
///////////////////////////////////////////////////////////////////////////////////////////////////
829

    
830
    public void savePreferences(SharedPreferences.Editor editor)
831
      {
832
      mPreRender.savePreferences(editor);
833
      }
834

    
835
///////////////////////////////////////////////////////////////////////////////////////////////////
836

    
837
    public void restorePreferences(SharedPreferences preferences)
838
      {
839
      mPreRender.restorePreferences(preferences);
840
      }
841

    
842
///////////////////////////////////////////////////////////////////////////////////////////////////
843

    
844
    public boolean retLocked()
845
      {
846
      return mIsLocked;
847
      }
848

    
849
///////////////////////////////////////////////////////////////////////////////////////////////////
850

    
851
    public void toggleLock()
852
      {
853
      mIsLocked = !mIsLocked;
854
      }
855

    
856
///////////////////////////////////////////////////////////////////////////////////////////////////
857

    
858
    public void unlock()
859
      {
860
      mIsLocked = false;
861
      }
862

    
863
///////////////////////////////////////////////////////////////////////////////////////////////////
864

    
865
    public void setLock(boolean value)
866
      {
867
      mRemLocked = mIsLocked;
868
      mIsLocked = value;
869
      }
870

    
871
///////////////////////////////////////////////////////////////////////////////////////////////////
872

    
873
    public void unsetLock()
874
      {
875
      mIsLocked = mRemLocked;
876
      }
877

    
878
///////////////////////////////////////////////////////////////////////////////////////////////////
879

    
880
    public void setTextureMap(int cubit, int face, int newColor)
881
      {
882
      mPreRender.setTextureMap(cubit,face,newColor);
883
      }
884

    
885
///////////////////////////////////////////////////////////////////////////////////////////////////
886

    
887
    public boolean onTouchEvent(MotionEvent event, int mode)
888
      {
889
      if( mObjectNode==null ) return true;
890

    
891
      int action = event.getActionMasked();
892

    
893
      if( mode!=mLastMode)
894
        {
895
        switchTouchControl(mLastMode,mode);
896
        mLastMode = mode;
897
        }
898

    
899
      switch(action)
900
         {
901
         case MotionEvent.ACTION_DOWN        : prepareDown(event);
902
                                               actionDown(mX1, mY1);
903
                                               break;
904
         case MotionEvent.ACTION_MOVE        : prepareMove(event);
905
                                               actionMove(mX1, mY1, mX2, mY2);
906
                                               break;
907
         case MotionEvent.ACTION_UP          : prepareUp(event);
908
                                               actionUp();
909
                                               break;
910
         case MotionEvent.ACTION_POINTER_DOWN: prepareDown2(event);
911
                                               actionDown2(mX1, mY1, mX2, mY2);
912
                                               break;
913
         case MotionEvent.ACTION_POINTER_UP  : prepareUp2(event);
914
                                               boolean p1isUp = mPointer1==INVALID_POINTER_ID;
915
                                               boolean p2isUp = mPointer2==INVALID_POINTER_ID;
916
                                               actionUp2(p1isUp, mX1, mY1, p2isUp, mX2, mY2);
917
                                               break;
918
         }
919

    
920
      return true;
921
      }
922
}
923

    
(2-2/12)