Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.main;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.opengl.GLES30;
26
import android.opengl.GLSurfaceView;
27
import android.util.AttributeSet;
28
import android.util.DisplayMetrics;
29
import android.view.MotionEvent;
30

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

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

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

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

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

    
61
    private final Static4D CAMERA_POINT = new Static4D(0, 0, 1, 0);
62

    
63
    private RubikRenderer mRenderer;
64
    private RubikPreRender mPreRender;
65
    private RubikMovement mMovement;
66
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
67
    private int mScreenWidth, mScreenHeight, mScreenMin;
68

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

    
84
    private static Static4D mQuat= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
85
    private static Static4D mTemp= new Static4D(0,0,0,1);
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
    void setScreenSize(int width, int height)
90
      {
91
      mScreenWidth = width;
92
      mScreenHeight= height;
93

    
94
      mScreenMin = Math.min(width, height);
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
    boolean isVertical()
100
      {
101
      return mScreenHeight>mScreenWidth;
102
      }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
    RubikRenderer getRenderer()
107
      {
108
      return mRenderer;
109
      }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
    RubikPreRender getPreRender()
114
      {
115
      return mPreRender;
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
    void setQuat()
121
      {
122
      mQuat.set(mTemp);
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
    Static4D getQuat()
128
      {
129
      return mQuat;
130
      }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
    void setMovement(RubikMovement movement)
135
      {
136
      mMovement = movement;
137
      }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

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

    
148
      if( axisL>0 )
149
        {
150
        axisX /= axisL;
151
        axisY /= axisL;
152
        axisZ /= axisL;
153

    
154
        float ratio = axisL;
155
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
156

    
157
        float cosA = (float)Math.cos(Math.PI*ratio);
158
        float sinA = (float)Math.sqrt(1-cosA*cosA);
159

    
160
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
161
        }
162

    
163
      return new Static4D(0f, 0f, 0f, 1f);
164
      }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
// cast the 3D axis we are currently rotating along to the 2D in-screen-surface axis
168

    
169
    private void computeCurrentAxis(Static3D axis)
170
      {
171
      Static4D axis4D = new Static4D(axis.get0(), axis.get1(), axis.get2(), 0);
172
      Static4D result = rotateVectorByQuat(axis4D, mQuat);
173

    
174
      mAxisX =result.get0();
175
      mAxisY =result.get1();
176

    
177
      float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
178
      mAxisX /= len;
179
      mAxisY /= len;
180
      }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
// return quat1*quat2
184

    
185
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
186
      {
187
      float qx = quat1.get0();
188
      float qy = quat1.get1();
189
      float qz = quat1.get2();
190
      float qw = quat1.get3();
191

    
192
      float rx = quat2.get0();
193
      float ry = quat2.get1();
194
      float rz = quat2.get2();
195
      float rw = quat2.get3();
196

    
197
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
198
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
199
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
200
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
201

    
202
      return new Static4D(tx,ty,tz,tw);
203
      }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
207

    
208
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
209
      {
210
      float qx = quat.get0();
211
      float qy = quat.get1();
212
      float qz = quat.get2();
213
      float qw = quat.get3();
214

    
215
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
216
      Static4D tmp = quatMultiply(quat,vector);
217

    
218
      return quatMultiply(tmp,quatInverted);
219
      }
220

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

    
224
    public static Static4D rotateVectorByInvertedQuat(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(quatInverted,vector);
233

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

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
    private void addSpeedProbe(float x, float y)
240
      {
241
      long currTime = System.currentTimeMillis();
242
      boolean theSame = mLastIndex==mFirstIndex;
243

    
244
      mLastIndex++;
245
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
246

    
247
      mLastT[mLastIndex] = currTime;
248
      mLastX[mLastIndex] = x;
249
      mLastY[mLastIndex] = y;
250

    
251
      if( mLastIndex==mFirstIndex)
252
        {
253
        mFirstIndex++;
254
        if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
255
        }
256

    
257
      if( theSame )
258
        {
259
        mLastT[mFirstIndex] = currTime;
260
        mLastX[mFirstIndex] = x;
261
        mLastY[mFirstIndex] = y;
262
        }
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
    private void computeCurrentSpeedInInchesPerSecond()
268
      {
269
      long firstTime = mLastT[mFirstIndex];
270
      long lastTime  = mLastT[mLastIndex];
271
      float fX = mLastX[mFirstIndex];
272
      float fY = mLastY[mFirstIndex];
273
      float lX = mLastX[mLastIndex];
274
      float lY = mLastY[mLastIndex];
275

    
276
      long timeDiff = lastTime-firstTime;
277

    
278
      mLastIndex = 0;
279
      mFirstIndex= 0;
280

    
281
      mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX,fY,lX,lY)/timeDiff : 0;
282
      }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
    private float retFingerDragDistanceInInches(float xFrom, float yFrom, float xTo, float yTo)
287
      {
288
      float xDist = mScreenWidth*(xFrom-xTo);
289
      float yDist = mScreenHeight*(yFrom-yTo);
290
      float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
291

    
292
      return distInPixels/mDensity;
293
      }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

    
297
    private void setUpDragOrRotate(boolean down, float x, float y)
298
      {
299
      int mode = RubikState.getMode();
300

    
301
      if( mode==MODE_DRAG )
302
        {
303
        mDragging           = true;
304
        mBeginningRotation  = false;
305
        mContinuingRotation = false;
306
        }
307
      else
308
        {
309
        Static4D touchPoint1 = new Static4D(x, y, 0, 0);
310
        Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuat);
311
        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
312

    
313
        if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
314
          {
315
          mDragging           = false;
316
          mContinuingRotation = false;
317

    
318
          if( mode==MODE_ROTATE )
319
            {
320
            mBeginningRotation= mPreRender.canRotate();
321
            }
322
          else if( mode==MODE_REPLACE )
323
            {
324
            mBeginningRotation= false;
325

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

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
    private void drag(MotionEvent event, float x, float y)
351
      {
352
      if( mPtrID1!=INVALID_POINTER_ID && mPtrID2!=INVALID_POINTER_ID)
353
        {
354
        int pointer = event.findPointerIndex(mPtrID2);
355
        float pX,pY;
356

    
357
        try
358
          {
359
          pX = event.getX(pointer);
360
          pY = event.getY(pointer);
361
          }
362
        catch(IllegalArgumentException ex)
363
          {
364
          mPtrID1=INVALID_POINTER_ID;
365
          mPtrID2=INVALID_POINTER_ID;
366

    
367
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
368
          crashlytics.setCustomKey("DragError", "pointer="+pointer );
369
          crashlytics.recordException(ex);
370

    
371
          return;
372
          }
373

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

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

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

    
385
        mRotAngle = angleNow;
386

    
387
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
388
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
389
        mInitDistance = distNow;
390

    
391
        RubikObject object = mPreRender.getObject();
392
        object.setObjectRatio(distQuot);
393
        }
394
      else
395
        {
396
        Static4D dragQuat = quatMultiply(quatFromDrag(mX-x,y-mY), mQuat);
397
        mTemp.set(dragQuat);
398
        }
399

    
400
      mPreRender.setQuatOnNextRender();
401
      mX = x;
402
      mY = y;
403
      }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
    private void finishRotation()
408
      {
409
      computeCurrentSpeedInInchesPerSecond();
410
      int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
411
      mPreRender.finishRotation(angle);
412

    
413
      if( angle!=0 )
414
        {
415
        if( RubikState.getCurrentState()==RubikState.SOLV )
416
          {
417
          RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
418
          solving.addMove(mCurrentAxis, mCurrentRow, angle);
419
          }
420
        if( RubikState.getCurrentState()==RubikState.PLAY )
421
          {
422
          RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
423
          play.addMove(mCurrentAxis, mCurrentRow, angle);
424
          }
425
        }
426

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

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

    
434
    private void continueRotation(float x, float y)
435
      {
436
      float dx = x-mStartRotX;
437
      float dy = y-mStartRotY;
438
      float alpha = dx*mAxisX + dy*mAxisY;
439
      float x2 = dx - alpha*mAxisX;
440
      float y2 = dy - alpha*mAxisY;
441

    
442
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
443

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

    
447
      float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
448
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
449
      mPreRender.getObject().continueRotation(mCurrentAngle);
450

    
451
      addSpeedProbe(x2,y2);
452
      }
453

    
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455

    
456
    private void beginRotation(float x, float y)
457
      {
458
      mStartRotX = x;
459
      mStartRotY = y;
460

    
461
      Static4D touchPoint2 = new Static4D(x, y, 0, 0);
462
      Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuat);
463

    
464
      Static2D res = mMovement.newRotation(rotatedTouchPoint2);
465
      RubikObject object = mPreRender.getObject();
466

    
467
      mCurrentAxis = (int)res.get0();
468
      float offset = res.get1();
469
      mCurrentRow = (int)(object.returnMultiplier()*offset);
470
      computeCurrentAxis( object.getRotationAxis()[mCurrentAxis] );
471
      mRotationFactor = object.returnRotationFactor(offset);
472

    
473
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
474

    
475
      if( RubikState.getCurrentState()==RubikState.READ )
476
        {
477
        RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
478
        solving.resetElapsed();
479

    
480
        final RubikActivity act = (RubikActivity)getContext();
481

    
482
        act.runOnUiThread(new Runnable()
483
          {
484
          @Override
485
          public void run()
486
            {
487
            RubikState.switchState( act, RubikState.SOLV);
488
            }
489
          });
490
        }
491

    
492
      addSpeedProbe(x,y);
493

    
494
      mBeginningRotation = false;
495
      mContinuingRotation= true;
496
      }
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

    
500
    private float getAngle(float x1, float y1, float x2, float y2)
501
      {
502
      return (float) Math.atan2(y1-y2, x1-x2);
503
      }
504

    
505
///////////////////////////////////////////////////////////////////////////////////////////////////
506

    
507
    private void actionMove(MotionEvent event)
508
      {
509
      int pointer = event.findPointerIndex(mPtrID1 != INVALID_POINTER_ID ? mPtrID1:mPtrID2);
510

    
511
      if( pointer<0 ) return;
512

    
513
      float pX = event.getX(pointer);
514
      float pY = event.getY(pointer);
515

    
516
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
517
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
518

    
519
      if( mBeginningRotation )
520
        {
521
        if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
522
          {
523
          beginRotation(x,y);
524
          }
525
        }
526
      else if( mContinuingRotation )
527
        {
528
        continueRotation(x,y);
529
        }
530
      else if( mDragging )
531
        {
532
        drag(event,x,y);
533
        }
534
      else
535
        {
536
        setUpDragOrRotate(false,x,y);
537
        }
538
      }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541

    
542
    private void actionDown(MotionEvent event)
543
      {
544
      mPtrID1 = event.getPointerId(0);
545

    
546
      float x = event.getX();
547
      float y = event.getY();
548

    
549
      mX = (x - mScreenWidth*0.5f)/mScreenMin;
550
      mY = (mScreenHeight*0.5f -y)/mScreenMin;
551

    
552
      setUpDragOrRotate(true,mX,mY);
553
      }
554

    
555
///////////////////////////////////////////////////////////////////////////////////////////////////
556

    
557
    private void actionUp(MotionEvent event)
558
      {
559
      mPtrID1 = INVALID_POINTER_ID;
560
      mPtrID2 = INVALID_POINTER_ID;
561

    
562
      if( mContinuingRotation )
563
        {
564
        finishRotation();
565
        }
566

    
567
      if( mLastCubitColor>=0 )
568
        {
569
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
570
        mLastCubitColor = -1;
571
        }
572
      }
573

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

    
576
    private void actionDown2(MotionEvent event)
577
      {
578
      int index = event.getActionIndex();
579

    
580
      if( mPtrID1==INVALID_POINTER_ID )
581
        {
582
        mPtrID1 = event.getPointerId(index);
583
        float x = event.getX();
584
        float y = event.getY();
585

    
586
        if( mPtrID2 != INVALID_POINTER_ID )
587
          {
588
          int pointer = event.findPointerIndex(mPtrID2);
589

    
590
          float x2 = event.getX(pointer);
591
          float y2 = event.getY(pointer);
592

    
593
          mRotAngle = getAngle(x,-y,x2,-y2);
594
          mInitDistance = -1;
595
          }
596

    
597
        mX = (x - mScreenWidth*0.5f)/mScreenMin;
598
        mY = (mScreenHeight*0.5f -y)/mScreenMin;
599
        }
600
      else if( mPtrID2==INVALID_POINTER_ID )
601
        {
602
        mPtrID2 = event.getPointerId(index);
603

    
604
        float x = event.getX();
605
        float y = event.getY();
606

    
607
        if( mPtrID2 != INVALID_POINTER_ID )
608
          {
609
          int pointer = event.findPointerIndex(mPtrID2);
610

    
611
          float x2 = event.getX(pointer);
612
          float y2 = event.getY(pointer);
613

    
614
          mRotAngle = getAngle(x,-y,x2,-y2);
615
          mInitDistance = -1;
616
          }
617

    
618
        if( mBeginningRotation || mContinuingRotation )
619
          {
620
          mX = (x - mScreenWidth*0.5f)/mScreenMin;
621
          mY = (mScreenHeight*0.5f -y)/mScreenMin;
622
          }
623
        }
624

    
625
      if( mBeginningRotation )
626
        {
627
        mContinuingRotation = false;
628
        mBeginningRotation  = false;
629
        mDragging           = true;
630
        }
631
      else if( mContinuingRotation )
632
        {
633
        finishRotation();
634
        }
635
      }
636

    
637
///////////////////////////////////////////////////////////////////////////////////////////////////
638

    
639
    private void actionUp2(MotionEvent event)
640
      {
641
      int index = event.getActionIndex();
642

    
643
      if( index==event.findPointerIndex(mPtrID1) )
644
        {
645
        mPtrID1 = INVALID_POINTER_ID;
646
        int pointer = event.findPointerIndex(mPtrID2);
647

    
648
        if( pointer>=0 )
649
          {
650
          float x1 = event.getX(pointer);
651
          float y1 = event.getY(pointer);
652

    
653
          mX = (x1 - mScreenWidth*0.5f)/mScreenMin;
654
          mY = (mScreenHeight*0.5f -y1)/mScreenMin;
655
          }
656
        }
657
      else if( index==event.findPointerIndex(mPtrID2) )
658
        {
659
        mPtrID2 = INVALID_POINTER_ID;
660
        }
661
      }
662

    
663
///////////////////////////////////////////////////////////////////////////////////////////////////
664

    
665
    void initialize()
666
      {
667
      mPtrID1 = INVALID_POINTER_ID;
668
      mPtrID2 = INVALID_POINTER_ID;
669
      }
670

    
671
///////////////////////////////////////////////////////////////////////////////////////////////////
672
// PUBLIC API
673
///////////////////////////////////////////////////////////////////////////////////////////////////
674

    
675
    public RubikSurfaceView(Context context, AttributeSet attrs)
676
      {
677
      super(context,attrs);
678

    
679
      if(!isInEditMode())
680
        {
681
        mLastCubitColor = -1;
682
        mCurrRotSpeed   = 0.0f;
683

    
684
        mLastX = new float[NUM_SPEED_PROBES];
685
        mLastY = new float[NUM_SPEED_PROBES];
686
        mLastT = new long[NUM_SPEED_PROBES];
687
        mFirstIndex =0;
688
        mLastIndex  =0;
689

    
690
        mRenderer  = new RubikRenderer(this);
691
        mPreRender = new RubikPreRender(this);
692

    
693
        RubikActivity act = (RubikActivity)context;
694
        DisplayMetrics dm = new DisplayMetrics();
695
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
696

    
697
        mDensity = dm.densityDpi;
698

    
699
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
700

    
701
        try
702
          {
703
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
704
          setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
705
          setRenderer(mRenderer);
706
          }
707
        catch(Exception ex)
708
          {
709
          act.OpenGLError("This device does not support OpenGL ES 3.0");
710

    
711
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
712
          String version = GLES30.glGetString(GLES30.GL_VERSION);
713
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
714
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
715

    
716
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
717
          crashlytics.setCustomKey("GLSL Version"  , shading );
718
          crashlytics.setCustomKey("GLversion"     , version );
719
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
720
          crashlytics.setCustomKey("GLSLrenderer"  , renderer);
721
          crashlytics.recordException(ex);
722
          }
723
        }
724
      }
725

    
726
///////////////////////////////////////////////////////////////////////////////////////////////////
727

    
728
    @Override
729
    public boolean onTouchEvent(MotionEvent event)
730
      {
731
      int action = event.getActionMasked();
732

    
733
      switch(action)
734
         {
735
         case MotionEvent.ACTION_DOWN        : actionDown(event) ; break;
736
         case MotionEvent.ACTION_MOVE        : actionMove(event) ; break;
737
         case MotionEvent.ACTION_UP          : actionUp(event)   ; break;
738
         case MotionEvent.ACTION_POINTER_DOWN: actionDown2(event); break;
739
         case MotionEvent.ACTION_POINTER_UP  : actionUp2(event)  ; break;
740
         }
741

    
742
      return true;
743
      }
744
}
745

    
(4-4/4)