Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ 15846fe4

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.RubikStatePlay;
38
import org.distorted.states.RubikStateSolver;
39
import org.distorted.states.RubikStateSolving;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

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

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

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

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

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

    
75
    private float mRotAngle;
76
    private int mPtrID1, mPtrID2;
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 quatFromDrag(float dragX, float dragY)
148
      {
149
      float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
150
      float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
151
      float axisZ = 0;
152
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
153

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

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

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

    
166
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
167
        }
168

    
169
      return new Static4D(0f, 0f, 0f, 1f);
170
      }
171

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

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

    
180
      mAxisX =result.get0();
181
      mAxisY =result.get1();
182

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

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
// return quat1*quat2
190

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

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

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

    
208
      return new Static4D(tx,ty,tz,tw);
209
      }
210

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

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

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

    
224
      return quatMultiply(tmp,quatInverted);
225
      }
226

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

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

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

    
240
      return quatMultiply(tmp,quat);
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

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

    
250
      mLastIndex++;
251
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
252

    
253
      mLastT[mLastIndex] = currTime;
254
      mLastX[mLastIndex] = x;
255
      mLastY[mLastIndex] = y;
256

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

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

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

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

    
282
      long timeDiff = lastTime-firstTime;
283

    
284
      mLastIndex = 0;
285
      mFirstIndex= 0;
286

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

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

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

    
298
      return distInPixels/mDensity;
299
      }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

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

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

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

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

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

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

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

    
362
        float pX = event.getX(pointer);
363
        float pY = event.getY(pointer);
364

    
365
        float x2 = (pX - mScreenWidth*0.5f)/mScreenMin;
366
        float y2 = (mScreenHeight*0.5f -pY)/mScreenMin;
367

    
368
        float angleNow = getAngle(x,y,x2,y2);
369
        float angleDiff = angleNow-mRotAngle;
370
        float sinA =-(float)Math.sin(angleDiff);
371
        float cosA = (float)Math.cos(angleDiff);
372

    
373
        Static4D dragQuat = quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
374
        mTemp.set(dragQuat);
375

    
376
        mRotAngle = angleNow;
377
        }
378
      else
379
        {
380
        Static4D dragQuat = quatMultiply(quatFromDrag(mX-x,y-mY), mQuat);
381
        mTemp.set(dragQuat);
382
        }
383

    
384
      mPreRender.setQuatOnNextRender();
385
      mX = x;
386
      mY = y;
387
      }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
    private void finishRotation()
392
      {
393
      computeCurrentSpeedInInchesPerSecond();
394
      int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
395
      mPreRender.finishRotation(angle);
396

    
397
      if( angle!=0 )
398
        {
399
        if( RubikState.getCurrentState()==RubikState.SOLV )
400
          {
401
          RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
402
          solving.addMove(mCurrentAxis, mCurrentRow, angle);
403
          }
404
        if( RubikState.getCurrentState()==RubikState.PLAY )
405
          {
406
          RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
407
          play.addMove(mCurrentAxis, mCurrentRow, angle);
408
          }
409
        }
410

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

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

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

    
426
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
427

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

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

    
435
      addSpeedProbe(x2,y2);
436
      }
437

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

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

    
445
      Static4D touchPoint2 = new Static4D(x, y, 0, 0);
446
      Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuat);
447

    
448
      Static2D res = mMovement.newRotation(rotatedTouchPoint2);
449
      RubikObject object = mPreRender.getObject();
450

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

    
457
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
458

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

    
464
        final RubikActivity act = (RubikActivity)getContext();
465

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

    
476
      addSpeedProbe(x,y);
477

    
478
      mBeginningRotation = false;
479
      mContinuingRotation= true;
480
      }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

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

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

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

    
495
      if( pointer<0 ) return;
496

    
497
      float pX = event.getX(pointer);
498
      float pY = event.getY(pointer);
499

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

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

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525

    
526
    private void actionDown(MotionEvent event)
527
      {
528
      mPtrID1 = event.getPointerId(0);
529

    
530
      float x = event.getX();
531
      float y = event.getY();
532

    
533
      mX = (x - mScreenWidth*0.5f)/mScreenMin;
534
      mY = (mScreenHeight*0.5f -y)/mScreenMin;
535

    
536
      setUpDragOrRotate(true,mX,mY);
537
      }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

    
541
    private void actionUp(MotionEvent event)
542
      {
543
      mPtrID1 = INVALID_POINTER_ID;
544
      mPtrID2 = INVALID_POINTER_ID;
545

    
546
      if( mContinuingRotation )
547
        {
548
        finishRotation();
549
        }
550

    
551
      if( mLastCubitColor>=0 )
552
        {
553
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
554
        mLastCubitColor = -1;
555
        }
556
      }
557

    
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559

    
560
    private void actionDown2(MotionEvent event)
561
      {
562
      int index = event.getActionIndex();
563

    
564
      if( mPtrID1==INVALID_POINTER_ID )
565
        {
566
        mPtrID1 = event.getPointerId(index);
567
        float x = event.getX();
568
        float y = event.getY();
569

    
570
        if( mPtrID2 != INVALID_POINTER_ID )
571
          {
572
          int pointer = event.findPointerIndex(mPtrID2);
573

    
574
          float x2 = event.getX(pointer);
575
          float y2 = event.getY(pointer);
576

    
577
          mRotAngle = getAngle(x,-y,x2,-y2);
578
          }
579

    
580
        mX = (x - mScreenWidth*0.5f)/mScreenMin;
581
        mY = (mScreenHeight*0.5f -y)/mScreenMin;
582
        }
583
      else if( mPtrID2==INVALID_POINTER_ID )
584
        {
585
        mPtrID2 = event.getPointerId(index);
586

    
587
        float x = event.getX();
588
        float y = event.getY();
589

    
590
        if( mPtrID2 != INVALID_POINTER_ID )
591
          {
592
          int pointer = event.findPointerIndex(mPtrID2);
593

    
594
          float x2 = event.getX(pointer);
595
          float y2 = event.getY(pointer);
596

    
597
          mRotAngle = getAngle(x,-y,x2,-y2);
598
          }
599

    
600
        if( mBeginningRotation || mContinuingRotation )
601
          {
602
          mX = (x - mScreenWidth*0.5f)/mScreenMin;
603
          mY = (mScreenHeight*0.5f -y)/mScreenMin;
604
          }
605
        }
606

    
607
      if( mBeginningRotation )
608
        {
609
        mContinuingRotation = false;
610
        mBeginningRotation  = false;
611
        mDragging           = true;
612
        }
613
      else if( mContinuingRotation )
614
        {
615
        finishRotation();
616
        }
617
      }
618

    
619
///////////////////////////////////////////////////////////////////////////////////////////////////
620

    
621
    private void actionUp2(MotionEvent event)
622
      {
623
      int index = event.getActionIndex();
624

    
625
      if( index==event.findPointerIndex(mPtrID1) )
626
        {
627
        mPtrID1 = INVALID_POINTER_ID;
628
        int pointer = event.findPointerIndex(mPtrID2);
629

    
630
        if( pointer>=0 )
631
          {
632
          float x1 = event.getX(pointer);
633
          float y1 = event.getY(pointer);
634

    
635
          mX = (x1 - mScreenWidth*0.5f)/mScreenMin;
636
          mY = (mScreenHeight*0.5f -y1)/mScreenMin;
637
          }
638
        }
639
      else if( index==event.findPointerIndex(mPtrID2) )
640
        {
641
        mPtrID2 = INVALID_POINTER_ID;
642
        }
643
      }
644

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

    
647
    void initialize()
648
      {
649
      mPtrID1 = INVALID_POINTER_ID;
650
      mPtrID2 = INVALID_POINTER_ID;
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
        mRenderer  = new RubikRenderer(this);
673
        mPreRender = new RubikPreRender(this);
674

    
675
        RubikActivity act = (RubikActivity)context;
676
        DisplayMetrics dm = new DisplayMetrics();
677
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
678

    
679
        mDensity = dm.densityDpi;
680

    
681
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
682

    
683
        if( activityManager!=null )
684
          {
685
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
686
          setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
687
          setRenderer(mRenderer);
688
          }
689
        }
690
      }
691

    
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693

    
694
    @Override
695
    public boolean onTouchEvent(MotionEvent event)
696
      {
697
      int action = event.getActionMasked();
698

    
699
      switch(action)
700
         {
701
         case MotionEvent.ACTION_DOWN        : actionDown(event) ; break;
702
         case MotionEvent.ACTION_MOVE        : actionMove(event) ; break;
703
         case MotionEvent.ACTION_UP          : actionUp(event)   ; break;
704
         case MotionEvent.ACTION_POINTER_DOWN: actionDown2(event); break;
705
         case MotionEvent.ACTION_POINTER_UP  : actionUp2(event)  ; break;
706
         }
707

    
708
      return true;
709
      }
710
}
711

    
(4-4/4)