Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ eaf46415

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.main;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.opengl.GLES30;
26
import android.opengl.GLSurfaceView;
27
import android.util.AttributeSet;
28
import android.util.DisplayMetrics;
29
import android.view.MotionEvent;
30

    
31
import com.google.firebase.crashlytics.FirebaseCrashlytics;
32

    
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.type.Static2D;
35
import org.distorted.library.type.Static4D;
36
import org.distorted.library.main.QuatHelper;
37

    
38
import org.distorted.objectlib.helpers.ObjectSurfaceView;
39
import org.distorted.objectlib.helpers.TwistyActivity;
40
import org.distorted.objectlib.main.ObjectPreRender;
41
import org.distorted.objectlib.main.TwistyObject;
42
import org.distorted.objectlib.main.Movement;
43

    
44
import org.distorted.screens.RubikScreenReady;
45
import org.distorted.screens.ScreenList;
46
import org.distorted.screens.RubikScreenPlay;
47
import org.distorted.screens.RubikScreenSolver;
48
import org.distorted.screens.RubikScreenSolving;
49
import org.distorted.solvers.SolverMain;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
public class RubikSurfaceView extends GLSurfaceView implements ObjectSurfaceView
54
{
55
    public static final int NUM_SPEED_PROBES = 10;
56
    public static final int INVALID_POINTER_ID = -1;
57

    
58
    public static final int MODE_ROTATE  = 0;
59
    public static final int MODE_DRAG    = 1;
60
    public static final int MODE_REPLACE = 2;
61

    
62
    // Moving the finger from the middle of the vertical screen to the right edge will rotate a
63
    // given face by SWIPING_SENSITIVITY/2 degrees.
64
    public final static int SWIPING_SENSITIVITY  = 240;
65
    // Moving the finger by 0.3 of an inch will start a Rotation.
66
    public final static float ROTATION_SENSITIVITY = 0.3f;
67

    
68
    private final Static4D CAMERA_POINT = new Static4D(0, 0, 0, 0);
69

    
70
    private RubikRenderer mRenderer;
71
    private ObjectPreRender mPreRender;
72
    private Movement mMovement;
73
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
74
    private int mScreenWidth, mScreenHeight, mScreenMin;
75

    
76
    private float mRotAngle, mInitDistance;
77
    private float mStartRotX, mStartRotY;
78
    private float mAxisX, mAxisY;
79
    private float mRotationFactor;
80
    private int mLastCubitColor, mLastCubitFace, mLastCubit;
81
    private int mCurrentAxis, mCurrentRow;
82
    private float mCurrentAngle, mCurrRotSpeed;
83
    private float[] mLastX;
84
    private float[] mLastY;
85
    private long[] mLastT;
86
    private int mFirstIndex, mLastIndex;
87
    private int mDensity;
88

    
89
    private int mPointer1, mPointer2;
90
    private float mX1, mY1, mX2, mY2, mX, mY;
91
    private boolean mIsAutomatic;
92

    
93
    private static final Static4D mQuat= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
94
    private static final Static4D mTemp= new Static4D(0,0,0,1);
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
    void setScreenSize(int width, int height)
99
      {
100
      mScreenWidth = width;
101
      mScreenHeight= height;
102

    
103
      mScreenMin = Math.min(width, height);
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
    boolean isVertical()
109
      {
110
      return mScreenHeight>mScreenWidth;
111
      }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
    RubikRenderer getRenderer()
116
      {
117
      return mRenderer;
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
    ObjectPreRender getPreRender()
123
      {
124
      return mPreRender;
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
// cast the 3D axis we are currently rotating along (which is already casted to the surface of the
129
// currently touched face AND converted into a 4D vector - fourth 0) to a 2D in-screen-surface axis
130

    
131
    private void computeCurrentAxis(Static4D axis)
132
      {
133
      Static4D result = QuatHelper.rotateVectorByQuat(axis, mQuat);
134

    
135
      mAxisX =result.get0();
136
      mAxisY =result.get1();
137

    
138
      float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
139
      mAxisX /= len;
140
      mAxisY /= len;
141
      }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
    private void addSpeedProbe(float x, float y)
146
      {
147
      long currTime = System.currentTimeMillis();
148
      boolean theSame = mLastIndex==mFirstIndex;
149

    
150
      mLastIndex++;
151
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
152

    
153
      mLastT[mLastIndex] = currTime;
154
      mLastX[mLastIndex] = x;
155
      mLastY[mLastIndex] = y;
156

    
157
      if( mLastIndex==mFirstIndex)
158
        {
159
        mFirstIndex++;
160
        if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
161
        }
162

    
163
      if( theSame )
164
        {
165
        mLastT[mFirstIndex] = currTime;
166
        mLastX[mFirstIndex] = x;
167
        mLastY[mFirstIndex] = y;
168
        }
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
    private void computeCurrentSpeedInInchesPerSecond()
174
      {
175
      long firstTime = mLastT[mFirstIndex];
176
      long lastTime  = mLastT[mLastIndex];
177
      float fX = mLastX[mFirstIndex];
178
      float fY = mLastY[mFirstIndex];
179
      float lX = mLastX[mLastIndex];
180
      float lY = mLastY[mLastIndex];
181

    
182
      long timeDiff = lastTime-firstTime;
183

    
184
      mLastIndex = 0;
185
      mFirstIndex= 0;
186

    
187
      mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX,fY,lX,lY)/timeDiff : 0;
188
      }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
    private float retFingerDragDistanceInInches(float xFrom, float yFrom, float xTo, float yTo)
193
      {
194
      float xDist = mScreenWidth*(xFrom-xTo);
195
      float yDist = mScreenHeight*(yFrom-yTo);
196
      float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
197

    
198
      return distInPixels/mDensity;
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
    private void setUpDragOrRotate(boolean down, float x, float y)
204
      {
205
      int mode = ScreenList.getMode();
206

    
207
      if( mode==MODE_DRAG )
208
        {
209
        mDragging           = true;
210
        mBeginningRotation  = false;
211
        mContinuingRotation = false;
212
        }
213
      else
214
        {
215
        TwistyObject object = mPreRender.getObject();
216
        CAMERA_POINT.set2( object==null ? 1.21f : object.getCameraDist() );
217

    
218
        Static4D touchPoint = new Static4D(x, y, 0, 0);
219
        Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, mQuat);
220
        Static4D rotatedCamera= QuatHelper.rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
221

    
222
        if( object!=null && mMovement!=null && mMovement.faceTouched(rotatedTouchPoint,rotatedCamera,object.getObjectRatio() ) )
223
          {
224
          mDragging           = false;
225
          mContinuingRotation = false;
226

    
227
          if( mode==MODE_ROTATE )
228
            {
229
            mBeginningRotation= !mPreRender.isTouchBlocked();
230
            }
231
          else if( mode==MODE_REPLACE )
232
            {
233
            mBeginningRotation= false;
234

    
235
            if( down )
236
              {
237
              RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
238
              mLastCubitFace = mMovement.getTouchedFace();
239
              float[] point = mMovement.getTouchedPoint3D();
240
              int color = solver.getCurrentColor();
241
              mLastCubit = object.getCubit(point);
242
              mPreRender.setTextureMap( mLastCubit, mLastCubitFace, color );
243
              mLastCubitColor = SolverMain.cubitIsLocked(object.getObjectType(), mLastCubit);
244
              }
245
            }
246
          }
247
        else
248
          {
249
          final RubikActivity act = (RubikActivity)getContext();
250
          final boolean locked= act.isLocked();
251
          mDragging           = (!locked || mIsAutomatic);
252
          mBeginningRotation  = false;
253
          mContinuingRotation = false;
254
          if( !mDragging ) reddenLockIcon(act);
255
          }
256
        }
257
      }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
    private void reddenLockIcon(RubikActivity act)
262
      {
263
      ScreenList curr = ScreenList.getCurrentScreen();
264

    
265
      if( curr==ScreenList.PLAY )
266
        {
267
        RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
268
        play.reddenLock(act);
269
        }
270
      else if( curr==ScreenList.READ )
271
        {
272
        RubikScreenReady read = (RubikScreenReady) ScreenList.READ.getScreenClass();
273
        read.reddenLock(act);
274
        }
275
      else if( curr==ScreenList.SOLV )
276
        {
277
        RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
278
        solv.reddenLock(act);
279
        }
280
      }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
    private void drag(float x, float y)
285
      {
286
      if( mPointer1!=INVALID_POINTER_ID && mPointer2!=INVALID_POINTER_ID)
287
        {
288
        float x2 = (mX2 - mScreenWidth*0.5f)/mScreenMin;
289
        float y2 = (mScreenHeight*0.5f - mY2)/mScreenMin;
290

    
291
        float angleNow = getAngle(x,y,x2,y2);
292
        float angleDiff = angleNow-mRotAngle;
293
        float sinA =-(float)Math.sin(angleDiff);
294
        float cosA = (float)Math.cos(angleDiff);
295

    
296
        Static4D dragQuat = QuatHelper.quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
297
        mTemp.set(dragQuat);
298

    
299
        mRotAngle = angleNow;
300

    
301
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
302
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
303
        mInitDistance = distNow;
304
        TwistyObject object = mPreRender.getObject();
305
        if( object!=null ) object.setObjectRatio(distQuot);
306
        }
307
      else
308
        {
309
        Static4D dragQuat = QuatHelper.quatMultiply(QuatHelper.quatFromDrag(mX-x,y-mY), mQuat);
310
        mTemp.set(dragQuat);
311
        }
312

    
313
      mPreRender.setQuatOnNextRender();
314
      mX = x;
315
      mY = y;
316
      }
317

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

    
320
    private void finishRotation()
321
      {
322
      computeCurrentSpeedInInchesPerSecond();
323
      int angle = mPreRender.getObject().computeNearestAngle(mCurrentAxis,mCurrentAngle, mCurrRotSpeed);
324
      mPreRender.finishRotation(angle);
325
      mPreRender.rememberMove(mCurrentAxis,mCurrentRow,angle);
326

    
327
      if( angle!=0 )
328
        {
329
        if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
330
          {
331
          RubikActivity act = (RubikActivity)getContext();
332
          RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
333
          solving.addMove(act, mCurrentAxis, mCurrentRow, angle);
334
          }
335
        if( ScreenList.getCurrentScreen()== ScreenList.PLAY )
336
          {
337
          RubikActivity act = (RubikActivity)getContext();
338
          RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
339
          play.addMove(act, mCurrentAxis, mCurrentRow, angle);
340
          }
341
        }
342

    
343
      mContinuingRotation = false;
344
      mBeginningRotation  = false;
345
      mDragging           = true;
346
      }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
    private void continueRotation(float x, float y)
351
      {
352
      float dx = x-mStartRotX;
353
      float dy = y-mStartRotY;
354
      float alpha = dx*mAxisX + dy*mAxisY;
355
      float x2 = dx - alpha*mAxisX;
356
      float y2 = dy - alpha*mAxisY;
357

    
358
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
359

    
360
      // we have the length of 1D vector 'angle', now the direction:
361
      float tmp = mAxisY==0 ? -mAxisX*y2 : mAxisY*x2;
362

    
363
      float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
364
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
365
      mPreRender.getObject().continueRotation(mCurrentAngle);
366

    
367
      addSpeedProbe(x2,y2);
368
      }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

    
372
    private void beginRotation(float x, float y)
373
      {
374
      mStartRotX = x;
375
      mStartRotY = y;
376

    
377
      TwistyObject object = mPreRender.getObject();
378
      int numLayers = object.getNumLayers();
379

    
380
      Static4D touchPoint2 = new Static4D(x, y, 0, 0);
381
      Static4D rotatedTouchPoint2= QuatHelper.rotateVectorByInvertedQuat(touchPoint2, mQuat);
382
      Static2D res = mMovement.newRotation(rotatedTouchPoint2,object.getObjectRatio());
383

    
384
      mCurrentAxis = (int)res.get0();
385
      mCurrentRow  = (int)res.get1();
386

    
387
      computeCurrentAxis( mMovement.getCastedRotAxis(mCurrentAxis) );
388
      mRotationFactor = mMovement.returnRotationFactor(numLayers,mCurrentRow);
389

    
390
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
391

    
392
      if( ScreenList.getCurrentScreen()== ScreenList.READ )
393
        {
394
        RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
395
        solving.resetElapsed();
396

    
397
        final RubikActivity act = (RubikActivity)getContext();
398

    
399
        act.runOnUiThread(new Runnable()
400
          {
401
          @Override
402
          public void run()
403
            {
404
            ScreenList.switchScreen( act, ScreenList.SOLV);
405
            }
406
          });
407
        }
408

    
409
      addSpeedProbe(x,y);
410

    
411
      mBeginningRotation = false;
412
      mContinuingRotation= true;
413
      }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
    private float getAngle(float x1, float y1, float x2, float y2)
418
      {
419
      return (float) Math.atan2(y1-y2, x1-x2);
420
      }
421

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

    
424
    void initialize()
425
      {
426
      mPointer1 = INVALID_POINTER_ID;
427
      mPointer2 = INVALID_POINTER_ID;
428
      }
429

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

    
432
    private void prepareDown(MotionEvent event)
433
      {
434
      mPointer1 = event.getPointerId(0);
435
      mX1 = event.getX();
436
      mY1 = event.getY();
437
      mPointer2 = INVALID_POINTER_ID;
438
      }
439

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

    
442
    private void prepareMove(MotionEvent event)
443
      {
444
      int index1 = event.findPointerIndex(mPointer1);
445

    
446
      if( index1>=0 )
447
        {
448
        mX1 = event.getX(index1);
449
        mY1 = event.getY(index1);
450
        }
451

    
452
      int index2 = event.findPointerIndex(mPointer2);
453

    
454
      if( index2>=0 )
455
        {
456
        mX2 = event.getX(index2);
457
        mY2 = event.getY(index2);
458
        }
459
      }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

    
463
    private void prepareUp(MotionEvent event)
464
      {
465
      mPointer1 = INVALID_POINTER_ID;
466
      mPointer2 = INVALID_POINTER_ID;
467
      }
468

    
469
///////////////////////////////////////////////////////////////////////////////////////////////////
470

    
471
    private void prepareDown2(MotionEvent event)
472
      {
473
      int index = event.getActionIndex();
474

    
475
      if( mPointer1==INVALID_POINTER_ID )
476
        {
477
        mPointer1 = event.getPointerId(index);
478
        mX1 = event.getX(index);
479
        mY1 = event.getY(index);
480
        }
481
      else if( mPointer2==INVALID_POINTER_ID )
482
        {
483
        mPointer2 = event.getPointerId(index);
484
        mX2 = event.getX(index);
485
        mY2 = event.getY(index);
486
        }
487
      }
488

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490

    
491
    private void prepareUp2(MotionEvent event)
492
      {
493
      int index = event.getActionIndex();
494

    
495
           if( index==event.findPointerIndex(mPointer1) ) mPointer1 = INVALID_POINTER_ID;
496
      else if( index==event.findPointerIndex(mPointer2) ) mPointer2 = INVALID_POINTER_ID;
497
      }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500
// PUBLIC API
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502

    
503
    public RubikSurfaceView(Context context, AttributeSet attrs)
504
      {
505
      super(context,attrs);
506

    
507
      if(!isInEditMode())
508
        {
509
        mIsAutomatic = false;
510

    
511
        mLastCubitColor = -1;
512
        mCurrRotSpeed   = 0.0f;
513

    
514
        mLastX = new float[NUM_SPEED_PROBES];
515
        mLastY = new float[NUM_SPEED_PROBES];
516
        mLastT = new long[NUM_SPEED_PROBES];
517
        mFirstIndex =0;
518
        mLastIndex  =0;
519

    
520
        mRenderer  = new RubikRenderer(this);
521
        mPreRender = new ObjectPreRender(this,new RubikObjectStateActioner());
522

    
523
        RubikActivity act = (RubikActivity)context;
524
        DisplayMetrics dm = new DisplayMetrics();
525
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
526

    
527
        mDensity = dm.densityDpi;
528

    
529
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
530

    
531
        try
532
          {
533
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
534
          int esVersion = configurationInfo.reqGlEsVersion>>16;
535
          setEGLContextClientVersion(esVersion);
536
          setRenderer(mRenderer);
537
          }
538
        catch(Exception ex)
539
          {
540
          act.OpenGLError();
541

    
542
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
543
          String version = GLES30.glGetString(GLES30.GL_VERSION);
544
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
545
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
546

    
547
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
548
          crashlytics.setCustomKey("GLSL Version"  , shading );
549
          crashlytics.setCustomKey("GL version"    , version );
550
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
551
          crashlytics.setCustomKey("GLSL renderer" , renderer);
552
          crashlytics.recordException(ex);
553
          }
554
        }
555
      }
556

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

    
559
    public void setQuat()
560
      {
561
      mQuat.set(mTemp);
562
      }
563

    
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565

    
566
    public Static4D getQuat()
567
      {
568
      return mQuat;
569
      }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572

    
573
    public void setMovement(Movement movement)
574
      {
575
      mMovement = movement;
576
      }
577

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

    
580
    public TwistyActivity getActivity()
581
      {
582
      return (TwistyActivity)getContext();
583
      }
584

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

    
587
    public DistortedScreen getScreen()
588
      {
589
      return mRenderer.getScreen();
590
      }
591

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

    
594
    public void prepareDown()
595
      {
596
      mIsAutomatic = true;
597
      mPointer1 = 0;
598
      mPointer2 = INVALID_POINTER_ID;
599
      }
600

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

    
603
    public void prepareDown2()
604
      {
605
      mPointer2 = 0;
606
      }
607

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

    
610
    public void prepareMove(float x1, float y1, float x2, float y2)
611
      {
612
      mX1 = x1;
613
      mY1 = y1;
614
      mX2 = x2;
615
      mY2 = y2;
616
      }
617

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

    
620
    public void prepareUp()
621
      {
622
      mIsAutomatic = false;
623
      mPointer1 = INVALID_POINTER_ID;
624
      mPointer2 = INVALID_POINTER_ID;
625
      }
626

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

    
629
    public void actionMove(float x1, float y1, float x2, float y2)
630
      {
631
      float pX = mPointer1 != INVALID_POINTER_ID ? x1 : x2;
632
      float pY = mPointer1 != INVALID_POINTER_ID ? y1 : y2;
633

    
634
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
635
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
636

    
637
      if( mBeginningRotation )
638
        {
639
        if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
640
          {
641
          beginRotation(x,y);
642
          }
643
        }
644
      else if( mContinuingRotation )
645
        {
646
        continueRotation(x,y);
647
        }
648
      else if( mDragging )
649
        {
650
        drag(x,y);
651
        }
652
      else
653
        {
654
        setUpDragOrRotate(false,x,y);
655
        }
656
      }
657

    
658
///////////////////////////////////////////////////////////////////////////////////////////////////
659

    
660
    public void actionDown(float x, float y)
661
      {
662
      mX = (x -  mScreenWidth*0.5f)/mScreenMin;
663
      mY = (mScreenHeight*0.5f - y)/mScreenMin;
664

    
665
      setUpDragOrRotate(true,mX,mY);
666
      }
667

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

    
670
    public void actionUp()
671
      {
672
      if( mContinuingRotation )
673
        {
674
        finishRotation();
675
        }
676

    
677
      if( mLastCubitColor>=0 )
678
        {
679
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
680
        mLastCubitColor = -1;
681
        }
682
      }
683

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

    
686
    public void actionDown2(float x1, float y1, float x2, float y2)
687
      {
688
      mRotAngle = getAngle(x1,-y1, x2,-y2);
689
      mInitDistance = -1;
690

    
691
      mX = (x1 - mScreenWidth*0.5f )/mScreenMin;
692
      mY = (mScreenHeight*0.5f - y1)/mScreenMin;
693

    
694
      if( mBeginningRotation )
695
        {
696
        mContinuingRotation = false;
697
        mBeginningRotation  = false;
698
        mDragging           = true;
699
        }
700
      else if( mContinuingRotation )
701
        {
702
        finishRotation();
703
        }
704
      }
705

    
706
///////////////////////////////////////////////////////////////////////////////////////////////////
707

    
708
    public void actionUp2(boolean p1isUp, float x1, float y1, boolean p2isUp, float x2, float y2)
709
      {
710
      if( p1isUp )
711
        {
712
        mX = (x2 -  mScreenWidth*0.5f)/mScreenMin;
713
        mY = (mScreenHeight*0.5f - y2)/mScreenMin;
714
        }
715
      if( p2isUp )
716
        {
717
        mX = (x1 -  mScreenWidth*0.5f)/mScreenMin;
718
        mY = (mScreenHeight*0.5f - y1)/mScreenMin;
719
        }
720
      }
721

    
722
///////////////////////////////////////////////////////////////////////////////////////////////////
723

    
724
    @Override
725
    public boolean onTouchEvent(MotionEvent event)
726
      {
727
      int action = event.getActionMasked();
728

    
729
      switch(action)
730
         {
731
         case MotionEvent.ACTION_DOWN        : prepareDown(event);
732
                                               actionDown(mX1, mY1);
733
                                               break;
734
         case MotionEvent.ACTION_MOVE        : prepareMove(event);
735
                                               actionMove(mX1, mY1, mX2, mY2);
736
                                               break;
737
         case MotionEvent.ACTION_UP          : prepareUp(event);
738
                                               actionUp();
739
                                               break;
740
         case MotionEvent.ACTION_POINTER_DOWN: prepareDown2(event);
741
                                               actionDown2(mX1, mY1, mX2, mY2);
742
                                               break;
743
         case MotionEvent.ACTION_POINTER_UP  : prepareUp2(event);
744
                                               boolean p1isUp = mPointer1==INVALID_POINTER_ID;
745
                                               boolean p2isUp = mPointer2==INVALID_POINTER_ID;
746
                                               actionUp2(p1isUp, mX1, mY1, p2isUp, mX2, mY2);
747
                                               break;
748
         }
749

    
750
      return true;
751
      }
752
}
753

    
(4-4/4)