Project

General

Profile

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

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

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.main;
21 0c52af30 Leszek Koltunski
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25 4489e68e Leszek Koltunski
import android.opengl.GLES30;
26 0c52af30 Leszek Koltunski
import android.opengl.GLSurfaceView;
27
import android.util.AttributeSet;
28 cd83d0aa Leszek Koltunski
import android.util.DisplayMetrics;
29 0c52af30 Leszek Koltunski
import android.view.MotionEvent;
30
31 b96a20a4 Leszek Koltunski
import com.google.firebase.crashlytics.FirebaseCrashlytics;
32
33 775e675d Leszek Koltunski
import org.distorted.library.type.Static2D;
34 12ad3fca Leszek Koltunski
import org.distorted.library.type.Static3D;
35 0c52af30 Leszek Koltunski
import org.distorted.library.type.Static4D;
36 1f9772f3 Leszek Koltunski
import org.distorted.objects.RubikObject;
37 faa3aed0 Leszek Koltunski
import org.distorted.objects.RubikMovement;
38 46a961fd Leszek Koltunski
import org.distorted.solvers.SolverMain;
39 1f9772f3 Leszek Koltunski
import org.distorted.states.RubikState;
40 15846fe4 Leszek Koltunski
import org.distorted.states.RubikStatePlay;
41 473611ee Leszek Koltunski
import org.distorted.states.RubikStateSolver;
42 1f9772f3 Leszek Koltunski
import org.distorted.states.RubikStateSolving;
43 0c52af30 Leszek Koltunski
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 beb325a0 Leszek Koltunski
public class RubikSurfaceView extends GLSurfaceView
47 0c52af30 Leszek Koltunski
{
48 168b6b56 Leszek Koltunski
    private static final int NUM_SPEED_PROBES = 10;
49 a4472437 Leszek Koltunski
    private static final int INVALID_POINTER_ID = -1;
50 168b6b56 Leszek Koltunski
51 473611ee Leszek Koltunski
    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 775e675d Leszek Koltunski
    // 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 4da7d87a Leszek Koltunski
    // Moving the finger by 0.3 of an inch will start a Rotation.
59
    private final static float ROTATION_SENSITIVITY = 0.3f;
60 eac805bd Leszek Koltunski
61 c7e79b69 Leszek Koltunski
    private final Static4D CAMERA_POINT = new Static4D(0, 0, 1, 0);
62 86c73a69 Leszek Koltunski
63 beb325a0 Leszek Koltunski
    private RubikRenderer mRenderer;
64 5a4d4fba Leszek Koltunski
    private RubikPreRender mPreRender;
65 faa3aed0 Leszek Koltunski
    private RubikMovement mMovement;
66 0c52af30 Leszek Koltunski
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
67 beb325a0 Leszek Koltunski
    private int mScreenWidth, mScreenHeight, mScreenMin;
68 0c52af30 Leszek Koltunski
69 c7b00dfb Leszek Koltunski
    private float mRotAngle, mInitDistance;
70 a4472437 Leszek Koltunski
    private int mPtrID1, mPtrID2;
71 0b7e1b05 Leszek Koltunski
    private float mX, mY;
72 12ad3fca Leszek Koltunski
    private float mStartRotX, mStartRotY;
73
    private float mAxisX, mAxisY;
74 e46e17fb Leszek Koltunski
    private float mRotationFactor;
75 46a961fd Leszek Koltunski
    private int mLastCubitColor, mLastCubitFace, mLastCubit;
76 0e5ad27c Leszek Koltunski
    private int mCurrentAxis, mCurrentRow;
77 168b6b56 Leszek Koltunski
    private float mCurrentAngle, mCurrRotSpeed;
78 4c864c68 Leszek Koltunski
    private float[] mLastX;
79
    private float[] mLastY;
80
    private long[] mLastT;
81 168b6b56 Leszek Koltunski
    private int mFirstIndex, mLastIndex;
82 cd83d0aa Leszek Koltunski
    private int mDensity;
83 12ad3fca Leszek Koltunski
84 4da7d87a Leszek Koltunski
    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 ee5c2ae1 Leszek Koltunski
87 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89
    void setScreenSize(int width, int height)
90
      {
91
      mScreenWidth = width;
92
      mScreenHeight= height;
93
94 12ad3fca Leszek Koltunski
      mScreenMin = Math.min(width, height);
95 0fd3ac1f Leszek Koltunski
      }
96
97 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99
    boolean isVertical()
100
      {
101
      return mScreenHeight>mScreenWidth;
102
      }
103
104 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106
    RubikRenderer getRenderer()
107
      {
108
      return mRenderer;
109
      }
110
111 8becce57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
112
113 5a4d4fba Leszek Koltunski
    RubikPreRender getPreRender()
114 8becce57 Leszek Koltunski
      {
115 5a4d4fba Leszek Koltunski
      return mPreRender;
116 8becce57 Leszek Koltunski
      }
117
118 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120 4da7d87a Leszek Koltunski
    void setQuat()
121 0fd3ac1f Leszek Koltunski
      {
122 4da7d87a Leszek Koltunski
      mQuat.set(mTemp);
123 0fd3ac1f Leszek Koltunski
      }
124
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
127 4da7d87a Leszek Koltunski
    Static4D getQuat()
128 0fd3ac1f Leszek Koltunski
      {
129 4da7d87a Leszek Koltunski
      return mQuat;
130 0fd3ac1f Leszek Koltunski
      }
131
132 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
133
134 faa3aed0 Leszek Koltunski
    void setMovement(RubikMovement movement)
135 27a70eae Leszek Koltunski
      {
136
      mMovement = movement;
137
      }
138
139 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 12ad3fca Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
167
// cast the 3D axis we are currently rotating along to the 2D in-screen-surface axis
168
169
    private void computeCurrentAxis(Static3D axis)
170
      {
171
      Static4D axis4D = new Static4D(axis.get0(), axis.get1(), axis.get2(), 0);
172 4da7d87a Leszek Koltunski
      Static4D result = rotateVectorByQuat(axis4D, mQuat);
173 12ad3fca Leszek Koltunski
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 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
183 47ba5ddc Leszek Koltunski
// return quat1*quat2
184 0c52af30 Leszek Koltunski
185 beb325a0 Leszek Koltunski
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
186 45686da2 Leszek Koltunski
      {
187 348dfe69 Leszek Koltunski
      float qx = quat1.get0();
188
      float qy = quat1.get1();
189
      float qz = quat1.get2();
190
      float qw = quat1.get3();
191 45686da2 Leszek Koltunski
192 348dfe69 Leszek Koltunski
      float rx = quat2.get0();
193
      float ry = quat2.get1();
194
      float rz = quat2.get2();
195
      float rw = quat2.get3();
196 45686da2 Leszek Koltunski
197 47ba5ddc Leszek Koltunski
      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 45686da2 Leszek Koltunski
202 47ba5ddc Leszek Koltunski
      return new Static4D(tx,ty,tz,tw);
203 45686da2 Leszek Koltunski
      }
204
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206 beb325a0 Leszek Koltunski
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
207 45686da2 Leszek Koltunski
208 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
209 45686da2 Leszek Koltunski
      {
210 348dfe69 Leszek Koltunski
      float qx = quat.get0();
211
      float qy = quat.get1();
212
      float qz = quat.get2();
213
      float qw = quat.get3();
214 45686da2 Leszek Koltunski
215 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
216 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quat,vector);
217 0c52af30 Leszek Koltunski
218 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quatInverted);
219 0c52af30 Leszek Koltunski
      }
220
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222 beb325a0 Leszek Koltunski
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
223 0c52af30 Leszek Koltunski
224 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
225 0c52af30 Leszek Koltunski
      {
226 348dfe69 Leszek Koltunski
      float qx = quat.get0();
227
      float qy = quat.get1();
228
      float qz = quat.get2();
229
      float qw = quat.get3();
230 0c52af30 Leszek Koltunski
231 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
232 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quatInverted,vector);
233 0c52af30 Leszek Koltunski
234 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quat);
235 0c52af30 Leszek Koltunski
      }
236
237 168b6b56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
238
239 4c864c68 Leszek Koltunski
    private void addSpeedProbe(float x, float y)
240 168b6b56 Leszek Koltunski
      {
241
      long currTime = System.currentTimeMillis();
242
      boolean theSame = mLastIndex==mFirstIndex;
243
244
      mLastIndex++;
245
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
246
247 4c864c68 Leszek Koltunski
      mLastT[mLastIndex] = currTime;
248
      mLastX[mLastIndex] = x;
249
      mLastY[mLastIndex] = y;
250 168b6b56 Leszek Koltunski
251
      if( mLastIndex==mFirstIndex)
252
        {
253
        mFirstIndex++;
254
        if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
255
        }
256
257
      if( theSame )
258
        {
259 4c864c68 Leszek Koltunski
        mLastT[mFirstIndex] = currTime;
260
        mLastX[mFirstIndex] = x;
261
        mLastY[mFirstIndex] = y;
262 168b6b56 Leszek Koltunski
        }
263
      }
264
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
267 4c864c68 Leszek Koltunski
    private void computeCurrentSpeedInInchesPerSecond()
268 168b6b56 Leszek Koltunski
      {
269 4c864c68 Leszek Koltunski
      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 168b6b56 Leszek Koltunski
276
      long timeDiff = lastTime-firstTime;
277
278
      mLastIndex = 0;
279
      mFirstIndex= 0;
280
281 4c864c68 Leszek Koltunski
      mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX,fY,lX,lY)/timeDiff : 0;
282 168b6b56 Leszek Koltunski
      }
283
284 f0533889 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
285
286 cd83d0aa Leszek Koltunski
    private float retFingerDragDistanceInInches(float xFrom, float yFrom, float xTo, float yTo)
287 f0533889 Leszek Koltunski
      {
288 cd83d0aa Leszek Koltunski
      float xDist = mScreenWidth*(xFrom-xTo);
289
      float yDist = mScreenHeight*(yFrom-yTo);
290
      float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
291 f0533889 Leszek Koltunski
292 cd83d0aa Leszek Koltunski
      return distInPixels/mDensity;
293 f0533889 Leszek Koltunski
      }
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 4da7d87a Leszek Koltunski
        Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuat);
311
        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
312 f0533889 Leszek Koltunski
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 7695a3be Leszek Koltunski
    private void drag(MotionEvent event, float x, float y)
351 f0533889 Leszek Koltunski
      {
352 7695a3be Leszek Koltunski
      if( mPtrID1!=INVALID_POINTER_ID && mPtrID2!=INVALID_POINTER_ID)
353
        {
354
        int pointer = event.findPointerIndex(mPtrID2);
355 b96a20a4 Leszek Koltunski
        float pX,pY;
356 7695a3be Leszek Koltunski
357 b96a20a4 Leszek Koltunski
        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 7695a3be Leszek Koltunski
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 c7b00dfb Leszek Koltunski
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 7695a3be Leszek Koltunski
        }
394
      else
395
        {
396
        Static4D dragQuat = quatMultiply(quatFromDrag(mX-x,y-mY), mQuat);
397
        mTemp.set(dragQuat);
398
        }
399
400 4da7d87a Leszek Koltunski
      mPreRender.setQuatOnNextRender();
401
      mX = x;
402
      mY = y;
403 0b7e1b05 Leszek Koltunski
      }
404
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406
407
    private void finishRotation()
408
      {
409
      computeCurrentSpeedInInchesPerSecond();
410
      int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
411
      mPreRender.finishRotation(angle);
412
413 15846fe4 Leszek Koltunski
      if( angle!=0 )
414 0b7e1b05 Leszek Koltunski
        {
415 15846fe4 Leszek Koltunski
        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 0b7e1b05 Leszek Koltunski
        }
426
427
      mContinuingRotation = false;
428
      mBeginningRotation  = false;
429
      mDragging           = true;
430 f0533889 Leszek Koltunski
      }
431
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433
434 0b7e1b05 Leszek Koltunski
    private void continueRotation(float x, float y)
435 f0533889 Leszek Koltunski
      {
436 0b7e1b05 Leszek Koltunski
      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 8d50e08d Leszek Koltunski
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
449
      mPreRender.getObject().continueRotation(mCurrentAngle);
450 f0533889 Leszek Koltunski
451 0b7e1b05 Leszek Koltunski
      addSpeedProbe(x2,y2);
452 8d50e08d Leszek Koltunski
      }
453 f0533889 Leszek Koltunski
454 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
455 f0533889 Leszek Koltunski
456 0b7e1b05 Leszek Koltunski
    private void beginRotation(float x, float y)
457 8d50e08d Leszek Koltunski
      {
458
      mStartRotX = x;
459
      mStartRotY = y;
460 f0533889 Leszek Koltunski
461 8d50e08d Leszek Koltunski
      Static4D touchPoint2 = new Static4D(x, y, 0, 0);
462 4da7d87a Leszek Koltunski
      Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuat);
463 f0533889 Leszek Koltunski
464 8d50e08d Leszek Koltunski
      Static2D res = mMovement.newRotation(rotatedTouchPoint2);
465
      RubikObject object = mPreRender.getObject();
466 f0533889 Leszek Koltunski
467 8d50e08d Leszek Koltunski
      mCurrentAxis = (int)res.get0();
468
      float offset = res.get1();
469
      mCurrentRow = (int)(object.returnMultiplier()*offset);
470
      computeCurrentAxis( object.getRotationAxis()[mCurrentAxis] );
471
      mRotationFactor = object.returnRotationFactor(offset);
472 f0533889 Leszek Koltunski
473 8d50e08d Leszek Koltunski
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
474 f0533889 Leszek Koltunski
475 8d50e08d Leszek Koltunski
      if( RubikState.getCurrentState()==RubikState.READ )
476 f0533889 Leszek Koltunski
        {
477 8d50e08d Leszek Koltunski
        RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
478
        solving.resetElapsed();
479
480
        final RubikActivity act = (RubikActivity)getContext();
481 f0533889 Leszek Koltunski
482 8d50e08d Leszek Koltunski
        act.runOnUiThread(new Runnable()
483
          {
484
          @Override
485
          public void run()
486
            {
487
            RubikState.switchState( act, RubikState.SOLV);
488
            }
489
          });
490 f0533889 Leszek Koltunski
        }
491 8d50e08d Leszek Koltunski
492
      addSpeedProbe(x,y);
493
494
      mBeginningRotation = false;
495
      mContinuingRotation= true;
496
      }
497
498 7695a3be Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
499
500
    private float getAngle(float x1, float y1, float x2, float y2)
501
      {
502
      return (float) Math.atan2(y1-y2, x1-x2);
503
      }
504
505 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
506
507
    private void actionMove(MotionEvent event)
508
      {
509 a4472437 Leszek Koltunski
      int pointer = event.findPointerIndex(mPtrID1 != INVALID_POINTER_ID ? mPtrID1:mPtrID2);
510 f0533889 Leszek Koltunski
511 7695a3be Leszek Koltunski
      if( pointer<0 ) return;
512
513 a4472437 Leszek Koltunski
      float pX = event.getX(pointer);
514
      float pY = event.getY(pointer);
515
516
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
517
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
518 0b7e1b05 Leszek Koltunski
519
      if( mBeginningRotation )
520
        {
521
        if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
522 8d50e08d Leszek Koltunski
          {
523 0b7e1b05 Leszek Koltunski
          beginRotation(x,y);
524 f0533889 Leszek Koltunski
          }
525
        }
526 0b7e1b05 Leszek Koltunski
      else if( mContinuingRotation )
527
        {
528
        continueRotation(x,y);
529
        }
530
      else if( mDragging )
531
        {
532 7695a3be Leszek Koltunski
        drag(event,x,y);
533 0b7e1b05 Leszek Koltunski
        }
534 f0533889 Leszek Koltunski
      else
535
        {
536 0b7e1b05 Leszek Koltunski
        setUpDragOrRotate(false,x,y);
537 f0533889 Leszek Koltunski
        }
538
      }
539
540 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
541
542
    private void actionDown(MotionEvent event)
543
      {
544 a4472437 Leszek Koltunski
      mPtrID1 = event.getPointerId(0);
545 0b7e1b05 Leszek Koltunski
546 4da7d87a Leszek Koltunski
      float x = event.getX();
547
      float y = event.getY();
548 8d50e08d Leszek Koltunski
549 4da7d87a Leszek Koltunski
      mX = (x - mScreenWidth*0.5f)/mScreenMin;
550
      mY = (mScreenHeight*0.5f -y)/mScreenMin;
551 8d50e08d Leszek Koltunski
552
      setUpDragOrRotate(true,mX,mY);
553
      }
554
555 f0533889 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
556
557 0b7e1b05 Leszek Koltunski
    private void actionUp(MotionEvent event)
558 f0533889 Leszek Koltunski
      {
559 a4472437 Leszek Koltunski
      mPtrID1 = INVALID_POINTER_ID;
560
      mPtrID2 = INVALID_POINTER_ID;
561 f0533889 Leszek Koltunski
562
      if( mContinuingRotation )
563
        {
564 0b7e1b05 Leszek Koltunski
        finishRotation();
565 f0533889 Leszek Koltunski
        }
566
567
      if( mLastCubitColor>=0 )
568
        {
569
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
570 c558f011 Leszek Koltunski
        mLastCubitColor = -1;
571 f0533889 Leszek Koltunski
        }
572
      }
573
574 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
575
576
    private void actionDown2(MotionEvent event)
577
      {
578 a4472437 Leszek Koltunski
      int index = event.getActionIndex();
579
580
      if( mPtrID1==INVALID_POINTER_ID )
581
        {
582
        mPtrID1 = event.getPointerId(index);
583 7695a3be Leszek Koltunski
        float x = event.getX();
584
        float y = event.getY();
585
586
        if( mPtrID2 != INVALID_POINTER_ID )
587
          {
588
          int pointer = event.findPointerIndex(mPtrID2);
589
590
          float x2 = event.getX(pointer);
591
          float y2 = event.getY(pointer);
592
593
          mRotAngle = getAngle(x,-y,x2,-y2);
594 c7b00dfb Leszek Koltunski
          mInitDistance = -1;
595 7695a3be Leszek Koltunski
          }
596 a4472437 Leszek Koltunski
597
        mX = (x - mScreenWidth*0.5f)/mScreenMin;
598
        mY = (mScreenHeight*0.5f -y)/mScreenMin;
599
        }
600
      else if( mPtrID2==INVALID_POINTER_ID )
601
        {
602
        mPtrID2 = event.getPointerId(index);
603
604 7695a3be Leszek Koltunski
        float x = event.getX();
605
        float y = event.getY();
606
607
        if( mPtrID2 != INVALID_POINTER_ID )
608 a4472437 Leszek Koltunski
          {
609 7695a3be Leszek Koltunski
          int pointer = event.findPointerIndex(mPtrID2);
610
611
          float x2 = event.getX(pointer);
612
          float y2 = event.getY(pointer);
613 a4472437 Leszek Koltunski
614 7695a3be Leszek Koltunski
          mRotAngle = getAngle(x,-y,x2,-y2);
615 c7b00dfb Leszek Koltunski
          mInitDistance = -1;
616 7695a3be Leszek Koltunski
          }
617
618
        if( mBeginningRotation || mContinuingRotation )
619
          {
620 a4472437 Leszek Koltunski
          mX = (x - mScreenWidth*0.5f)/mScreenMin;
621
          mY = (mScreenHeight*0.5f -y)/mScreenMin;
622
          }
623
        }
624
625 0b7e1b05 Leszek Koltunski
      if( mBeginningRotation )
626
        {
627
        mContinuingRotation = false;
628
        mBeginningRotation  = false;
629
        mDragging           = true;
630
        }
631
      else if( mContinuingRotation )
632
        {
633
        finishRotation();
634
        }
635 8d50e08d Leszek Koltunski
      }
636
637
///////////////////////////////////////////////////////////////////////////////////////////////////
638
639 0b7e1b05 Leszek Koltunski
    private void actionUp2(MotionEvent event)
640 8d50e08d Leszek Koltunski
      {
641 4da7d87a Leszek Koltunski
      int index = event.getActionIndex();
642
643 7695a3be Leszek Koltunski
      if( index==event.findPointerIndex(mPtrID1) )
644 a4472437 Leszek Koltunski
        {
645
        mPtrID1 = INVALID_POINTER_ID;
646
        int pointer = event.findPointerIndex(mPtrID2);
647
648 7695a3be Leszek Koltunski
        if( pointer>=0 )
649
          {
650
          float x1 = event.getX(pointer);
651
          float y1 = event.getY(pointer);
652
653
          mX = (x1 - mScreenWidth*0.5f)/mScreenMin;
654
          mY = (mScreenHeight*0.5f -y1)/mScreenMin;
655
          }
656 a4472437 Leszek Koltunski
        }
657 7695a3be Leszek Koltunski
      else if( index==event.findPointerIndex(mPtrID2) )
658 a4472437 Leszek Koltunski
        {
659
        mPtrID2 = INVALID_POINTER_ID;
660
        }
661 8d50e08d Leszek Koltunski
      }
662
663 e03e0352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
664
665
    void initialize()
666
      {
667
      mPtrID1 = INVALID_POINTER_ID;
668
      mPtrID2 = INVALID_POINTER_ID;
669
      }
670
671 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
672
// PUBLIC API
673
///////////////////////////////////////////////////////////////////////////////////////////////////
674
675
    public RubikSurfaceView(Context context, AttributeSet attrs)
676
      {
677
      super(context,attrs);
678
679
      if(!isInEditMode())
680
        {
681 4e248bcc Leszek Koltunski
        mLastCubitColor = -1;
682 168b6b56 Leszek Koltunski
        mCurrRotSpeed   = 0.0f;
683
684 4c864c68 Leszek Koltunski
        mLastX = new float[NUM_SPEED_PROBES];
685
        mLastY = new float[NUM_SPEED_PROBES];
686
        mLastT = new long[NUM_SPEED_PROBES];
687 168b6b56 Leszek Koltunski
        mFirstIndex =0;
688
        mLastIndex  =0;
689 4e248bcc Leszek Koltunski
690 5a4d4fba Leszek Koltunski
        mRenderer  = new RubikRenderer(this);
691
        mPreRender = new RubikPreRender(this);
692 47ba5ddc Leszek Koltunski
693 cd83d0aa Leszek Koltunski
        RubikActivity act = (RubikActivity)context;
694
        DisplayMetrics dm = new DisplayMetrics();
695
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
696
697
        mDensity = dm.densityDpi;
698
699
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
700
701 4489e68e Leszek Koltunski
        try
702 cd83d0aa Leszek Koltunski
          {
703
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
704
          setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
705
          setRenderer(mRenderer);
706
          }
707 4489e68e Leszek Koltunski
        catch(Exception ex)
708
          {
709
          act.OpenGLError("This device does not support OpenGL ES 3.0");
710
711
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
712
          String version = GLES30.glGetString(GLES30.GL_VERSION);
713
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
714
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
715
716
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
717
          crashlytics.setCustomKey("GLSL Version"  , shading );
718
          crashlytics.setCustomKey("GLversion"     , version );
719
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
720
          crashlytics.setCustomKey("GLSLrenderer"  , renderer);
721
          crashlytics.recordException(ex);
722
          }
723 47ba5ddc Leszek Koltunski
        }
724
      }
725
726
///////////////////////////////////////////////////////////////////////////////////////////////////
727
728
    @Override
729
    public boolean onTouchEvent(MotionEvent event)
730
      {
731 8d50e08d Leszek Koltunski
      int action = event.getActionMasked();
732 47ba5ddc Leszek Koltunski
733
      switch(action)
734
         {
735 8d50e08d Leszek Koltunski
         case MotionEvent.ACTION_DOWN        : actionDown(event) ; break;
736
         case MotionEvent.ACTION_MOVE        : actionMove(event) ; break;
737 0b7e1b05 Leszek Koltunski
         case MotionEvent.ACTION_UP          : actionUp(event)   ; break;
738 8d50e08d Leszek Koltunski
         case MotionEvent.ACTION_POINTER_DOWN: actionDown2(event); break;
739 0b7e1b05 Leszek Koltunski
         case MotionEvent.ACTION_POINTER_UP  : actionUp2(event)  ; break;
740 47ba5ddc Leszek Koltunski
         }
741
742
      return true;
743
      }
744 0c52af30 Leszek Koltunski
}