Project

General

Profile

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

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

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 Movement mMovement;
69
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
70
    private int mScreenWidth, mScreenHeight, mScreenMin;
71

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

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

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

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

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

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

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

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

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

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

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

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

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

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

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

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

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

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

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

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

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

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

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

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

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

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

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

    
199
      long timeDiff = lastTime-firstTime;
200

    
201
      mLastIndex = 0;
202
      mFirstIndex= 0;
203

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

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

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

    
215
      return distInPixels/mDensity;
216
      }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

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

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

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

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

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

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

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

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

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

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

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

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

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

    
316
        mRotAngle = angleNow;
317

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

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

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

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

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

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

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

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

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

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

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

    
384
      addSpeedProbe(x2,y2);
385
      }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

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

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

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

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

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

    
407
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
408

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

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

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

    
426
      addSpeedProbe(x,y);
427

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

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

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

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

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

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448

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

    
457
///////////////////////////////////////////////////////////////////////////////////////////////////
458

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

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

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

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

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479

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

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

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

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

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507

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

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

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517
// PUBLIC API
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519

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

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

    
528
        mLastCubitColor = -1;
529
        mCurrRotSpeed   = 0.0f;
530

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

    
537
        mRenderer  = new RubikRenderer(this);
538
        mPreRender = new RubikPreRender(this);
539

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

    
544
        mDensity = dm.densityDpi;
545

    
546
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
547

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

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

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

    
574
///////////////////////////////////////////////////////////////////////////////////////////////////
575

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

    
583
///////////////////////////////////////////////////////////////////////////////////////////////////
584

    
585
    public void prepareDown2()
586
      {
587
      mPointer2 = 0;
588
      }
589

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

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

    
600
///////////////////////////////////////////////////////////////////////////////////////////////////
601

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

    
609
///////////////////////////////////////////////////////////////////////////////////////////////////
610

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

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

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

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

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

    
647
      setUpDragOrRotate(true,mX,mY);
648
      }
649

    
650
///////////////////////////////////////////////////////////////////////////////////////////////////
651

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

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

    
666
///////////////////////////////////////////////////////////////////////////////////////////////////
667

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

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

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

    
688
///////////////////////////////////////////////////////////////////////////////////////////////////
689

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

    
704
///////////////////////////////////////////////////////////////////////////////////////////////////
705

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

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

    
732
      return true;
733
      }
734
}
735

    
(4-4/4)