Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ 967b79dc

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.screens.RubikScreenReady;
38
import org.distorted.solvers.SolverMain;
39
import org.distorted.screens.ScreenList;
40
import org.distorted.screens.RubikScreenPlay;
41
import org.distorted.screens.RubikScreenSolver;
42
import org.distorted.screens.RubikScreenSolving;
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, 0, 0);
62

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

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

    
82
    private int mPointer1, mPointer2;
83
    private float mX1, mY1, mX2, mY2, mX, mY;
84
    private boolean mIsAutomatic;
85

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

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

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

    
96
      mScreenMin = Math.min(width, height);
97
      }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
    boolean isVertical()
102
      {
103
      return mScreenHeight>mScreenWidth;
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
    RubikRenderer getRenderer()
109
      {
110
      return mRenderer;
111
      }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
    RubikPreRender getPreRender()
116
      {
117
      return mPreRender;
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
    void setQuat()
123
      {
124
      mQuat.set(mTemp);
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
    Static4D getQuat()
130
      {
131
      return mQuat;
132
      }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
    void setMovement(Movement movement)
137
      {
138
      mMovement = movement;
139
      }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

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

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

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

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

    
162
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
163
        }
164

    
165
      return new Static4D(0f, 0f, 0f, 1f);
166
      }
167

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

    
172
    private void computeCurrentAxis(Static4D axis)
173
      {
174
      Static4D result = rotateVectorByQuat(axis, mQuat);
175

    
176
      mAxisX =result.get0();
177
      mAxisY =result.get1();
178

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

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// return quat1*quat2
186

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

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

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

    
204
      return new Static4D(tx,ty,tz,tw);
205
      }
206

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

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

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

    
220
      return quatMultiply(tmp,quatInverted);
221
      }
222

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

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

    
233
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
234
      Static4D tmp = quatMultiply(quatInverted,vector);
235

    
236
      return quatMultiply(tmp,quat);
237
      }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

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

    
246
      mLastIndex++;
247
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
248

    
249
      mLastT[mLastIndex] = currTime;
250
      mLastX[mLastIndex] = x;
251
      mLastY[mLastIndex] = y;
252

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

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

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

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

    
278
      long timeDiff = lastTime-firstTime;
279

    
280
      mLastIndex = 0;
281
      mFirstIndex= 0;
282

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

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

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

    
294
      return distInPixels/mDensity;
295
      }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
    private void setUpDragOrRotate(boolean down, float x, float y)
300
      {
301
      int mode = ScreenList.getMode();
302

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

    
314
        Static4D touchPoint = new Static4D(x, y, 0, 0);
315
        Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuat);
316
        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
317

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

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

    
331
            if( down )
332
              {
333
              RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
334
              mLastCubitFace = mMovement.getTouchedFace();
335
              float[] point = mMovement.getTouchedPoint3D();
336
              int color = solver.getCurrentColor();
337
              mLastCubit = object.getCubit(point);
338
              mPreRender.setTextureMap( mLastCubit, mLastCubitFace, color );
339
              mLastCubitColor = SolverMain.cubitIsLocked(object.getObjectList(), object.getNumLayers(), mLastCubit);
340
              }
341
            }
342
          }
343
        else
344
          {
345
          final RubikActivity act = (RubikActivity)getContext();
346
          final boolean locked= act.isLocked();
347
          mDragging           = (!locked || mIsAutomatic);
348
          mBeginningRotation  = false;
349
          mContinuingRotation = false;
350
          if( !mDragging ) reddenLockIcon(act);
351
          }
352
        }
353
      }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
    private void reddenLockIcon(RubikActivity act)
358
      {
359
      ScreenList curr = ScreenList.getCurrentScreen();
360

    
361
      if( curr==ScreenList.PLAY )
362
        {
363
        RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
364
        play.reddenLock(act);
365
        }
366
      else if( curr==ScreenList.READ )
367
        {
368
        RubikScreenReady read = (RubikScreenReady) ScreenList.READ.getScreenClass();
369
        read.reddenLock(act);
370
        }
371
      else if( curr==ScreenList.SOLV )
372
        {
373
        RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
374
        solv.reddenLock(act);
375
        }
376
      }
377

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

    
380
    private void drag(float x, float y)
381
      {
382
      if( mPointer1!=INVALID_POINTER_ID && mPointer2!=INVALID_POINTER_ID)
383
        {
384
        float x2 = (mX2 - mScreenWidth*0.5f)/mScreenMin;
385
        float y2 = (mScreenHeight*0.5f - mY2)/mScreenMin;
386

    
387
        float angleNow = getAngle(x,y,x2,y2);
388
        float angleDiff = angleNow-mRotAngle;
389
        float sinA =-(float)Math.sin(angleDiff);
390
        float cosA = (float)Math.cos(angleDiff);
391

    
392
        Static4D dragQuat = quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
393
        mTemp.set(dragQuat);
394

    
395
        mRotAngle = angleNow;
396

    
397
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
398
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
399
        mInitDistance = distNow;
400
        TwistyObject object = mPreRender.getObject();
401
        if( object!=null ) object.setObjectRatio(distQuot);
402
        }
403
      else
404
        {
405
        Static4D dragQuat = quatMultiply(quatFromDrag(mX-x,y-mY), mQuat);
406
        mTemp.set(dragQuat);
407
        }
408

    
409
      mPreRender.setQuatOnNextRender();
410
      mX = x;
411
      mY = y;
412
      }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415

    
416
    private void finishRotation()
417
      {
418
      computeCurrentSpeedInInchesPerSecond();
419
      int angle = mPreRender.getObject().computeNearestAngle(mCurrentAxis,mCurrentAngle, mCurrRotSpeed);
420
      mPreRender.finishRotation(angle);
421
      mPreRender.rememberMove(mCurrentAxis,mCurrentRow,angle);
422

    
423
      if( angle!=0 )
424
        {
425
        if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
426
          {
427
          RubikActivity act = (RubikActivity)getContext();
428
          RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
429
          solving.addMove(act, mCurrentAxis, mCurrentRow, angle);
430
          }
431
        if( ScreenList.getCurrentScreen()== ScreenList.PLAY )
432
          {
433
          RubikActivity act = (RubikActivity)getContext();
434
          RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
435
          play.addMove(act, mCurrentAxis, mCurrentRow, angle);
436
          }
437
        }
438

    
439
      mContinuingRotation = false;
440
      mBeginningRotation  = false;
441
      mDragging           = true;
442
      }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

    
446
    private void continueRotation(float x, float y)
447
      {
448
      float dx = x-mStartRotX;
449
      float dy = y-mStartRotY;
450
      float alpha = dx*mAxisX + dy*mAxisY;
451
      float x2 = dx - alpha*mAxisX;
452
      float y2 = dy - alpha*mAxisY;
453

    
454
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
455

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

    
459
      float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
460
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
461
      mPreRender.getObject().continueRotation(mCurrentAngle);
462

    
463
      addSpeedProbe(x2,y2);
464
      }
465

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

    
468
    private void beginRotation(float x, float y)
469
      {
470
      mStartRotX = x;
471
      mStartRotY = y;
472

    
473
      TwistyObject object = mPreRender.getObject();
474
      int numLayers = object.getNumLayers();
475

    
476
      Static4D touchPoint2 = new Static4D(x, y, 0, 0);
477
      Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuat);
478
      Static2D res = mMovement.newRotation(numLayers,rotatedTouchPoint2);
479

    
480
      mCurrentAxis = (int)res.get0();
481
      mCurrentRow  = (int)res.get1();
482

    
483
      computeCurrentAxis( mMovement.getCastedRotAxis(mCurrentAxis) );
484
      mRotationFactor = mMovement.returnRotationFactor(numLayers,mCurrentRow);
485

    
486
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
487

    
488
      if( ScreenList.getCurrentScreen()== ScreenList.READ )
489
        {
490
        RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
491
        solving.resetElapsed();
492

    
493
        final RubikActivity act = (RubikActivity)getContext();
494

    
495
        act.runOnUiThread(new Runnable()
496
          {
497
          @Override
498
          public void run()
499
            {
500
            ScreenList.switchScreen( act, ScreenList.SOLV);
501
            }
502
          });
503
        }
504

    
505
      addSpeedProbe(x,y);
506

    
507
      mBeginningRotation = false;
508
      mContinuingRotation= true;
509
      }
510

    
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512

    
513
    private float getAngle(float x1, float y1, float x2, float y2)
514
      {
515
      return (float) Math.atan2(y1-y2, x1-x2);
516
      }
517

    
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519

    
520
    void initialize()
521
      {
522
      mPointer1 = INVALID_POINTER_ID;
523
      mPointer2 = INVALID_POINTER_ID;
524
      }
525

    
526
///////////////////////////////////////////////////////////////////////////////////////////////////
527

    
528
    private void prepareDown(MotionEvent event)
529
      {
530
      mPointer1 = event.getPointerId(0);
531
      mX1 = event.getX();
532
      mY1 = event.getY();
533
      mPointer2 = INVALID_POINTER_ID;
534
      }
535

    
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537

    
538
    private void prepareMove(MotionEvent event)
539
      {
540
      int index1 = event.findPointerIndex(mPointer1);
541

    
542
      if( index1>=0 )
543
        {
544
        mX1 = event.getX(index1);
545
        mY1 = event.getY(index1);
546
        }
547

    
548
      int index2 = event.findPointerIndex(mPointer2);
549

    
550
      if( index2>=0 )
551
        {
552
        mX2 = event.getX(index2);
553
        mY2 = event.getY(index2);
554
        }
555
      }
556

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

    
559
    private void prepareUp(MotionEvent event)
560
      {
561
      mPointer1 = INVALID_POINTER_ID;
562
      mPointer2 = INVALID_POINTER_ID;
563
      }
564

    
565
///////////////////////////////////////////////////////////////////////////////////////////////////
566

    
567
    private void prepareDown2(MotionEvent event)
568
      {
569
      int index = event.getActionIndex();
570

    
571
      if( mPointer1==INVALID_POINTER_ID )
572
        {
573
        mPointer1 = event.getPointerId(index);
574
        mX1 = event.getX(index);
575
        mY1 = event.getY(index);
576
        }
577
      else if( mPointer2==INVALID_POINTER_ID )
578
        {
579
        mPointer2 = event.getPointerId(index);
580
        mX2 = event.getX(index);
581
        mY2 = event.getY(index);
582
        }
583
      }
584

    
585
///////////////////////////////////////////////////////////////////////////////////////////////////
586

    
587
    private void prepareUp2(MotionEvent event)
588
      {
589
      int index = event.getActionIndex();
590

    
591
           if( index==event.findPointerIndex(mPointer1) ) mPointer1 = INVALID_POINTER_ID;
592
      else if( index==event.findPointerIndex(mPointer2) ) mPointer2 = INVALID_POINTER_ID;
593
      }
594

    
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596
// PUBLIC API
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598

    
599
    public RubikSurfaceView(Context context, AttributeSet attrs)
600
      {
601
      super(context,attrs);
602

    
603
      if(!isInEditMode())
604
        {
605
        mIsAutomatic = false;
606

    
607
        mLastCubitColor = -1;
608
        mCurrRotSpeed   = 0.0f;
609

    
610
        mLastX = new float[NUM_SPEED_PROBES];
611
        mLastY = new float[NUM_SPEED_PROBES];
612
        mLastT = new long[NUM_SPEED_PROBES];
613
        mFirstIndex =0;
614
        mLastIndex  =0;
615

    
616
        mRenderer  = new RubikRenderer(this);
617
        mPreRender = new RubikPreRender(this);
618

    
619
        RubikActivity act = (RubikActivity)context;
620
        DisplayMetrics dm = new DisplayMetrics();
621
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
622

    
623
        mDensity = dm.densityDpi;
624

    
625
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
626

    
627
        try
628
          {
629
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
630
          int esVersion = configurationInfo.reqGlEsVersion>>16;
631
          setEGLContextClientVersion(esVersion);
632
          setRenderer(mRenderer);
633
          }
634
        catch(Exception ex)
635
          {
636
          act.OpenGLError();
637

    
638
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
639
          String version = GLES30.glGetString(GLES30.GL_VERSION);
640
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
641
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
642

    
643
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
644
          crashlytics.setCustomKey("GLSL Version"  , shading );
645
          crashlytics.setCustomKey("GL version"    , version );
646
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
647
          crashlytics.setCustomKey("GLSL renderer" , renderer);
648
          crashlytics.recordException(ex);
649
          }
650
        }
651
      }
652

    
653
///////////////////////////////////////////////////////////////////////////////////////////////////
654

    
655
    public void prepareDown()
656
      {
657
      mIsAutomatic = true;
658
      mPointer1 = 0;
659
      mPointer2 = INVALID_POINTER_ID;
660
      }
661

    
662
///////////////////////////////////////////////////////////////////////////////////////////////////
663

    
664
    public void prepareDown2()
665
      {
666
      mPointer2 = 0;
667
      }
668

    
669
///////////////////////////////////////////////////////////////////////////////////////////////////
670

    
671
    public void prepareMove(float x1, float y1, float x2, float y2)
672
      {
673
      mX1 = x1;
674
      mY1 = y1;
675
      mX2 = x2;
676
      mY2 = y2;
677
      }
678

    
679
///////////////////////////////////////////////////////////////////////////////////////////////////
680

    
681
    public void prepareUp()
682
      {
683
      mIsAutomatic = false;
684
      mPointer1 = INVALID_POINTER_ID;
685
      mPointer2 = INVALID_POINTER_ID;
686
      }
687

    
688
///////////////////////////////////////////////////////////////////////////////////////////////////
689

    
690
    public void actionMove(float x1, float y1, float x2, float y2)
691
      {
692
      float pX = mPointer1 != INVALID_POINTER_ID ? x1 : x2;
693
      float pY = mPointer1 != INVALID_POINTER_ID ? y1 : y2;
694

    
695
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
696
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
697

    
698
      if( mBeginningRotation )
699
        {
700
        if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
701
          {
702
          beginRotation(x,y);
703
          }
704
        }
705
      else if( mContinuingRotation )
706
        {
707
        continueRotation(x,y);
708
        }
709
      else if( mDragging )
710
        {
711
        drag(x,y);
712
        }
713
      else
714
        {
715
        setUpDragOrRotate(false,x,y);
716
        }
717
      }
718

    
719
///////////////////////////////////////////////////////////////////////////////////////////////////
720

    
721
    public void actionDown(float x, float y)
722
      {
723
      mX = (x -  mScreenWidth*0.5f)/mScreenMin;
724
      mY = (mScreenHeight*0.5f - y)/mScreenMin;
725

    
726
      setUpDragOrRotate(true,mX,mY);
727
      }
728

    
729
///////////////////////////////////////////////////////////////////////////////////////////////////
730

    
731
    public void actionUp()
732
      {
733
      if( mContinuingRotation )
734
        {
735
        finishRotation();
736
        }
737

    
738
      if( mLastCubitColor>=0 )
739
        {
740
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
741
        mLastCubitColor = -1;
742
        }
743
      }
744

    
745
///////////////////////////////////////////////////////////////////////////////////////////////////
746

    
747
    public void actionDown2(float x1, float y1, float x2, float y2)
748
      {
749
      mRotAngle = getAngle(x1,-y1, x2,-y2);
750
      mInitDistance = -1;
751

    
752
      mX = (x1 - mScreenWidth*0.5f )/mScreenMin;
753
      mY = (mScreenHeight*0.5f - y1)/mScreenMin;
754

    
755
      if( mBeginningRotation )
756
        {
757
        mContinuingRotation = false;
758
        mBeginningRotation  = false;
759
        mDragging           = true;
760
        }
761
      else if( mContinuingRotation )
762
        {
763
        finishRotation();
764
        }
765
      }
766

    
767
///////////////////////////////////////////////////////////////////////////////////////////////////
768

    
769
    public void actionUp2(boolean p1isUp, float x1, float y1, boolean p2isUp, float x2, float y2)
770
      {
771
      if( p1isUp )
772
        {
773
        mX = (x2 -  mScreenWidth*0.5f)/mScreenMin;
774
        mY = (mScreenHeight*0.5f - y2)/mScreenMin;
775
        }
776
      if( p2isUp )
777
        {
778
        mX = (x1 -  mScreenWidth*0.5f)/mScreenMin;
779
        mY = (mScreenHeight*0.5f - y1)/mScreenMin;
780
        }
781
      }
782

    
783
///////////////////////////////////////////////////////////////////////////////////////////////////
784

    
785
    @Override
786
    public boolean onTouchEvent(MotionEvent event)
787
      {
788
      int action = event.getActionMasked();
789

    
790
      switch(action)
791
         {
792
         case MotionEvent.ACTION_DOWN        : prepareDown(event);
793
                                               actionDown(mX1, mY1);
794
                                               break;
795
         case MotionEvent.ACTION_MOVE        : prepareMove(event);
796
                                               actionMove(mX1, mY1, mX2, mY2);
797
                                               break;
798
         case MotionEvent.ACTION_UP          : prepareUp(event);
799
                                               actionUp();
800
                                               break;
801
         case MotionEvent.ACTION_POINTER_DOWN: prepareDown2(event);
802
                                               actionDown2(mX1, mY1, mX2, mY2);
803
                                               break;
804
         case MotionEvent.ACTION_POINTER_UP  : prepareUp2(event);
805
                                               boolean p1isUp = mPointer1==INVALID_POINTER_ID;
806
                                               boolean p2isUp = mPointer2==INVALID_POINTER_ID;
807
                                               actionUp2(p1isUp, mX1, mY1, p2isUp, mX2, mY2);
808
                                               break;
809
         }
810

    
811
      return true;
812
      }
813
}
814

    
(4-4/4)