Project

General

Profile

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

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

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.screens.ScreenList;
39
import org.distorted.screens.RubikScreenPlay;
40
import org.distorted.screens.RubikScreenSolver;
41
import org.distorted.screens.RubikScreenSolving;
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, 0, 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 final Static4D mQuat= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
84
    private static final 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 = ScreenList.getMode();
299

    
300
      if( mode==MODE_DRAG )
301
        {
302
        mDragging           = true;
303
        mBeginningRotation  = false;
304
        mContinuingRotation = false;
305
        }
306
      else
307
        {
308
        TwistyObject object = mPreRender.getObject();
309
        CAMERA_POINT.set2( object==null ? 1.21f : object.getCameraDist() );
310

    
311
        Static4D touchPoint = new Static4D(x, y, 0, 0);
312
        Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuat);
313
        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
314

    
315
        if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint,rotatedCamera) )
316
          {
317
          mDragging           = false;
318
          mContinuingRotation = false;
319

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

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

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

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

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

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

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

    
374
          return;
375
          }
376

    
377
        float x2 = (pX - mScreenWidth*0.5f)/mScreenMin;
378
        float y2 = (mScreenHeight*0.5f -pY)/mScreenMin;
379

    
380
        float angleNow = getAngle(x,y,x2,y2);
381
        float angleDiff = angleNow-mRotAngle;
382
        float sinA =-(float)Math.sin(angleDiff);
383
        float cosA = (float)Math.cos(angleDiff);
384

    
385
        Static4D dragQuat = quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
386
        mTemp.set(dragQuat);
387

    
388
        mRotAngle = angleNow;
389

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

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

    
403
      mPreRender.setQuatOnNextRender();
404
      mX = x;
405
      mY = y;
406
      }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

    
410
    private void finishRotation()
411
      {
412
      computeCurrentSpeedInInchesPerSecond();
413
      int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
414
      mPreRender.finishRotation(angle);
415
      mPreRender.rememberMove(mCurrentAxis,mCurrentRow,angle);
416

    
417
      if( angle!=0 )
418
        {
419
        if( ScreenList.getCurrentState()== ScreenList.SOLV )
420
          {
421
          RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getStateClass();
422
          solving.addMove(mCurrentAxis, mCurrentRow, angle);
423
          }
424
        if( ScreenList.getCurrentState()== ScreenList.PLAY )
425
          {
426
          RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getStateClass();
427
          play.addMove(mCurrentAxis, mCurrentRow, angle);
428
          }
429
        }
430

    
431
      mContinuingRotation = false;
432
      mBeginningRotation  = false;
433
      mDragging           = true;
434
      }
435

    
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437

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

    
446
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
447

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

    
451
      float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
452
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
453
      mPreRender.getObject().continueRotation(mCurrentAngle);
454

    
455
      addSpeedProbe(x2,y2);
456
      }
457

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

    
460
    private void beginRotation(float x, float y)
461
      {
462
      mStartRotX = x;
463
      mStartRotY = y;
464

    
465
      TwistyObject object = mPreRender.getObject();
466
      int numLayers = object.getNumLayers();
467

    
468
      Static4D touchPoint2 = new Static4D(x, y, 0, 0);
469
      Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuat);
470
      Static2D res = mMovement.newRotation(numLayers,rotatedTouchPoint2);
471

    
472
      mCurrentAxis = (int)res.get0();
473
      mCurrentRow  = (int)res.get1();
474

    
475
      computeCurrentAxis( mMovement.getCastedRotAxis(mCurrentAxis) );
476
      mRotationFactor = mMovement.returnRotationFactor(numLayers,mCurrentRow);
477

    
478
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
479

    
480
      if( ScreenList.getCurrentState()== ScreenList.READ )
481
        {
482
        RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getStateClass();
483
        solving.resetElapsed();
484

    
485
        final RubikActivity act = (RubikActivity)getContext();
486

    
487
        act.runOnUiThread(new Runnable()
488
          {
489
          @Override
490
          public void run()
491
            {
492
            ScreenList.switchState( act, ScreenList.SOLV);
493
            }
494
          });
495
        }
496

    
497
      addSpeedProbe(x,y);
498

    
499
      mBeginningRotation = false;
500
      mContinuingRotation= true;
501
      }
502

    
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504

    
505
    private float getAngle(float x1, float y1, float x2, float y2)
506
      {
507
      return (float) Math.atan2(y1-y2, x1-x2);
508
      }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511

    
512
    private void actionMove(MotionEvent event)
513
      {
514
      int pointer = event.findPointerIndex(mPtrID1 != INVALID_POINTER_ID ? mPtrID1:mPtrID2);
515

    
516
      if( pointer<0 ) return;
517

    
518
      float pX = event.getX(pointer);
519
      float pY = event.getY(pointer);
520

    
521
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
522
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
523

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

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546

    
547
    private void actionDown(MotionEvent event)
548
      {
549
      mPtrID1 = event.getPointerId(0);
550

    
551
      float x = event.getX();
552
      float y = event.getY();
553

    
554
      mX = (x - mScreenWidth*0.5f)/mScreenMin;
555
      mY = (mScreenHeight*0.5f -y)/mScreenMin;
556

    
557
      setUpDragOrRotate(true,mX,mY);
558
      }
559

    
560
///////////////////////////////////////////////////////////////////////////////////////////////////
561

    
562
    private void actionUp(MotionEvent event)
563
      {
564
      mPtrID1 = INVALID_POINTER_ID;
565
      mPtrID2 = INVALID_POINTER_ID;
566

    
567
      if( mContinuingRotation )
568
        {
569
        finishRotation();
570
        }
571

    
572
      if( mLastCubitColor>=0 )
573
        {
574
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
575
        mLastCubitColor = -1;
576
        }
577
      }
578

    
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580

    
581
    private void actionDown2(MotionEvent event)
582
      {
583
      int index = event.getActionIndex();
584

    
585
      if( mPtrID1==INVALID_POINTER_ID )
586
        {
587
        mPtrID1 = event.getPointerId(index);
588
        float x = event.getX();
589
        float y = event.getY();
590

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

    
595
          try
596
            {
597
            float x2 = event.getX(pointer);
598
            float y2 = event.getY(pointer);
599

    
600
            mRotAngle = getAngle(x,-y,x2,-y2);
601
            mInitDistance = -1;
602
            }
603
          catch(IllegalArgumentException ex)
604
            {
605
            mPtrID1=INVALID_POINTER_ID;
606
            mPtrID2=INVALID_POINTER_ID;
607

    
608
            FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
609
            crashlytics.setCustomKey("DragError", "pointer="+pointer );
610
            crashlytics.recordException(ex);
611

    
612
            return;
613
            }
614
          }
615

    
616
        mX = (x - mScreenWidth*0.5f)/mScreenMin;
617
        mY = (mScreenHeight*0.5f -y)/mScreenMin;
618
        }
619
      else if( mPtrID2==INVALID_POINTER_ID )
620
        {
621
        mPtrID2 = event.getPointerId(index);
622

    
623
        float x = event.getX();
624
        float y = event.getY();
625

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

    
630
          try
631
            {
632
            float x2 = event.getX(pointer);
633
            float y2 = event.getY(pointer);
634

    
635
            mRotAngle = getAngle(x,-y,x2,-y2);
636
            mInitDistance = -1;
637
            }
638
          catch(IllegalArgumentException ex)
639
            {
640
            mPtrID1=INVALID_POINTER_ID;
641
            mPtrID2=INVALID_POINTER_ID;
642

    
643
            FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
644
            crashlytics.setCustomKey("DragError", "pointer="+pointer );
645
            crashlytics.recordException(ex);
646

    
647
            return;
648
            }
649
          }
650

    
651
        if( mBeginningRotation || mContinuingRotation )
652
          {
653
          mX = (x - mScreenWidth*0.5f)/mScreenMin;
654
          mY = (mScreenHeight*0.5f -y)/mScreenMin;
655
          }
656
        }
657

    
658
      if( mBeginningRotation )
659
        {
660
        mContinuingRotation = false;
661
        mBeginningRotation  = false;
662
        mDragging           = true;
663
        }
664
      else if( mContinuingRotation )
665
        {
666
        finishRotation();
667
        }
668
      }
669

    
670
///////////////////////////////////////////////////////////////////////////////////////////////////
671

    
672
    private void actionUp2(MotionEvent event)
673
      {
674
      int index = event.getActionIndex();
675

    
676
      if( index==event.findPointerIndex(mPtrID1) )
677
        {
678
        mPtrID1 = INVALID_POINTER_ID;
679
        int pointer = event.findPointerIndex(mPtrID2);
680

    
681
        if( pointer>=0 )
682
          {
683
          float x1 = event.getX(pointer);
684
          float y1 = event.getY(pointer);
685

    
686
          mX = (x1 - mScreenWidth*0.5f)/mScreenMin;
687
          mY = (mScreenHeight*0.5f -y1)/mScreenMin;
688
          }
689
        }
690
      else if( index==event.findPointerIndex(mPtrID2) )
691
        {
692
        mPtrID2 = INVALID_POINTER_ID;
693
        }
694
      }
695

    
696
///////////////////////////////////////////////////////////////////////////////////////////////////
697

    
698
    void initialize()
699
      {
700
      mPtrID1 = INVALID_POINTER_ID;
701
      mPtrID2 = INVALID_POINTER_ID;
702
      }
703

    
704
///////////////////////////////////////////////////////////////////////////////////////////////////
705
// PUBLIC API
706
///////////////////////////////////////////////////////////////////////////////////////////////////
707

    
708
    public RubikSurfaceView(Context context, AttributeSet attrs)
709
      {
710
      super(context,attrs);
711

    
712
      if(!isInEditMode())
713
        {
714
        mLastCubitColor = -1;
715
        mCurrRotSpeed   = 0.0f;
716

    
717
        mLastX = new float[NUM_SPEED_PROBES];
718
        mLastY = new float[NUM_SPEED_PROBES];
719
        mLastT = new long[NUM_SPEED_PROBES];
720
        mFirstIndex =0;
721
        mLastIndex  =0;
722

    
723
        mRenderer  = new RubikRenderer(this);
724
        mPreRender = new RubikPreRender(this);
725

    
726
        RubikActivity act = (RubikActivity)context;
727
        DisplayMetrics dm = new DisplayMetrics();
728
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
729

    
730
        mDensity = dm.densityDpi;
731

    
732
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
733

    
734
        try
735
          {
736
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
737
          int esVersion = configurationInfo.reqGlEsVersion>>16;
738
          setEGLContextClientVersion(esVersion);
739
          setRenderer(mRenderer);
740
          }
741
        catch(Exception ex)
742
          {
743
          act.OpenGLError();
744

    
745
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
746
          String version = GLES30.glGetString(GLES30.GL_VERSION);
747
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
748
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
749

    
750
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
751
          crashlytics.setCustomKey("GLSL Version"  , shading );
752
          crashlytics.setCustomKey("GLversion"     , version );
753
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
754
          crashlytics.setCustomKey("GLSLrenderer"  , renderer);
755
          crashlytics.recordException(ex);
756
          }
757
        }
758
      }
759

    
760
///////////////////////////////////////////////////////////////////////////////////////////////////
761

    
762
    @Override
763
    public boolean onTouchEvent(MotionEvent event)
764
      {
765
      int action = event.getActionMasked();
766

    
767
      switch(action)
768
         {
769
         case MotionEvent.ACTION_DOWN        : actionDown(event) ; break;
770
         case MotionEvent.ACTION_MOVE        : actionMove(event) ; break;
771
         case MotionEvent.ACTION_UP          : actionUp(event)   ; break;
772
         case MotionEvent.ACTION_POINTER_DOWN: actionDown2(event); break;
773
         case MotionEvent.ACTION_POINTER_UP  : actionUp2(event)  ; break;
774
         }
775

    
776
      return true;
777
      }
778
}
779

    
(4-4/4)