Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ 82f42eeb

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.GLSurfaceView;
26
import android.util.AttributeSet;
27
import android.util.DisplayMetrics;
28
import android.view.MotionEvent;
29

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

    
32
import org.distorted.library.type.Static2D;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35
import org.distorted.objects.RubikObject;
36
import org.distorted.objects.RubikObjectMovement;
37
import org.distorted.solvers.SolverMain;
38
import org.distorted.states.RubikState;
39
import org.distorted.states.RubikStatePlay;
40
import org.distorted.states.RubikStateSolver;
41
import org.distorted.states.RubikStateSolving;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class RubikSurfaceView extends GLSurfaceView
46
{
47
    private static final int NUM_SPEED_PROBES = 10;
48
    private static final int INVALID_POINTER_ID = -1;
49

    
50
    public static final int MODE_ROTATE  = 0;
51
    public static final int MODE_DRAG    = 1;
52
    public static final int MODE_REPLACE = 2;
53

    
54
    // Moving the finger from the middle of the vertical screen to the right edge will rotate a
55
    // given face by SWIPING_SENSITIVITY/2 degrees.
56
    private final static int SWIPING_SENSITIVITY  = 240;
57
    // Moving the finger by 0.3 of an inch will start a Rotation.
58
    private final static float ROTATION_SENSITIVITY = 0.3f;
59

    
60
    // Where did we get this sqrt(3)/2 ? From the (default, i.e. 60 degrees - see InternalOutputSurface!)
61
    // FOV of the projection matrix of the Node onto the Screen.
62
    // Take a look how the CAMERA_POINT is used in onTouchEvent - (x,y) there are expressed in sort of
63
    // 'half-NDC' coordinates i.e. they range from +0.5 to -0.5; thus CAMERA_POINT also needs to be
64
    // in 'half-NDC'. Since in this coordinate system the height of the screen is equal to 1, then the
65
    // Z-distance from the center of the object to the camera is equal to (scrHeight/2)/tan(FOV/2) =
66
    // 0.5/tan(30) = sqrt(3)/2.
67
    // Why is the Z-distance between the camera and the object equal to (scrHeight/2)/tan(FOV/2)?
68
    // Because of the way the View part of the ModelView matrix is constructed in EffectQueueMatrix.send().
69
    private final Static4D CAMERA_POINT = new Static4D(0, 0, (float)Math.sqrt(3)*0.5f, 0);
70

    
71
    private RubikRenderer mRenderer;
72
    private RubikPreRender mPreRender;
73
    private RubikObjectMovement mMovement;
74
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
75
    private int mScreenWidth, mScreenHeight, mScreenMin;
76

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

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

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

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

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

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

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

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

    
121
    RubikPreRender getPreRender()
122
      {
123
      return mPreRender;
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
    void setQuat()
129
      {
130
      mQuat.set(mTemp);
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
    Static4D getQuat()
136
      {
137
      return mQuat;
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
    void setMovement(RubikObjectMovement movement)
143
      {
144
      mMovement = movement;
145
      }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
    private Static4D quatFromDrag(float dragX, float dragY)
150
      {
151
      float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
152
      float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
153
      float axisZ = 0;
154
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
155

    
156
      if( axisL>0 )
157
        {
158
        axisX /= axisL;
159
        axisY /= axisL;
160
        axisZ /= axisL;
161

    
162
        float ratio = axisL;
163
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
164

    
165
        float cosA = (float)Math.cos(Math.PI*ratio);
166
        float sinA = (float)Math.sqrt(1-cosA*cosA);
167

    
168
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
169
        }
170

    
171
      return new Static4D(0f, 0f, 0f, 1f);
172
      }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
// cast the 3D axis we are currently rotating along to the 2D in-screen-surface axis
176

    
177
    private void computeCurrentAxis(Static3D axis)
178
      {
179
      Static4D axis4D = new Static4D(axis.get0(), axis.get1(), axis.get2(), 0);
180
      Static4D result = rotateVectorByQuat(axis4D, mQuat);
181

    
182
      mAxisX =result.get0();
183
      mAxisY =result.get1();
184

    
185
      float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
186
      mAxisX /= len;
187
      mAxisY /= len;
188
      }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
// return quat1*quat2
192

    
193
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
194
      {
195
      float qx = quat1.get0();
196
      float qy = quat1.get1();
197
      float qz = quat1.get2();
198
      float qw = quat1.get3();
199

    
200
      float rx = quat2.get0();
201
      float ry = quat2.get1();
202
      float rz = quat2.get2();
203
      float rw = quat2.get3();
204

    
205
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
206
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
207
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
208
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
209

    
210
      return new Static4D(tx,ty,tz,tw);
211
      }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
215

    
216
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
217
      {
218
      float qx = quat.get0();
219
      float qy = quat.get1();
220
      float qz = quat.get2();
221
      float qw = quat.get3();
222

    
223
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
224
      Static4D tmp = quatMultiply(quat,vector);
225

    
226
      return quatMultiply(tmp,quatInverted);
227
      }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
231

    
232
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
233
      {
234
      float qx = quat.get0();
235
      float qy = quat.get1();
236
      float qz = quat.get2();
237
      float qw = quat.get3();
238

    
239
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
240
      Static4D tmp = quatMultiply(quatInverted,vector);
241

    
242
      return quatMultiply(tmp,quat);
243
      }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
    private void addSpeedProbe(float x, float y)
248
      {
249
      long currTime = System.currentTimeMillis();
250
      boolean theSame = mLastIndex==mFirstIndex;
251

    
252
      mLastIndex++;
253
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
254

    
255
      mLastT[mLastIndex] = currTime;
256
      mLastX[mLastIndex] = x;
257
      mLastY[mLastIndex] = y;
258

    
259
      if( mLastIndex==mFirstIndex)
260
        {
261
        mFirstIndex++;
262
        if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
263
        }
264

    
265
      if( theSame )
266
        {
267
        mLastT[mFirstIndex] = currTime;
268
        mLastX[mFirstIndex] = x;
269
        mLastY[mFirstIndex] = y;
270
        }
271
      }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
    private void computeCurrentSpeedInInchesPerSecond()
276
      {
277
      long firstTime = mLastT[mFirstIndex];
278
      long lastTime  = mLastT[mLastIndex];
279
      float fX = mLastX[mFirstIndex];
280
      float fY = mLastY[mFirstIndex];
281
      float lX = mLastX[mLastIndex];
282
      float lY = mLastY[mLastIndex];
283

    
284
      long timeDiff = lastTime-firstTime;
285

    
286
      mLastIndex = 0;
287
      mFirstIndex= 0;
288

    
289
      mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX,fY,lX,lY)/timeDiff : 0;
290
      }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
    private float retFingerDragDistanceInInches(float xFrom, float yFrom, float xTo, float yTo)
295
      {
296
      float xDist = mScreenWidth*(xFrom-xTo);
297
      float yDist = mScreenHeight*(yFrom-yTo);
298
      float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
299

    
300
      return distInPixels/mDensity;
301
      }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
    private void setUpDragOrRotate(boolean down, float x, float y)
306
      {
307
      int mode = RubikState.getMode();
308

    
309
      if( mode==MODE_DRAG )
310
        {
311
        mDragging           = true;
312
        mBeginningRotation  = false;
313
        mContinuingRotation = false;
314
        }
315
      else
316
        {
317
        Static4D touchPoint1 = new Static4D(x, y, 0, 0);
318
        Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuat);
319
        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
320

    
321
        if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
322
          {
323
          mDragging           = false;
324
          mContinuingRotation = false;
325

    
326
          if( mode==MODE_ROTATE )
327
            {
328
            mBeginningRotation= mPreRender.canRotate();
329
            }
330
          else if( mode==MODE_REPLACE )
331
            {
332
            mBeginningRotation= false;
333

    
334
            if( down )
335
              {
336
              RubikStateSolver solver = (RubikStateSolver) RubikState.SVER.getStateClass();
337
              mLastCubitFace = mMovement.getTouchedFace();
338
              float[] point = mMovement.getTouchedPoint3D();
339
              int color = solver.getCurrentColor();
340
              RubikObject object = mPreRender.getObject();
341
              mLastCubit = object.getCubit(point);
342
              mPreRender.setTextureMap( mLastCubit, mLastCubitFace, color );
343
              mLastCubitColor = SolverMain.cubitIsLocked(object.getObjectList(), object.getSize(), mLastCubit);
344
              }
345
            }
346
          }
347
        else
348
          {
349
          mDragging           = true;
350
          mBeginningRotation  = false;
351
          mContinuingRotation = false;
352
          }
353
        }
354
      }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
    private void drag(MotionEvent event, float x, float y)
359
      {
360
      if( mPtrID1!=INVALID_POINTER_ID && mPtrID2!=INVALID_POINTER_ID)
361
        {
362
        int pointer = event.findPointerIndex(mPtrID2);
363
        float pX,pY;
364

    
365
        try
366
          {
367
          pX = event.getX(pointer);
368
          pY = event.getY(pointer);
369
          }
370
        catch(IllegalArgumentException ex)
371
          {
372
          mPtrID1=INVALID_POINTER_ID;
373
          mPtrID2=INVALID_POINTER_ID;
374

    
375
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
376
          crashlytics.setCustomKey("DragError", "pointer="+pointer );
377
          crashlytics.recordException(ex);
378

    
379
          return;
380
          }
381

    
382
        float x2 = (pX - mScreenWidth*0.5f)/mScreenMin;
383
        float y2 = (mScreenHeight*0.5f -pY)/mScreenMin;
384

    
385
        float angleNow = getAngle(x,y,x2,y2);
386
        float angleDiff = angleNow-mRotAngle;
387
        float sinA =-(float)Math.sin(angleDiff);
388
        float cosA = (float)Math.cos(angleDiff);
389

    
390
        Static4D dragQuat = quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
391
        mTemp.set(dragQuat);
392

    
393
        mRotAngle = angleNow;
394

    
395
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
396
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
397
        mInitDistance = distNow;
398

    
399
        RubikObject object = mPreRender.getObject();
400
        object.setObjectRatio(distQuot);
401
        }
402
      else
403
        {
404
        Static4D dragQuat = quatMultiply(quatFromDrag(mX-x,y-mY), mQuat);
405
        mTemp.set(dragQuat);
406
        }
407

    
408
      mPreRender.setQuatOnNextRender();
409
      mX = x;
410
      mY = y;
411
      }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
    private void finishRotation()
416
      {
417
      computeCurrentSpeedInInchesPerSecond();
418
      int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
419
      mPreRender.finishRotation(angle);
420

    
421
      if( angle!=0 )
422
        {
423
        if( RubikState.getCurrentState()==RubikState.SOLV )
424
          {
425
          RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
426
          solving.addMove(mCurrentAxis, mCurrentRow, angle);
427
          }
428
        if( RubikState.getCurrentState()==RubikState.PLAY )
429
          {
430
          RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
431
          play.addMove(mCurrentAxis, mCurrentRow, angle);
432
          }
433
        }
434

    
435
      mContinuingRotation = false;
436
      mBeginningRotation  = false;
437
      mDragging           = true;
438
      }
439

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

    
442
    private void continueRotation(float x, float y)
443
      {
444
      float dx = x-mStartRotX;
445
      float dy = y-mStartRotY;
446
      float alpha = dx*mAxisX + dy*mAxisY;
447
      float x2 = dx - alpha*mAxisX;
448
      float y2 = dy - alpha*mAxisY;
449

    
450
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
451

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

    
455
      float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
456
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
457
      mPreRender.getObject().continueRotation(mCurrentAngle);
458

    
459
      addSpeedProbe(x2,y2);
460
      }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

    
464
    private void beginRotation(float x, float y)
465
      {
466
      mStartRotX = x;
467
      mStartRotY = y;
468

    
469
      Static4D touchPoint2 = new Static4D(x, y, 0, 0);
470
      Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuat);
471

    
472
      Static2D res = mMovement.newRotation(rotatedTouchPoint2);
473
      RubikObject object = mPreRender.getObject();
474

    
475
      mCurrentAxis = (int)res.get0();
476
      float offset = res.get1();
477
      mCurrentRow = (int)(object.returnMultiplier()*offset);
478
      computeCurrentAxis( object.getRotationAxis()[mCurrentAxis] );
479
      mRotationFactor = object.returnRotationFactor(offset);
480

    
481
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
482

    
483
      if( RubikState.getCurrentState()==RubikState.READ )
484
        {
485
        RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
486
        solving.resetElapsed();
487

    
488
        final RubikActivity act = (RubikActivity)getContext();
489

    
490
        act.runOnUiThread(new Runnable()
491
          {
492
          @Override
493
          public void run()
494
            {
495
            RubikState.switchState( act, RubikState.SOLV);
496
            }
497
          });
498
        }
499

    
500
      addSpeedProbe(x,y);
501

    
502
      mBeginningRotation = false;
503
      mContinuingRotation= true;
504
      }
505

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

    
508
    private float getAngle(float x1, float y1, float x2, float y2)
509
      {
510
      return (float) Math.atan2(y1-y2, x1-x2);
511
      }
512

    
513
///////////////////////////////////////////////////////////////////////////////////////////////////
514

    
515
    private void actionMove(MotionEvent event)
516
      {
517
      int pointer = event.findPointerIndex(mPtrID1 != INVALID_POINTER_ID ? mPtrID1:mPtrID2);
518

    
519
      if( pointer<0 ) return;
520

    
521
      float pX = event.getX(pointer);
522
      float pY = event.getY(pointer);
523

    
524
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
525
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
526

    
527
      if( mBeginningRotation )
528
        {
529
        if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
530
          {
531
          beginRotation(x,y);
532
          }
533
        }
534
      else if( mContinuingRotation )
535
        {
536
        continueRotation(x,y);
537
        }
538
      else if( mDragging )
539
        {
540
        drag(event,x,y);
541
        }
542
      else
543
        {
544
        setUpDragOrRotate(false,x,y);
545
        }
546
      }
547

    
548
///////////////////////////////////////////////////////////////////////////////////////////////////
549

    
550
    private void actionDown(MotionEvent event)
551
      {
552
      mPtrID1 = event.getPointerId(0);
553

    
554
      float x = event.getX();
555
      float y = event.getY();
556

    
557
      mX = (x - mScreenWidth*0.5f)/mScreenMin;
558
      mY = (mScreenHeight*0.5f -y)/mScreenMin;
559

    
560
      setUpDragOrRotate(true,mX,mY);
561
      }
562

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

    
565
    private void actionUp(MotionEvent event)
566
      {
567
      mPtrID1 = INVALID_POINTER_ID;
568
      mPtrID2 = INVALID_POINTER_ID;
569

    
570
      if( mContinuingRotation )
571
        {
572
        finishRotation();
573
        }
574

    
575
      if( mLastCubitColor>=0 )
576
        {
577
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
578
        mLastCubitColor = -1;
579
        }
580
      }
581

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

    
584
    private void actionDown2(MotionEvent event)
585
      {
586
      int index = event.getActionIndex();
587

    
588
      if( mPtrID1==INVALID_POINTER_ID )
589
        {
590
        mPtrID1 = event.getPointerId(index);
591
        float x = event.getX();
592
        float y = event.getY();
593

    
594
        if( mPtrID2 != INVALID_POINTER_ID )
595
          {
596
          int pointer = event.findPointerIndex(mPtrID2);
597

    
598
          float x2 = event.getX(pointer);
599
          float y2 = event.getY(pointer);
600

    
601
          mRotAngle = getAngle(x,-y,x2,-y2);
602
          mInitDistance = -1;
603
          }
604

    
605
        mX = (x - mScreenWidth*0.5f)/mScreenMin;
606
        mY = (mScreenHeight*0.5f -y)/mScreenMin;
607
        }
608
      else if( mPtrID2==INVALID_POINTER_ID )
609
        {
610
        mPtrID2 = event.getPointerId(index);
611

    
612
        float x = event.getX();
613
        float y = event.getY();
614

    
615
        if( mPtrID2 != INVALID_POINTER_ID )
616
          {
617
          int pointer = event.findPointerIndex(mPtrID2);
618

    
619
          float x2 = event.getX(pointer);
620
          float y2 = event.getY(pointer);
621

    
622
          mRotAngle = getAngle(x,-y,x2,-y2);
623
          mInitDistance = -1;
624
          }
625

    
626
        if( mBeginningRotation || mContinuingRotation )
627
          {
628
          mX = (x - mScreenWidth*0.5f)/mScreenMin;
629
          mY = (mScreenHeight*0.5f -y)/mScreenMin;
630
          }
631
        }
632

    
633
      if( mBeginningRotation )
634
        {
635
        mContinuingRotation = false;
636
        mBeginningRotation  = false;
637
        mDragging           = true;
638
        }
639
      else if( mContinuingRotation )
640
        {
641
        finishRotation();
642
        }
643
      }
644

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

    
647
    private void actionUp2(MotionEvent event)
648
      {
649
      int index = event.getActionIndex();
650

    
651
      if( index==event.findPointerIndex(mPtrID1) )
652
        {
653
        mPtrID1 = INVALID_POINTER_ID;
654
        int pointer = event.findPointerIndex(mPtrID2);
655

    
656
        if( pointer>=0 )
657
          {
658
          float x1 = event.getX(pointer);
659
          float y1 = event.getY(pointer);
660

    
661
          mX = (x1 - mScreenWidth*0.5f)/mScreenMin;
662
          mY = (mScreenHeight*0.5f -y1)/mScreenMin;
663
          }
664
        }
665
      else if( index==event.findPointerIndex(mPtrID2) )
666
        {
667
        mPtrID2 = INVALID_POINTER_ID;
668
        }
669
      }
670

    
671
///////////////////////////////////////////////////////////////////////////////////////////////////
672

    
673
    void initialize()
674
      {
675
      mPtrID1 = INVALID_POINTER_ID;
676
      mPtrID2 = INVALID_POINTER_ID;
677
      }
678

    
679
///////////////////////////////////////////////////////////////////////////////////////////////////
680
// PUBLIC API
681
///////////////////////////////////////////////////////////////////////////////////////////////////
682

    
683
    public RubikSurfaceView(Context context, AttributeSet attrs)
684
      {
685
      super(context,attrs);
686

    
687
      if(!isInEditMode())
688
        {
689
        mLastCubitColor = -1;
690
        mCurrRotSpeed   = 0.0f;
691

    
692
        mLastX = new float[NUM_SPEED_PROBES];
693
        mLastY = new float[NUM_SPEED_PROBES];
694
        mLastT = new long[NUM_SPEED_PROBES];
695
        mFirstIndex =0;
696
        mLastIndex  =0;
697

    
698
        mRenderer  = new RubikRenderer(this);
699
        mPreRender = new RubikPreRender(this);
700

    
701
        RubikActivity act = (RubikActivity)context;
702
        DisplayMetrics dm = new DisplayMetrics();
703
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
704

    
705
        mDensity = dm.densityDpi;
706

    
707
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
708

    
709
        if( activityManager!=null )
710
          {
711
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
712
          setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
713
          setRenderer(mRenderer);
714
          }
715
        }
716
      }
717

    
718
///////////////////////////////////////////////////////////////////////////////////////////////////
719

    
720
    @Override
721
    public boolean onTouchEvent(MotionEvent event)
722
      {
723
      int action = event.getActionMasked();
724

    
725
      switch(action)
726
         {
727
         case MotionEvent.ACTION_DOWN        : actionDown(event) ; break;
728
         case MotionEvent.ACTION_MOVE        : actionMove(event) ; break;
729
         case MotionEvent.ACTION_UP          : actionUp(event)   ; break;
730
         case MotionEvent.ACTION_POINTER_DOWN: actionDown2(event); break;
731
         case MotionEvent.ACTION_POINTER_UP  : actionUp2(event)  ; break;
732
         }
733

    
734
      return true;
735
      }
736
}
737

    
(4-4/4)