Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectControl.java @ 11fa413d

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 NUM_SPEED_PROBES = 10;
43
    public static final int INVALID_POINTER_ID = -1;
44

    
45
    public static final int MODE_ROTATE  = 0;
46
    public static final int MODE_DRAG    = 1;
47
    public static final int MODE_REPLACE = 2;
48

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

    
55
    private final Static4D CAMERA_POINT = new Static4D(0, 0, 0, 0);
56

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

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

    
77
    private int mPointer1, mPointer2;
78
    private float mX1, mY1, mX2, mY2, mX, mY;
79
    private final boolean mIsAutomatic;
80

    
81
    private boolean mIsLocked, mRemLocked;
82
    private final int[] mBuffer;
83
    private final float[] mAxis;
84

    
85
    private static final Static4D mQuat= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
86
    private static final Static4D mTemp= new Static4D(0,0,0,1);
87

    
88
    private static boolean mForcedIconMode = false;
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
    private void addSpeedProbe(float x, float y)
93
      {
94
      long currTime = System.currentTimeMillis();
95
      boolean theSame = mLastIndex==mFirstIndex;
96

    
97
      mLastIndex++;
98
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
99

    
100
      mLastT[mLastIndex] = currTime;
101
      mLastX[mLastIndex] = x;
102
      mLastY[mLastIndex] = y;
103

    
104
      if( mLastIndex==mFirstIndex)
105
        {
106
        mFirstIndex++;
107
        if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
108
        }
109

    
110
      if( theSame )
111
        {
112
        mLastT[mFirstIndex] = currTime;
113
        mLastX[mFirstIndex] = x;
114
        mLastY[mFirstIndex] = y;
115
        }
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

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

    
129
      long timeDiff = lastTime-firstTime;
130

    
131
      mLastIndex = 0;
132
      mFirstIndex= 0;
133

    
134
      mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX,fY,lX,lY)/timeDiff : 0;
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
    private float retFingerDragDistanceInInches(float xFrom, float yFrom, float xTo, float yTo)
140
      {
141
      float xDist = mScreenWidth*(xFrom-xTo);
142
      float yDist = mScreenHeight*(yFrom-yTo);
143
      float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
144

    
145
      return distInPixels/mDensity;
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
    private void replaceMode(boolean down)
151
      {
152
      mBeginningRotation= false;
153

    
154
      if( down )
155
        {
156
        int cubit = mTouchControl.getTouchedCubit();
157
        int face  = mTouchControl.getTouchedCubitFace();
158
        mInterface.onReplaceModeDown(cubit,face);
159
        }
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

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

    
179
        if( mTouchControl!=null && mTouchControl.objectTouched(rotatedTouchPoint,rotatedCamera) )
180
          {
181
          mDragging           = false;
182
          mContinuingRotation = false;
183

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

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

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

    
206
        float angleNow = getAngle(x,y,x2,y2);
207
        float angleDiff = angleNow-mRotAngle;
208
        float sinA =-(float)Math.sin(angleDiff);
209
        float cosA = (float)Math.cos(angleDiff);
210

    
211
        Static4D dragQuat = QuatHelper.quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
212
        mTemp.set(dragQuat);
213

    
214
        mRotAngle = angleNow;
215

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

    
228
      mPreRender.setQuatOnNextRender();
229
      mX = x;
230
      mY = y;
231
      }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
    private void finishRotation()
236
      {
237
      TwistyObject object = mPreRender.getObject();
238
      int[] angles = object.getBasicAngle();
239

    
240
      if( mCurrentAxis<angles.length )
241
        {
242
        computeCurrentSpeedInInchesPerSecond();
243

    
244
        int angle = object.computeNearestAngle(mCurrentAxis,mCurrentAngle, mCurrRotSpeed);
245
        mPreRender.finishRotation(angle);
246
        mPreRender.rememberMove(mCurrentAxis,mCurrentRow,angle);
247

    
248
        if( angle!=0 )
249
          {
250
          int basicAngle= angles[mCurrentAxis];
251
          int realAngle = (angle*basicAngle)/360;
252
          mInterface.onFinishRotation(mCurrentAxis,mCurrentRow,realAngle);
253
          }
254

    
255
        mContinuingRotation = false;
256
        mBeginningRotation  = false;
257
        mDragging           = true;
258
        }
259
      }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

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

    
271
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
272

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

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

    
280
      addSpeedProbe(x2,y2);
281
      }
282

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

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

    
290
      TwistyObject object = mPreRender.getObject();
291
      int[] numLayers = object.getNumLayers();
292

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

    
297
      mCurrentAxis = mBuffer[0];
298
      mCurrentRow  = mBuffer[1];
299

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

    
303
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
304

    
305
      mInterface.onBeginRotation();
306

    
307
      addSpeedProbe(x,y);
308

    
309
      mBeginningRotation = false;
310
      mContinuingRotation= true;
311
      }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

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

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

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

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

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

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

    
342
      int index2 = event.findPointerIndex(mPointer2);
343

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

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

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

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

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

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

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

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

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

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

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

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

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

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

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

    
427
      setUpDragOrRotate(true,mX,mY);
428
      }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

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

    
439
      if( mLastMode==MODE_REPLACE ) mInterface.onReplaceModeUp();
440
      }
441

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

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

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

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

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

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

    
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481

    
482
    void setTouchControl(TwistyObject object)
483
      {
484
      if( mLastMode!=MODE_REPLACE )  mTouchControl = object.getTouchControl();
485
      else                           mTouchControl = new TouchControlShapeChanging(object);
486
      }
487

    
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489
// INTERNAL API (for AutomaticControl)
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491

    
492
    public ObjectPreRender getPreRender()
493
      {
494
      return mPreRender;
495
      }
496

    
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498

    
499
    public ObjectLibInterface getInterface()
500
      {
501
      return mInterface;
502
      }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
    public static void setIconMode(boolean mode)
507
      {
508
      mForcedIconMode = mode;
509
      }
510

    
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512

    
513
    public static boolean isInIconMode()
514
      {
515
      return mForcedIconMode;
516
      }
517

    
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519
// PUBLIC API
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521

    
522
    public ObjectControl(Activity act, ObjectLibInterface actioner)
523
      {
524
      mIsAutomatic = false;
525

    
526
      mBuffer = new int[2];
527
      mAxis   = new float[2];
528

    
529
      mCurrRotSpeed= 0.0f;
530
      mLastMode    = -1;
531

    
532
      mLastX = new float[NUM_SPEED_PROBES];
533
      mLastY = new float[NUM_SPEED_PROBES];
534
      mLastT = new long[NUM_SPEED_PROBES];
535
      mFirstIndex =0;
536
      mLastIndex  =0;
537

    
538
      DisplayMetrics dm = new DisplayMetrics();
539
      act.getWindowManager().getDefaultDisplay().getMetrics(dm);
540

    
541
      mDensity = dm.densityDpi;
542

    
543
      mPreRender = new ObjectPreRender(act,this,actioner);
544
      mInterface = actioner;
545
      }
546

    
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548

    
549
    public TwistyObjectNode getNode()
550
      {
551
      return mObjectNode;
552
      }
553

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555

    
556
    public void createNode(int width, int height)
557
      {
558
      if( mObjectNode==null ) mObjectNode = new TwistyObjectNode(width,height);
559
      }
560

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562

    
563
    public void setScreenSize(int width, int height)
564
      {
565
      mScreenWidth = width;
566
      mScreenHeight= height;
567
      mScreenMin   = Math.min(width,height);
568

    
569
      if( mObjectNode!=null ) mObjectNode.setSize(width,height);
570

    
571
      TwistyObject object = mPreRender.getObject();
572

    
573
      if( object!=null )
574
        {
575
        object.setTexture();
576
        object.setNodeSize(mScreenMin);
577
        }
578
      }
579

    
580
///////////////////////////////////////////////////////////////////////////////////////////////////
581

    
582
    public void setObjectMove(int xmove, int ymove)
583
      {
584
      mMoveX = xmove;
585
      mMoveY = ymove;
586

    
587
      mPreRender.setMove(xmove,ymove);
588
      }
589

    
590
///////////////////////////////////////////////////////////////////////////////////////////////////
591

    
592
    public void setObjectScale(float scale)
593
      {
594
      mPreRender.setScale(scale);
595
      }
596

    
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598

    
599
    public void onPause()
600
      {
601
      BlockController.onPause();
602
      }
603

    
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605

    
606
    public void onResume()
607
      {
608
      mPointer1 = INVALID_POINTER_ID;
609
      mPointer2 = INVALID_POINTER_ID;
610

    
611
      unlock();
612

    
613
      BlockController.onResume();
614
      }
615

    
616
///////////////////////////////////////////////////////////////////////////////////////////////////
617

    
618
    public void rotateNow(Static4D quat)
619
      {
620
      mTemp.set(quat);
621
      mQuat.set(mTemp);
622
      }
623

    
624
///////////////////////////////////////////////////////////////////////////////////////////////////
625

    
626
    public void scaleNow(float scale)
627
      {
628
      mPreRender.getObject().setObjectRatioNow(scale,mObjectNode.getMinSize() );
629
      }
630

    
631
///////////////////////////////////////////////////////////////////////////////////////////////////
632

    
633
    public void setQuat()
634
      {
635
      mQuat.set(mTemp);
636
      }
637

    
638
///////////////////////////////////////////////////////////////////////////////////////////////////
639

    
640
    public Static4D getQuat()
641
      {
642
      return mQuat;
643
      }
644

    
645
///////////////////////////////////////////////////////////////////////////////////////////////////
646

    
647
    public void preRender()
648
      {
649
      mPreRender.preRender();
650
      }
651

    
652
///////////////////////////////////////////////////////////////////////////////////////////////////
653

    
654
    public void blockEverything(int place)
655
      {
656
      setLock(true);
657
      mPreRender.blockEverything(place);
658
      }
659

    
660
///////////////////////////////////////////////////////////////////////////////////////////////////
661

    
662
    public void blockTouch(int place)
663
      {
664
      setLock(true);
665
      mPreRender.blockTouch(place);
666
      }
667

    
668
///////////////////////////////////////////////////////////////////////////////////////////////////
669

    
670
    public void unblockEverything()
671
      {
672
      unsetLock();
673
      mPreRender.unblockEverything();
674
      }
675

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

    
678
    public void unblockTouch()
679
      {
680
      unsetLock();
681
      mPreRender.unblockTouch();
682
      }
683

    
684
///////////////////////////////////////////////////////////////////////////////////////////////////
685

    
686
    public void unblockUI()
687
      {
688
      mPreRender.unblockUI();
689
      }
690

    
691
///////////////////////////////////////////////////////////////////////////////////////////////////
692

    
693
    public boolean isTouchBlocked()
694
      {
695
      return mPreRender.isTouchBlocked();
696
      }
697

    
698
///////////////////////////////////////////////////////////////////////////////////////////////////
699

    
700
    public boolean isUINotBlocked()
701
      {
702
      return mPreRender.isUINotBlocked();
703
      }
704

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

    
707
    public void initializeObject(int[][] moves)
708
      {
709
      mPreRender.initializeObject(moves);
710
      }
711

    
712
///////////////////////////////////////////////////////////////////////////////////////////////////
713

    
714
    public void changeObject(int ordinal, InputStream jsonStream, InputStream meshStream)
715
      {
716
      mPreRender.changeObject(ordinal, jsonStream, meshStream);
717
      }
718

    
719
///////////////////////////////////////////////////////////////////////////////////////////////////
720

    
721
    public void changeIfDifferent(int ordinal, InputStream jsonStream, InputStream meshStream)
722
      {
723
      TwistyObject object = mPreRender.getObject();
724
      ObjectType old = object==null ? null : object.getObjectType();
725

    
726
      if( old==null || old.ordinal() != ordinal )
727
        {
728
        mPreRender.changeObject(ordinal, jsonStream, meshStream);
729
        }
730
      }
731

    
732
///////////////////////////////////////////////////////////////////////////////////////////////////
733

    
734
    public void scrambleObject(int num)
735
      {
736
      mPreRender.scrambleObject(num);
737
      }
738

    
739
///////////////////////////////////////////////////////////////////////////////////////////////////
740

    
741
    public void solveObject()
742
      {
743
      mPreRender.solveObject();
744
      }
745

    
746
///////////////////////////////////////////////////////////////////////////////////////////////////
747

    
748
    public void solveOnly()
749
      {
750
      mPreRender.solveOnly();
751
      }
752

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

    
755
    public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, int duration)
756
      {
757
      mPreRender.addRotation(listener,axis,rowBitmap,angle,duration);
758
      }
759

    
760
///////////////////////////////////////////////////////////////////////////////////////////////////
761

    
762
    public void resetAllTextureMaps()
763
      {
764
      mPreRender.resetAllTextureMaps();
765
      }
766

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

    
769
    public TwistyObject getObject()
770
      {
771
      return mPreRender.getObject();
772
      }
773

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

    
776
    public void savePreferences(SharedPreferences.Editor editor)
777
      {
778
      mPreRender.savePreferences(editor);
779
      }
780

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

    
783
    public void restorePreferences(SharedPreferences preferences)
784
      {
785
      mPreRender.restorePreferences(preferences);
786
      }
787
///////////////////////////////////////////////////////////////////////////////////////////////////
788

    
789
    public boolean retLocked()
790
      {
791
      return mIsLocked;
792
      }
793

    
794
///////////////////////////////////////////////////////////////////////////////////////////////////
795

    
796
    public void toggleLock()
797
      {
798
      mIsLocked = !mIsLocked;
799
      }
800

    
801
///////////////////////////////////////////////////////////////////////////////////////////////////
802

    
803
    public void unlock()
804
      {
805
      mIsLocked = false;
806
      }
807

    
808
///////////////////////////////////////////////////////////////////////////////////////////////////
809

    
810
    public void setLock(boolean value)
811
      {
812
      mRemLocked = mIsLocked;
813
      mIsLocked = value;
814
      }
815

    
816
///////////////////////////////////////////////////////////////////////////////////////////////////
817

    
818
    public void unsetLock()
819
      {
820
      mIsLocked = mRemLocked;
821
      }
822

    
823
///////////////////////////////////////////////////////////////////////////////////////////////////
824

    
825
    public void setTextureMap(int cubit, int face, int newColor)
826
      {
827
      mPreRender.setTextureMap(cubit,face,newColor);
828
      }
829

    
830
///////////////////////////////////////////////////////////////////////////////////////////////////
831

    
832
    private void switchTouchControl(int oldMode, int newMode)
833
      {
834
      if( newMode==MODE_REPLACE )
835
        {
836
        if( mTouchControlBackup!=null )
837
          {
838
          TouchControl tmp = mTouchControlBackup;
839
          mTouchControlBackup = mTouchControl;
840
          mTouchControl = tmp;
841
          }
842
        else
843
          {
844
          mTouchControlBackup = mTouchControl;
845
          TwistyObject object = getObject();
846
          mTouchControl = new TouchControlShapeChanging(object);
847
          float ratio = object.getObjectRatio();
848
          mTouchControl.setObjectRatio(ratio);
849
          }
850
        }
851
      if( oldMode==MODE_REPLACE )
852
        {
853
        if( mTouchControlBackup!=null )
854
          {
855
          TouchControl tmp = mTouchControlBackup;
856
          mTouchControlBackup = mTouchControl;
857
          mTouchControl = tmp;
858
          }
859
        else
860
          {
861
          mTouchControlBackup = mTouchControl;
862
          TwistyObject object = getObject();
863
          mTouchControl = object.getTouchControl();
864
          float ratio = object.getObjectRatio();
865
          mTouchControl.setObjectRatio(ratio);
866
          }
867
        }
868
      }
869

    
870
///////////////////////////////////////////////////////////////////////////////////////////////////
871

    
872
    public boolean onTouchEvent(MotionEvent event, int mode)
873
      {
874
      int action = event.getActionMasked();
875

    
876
      if( mode!=mLastMode)
877
        {
878
        switchTouchControl(mLastMode,mode);
879
        mLastMode = mode;
880
        }
881

    
882
      switch(action)
883
         {
884
         case MotionEvent.ACTION_DOWN        : prepareDown(event);
885
                                               actionDown(mX1, mY1);
886
                                               break;
887
         case MotionEvent.ACTION_MOVE        : prepareMove(event);
888
                                               actionMove(mX1, mY1, mX2, mY2);
889
                                               break;
890
         case MotionEvent.ACTION_UP          : prepareUp(event);
891
                                               actionUp();
892
                                               break;
893
         case MotionEvent.ACTION_POINTER_DOWN: prepareDown2(event);
894
                                               actionDown2(mX1, mY1, mX2, mY2);
895
                                               break;
896
         case MotionEvent.ACTION_POINTER_UP  : prepareUp2(event);
897
                                               boolean p1isUp = mPointer1==INVALID_POINTER_ID;
898
                                               boolean p2isUp = mPointer2==INVALID_POINTER_ID;
899
                                               actionUp2(p1isUp, mX1, mY1, p2isUp, mX2, mY2);
900
                                               break;
901
         }
902

    
903
      return true;
904
      }
905
}
906

    
(2-2/12)