Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ 935f3663

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 (which is already casted to the surface of the
168
// currently touched face AND converted into a 4D vector - fourth 0) to a 2D in-screen-surface axis
169

    
170
    private void computeCurrentAxis(Static4D axis)
171
      {
172
      Static4D result = rotateVectorByQuat(axis, 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  = object.computeRowFromOffset(offset);
470

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

    
474
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
475

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

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

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

    
493
      addSpeedProbe(x,y);
494

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

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500

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

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507

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

    
512
      if( pointer<0 ) return;
513

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

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

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

    
541
///////////////////////////////////////////////////////////////////////////////////////////////////
542

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

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

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

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

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557

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

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

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

    
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576

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

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

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

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

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

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

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

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

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

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

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

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

    
638
///////////////////////////////////////////////////////////////////////////////////////////////////
639

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

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

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

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

    
664
///////////////////////////////////////////////////////////////////////////////////////////////////
665

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

    
672
///////////////////////////////////////////////////////////////////////////////////////////////////
673
// PUBLIC API
674
///////////////////////////////////////////////////////////////////////////////////////////////////
675

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

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

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

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

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

    
698
        mDensity = dm.densityDpi;
699

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

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

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

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

    
727
///////////////////////////////////////////////////////////////////////////////////////////////////
728

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

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

    
743
      return true;
744
      }
745
}
746

    
(4-4/4)