Project

General

Profile

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

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

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.Static4D;
35
import org.distorted.objects.TwistyObject;
36
import org.distorted.objects.Movement;
37
import org.distorted.solvers.SolverMain;
38
import org.distorted.states.StateList;
39
import org.distorted.states.RubikStatePlay;
40
import org.distorted.states.RubikStateSolver;
41
import org.distorted.states.RubikStateSolving;
42

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

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

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

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

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

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

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

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

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

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

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

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

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

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

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
    void setMovement(Movement movement)
134
      {
135
      mMovement = movement;
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

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

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

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

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

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

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

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
// cast the 3D axis we are currently rotating along (which is already casted to the surface of the
167
// currently touched face AND converted into a 4D vector - fourth 0) to a 2D in-screen-surface axis
168

    
169
    private void computeCurrentAxis(Static4D axis)
170
      {
171
      Static4D result = rotateVectorByQuat(axis, mQuat);
172

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

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

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
// return quat1*quat2
183

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

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

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

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

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

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

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

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

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

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

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

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

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

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

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

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

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

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

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

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

    
275
      long timeDiff = lastTime-firstTime;
276

    
277
      mLastIndex = 0;
278
      mFirstIndex= 0;
279

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

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

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

    
291
      return distInPixels/mDensity;
292
      }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

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

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

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

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

    
325
            if( down )
326
              {
327
              RubikStateSolver solver = (RubikStateSolver) StateList.SVER.getStateClass();
328
              mLastCubitFace = mMovement.getTouchedFace();
329
              float[] point = mMovement.getTouchedPoint3D();
330
              int color = solver.getCurrentColor();
331
              TwistyObject object = mPreRender.getObject();
332
              mLastCubit = object.getCubit(point);
333
              mPreRender.setTextureMap( mLastCubit, mLastCubitFace, color );
334
              mLastCubitColor = SolverMain.cubitIsLocked(object.getObjectList(), object.getSize(), mLastCubit);
335
              }
336
            }
337
          }
338
        else
339
          {
340
          final RubikActivity act = (RubikActivity)getContext();
341

    
342
          mDragging           = !act.isLocked();
343
          mBeginningRotation  = false;
344
          mContinuingRotation = false;
345
          }
346
        }
347
      }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

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

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

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

    
372
          return;
373
          }
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
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
389
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
390
        mInitDistance = distNow;
391

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

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

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

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

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

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

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

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

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

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

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

    
452
      addSpeedProbe(x2,y2);
453
      }
454

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456

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

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

    
465
      Static2D res = mMovement.newRotation(rotatedTouchPoint2);
466
      TwistyObject object = mPreRender.getObject();
467

    
468
      mCurrentAxis = (int)res.get0();
469
      float offset = res.get1();
470
      mCurrentRow  = object.computeRowFromOffset(offset);
471

    
472
      computeCurrentAxis( mMovement.getCastedRotAxis(mCurrentAxis) );
473
      mRotationFactor = object.returnRotationFactor(offset);
474

    
475
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
476

    
477
      if( StateList.getCurrentState()== StateList.READ )
478
        {
479
        RubikStateSolving solving = (RubikStateSolving) StateList.SOLV.getStateClass();
480
        solving.resetElapsed();
481

    
482
        final RubikActivity act = (RubikActivity)getContext();
483

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

    
494
      addSpeedProbe(x,y);
495

    
496
      mBeginningRotation = false;
497
      mContinuingRotation= true;
498
      }
499

    
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501

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

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508

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

    
513
      if( pointer<0 ) return;
514

    
515
      float pX = event.getX(pointer);
516
      float pY = event.getY(pointer);
517

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

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

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

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

    
548
      float x = event.getX();
549
      float y = event.getY();
550

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

    
554
      setUpDragOrRotate(true,mX,mY);
555
      }
556

    
557
///////////////////////////////////////////////////////////////////////////////////////////////////
558

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

    
564
      if( mContinuingRotation )
565
        {
566
        finishRotation();
567
        }
568

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

    
576
///////////////////////////////////////////////////////////////////////////////////////////////////
577

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

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

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

    
592
          float x2 = event.getX(pointer);
593
          float y2 = event.getY(pointer);
594

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

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

    
606
        float x = event.getX();
607
        float y = event.getY();
608

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

    
613
          float x2 = event.getX(pointer);
614
          float y2 = event.getY(pointer);
615

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

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

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

    
639
///////////////////////////////////////////////////////////////////////////////////////////////////
640

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

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

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

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

    
665
///////////////////////////////////////////////////////////////////////////////////////////////////
666

    
667
    void initialize()
668
      {
669
      mPtrID1 = INVALID_POINTER_ID;
670
      mPtrID2 = INVALID_POINTER_ID;
671
      }
672

    
673
///////////////////////////////////////////////////////////////////////////////////////////////////
674
// PUBLIC API
675
///////////////////////////////////////////////////////////////////////////////////////////////////
676

    
677
    public RubikSurfaceView(Context context, AttributeSet attrs)
678
      {
679
      super(context,attrs);
680

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

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

    
692
        mRenderer  = new RubikRenderer(this);
693
        mPreRender = new RubikPreRender(this);
694

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

    
699
        mDensity = dm.densityDpi;
700

    
701
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
702

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

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

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

    
728
///////////////////////////////////////////////////////////////////////////////////////////////////
729

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

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

    
744
      return true;
745
      }
746
}
747

    
(4-4/4)