Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ 1cd323dd

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.type.Static2D;
34
import org.distorted.library.type.Static4D;
35
import org.distorted.library.main.QuatHelper;
36

    
37
import org.distorted.objectlib.main.TwistyObject;
38
import org.distorted.objectlib.main.Movement;
39

    
40
import org.distorted.screens.RubikScreenReady;
41
import org.distorted.screens.ScreenList;
42
import org.distorted.screens.RubikScreenPlay;
43
import org.distorted.screens.RubikScreenSolver;
44
import org.distorted.screens.RubikScreenSolving;
45
import org.distorted.solvers.SolverMain;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
public class RubikSurfaceView extends GLSurfaceView
50
{
51
    public static final int NUM_SPEED_PROBES = 10;
52
    public static final int INVALID_POINTER_ID = -1;
53

    
54
    public static final int MODE_ROTATE  = 0;
55
    public static final int MODE_DRAG    = 1;
56
    public static final int MODE_REPLACE = 2;
57

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

    
64
    private final Static4D CAMERA_POINT = new Static4D(0, 0, 0, 0);
65

    
66
    private RubikRenderer mRenderer;
67
    private RubikPreRender mPreRender;
68
    private RubikObjectStateActioner mActioner;
69
    private Movement mMovement;
70
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
71
    private int mScreenWidth, mScreenHeight, mScreenMin;
72

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

    
86
    private int mPointer1, mPointer2;
87
    private float mX1, mY1, mX2, mY2, mX, mY;
88
    private boolean mIsAutomatic;
89

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

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

    
95
    void setScreenSize(int width, int height)
96
      {
97
      mScreenWidth = width;
98
      mScreenHeight= height;
99

    
100
      mScreenMin = Math.min(width, height);
101
      }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
    boolean isVertical()
106
      {
107
      return mScreenHeight>mScreenWidth;
108
      }
109

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

    
112
    RubikRenderer getRenderer()
113
      {
114
      return mRenderer;
115
      }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
    RubikPreRender getPreRender()
120
      {
121
      return mPreRender;
122
      }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
    void setQuat()
127
      {
128
      mQuat.set(mTemp);
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
    Static4D getQuat()
134
      {
135
      return mQuat;
136
      }
137

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

    
140
    void setMovement(Movement movement)
141
      {
142
      mMovement = movement;
143
      }
144

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

    
149
    private void computeCurrentAxis(Static4D axis)
150
      {
151
      Static4D result = QuatHelper.rotateVectorByQuat(axis, mQuat);
152

    
153
      mAxisX =result.get0();
154
      mAxisY =result.get1();
155

    
156
      float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
157
      mAxisX /= len;
158
      mAxisY /= len;
159
      }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
    private void addSpeedProbe(float x, float y)
164
      {
165
      long currTime = System.currentTimeMillis();
166
      boolean theSame = mLastIndex==mFirstIndex;
167

    
168
      mLastIndex++;
169
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
170

    
171
      mLastT[mLastIndex] = currTime;
172
      mLastX[mLastIndex] = x;
173
      mLastY[mLastIndex] = y;
174

    
175
      if( mLastIndex==mFirstIndex)
176
        {
177
        mFirstIndex++;
178
        if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
179
        }
180

    
181
      if( theSame )
182
        {
183
        mLastT[mFirstIndex] = currTime;
184
        mLastX[mFirstIndex] = x;
185
        mLastY[mFirstIndex] = y;
186
        }
187
      }
188

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

    
191
    private void computeCurrentSpeedInInchesPerSecond()
192
      {
193
      long firstTime = mLastT[mFirstIndex];
194
      long lastTime  = mLastT[mLastIndex];
195
      float fX = mLastX[mFirstIndex];
196
      float fY = mLastY[mFirstIndex];
197
      float lX = mLastX[mLastIndex];
198
      float lY = mLastY[mLastIndex];
199

    
200
      long timeDiff = lastTime-firstTime;
201

    
202
      mLastIndex = 0;
203
      mFirstIndex= 0;
204

    
205
      mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX,fY,lX,lY)/timeDiff : 0;
206
      }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
    private float retFingerDragDistanceInInches(float xFrom, float yFrom, float xTo, float yTo)
211
      {
212
      float xDist = mScreenWidth*(xFrom-xTo);
213
      float yDist = mScreenHeight*(yFrom-yTo);
214
      float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
215

    
216
      return distInPixels/mDensity;
217
      }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
    private void setUpDragOrRotate(boolean down, float x, float y)
222
      {
223
      int mode = ScreenList.getMode();
224

    
225
      if( mode==MODE_DRAG )
226
        {
227
        mDragging           = true;
228
        mBeginningRotation  = false;
229
        mContinuingRotation = false;
230
        }
231
      else
232
        {
233
        TwistyObject object = mPreRender.getObject();
234
        CAMERA_POINT.set2( object==null ? 1.21f : object.getCameraDist() );
235

    
236
        Static4D touchPoint = new Static4D(x, y, 0, 0);
237
        Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, mQuat);
238
        Static4D rotatedCamera= QuatHelper.rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
239

    
240
        if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint,rotatedCamera,object.getObjectRatio() ) )
241
          {
242
          mDragging           = false;
243
          mContinuingRotation = false;
244

    
245
          if( mode==MODE_ROTATE )
246
            {
247
            mBeginningRotation= !mPreRender.isTouchBlocked();
248
            }
249
          else if( mode==MODE_REPLACE )
250
            {
251
            mBeginningRotation= false;
252

    
253
            if( down )
254
              {
255
              RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
256
              mLastCubitFace = mMovement.getTouchedFace();
257
              float[] point = mMovement.getTouchedPoint3D();
258
              int color = solver.getCurrentColor();
259
              mLastCubit = object.getCubit(point);
260
              mPreRender.setTextureMap( mLastCubit, mLastCubitFace, color );
261
              mLastCubitColor = SolverMain.cubitIsLocked(object.getObjectType(), mLastCubit);
262
              }
263
            }
264
          }
265
        else
266
          {
267
          final RubikActivity act = (RubikActivity)getContext();
268
          final boolean locked= act.isLocked();
269
          mDragging           = (!locked || mIsAutomatic);
270
          mBeginningRotation  = false;
271
          mContinuingRotation = false;
272
          if( !mDragging ) reddenLockIcon(act);
273
          }
274
        }
275
      }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
    private void reddenLockIcon(RubikActivity act)
280
      {
281
      ScreenList curr = ScreenList.getCurrentScreen();
282

    
283
      if( curr==ScreenList.PLAY )
284
        {
285
        RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
286
        play.reddenLock(act);
287
        }
288
      else if( curr==ScreenList.READ )
289
        {
290
        RubikScreenReady read = (RubikScreenReady) ScreenList.READ.getScreenClass();
291
        read.reddenLock(act);
292
        }
293
      else if( curr==ScreenList.SOLV )
294
        {
295
        RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
296
        solv.reddenLock(act);
297
        }
298
      }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
    private void drag(float x, float y)
303
      {
304
      if( mPointer1!=INVALID_POINTER_ID && mPointer2!=INVALID_POINTER_ID)
305
        {
306
        float x2 = (mX2 - mScreenWidth*0.5f)/mScreenMin;
307
        float y2 = (mScreenHeight*0.5f - mY2)/mScreenMin;
308

    
309
        float angleNow = getAngle(x,y,x2,y2);
310
        float angleDiff = angleNow-mRotAngle;
311
        float sinA =-(float)Math.sin(angleDiff);
312
        float cosA = (float)Math.cos(angleDiff);
313

    
314
        Static4D dragQuat = QuatHelper.quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
315
        mTemp.set(dragQuat);
316

    
317
        mRotAngle = angleNow;
318

    
319
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
320
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
321
        mInitDistance = distNow;
322
        TwistyObject object = mPreRender.getObject();
323
        if( object!=null ) object.setObjectRatio(distQuot);
324
        }
325
      else
326
        {
327
        Static4D dragQuat = QuatHelper.quatMultiply(QuatHelper.quatFromDrag(mX-x,y-mY), mQuat);
328
        mTemp.set(dragQuat);
329
        }
330

    
331
      mPreRender.setQuatOnNextRender();
332
      mX = x;
333
      mY = y;
334
      }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
    private void finishRotation()
339
      {
340
      computeCurrentSpeedInInchesPerSecond();
341
      int angle = mPreRender.getObject().computeNearestAngle(mCurrentAxis,mCurrentAngle, mCurrRotSpeed);
342
      mPreRender.finishRotation(angle);
343
      mPreRender.rememberMove(mCurrentAxis,mCurrentRow,angle);
344

    
345
      if( angle!=0 )
346
        {
347
        if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
348
          {
349
          RubikActivity act = (RubikActivity)getContext();
350
          RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
351
          solving.addMove(act, mCurrentAxis, mCurrentRow, angle);
352
          }
353
        if( ScreenList.getCurrentScreen()== ScreenList.PLAY )
354
          {
355
          RubikActivity act = (RubikActivity)getContext();
356
          RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
357
          play.addMove(act, mCurrentAxis, mCurrentRow, angle);
358
          }
359
        }
360

    
361
      mContinuingRotation = false;
362
      mBeginningRotation  = false;
363
      mDragging           = true;
364
      }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
    private void continueRotation(float x, float y)
369
      {
370
      float dx = x-mStartRotX;
371
      float dy = y-mStartRotY;
372
      float alpha = dx*mAxisX + dy*mAxisY;
373
      float x2 = dx - alpha*mAxisX;
374
      float y2 = dy - alpha*mAxisY;
375

    
376
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
377

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

    
381
      float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
382
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
383
      mPreRender.getObject().continueRotation(mCurrentAngle);
384

    
385
      addSpeedProbe(x2,y2);
386
      }
387

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389

    
390
    private void beginRotation(float x, float y)
391
      {
392
      mStartRotX = x;
393
      mStartRotY = y;
394

    
395
      TwistyObject object = mPreRender.getObject();
396
      int numLayers = object.getNumLayers();
397

    
398
      Static4D touchPoint2 = new Static4D(x, y, 0, 0);
399
      Static4D rotatedTouchPoint2= QuatHelper.rotateVectorByInvertedQuat(touchPoint2, mQuat);
400
      Static2D res = mMovement.newRotation(rotatedTouchPoint2,object.getObjectRatio());
401

    
402
      mCurrentAxis = (int)res.get0();
403
      mCurrentRow  = (int)res.get1();
404

    
405
      computeCurrentAxis( mMovement.getCastedRotAxis(mCurrentAxis) );
406
      mRotationFactor = mMovement.returnRotationFactor(numLayers,mCurrentRow);
407

    
408
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
409

    
410
      if( ScreenList.getCurrentScreen()== ScreenList.READ )
411
        {
412
        RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
413
        solving.resetElapsed();
414

    
415
        final RubikActivity act = (RubikActivity)getContext();
416

    
417
        act.runOnUiThread(new Runnable()
418
          {
419
          @Override
420
          public void run()
421
            {
422
            ScreenList.switchScreen( act, ScreenList.SOLV);
423
            }
424
          });
425
        }
426

    
427
      addSpeedProbe(x,y);
428

    
429
      mBeginningRotation = false;
430
      mContinuingRotation= true;
431
      }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

    
435
    private float getAngle(float x1, float y1, float x2, float y2)
436
      {
437
      return (float) Math.atan2(y1-y2, x1-x2);
438
      }
439

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

    
442
    void initialize()
443
      {
444
      mPointer1 = INVALID_POINTER_ID;
445
      mPointer2 = INVALID_POINTER_ID;
446
      }
447

    
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449

    
450
    private void prepareDown(MotionEvent event)
451
      {
452
      mPointer1 = event.getPointerId(0);
453
      mX1 = event.getX();
454
      mY1 = event.getY();
455
      mPointer2 = INVALID_POINTER_ID;
456
      }
457

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

    
460
    private void prepareMove(MotionEvent event)
461
      {
462
      int index1 = event.findPointerIndex(mPointer1);
463

    
464
      if( index1>=0 )
465
        {
466
        mX1 = event.getX(index1);
467
        mY1 = event.getY(index1);
468
        }
469

    
470
      int index2 = event.findPointerIndex(mPointer2);
471

    
472
      if( index2>=0 )
473
        {
474
        mX2 = event.getX(index2);
475
        mY2 = event.getY(index2);
476
        }
477
      }
478

    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480

    
481
    private void prepareUp(MotionEvent event)
482
      {
483
      mPointer1 = INVALID_POINTER_ID;
484
      mPointer2 = INVALID_POINTER_ID;
485
      }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488

    
489
    private void prepareDown2(MotionEvent event)
490
      {
491
      int index = event.getActionIndex();
492

    
493
      if( mPointer1==INVALID_POINTER_ID )
494
        {
495
        mPointer1 = event.getPointerId(index);
496
        mX1 = event.getX(index);
497
        mY1 = event.getY(index);
498
        }
499
      else if( mPointer2==INVALID_POINTER_ID )
500
        {
501
        mPointer2 = event.getPointerId(index);
502
        mX2 = event.getX(index);
503
        mY2 = event.getY(index);
504
        }
505
      }
506

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508

    
509
    private void prepareUp2(MotionEvent event)
510
      {
511
      int index = event.getActionIndex();
512

    
513
           if( index==event.findPointerIndex(mPointer1) ) mPointer1 = INVALID_POINTER_ID;
514
      else if( index==event.findPointerIndex(mPointer2) ) mPointer2 = INVALID_POINTER_ID;
515
      }
516

    
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518
// PUBLIC API
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520

    
521
    public RubikSurfaceView(Context context, AttributeSet attrs)
522
      {
523
      super(context,attrs);
524

    
525
      if(!isInEditMode())
526
        {
527
        mIsAutomatic = false;
528

    
529
        mLastCubitColor = -1;
530
        mCurrRotSpeed   = 0.0f;
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
        mActioner  = new RubikObjectStateActioner();
539
        mRenderer  = new RubikRenderer(this);
540
        mPreRender = new RubikPreRender(this,mActioner);
541

    
542
        RubikActivity act = (RubikActivity)context;
543
        DisplayMetrics dm = new DisplayMetrics();
544
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
545

    
546
        mDensity = dm.densityDpi;
547

    
548
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
549

    
550
        try
551
          {
552
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
553
          int esVersion = configurationInfo.reqGlEsVersion>>16;
554
          setEGLContextClientVersion(esVersion);
555
          setRenderer(mRenderer);
556
          }
557
        catch(Exception ex)
558
          {
559
          act.OpenGLError();
560

    
561
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
562
          String version = GLES30.glGetString(GLES30.GL_VERSION);
563
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
564
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
565

    
566
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
567
          crashlytics.setCustomKey("GLSL Version"  , shading );
568
          crashlytics.setCustomKey("GL version"    , version );
569
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
570
          crashlytics.setCustomKey("GLSL renderer" , renderer);
571
          crashlytics.recordException(ex);
572
          }
573
        }
574
      }
575

    
576
///////////////////////////////////////////////////////////////////////////////////////////////////
577

    
578
    public void prepareDown()
579
      {
580
      mIsAutomatic = true;
581
      mPointer1 = 0;
582
      mPointer2 = INVALID_POINTER_ID;
583
      }
584

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

    
587
    public void prepareDown2()
588
      {
589
      mPointer2 = 0;
590
      }
591

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

    
594
    public void prepareMove(float x1, float y1, float x2, float y2)
595
      {
596
      mX1 = x1;
597
      mY1 = y1;
598
      mX2 = x2;
599
      mY2 = y2;
600
      }
601

    
602
///////////////////////////////////////////////////////////////////////////////////////////////////
603

    
604
    public void prepareUp()
605
      {
606
      mIsAutomatic = false;
607
      mPointer1 = INVALID_POINTER_ID;
608
      mPointer2 = INVALID_POINTER_ID;
609
      }
610

    
611
///////////////////////////////////////////////////////////////////////////////////////////////////
612

    
613
    public void actionMove(float x1, float y1, float x2, float y2)
614
      {
615
      float pX = mPointer1 != INVALID_POINTER_ID ? x1 : x2;
616
      float pY = mPointer1 != INVALID_POINTER_ID ? y1 : y2;
617

    
618
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
619
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
620

    
621
      if( mBeginningRotation )
622
        {
623
        if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
624
          {
625
          beginRotation(x,y);
626
          }
627
        }
628
      else if( mContinuingRotation )
629
        {
630
        continueRotation(x,y);
631
        }
632
      else if( mDragging )
633
        {
634
        drag(x,y);
635
        }
636
      else
637
        {
638
        setUpDragOrRotate(false,x,y);
639
        }
640
      }
641

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

    
644
    public void actionDown(float x, float y)
645
      {
646
      mX = (x -  mScreenWidth*0.5f)/mScreenMin;
647
      mY = (mScreenHeight*0.5f - y)/mScreenMin;
648

    
649
      setUpDragOrRotate(true,mX,mY);
650
      }
651

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

    
654
    public void actionUp()
655
      {
656
      if( mContinuingRotation )
657
        {
658
        finishRotation();
659
        }
660

    
661
      if( mLastCubitColor>=0 )
662
        {
663
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
664
        mLastCubitColor = -1;
665
        }
666
      }
667

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

    
670
    public void actionDown2(float x1, float y1, float x2, float y2)
671
      {
672
      mRotAngle = getAngle(x1,-y1, x2,-y2);
673
      mInitDistance = -1;
674

    
675
      mX = (x1 - mScreenWidth*0.5f )/mScreenMin;
676
      mY = (mScreenHeight*0.5f - y1)/mScreenMin;
677

    
678
      if( mBeginningRotation )
679
        {
680
        mContinuingRotation = false;
681
        mBeginningRotation  = false;
682
        mDragging           = true;
683
        }
684
      else if( mContinuingRotation )
685
        {
686
        finishRotation();
687
        }
688
      }
689

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

    
692
    public void actionUp2(boolean p1isUp, float x1, float y1, boolean p2isUp, float x2, float y2)
693
      {
694
      if( p1isUp )
695
        {
696
        mX = (x2 -  mScreenWidth*0.5f)/mScreenMin;
697
        mY = (mScreenHeight*0.5f - y2)/mScreenMin;
698
        }
699
      if( p2isUp )
700
        {
701
        mX = (x1 -  mScreenWidth*0.5f)/mScreenMin;
702
        mY = (mScreenHeight*0.5f - y1)/mScreenMin;
703
        }
704
      }
705

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

    
708
    @Override
709
    public boolean onTouchEvent(MotionEvent event)
710
      {
711
      int action = event.getActionMasked();
712

    
713
      switch(action)
714
         {
715
         case MotionEvent.ACTION_DOWN        : prepareDown(event);
716
                                               actionDown(mX1, mY1);
717
                                               break;
718
         case MotionEvent.ACTION_MOVE        : prepareMove(event);
719
                                               actionMove(mX1, mY1, mX2, mY2);
720
                                               break;
721
         case MotionEvent.ACTION_UP          : prepareUp(event);
722
                                               actionUp();
723
                                               break;
724
         case MotionEvent.ACTION_POINTER_DOWN: prepareDown2(event);
725
                                               actionDown2(mX1, mY1, mX2, mY2);
726
                                               break;
727
         case MotionEvent.ACTION_POINTER_UP  : prepareUp2(event);
728
                                               boolean p1isUp = mPointer1==INVALID_POINTER_ID;
729
                                               boolean p2isUp = mPointer2==INVALID_POINTER_ID;
730
                                               actionUp2(p1isUp, mX1, mY1, p2isUp, mX2, mY2);
731
                                               break;
732
         }
733

    
734
      return true;
735
      }
736
}
737

    
(5-5/5)