Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ 7695a3be

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 org.distorted.library.type.Static2D;
31
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33
import org.distorted.objects.RubikObject;
34
import org.distorted.objects.RubikObjectMovement;
35
import org.distorted.solvers.SolverMain;
36
import org.distorted.states.RubikState;
37
import org.distorted.states.RubikStateSolver;
38
import org.distorted.states.RubikStateSolving;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
public class RubikSurfaceView extends GLSurfaceView
43
{
44
    private static final int NUM_SPEED_PROBES = 10;
45
    private static final int INVALID_POINTER_ID = -1;
46

    
47
    public static final int MODE_ROTATE  = 0;
48
    public static final int MODE_DRAG    = 1;
49
    public static final int MODE_REPLACE = 2;
50

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

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

    
68
    private RubikRenderer mRenderer;
69
    private RubikPreRender mPreRender;
70
    private RubikObjectMovement mMovement;
71
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
72
    private int mScreenWidth, mScreenHeight, mScreenMin;
73

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

    
90
    private static Static4D mQuat= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
91
    private static 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(RubikObjectMovement movement)
141
      {
142
      mMovement = movement;
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
    private Static4D quatFromAngle(float angle)
148
      {
149
      float cosA = (float)Math.cos(angle);
150
      float sinA =-(float)Math.sin(angle);
151

    
152
      return new Static4D(0, 0, sinA, cosA);
153
      }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
    private Static4D quatFromDrag(float dragX, float dragY)
158
      {
159
      float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
160
      float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
161
      float axisZ = 0;
162
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
163

    
164
      if( axisL>0 )
165
        {
166
        axisX /= axisL;
167
        axisY /= axisL;
168
        axisZ /= axisL;
169

    
170
        float ratio = axisL;
171
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
172

    
173
        float cosA = (float)Math.cos(Math.PI*ratio);
174
        float sinA = (float)Math.sqrt(1-cosA*cosA);
175

    
176
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
177
        }
178

    
179
      return new Static4D(0f, 0f, 0f, 1f);
180
      }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
// cast the 3D axis we are currently rotating along to the 2D in-screen-surface axis
184

    
185
    private void computeCurrentAxis(Static3D axis)
186
      {
187
      Static4D axis4D = new Static4D(axis.get0(), axis.get1(), axis.get2(), 0);
188
      Static4D result = rotateVectorByQuat(axis4D, mQuat);
189

    
190
      mAxisX =result.get0();
191
      mAxisY =result.get1();
192

    
193
      float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
194
      mAxisX /= len;
195
      mAxisY /= len;
196
      }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
// return quat1*quat2
200

    
201
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
202
      {
203
      float qx = quat1.get0();
204
      float qy = quat1.get1();
205
      float qz = quat1.get2();
206
      float qw = quat1.get3();
207

    
208
      float rx = quat2.get0();
209
      float ry = quat2.get1();
210
      float rz = quat2.get2();
211
      float rw = quat2.get3();
212

    
213
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
214
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
215
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
216
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
217

    
218
      return new Static4D(tx,ty,tz,tw);
219
      }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
223

    
224
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
225
      {
226
      float qx = quat.get0();
227
      float qy = quat.get1();
228
      float qz = quat.get2();
229
      float qw = quat.get3();
230

    
231
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
232
      Static4D tmp = quatMultiply(quat,vector);
233

    
234
      return quatMultiply(tmp,quatInverted);
235
      }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
239

    
240
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
241
      {
242
      float qx = quat.get0();
243
      float qy = quat.get1();
244
      float qz = quat.get2();
245
      float qw = quat.get3();
246

    
247
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
248
      Static4D tmp = quatMultiply(quatInverted,vector);
249

    
250
      return quatMultiply(tmp,quat);
251
      }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
    private void addSpeedProbe(float x, float y)
256
      {
257
      long currTime = System.currentTimeMillis();
258
      boolean theSame = mLastIndex==mFirstIndex;
259

    
260
      mLastIndex++;
261
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
262

    
263
      mLastT[mLastIndex] = currTime;
264
      mLastX[mLastIndex] = x;
265
      mLastY[mLastIndex] = y;
266

    
267
      if( mLastIndex==mFirstIndex)
268
        {
269
        mFirstIndex++;
270
        if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
271
        }
272

    
273
      if( theSame )
274
        {
275
        mLastT[mFirstIndex] = currTime;
276
        mLastX[mFirstIndex] = x;
277
        mLastY[mFirstIndex] = y;
278
        }
279
      }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
    private void computeCurrentSpeedInInchesPerSecond()
284
      {
285
      long firstTime = mLastT[mFirstIndex];
286
      long lastTime  = mLastT[mLastIndex];
287
      float fX = mLastX[mFirstIndex];
288
      float fY = mLastY[mFirstIndex];
289
      float lX = mLastX[mLastIndex];
290
      float lY = mLastY[mLastIndex];
291

    
292
      long timeDiff = lastTime-firstTime;
293

    
294
      mLastIndex = 0;
295
      mFirstIndex= 0;
296

    
297
      mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX,fY,lX,lY)/timeDiff : 0;
298
      }
299

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

    
302
    private float retFingerDragDistanceInInches(float xFrom, float yFrom, float xTo, float yTo)
303
      {
304
      float xDist = mScreenWidth*(xFrom-xTo);
305
      float yDist = mScreenHeight*(yFrom-yTo);
306
      float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
307

    
308
      return distInPixels/mDensity;
309
      }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
    private void setUpDragOrRotate(boolean down, float x, float y)
314
      {
315
      int mode = RubikState.getMode();
316

    
317
      if( mode==MODE_DRAG )
318
        {
319
        mDragging           = true;
320
        mBeginningRotation  = false;
321
        mContinuingRotation = false;
322
        }
323
      else
324
        {
325
        Static4D touchPoint1 = new Static4D(x, y, 0, 0);
326
        Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuat);
327
        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
328

    
329
        if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
330
          {
331
          mDragging           = false;
332
          mContinuingRotation = false;
333

    
334
          if( mode==MODE_ROTATE )
335
            {
336
            mBeginningRotation= mPreRender.canRotate();
337
            }
338
          else if( mode==MODE_REPLACE )
339
            {
340
            mBeginningRotation= false;
341

    
342
            if( down )
343
              {
344
              RubikStateSolver solver = (RubikStateSolver) RubikState.SVER.getStateClass();
345
              mLastCubitFace = mMovement.getTouchedFace();
346
              float[] point = mMovement.getTouchedPoint3D();
347
              int color = solver.getCurrentColor();
348
              RubikObject object = mPreRender.getObject();
349
              mLastCubit = object.getCubit(point);
350
              mPreRender.setTextureMap( mLastCubit, mLastCubitFace, color );
351
              mLastCubitColor = SolverMain.cubitIsLocked(object.getObjectList(), object.getSize(), mLastCubit);
352
              }
353
            }
354
          }
355
        else
356
          {
357
          mDragging           = true;
358
          mBeginningRotation  = false;
359
          mContinuingRotation = false;
360
          }
361
        }
362
      }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
    private void drag(MotionEvent event, float x, float y)
367
      {
368
      if( mPtrID1!=INVALID_POINTER_ID && mPtrID2!=INVALID_POINTER_ID)
369
        {
370
        int pointer = event.findPointerIndex(mPtrID2);
371

    
372
        float pX = event.getX(pointer);
373
        float pY = event.getY(pointer);
374

    
375
        float x2 = (pX - mScreenWidth*0.5f)/mScreenMin;
376
        float y2 = (mScreenHeight*0.5f -pY)/mScreenMin;
377

    
378
        float angleNow = getAngle(x,y,x2,y2);
379
        float angleDiff = angleNow-mRotAngle;
380
        float sinA =-(float)Math.sin(angleDiff);
381
        float cosA = (float)Math.cos(angleDiff);
382

    
383
        Static4D dragQuat = quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
384
        mTemp.set(dragQuat);
385

    
386
        mRotAngle = angleNow;
387
        }
388
      else
389
        {
390
        Static4D dragQuat = quatMultiply(quatFromDrag(mX-x,y-mY), mQuat);
391
        mTemp.set(dragQuat);
392
        }
393

    
394
      mPreRender.setQuatOnNextRender();
395
      mX = x;
396
      mY = y;
397
      }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

    
401
    private void finishRotation()
402
      {
403
      computeCurrentSpeedInInchesPerSecond();
404
      int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
405
      mPreRender.finishRotation(angle);
406

    
407
      if( RubikState.getCurrentState()==RubikState.SOLV && angle!=0 )
408
        {
409
        RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
410
        solving.addMove(mCurrentAxis, mCurrentRow, angle);
411
        }
412

    
413
      mContinuingRotation = false;
414
      mBeginningRotation  = false;
415
      mDragging           = true;
416
      }
417

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

    
420
    private void continueRotation(float x, float y)
421
      {
422
      float dx = x-mStartRotX;
423
      float dy = y-mStartRotY;
424
      float alpha = dx*mAxisX + dy*mAxisY;
425
      float x2 = dx - alpha*mAxisX;
426
      float y2 = dy - alpha*mAxisY;
427

    
428
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
429

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

    
433
      float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
434
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
435
      mPreRender.getObject().continueRotation(mCurrentAngle);
436

    
437
      addSpeedProbe(x2,y2);
438
      }
439

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

    
442
    private void beginRotation(float x, float y)
443
      {
444
      mStartRotX = x;
445
      mStartRotY = y;
446

    
447
      Static4D touchPoint2 = new Static4D(x, y, 0, 0);
448
      Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuat);
449

    
450
      Static2D res = mMovement.newRotation(rotatedTouchPoint2);
451
      RubikObject object = mPreRender.getObject();
452

    
453
      mCurrentAxis = (int)res.get0();
454
      float offset = res.get1();
455
      mCurrentRow = (int)(object.returnMultiplier()*offset);
456
      computeCurrentAxis( object.getRotationAxis()[mCurrentAxis] );
457
      mRotationFactor = object.returnRotationFactor(offset);
458

    
459
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
460

    
461
      if( RubikState.getCurrentState()==RubikState.READ )
462
        {
463
        RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
464
        solving.resetElapsed();
465

    
466
        final RubikActivity act = (RubikActivity)getContext();
467

    
468
        act.runOnUiThread(new Runnable()
469
          {
470
          @Override
471
          public void run()
472
            {
473
            RubikState.switchState( act, RubikState.SOLV);
474
            }
475
          });
476
        }
477

    
478
      addSpeedProbe(x,y);
479

    
480
      mBeginningRotation = false;
481
      mContinuingRotation= true;
482
      }
483

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485

    
486
    private float getAngle(float x1, float y1, float x2, float y2)
487
      {
488
      return (float) Math.atan2(y1-y2, x1-x2);
489
      }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492

    
493
    private void actionMove(MotionEvent event)
494
      {
495
      int pointer = event.findPointerIndex(mPtrID1 != INVALID_POINTER_ID ? mPtrID1:mPtrID2);
496

    
497
      if( pointer<0 ) return;
498

    
499
      float pX = event.getX(pointer);
500
      float pY = event.getY(pointer);
501

    
502
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
503
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
504

    
505
      if( mBeginningRotation )
506
        {
507
        if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
508
          {
509
          beginRotation(x,y);
510
          }
511
        }
512
      else if( mContinuingRotation )
513
        {
514
        continueRotation(x,y);
515
        }
516
      else if( mDragging )
517
        {
518
        drag(event,x,y);
519
        }
520
      else
521
        {
522
        setUpDragOrRotate(false,x,y);
523
        }
524
      }
525

    
526
///////////////////////////////////////////////////////////////////////////////////////////////////
527

    
528
    private void actionDown(MotionEvent event)
529
      {
530
      mNumFingersDown++;
531
      mPtrID1 = event.getPointerId(0);
532

    
533
      float x = event.getX();
534
      float y = event.getY();
535

    
536
      mX = (x - mScreenWidth*0.5f)/mScreenMin;
537
      mY = (mScreenHeight*0.5f -y)/mScreenMin;
538

    
539
      setUpDragOrRotate(true,mX,mY);
540
      }
541

    
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543

    
544
    private void actionUp(MotionEvent event)
545
      {
546
      mNumFingersDown--;
547

    
548
      mPtrID1 = INVALID_POINTER_ID;
549
      mPtrID2 = INVALID_POINTER_ID;
550

    
551
      if( mContinuingRotation )
552
        {
553
        finishRotation();
554
        }
555

    
556
      if( mLastCubitColor>=0 )
557
        {
558
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
559
        }
560
      }
561

    
562
///////////////////////////////////////////////////////////////////////////////////////////////////
563

    
564
    private void actionDown2(MotionEvent event)
565
      {
566
      mNumFingersDown++;
567

    
568
      int index = event.getActionIndex();
569

    
570
      if( mPtrID1==INVALID_POINTER_ID )
571
        {
572
        mPtrID1 = event.getPointerId(index);
573
        float x = event.getX();
574
        float y = event.getY();
575

    
576
        if( mPtrID2 != INVALID_POINTER_ID )
577
          {
578
          int pointer = event.findPointerIndex(mPtrID2);
579

    
580
          float x2 = event.getX(pointer);
581
          float y2 = event.getY(pointer);
582

    
583
          mRotAngle = getAngle(x,-y,x2,-y2);
584
          }
585

    
586
        mX = (x - mScreenWidth*0.5f)/mScreenMin;
587
        mY = (mScreenHeight*0.5f -y)/mScreenMin;
588
        }
589
      else if( mPtrID2==INVALID_POINTER_ID )
590
        {
591
        mPtrID2 = event.getPointerId(index);
592

    
593
        float x = event.getX();
594
        float y = event.getY();
595

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

    
600
          float x2 = event.getX(pointer);
601
          float y2 = event.getY(pointer);
602

    
603
          mRotAngle = getAngle(x,-y,x2,-y2);
604
          }
605

    
606
        if( mBeginningRotation || mContinuingRotation )
607
          {
608
          mX = (x - mScreenWidth*0.5f)/mScreenMin;
609
          mY = (mScreenHeight*0.5f -y)/mScreenMin;
610
          }
611
        }
612

    
613
      if( mBeginningRotation )
614
        {
615
        mContinuingRotation = false;
616
        mBeginningRotation  = false;
617
        mDragging           = true;
618
        }
619
      else if( mContinuingRotation )
620
        {
621
        finishRotation();
622
        }
623
      }
624

    
625
///////////////////////////////////////////////////////////////////////////////////////////////////
626

    
627
    private void actionUp2(MotionEvent event)
628
      {
629
      mNumFingersDown--;
630

    
631
      int index = event.getActionIndex();
632

    
633
      if( index==event.findPointerIndex(mPtrID1) )
634
        {
635
        mPtrID1 = INVALID_POINTER_ID;
636
        int pointer = event.findPointerIndex(mPtrID2);
637

    
638
        if( pointer>=0 )
639
          {
640
          float x1 = event.getX(pointer);
641
          float y1 = event.getY(pointer);
642

    
643
          mX = (x1 - mScreenWidth*0.5f)/mScreenMin;
644
          mY = (mScreenHeight*0.5f -y1)/mScreenMin;
645
          }
646
        }
647
      else if( index==event.findPointerIndex(mPtrID2) )
648
        {
649
        mPtrID2 = INVALID_POINTER_ID;
650
        }
651
      }
652

    
653
///////////////////////////////////////////////////////////////////////////////////////////////////
654
// PUBLIC API
655
///////////////////////////////////////////////////////////////////////////////////////////////////
656

    
657
    public RubikSurfaceView(Context context, AttributeSet attrs)
658
      {
659
      super(context,attrs);
660

    
661
      if(!isInEditMode())
662
        {
663
        mLastCubitColor = -1;
664
        mCurrRotSpeed   = 0.0f;
665

    
666
        mLastX = new float[NUM_SPEED_PROBES];
667
        mLastY = new float[NUM_SPEED_PROBES];
668
        mLastT = new long[NUM_SPEED_PROBES];
669
        mFirstIndex =0;
670
        mLastIndex  =0;
671

    
672
        mPtrID1 = INVALID_POINTER_ID;
673
        mPtrID2 = INVALID_POINTER_ID;
674

    
675
        mNumFingersDown = 0;
676

    
677
        mRenderer  = new RubikRenderer(this);
678
        mPreRender = new RubikPreRender(this);
679

    
680
        RubikActivity act = (RubikActivity)context;
681
        DisplayMetrics dm = new DisplayMetrics();
682
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
683

    
684
        mDensity = dm.densityDpi;
685

    
686
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
687

    
688
        if( activityManager!=null )
689
          {
690
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
691
          setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
692
          setRenderer(mRenderer);
693
          }
694
        }
695
      }
696

    
697
///////////////////////////////////////////////////////////////////////////////////////////////////
698

    
699
    @Override
700
    public boolean onTouchEvent(MotionEvent event)
701
      {
702
      int action = event.getActionMasked();
703

    
704
      switch(action)
705
         {
706
         case MotionEvent.ACTION_DOWN        : actionDown(event) ; break;
707
         case MotionEvent.ACTION_MOVE        : actionMove(event) ; break;
708
         case MotionEvent.ACTION_UP          : actionUp(event)   ; break;
709
         case MotionEvent.ACTION_POINTER_DOWN: actionDown2(event); break;
710
         case MotionEvent.ACTION_POINTER_UP  : actionUp2(event)  ; break;
711
         }
712

    
713
      return true;
714
      }
715
}
716

    
(4-4/4)