Project

General

Profile

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

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

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
    private int mMeshState;
85

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

    
89
    private static boolean mForcedIconMode = false;
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

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

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

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

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

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

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

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

    
130
      long timeDiff = lastTime-firstTime;
131

    
132
      mLastIndex = 0;
133
      mFirstIndex= 0;
134

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

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

    
146
      return distInPixels/mDensity;
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

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

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

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

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

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

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

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

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

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

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

    
215
        mRotAngle = angleNow;
216

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

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

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

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

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

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

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

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

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

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

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

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

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

    
281
      addSpeedProbe(x2,y2);
282
      }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

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

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

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

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

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

    
304
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
305

    
306
      mInterface.onBeginRotation();
307

    
308
      addSpeedProbe(x,y);
309

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

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

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

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

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

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

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

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

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

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

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

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

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

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

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

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

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

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

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391

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

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

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

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422

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

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

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

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

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

    
443
///////////////////////////////////////////////////////////////////////////////////////////////////
444

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

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

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

    
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466

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

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

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

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

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

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

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

    
505
///////////////////////////////////////////////////////////////////////////////////////////////////
506

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

    
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513

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

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520
// PUBLIC API
521
///////////////////////////////////////////////////////////////////////////////////////////////////
522

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

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

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

    
533
      mLastX = new float[NUM_SPEED_PROBES];
534
      mLastY = new float[NUM_SPEED_PROBES];
535
      mLastT = new long[NUM_SPEED_PROBES];
536
      mFirstIndex= 0;
537
      mLastIndex = 0;
538
      mMeshState =-1;
539

    
540
      DisplayMetrics dm = new DisplayMetrics();
541
      act.getWindowManager().getDefaultDisplay().getMetrics(dm);
542

    
543
      mDensity = dm.densityDpi;
544

    
545
      mPreRender = new ObjectPreRender(act,this,actioner);
546
      mInterface = actioner;
547
      }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550

    
551
    public TwistyObjectNode getNode()
552
      {
553
      return mObjectNode;
554
      }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557

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

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564

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

    
571
      if( mObjectNode!=null ) mObjectNode.setSize(width,height);
572

    
573
      TwistyObject object = mPreRender.getObject();
574

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

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583

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

    
589
      mPreRender.setMove(xmove,ymove);
590
      }
591

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

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

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

    
601
    public void onPause()
602
      {
603
      BlockController.onPause();
604
      }
605

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

    
608
    public void onResume()
609
      {
610
      mPointer1 = INVALID_POINTER_ID;
611
      mPointer2 = INVALID_POINTER_ID;
612

    
613
      unlock();
614

    
615
      BlockController.onResume();
616
      }
617

    
618
///////////////////////////////////////////////////////////////////////////////////////////////////
619

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

    
626
///////////////////////////////////////////////////////////////////////////////////////////////////
627

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

    
633
///////////////////////////////////////////////////////////////////////////////////////////////////
634

    
635
    public void setQuat()
636
      {
637
      mQuat.set(mTemp);
638
      }
639

    
640
///////////////////////////////////////////////////////////////////////////////////////////////////
641

    
642
    public Static4D getQuat()
643
      {
644
      return mQuat;
645
      }
646

    
647
///////////////////////////////////////////////////////////////////////////////////////////////////
648

    
649
    public void preRender()
650
      {
651
      mPreRender.preRender();
652
      }
653

    
654
///////////////////////////////////////////////////////////////////////////////////////////////////
655

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

    
662
///////////////////////////////////////////////////////////////////////////////////////////////////
663

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

    
670
///////////////////////////////////////////////////////////////////////////////////////////////////
671

    
672
    public void unblockEverything()
673
      {
674
      unsetLock();
675
      mPreRender.unblockEverything();
676
      }
677

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

    
680
    public void unblockTouch()
681
      {
682
      unsetLock();
683
      mPreRender.unblockTouch();
684
      }
685

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

    
688
    public void unblockUI()
689
      {
690
      mPreRender.unblockUI();
691
      }
692

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

    
695
    public boolean isTouchBlocked()
696
      {
697
      return mPreRender.isTouchBlocked();
698
      }
699

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

    
702
    public boolean isUINotBlocked()
703
      {
704
      return mPreRender.isUINotBlocked();
705
      }
706

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

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

    
714
///////////////////////////////////////////////////////////////////////////////////////////////////
715

    
716
    public void changeObject(int ordinal, int meshState, InputStream jsonStream, InputStream meshStream)
717
      {
718
      mPreRender.changeObject(ordinal, meshState, jsonStream, meshStream);
719
      }
720

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

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

    
728
      if( old==null || old.ordinal() != ordinal || mMeshState!=meshState )
729
        {
730
        mMeshState = meshState;
731
        mPreRender.changeObject(ordinal, meshState, jsonStream, meshStream);
732
        }
733
      }
734

    
735
///////////////////////////////////////////////////////////////////////////////////////////////////
736

    
737
    public void scrambleObject(int num)
738
      {
739
      mPreRender.scrambleObject(num);
740
      }
741

    
742
///////////////////////////////////////////////////////////////////////////////////////////////////
743

    
744
    public void solveObject()
745
      {
746
      mPreRender.solveObject();
747
      }
748

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

    
751
    public void solveOnly()
752
      {
753
      mPreRender.solveOnly();
754
      }
755

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

    
758
    public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, int duration)
759
      {
760
      mPreRender.addRotation(listener,axis,rowBitmap,angle,duration);
761
      }
762

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

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

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

    
772
    public TwistyObject getObject()
773
      {
774
      return mPreRender.getObject();
775
      }
776

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

    
779
    public void savePreferences(SharedPreferences.Editor editor)
780
      {
781
      mPreRender.savePreferences(editor);
782
      }
783

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

    
786
    public void restorePreferences(SharedPreferences preferences)
787
      {
788
      mPreRender.restorePreferences(preferences);
789
      }
790

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

    
793
    public boolean retLocked()
794
      {
795
      return mIsLocked;
796
      }
797

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

    
800
    public void toggleLock()
801
      {
802
      mIsLocked = !mIsLocked;
803
      }
804

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

    
807
    public void unlock()
808
      {
809
      mIsLocked = false;
810
      }
811

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

    
814
    public void setLock(boolean value)
815
      {
816
      mRemLocked = mIsLocked;
817
      mIsLocked = value;
818
      }
819

    
820
///////////////////////////////////////////////////////////////////////////////////////////////////
821

    
822
    public void unsetLock()
823
      {
824
      mIsLocked = mRemLocked;
825
      }
826

    
827
///////////////////////////////////////////////////////////////////////////////////////////////////
828

    
829
    public void setTextureMap(int cubit, int face, int newColor)
830
      {
831
      mPreRender.setTextureMap(cubit,face,newColor);
832
      }
833

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

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

    
874
///////////////////////////////////////////////////////////////////////////////////////////////////
875

    
876
    public boolean onTouchEvent(MotionEvent event, int mode)
877
      {
878
      int action = event.getActionMasked();
879

    
880
      if( mode!=mLastMode)
881
        {
882
        switchTouchControl(mLastMode,mode);
883
        mLastMode = mode;
884
        }
885

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

    
907
      return true;
908
      }
909
}
910

    
(3-3/13)