Project

General

Profile

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

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

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 float mX, mY;
77
    private float mStartRotX, mStartRotY;
78
    private float mAxisX, mAxisY;
79
    private float mRotationFactor;
80
    private int mLastCubitColor, mLastCubitFace, mLastCubit;
81
    private int mCurrentAxis, mCurrentRow;
82
    private float mCurrentAngle, mCurrRotSpeed;
83
    private float[] mLastX;
84
    private float[] mLastY;
85
    private long[] mLastT;
86
    private int mFirstIndex, mLastIndex;
87
    private int mDensity;
88

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

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

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

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

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

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

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

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

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

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

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

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

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
// return quat1*quat2
189

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

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

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

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

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

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

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

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

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

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

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

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

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

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

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

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

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

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

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

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

    
281
      long timeDiff = lastTime-firstTime;
282

    
283
      mLastIndex = 0;
284
      mFirstIndex= 0;
285

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

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

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

    
297
      return distInPixels/mDensity;
298
      }
299

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

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

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

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

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

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

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

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

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

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

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

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

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

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

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

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

    
396
      if( RubikState.getCurrentState()==RubikState.SOLV && angle!=0 )
397
        {
398
        RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
399
        solving.addMove(mCurrentAxis, mCurrentRow, angle);
400
        }
401

    
402
      mContinuingRotation = false;
403
      mBeginningRotation  = false;
404
      mDragging           = true;
405
      }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
    private void continueRotation(float x, float y)
410
      {
411
      float dx = x-mStartRotX;
412
      float dy = y-mStartRotY;
413
      float alpha = dx*mAxisX + dy*mAxisY;
414
      float x2 = dx - alpha*mAxisX;
415
      float y2 = dy - alpha*mAxisY;
416

    
417
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
418

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

    
422
      float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
423
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
424
      mPreRender.getObject().continueRotation(mCurrentAngle);
425

    
426
      addSpeedProbe(x2,y2);
427
      }
428

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430

    
431
    private void beginRotation(float x, float y)
432
      {
433
      mStartRotX = x;
434
      mStartRotY = y;
435

    
436
      Static4D touchPoint2 = new Static4D(x, y, 0, 0);
437
      Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuat);
438

    
439
      Static2D res = mMovement.newRotation(rotatedTouchPoint2);
440
      RubikObject object = mPreRender.getObject();
441

    
442
      mCurrentAxis = (int)res.get0();
443
      float offset = res.get1();
444
      mCurrentRow = (int)(object.returnMultiplier()*offset);
445
      computeCurrentAxis( object.getRotationAxis()[mCurrentAxis] );
446
      mRotationFactor = object.returnRotationFactor(offset);
447

    
448
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
449

    
450
      if( RubikState.getCurrentState()==RubikState.READ )
451
        {
452
        RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
453
        solving.resetElapsed();
454

    
455
        final RubikActivity act = (RubikActivity)getContext();
456

    
457
        act.runOnUiThread(new Runnable()
458
          {
459
          @Override
460
          public void run()
461
            {
462
            RubikState.switchState( act, RubikState.SOLV);
463
            }
464
          });
465
        }
466

    
467
      addSpeedProbe(x,y);
468

    
469
      mBeginningRotation = false;
470
      mContinuingRotation= true;
471
      }
472

    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474

    
475
    private float getAngle(float x1, float y1, float x2, float y2)
476
      {
477
      return (float) Math.atan2(y1-y2, x1-x2);
478
      }
479

    
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481

    
482
    private void actionMove(MotionEvent event)
483
      {
484
      int pointer = event.findPointerIndex(mPtrID1 != INVALID_POINTER_ID ? mPtrID1:mPtrID2);
485

    
486
      if( pointer<0 ) return;
487

    
488
      float pX = event.getX(pointer);
489
      float pY = event.getY(pointer);
490

    
491
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
492
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
493

    
494
      if( mBeginningRotation )
495
        {
496
        if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
497
          {
498
          beginRotation(x,y);
499
          }
500
        }
501
      else if( mContinuingRotation )
502
        {
503
        continueRotation(x,y);
504
        }
505
      else if( mDragging )
506
        {
507
        drag(event,x,y);
508
        }
509
      else
510
        {
511
        setUpDragOrRotate(false,x,y);
512
        }
513
      }
514

    
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516

    
517
    private void actionDown(MotionEvent event)
518
      {
519
      mPtrID1 = event.getPointerId(0);
520

    
521
      float x = event.getX();
522
      float y = event.getY();
523

    
524
      mX = (x - mScreenWidth*0.5f)/mScreenMin;
525
      mY = (mScreenHeight*0.5f -y)/mScreenMin;
526

    
527
      setUpDragOrRotate(true,mX,mY);
528
      }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531

    
532
    private void actionUp(MotionEvent event)
533
      {
534
      mPtrID1 = INVALID_POINTER_ID;
535
      mPtrID2 = INVALID_POINTER_ID;
536

    
537
      if( mContinuingRotation )
538
        {
539
        finishRotation();
540
        }
541

    
542
      if( mLastCubitColor>=0 )
543
        {
544
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
545
        mLastCubitColor = -1;
546
        }
547
      }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550

    
551
    private void actionDown2(MotionEvent event)
552
      {
553
      int index = event.getActionIndex();
554

    
555
      if( mPtrID1==INVALID_POINTER_ID )
556
        {
557
        mPtrID1 = event.getPointerId(index);
558
        float x = event.getX();
559
        float y = event.getY();
560

    
561
        if( mPtrID2 != INVALID_POINTER_ID )
562
          {
563
          int pointer = event.findPointerIndex(mPtrID2);
564

    
565
          float x2 = event.getX(pointer);
566
          float y2 = event.getY(pointer);
567

    
568
          mRotAngle = getAngle(x,-y,x2,-y2);
569
          }
570

    
571
        mX = (x - mScreenWidth*0.5f)/mScreenMin;
572
        mY = (mScreenHeight*0.5f -y)/mScreenMin;
573
        }
574
      else if( mPtrID2==INVALID_POINTER_ID )
575
        {
576
        mPtrID2 = event.getPointerId(index);
577

    
578
        float x = event.getX();
579
        float y = event.getY();
580

    
581
        if( mPtrID2 != INVALID_POINTER_ID )
582
          {
583
          int pointer = event.findPointerIndex(mPtrID2);
584

    
585
          float x2 = event.getX(pointer);
586
          float y2 = event.getY(pointer);
587

    
588
          mRotAngle = getAngle(x,-y,x2,-y2);
589
          }
590

    
591
        if( mBeginningRotation || mContinuingRotation )
592
          {
593
          mX = (x - mScreenWidth*0.5f)/mScreenMin;
594
          mY = (mScreenHeight*0.5f -y)/mScreenMin;
595
          }
596
        }
597

    
598
      if( mBeginningRotation )
599
        {
600
        mContinuingRotation = false;
601
        mBeginningRotation  = false;
602
        mDragging           = true;
603
        }
604
      else if( mContinuingRotation )
605
        {
606
        finishRotation();
607
        }
608
      }
609

    
610
///////////////////////////////////////////////////////////////////////////////////////////////////
611

    
612
    private void actionUp2(MotionEvent event)
613
      {
614
      int index = event.getActionIndex();
615

    
616
      if( index==event.findPointerIndex(mPtrID1) )
617
        {
618
        mPtrID1 = INVALID_POINTER_ID;
619
        int pointer = event.findPointerIndex(mPtrID2);
620

    
621
        if( pointer>=0 )
622
          {
623
          float x1 = event.getX(pointer);
624
          float y1 = event.getY(pointer);
625

    
626
          mX = (x1 - mScreenWidth*0.5f)/mScreenMin;
627
          mY = (mScreenHeight*0.5f -y1)/mScreenMin;
628
          }
629
        }
630
      else if( index==event.findPointerIndex(mPtrID2) )
631
        {
632
        mPtrID2 = INVALID_POINTER_ID;
633
        }
634
      }
635

    
636
///////////////////////////////////////////////////////////////////////////////////////////////////
637
// PUBLIC API
638
///////////////////////////////////////////////////////////////////////////////////////////////////
639

    
640
    public RubikSurfaceView(Context context, AttributeSet attrs)
641
      {
642
      super(context,attrs);
643

    
644
      if(!isInEditMode())
645
        {
646
        mLastCubitColor = -1;
647
        mCurrRotSpeed   = 0.0f;
648

    
649
        mLastX = new float[NUM_SPEED_PROBES];
650
        mLastY = new float[NUM_SPEED_PROBES];
651
        mLastT = new long[NUM_SPEED_PROBES];
652
        mFirstIndex =0;
653
        mLastIndex  =0;
654

    
655
        mPtrID1 = INVALID_POINTER_ID;
656
        mPtrID2 = INVALID_POINTER_ID;
657

    
658
        mRenderer  = new RubikRenderer(this);
659
        mPreRender = new RubikPreRender(this);
660

    
661
        RubikActivity act = (RubikActivity)context;
662
        DisplayMetrics dm = new DisplayMetrics();
663
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
664

    
665
        mDensity = dm.densityDpi;
666

    
667
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
668

    
669
        if( activityManager!=null )
670
          {
671
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
672
          setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
673
          setRenderer(mRenderer);
674
          }
675
        }
676
      }
677

    
678
///////////////////////////////////////////////////////////////////////////////////////////////////
679

    
680
    @Override
681
    public boolean onTouchEvent(MotionEvent event)
682
      {
683
      int action = event.getActionMasked();
684

    
685
      switch(action)
686
         {
687
         case MotionEvent.ACTION_DOWN        : actionDown(event) ; break;
688
         case MotionEvent.ACTION_MOVE        : actionMove(event) ; break;
689
         case MotionEvent.ACTION_UP          : actionUp(event)   ; break;
690
         case MotionEvent.ACTION_POINTER_DOWN: actionDown2(event); break;
691
         case MotionEvent.ACTION_POINTER_UP  : actionUp2(event)  ; break;
692
         }
693

    
694
      return true;
695
      }
696
}
697

    
(4-4/4)