Project

General

Profile

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

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

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 935f3663 Leszek Koltunski
// cast the 3D axis we are currently rotating along (which is already casted to the surface of the
168
// currently touched face AND converted into a 4D vector - fourth 0) to a 2D in-screen-surface axis
169 12ad3fca Leszek Koltunski
170 935f3663 Leszek Koltunski
    private void computeCurrentAxis(Static4D axis)
171 12ad3fca Leszek Koltunski
      {
172 935f3663 Leszek Koltunski
      Static4D result = rotateVectorByQuat(axis, 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 fb377dae Leszek Koltunski
      mCurrentRow  = object.computeRowFromOffset(offset);
470
471 935f3663 Leszek Koltunski
      computeCurrentAxis( mMovement.getCastedRotAxis(mCurrentAxis) );
472 8d50e08d Leszek Koltunski
      mRotationFactor = object.returnRotationFactor(offset);
473 f0533889 Leszek Koltunski
474 8d50e08d Leszek Koltunski
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
475 f0533889 Leszek Koltunski
476 8d50e08d Leszek Koltunski
      if( RubikState.getCurrentState()==RubikState.READ )
477 f0533889 Leszek Koltunski
        {
478 8d50e08d Leszek Koltunski
        RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
479
        solving.resetElapsed();
480
481
        final RubikActivity act = (RubikActivity)getContext();
482 f0533889 Leszek Koltunski
483 8d50e08d Leszek Koltunski
        act.runOnUiThread(new Runnable()
484
          {
485
          @Override
486
          public void run()
487
            {
488
            RubikState.switchState( act, RubikState.SOLV);
489
            }
490
          });
491 f0533889 Leszek Koltunski
        }
492 8d50e08d Leszek Koltunski
493
      addSpeedProbe(x,y);
494
495
      mBeginningRotation = false;
496
      mContinuingRotation= true;
497
      }
498
499 7695a3be Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
500
501
    private float getAngle(float x1, float y1, float x2, float y2)
502
      {
503
      return (float) Math.atan2(y1-y2, x1-x2);
504
      }
505
506 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
507
508
    private void actionMove(MotionEvent event)
509
      {
510 a4472437 Leszek Koltunski
      int pointer = event.findPointerIndex(mPtrID1 != INVALID_POINTER_ID ? mPtrID1:mPtrID2);
511 f0533889 Leszek Koltunski
512 7695a3be Leszek Koltunski
      if( pointer<0 ) return;
513
514 a4472437 Leszek Koltunski
      float pX = event.getX(pointer);
515
      float pY = event.getY(pointer);
516
517
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
518
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
519 0b7e1b05 Leszek Koltunski
520
      if( mBeginningRotation )
521
        {
522
        if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
523 8d50e08d Leszek Koltunski
          {
524 0b7e1b05 Leszek Koltunski
          beginRotation(x,y);
525 f0533889 Leszek Koltunski
          }
526
        }
527 0b7e1b05 Leszek Koltunski
      else if( mContinuingRotation )
528
        {
529
        continueRotation(x,y);
530
        }
531
      else if( mDragging )
532
        {
533 7695a3be Leszek Koltunski
        drag(event,x,y);
534 0b7e1b05 Leszek Koltunski
        }
535 f0533889 Leszek Koltunski
      else
536
        {
537 0b7e1b05 Leszek Koltunski
        setUpDragOrRotate(false,x,y);
538 f0533889 Leszek Koltunski
        }
539
      }
540
541 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
542
543
    private void actionDown(MotionEvent event)
544
      {
545 a4472437 Leszek Koltunski
      mPtrID1 = event.getPointerId(0);
546 0b7e1b05 Leszek Koltunski
547 4da7d87a Leszek Koltunski
      float x = event.getX();
548
      float y = event.getY();
549 8d50e08d Leszek Koltunski
550 4da7d87a Leszek Koltunski
      mX = (x - mScreenWidth*0.5f)/mScreenMin;
551
      mY = (mScreenHeight*0.5f -y)/mScreenMin;
552 8d50e08d Leszek Koltunski
553
      setUpDragOrRotate(true,mX,mY);
554
      }
555
556 f0533889 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
557
558 0b7e1b05 Leszek Koltunski
    private void actionUp(MotionEvent event)
559 f0533889 Leszek Koltunski
      {
560 a4472437 Leszek Koltunski
      mPtrID1 = INVALID_POINTER_ID;
561
      mPtrID2 = INVALID_POINTER_ID;
562 f0533889 Leszek Koltunski
563
      if( mContinuingRotation )
564
        {
565 0b7e1b05 Leszek Koltunski
        finishRotation();
566 f0533889 Leszek Koltunski
        }
567
568
      if( mLastCubitColor>=0 )
569
        {
570
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
571 c558f011 Leszek Koltunski
        mLastCubitColor = -1;
572 f0533889 Leszek Koltunski
        }
573
      }
574
575 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
576
577
    private void actionDown2(MotionEvent event)
578
      {
579 a4472437 Leszek Koltunski
      int index = event.getActionIndex();
580
581
      if( mPtrID1==INVALID_POINTER_ID )
582
        {
583
        mPtrID1 = event.getPointerId(index);
584 7695a3be Leszek Koltunski
        float x = event.getX();
585
        float y = event.getY();
586
587
        if( mPtrID2 != INVALID_POINTER_ID )
588
          {
589
          int pointer = event.findPointerIndex(mPtrID2);
590
591
          float x2 = event.getX(pointer);
592
          float y2 = event.getY(pointer);
593
594
          mRotAngle = getAngle(x,-y,x2,-y2);
595 c7b00dfb Leszek Koltunski
          mInitDistance = -1;
596 7695a3be Leszek Koltunski
          }
597 a4472437 Leszek Koltunski
598
        mX = (x - mScreenWidth*0.5f)/mScreenMin;
599
        mY = (mScreenHeight*0.5f -y)/mScreenMin;
600
        }
601
      else if( mPtrID2==INVALID_POINTER_ID )
602
        {
603
        mPtrID2 = event.getPointerId(index);
604
605 7695a3be Leszek Koltunski
        float x = event.getX();
606
        float y = event.getY();
607
608
        if( mPtrID2 != INVALID_POINTER_ID )
609 a4472437 Leszek Koltunski
          {
610 7695a3be Leszek Koltunski
          int pointer = event.findPointerIndex(mPtrID2);
611
612
          float x2 = event.getX(pointer);
613
          float y2 = event.getY(pointer);
614 a4472437 Leszek Koltunski
615 7695a3be Leszek Koltunski
          mRotAngle = getAngle(x,-y,x2,-y2);
616 c7b00dfb Leszek Koltunski
          mInitDistance = -1;
617 7695a3be Leszek Koltunski
          }
618
619
        if( mBeginningRotation || mContinuingRotation )
620
          {
621 a4472437 Leszek Koltunski
          mX = (x - mScreenWidth*0.5f)/mScreenMin;
622
          mY = (mScreenHeight*0.5f -y)/mScreenMin;
623
          }
624
        }
625
626 0b7e1b05 Leszek Koltunski
      if( mBeginningRotation )
627
        {
628
        mContinuingRotation = false;
629
        mBeginningRotation  = false;
630
        mDragging           = true;
631
        }
632
      else if( mContinuingRotation )
633
        {
634
        finishRotation();
635
        }
636 8d50e08d Leszek Koltunski
      }
637
638
///////////////////////////////////////////////////////////////////////////////////////////////////
639
640 0b7e1b05 Leszek Koltunski
    private void actionUp2(MotionEvent event)
641 8d50e08d Leszek Koltunski
      {
642 4da7d87a Leszek Koltunski
      int index = event.getActionIndex();
643
644 7695a3be Leszek Koltunski
      if( index==event.findPointerIndex(mPtrID1) )
645 a4472437 Leszek Koltunski
        {
646
        mPtrID1 = INVALID_POINTER_ID;
647
        int pointer = event.findPointerIndex(mPtrID2);
648
649 7695a3be Leszek Koltunski
        if( pointer>=0 )
650
          {
651
          float x1 = event.getX(pointer);
652
          float y1 = event.getY(pointer);
653
654
          mX = (x1 - mScreenWidth*0.5f)/mScreenMin;
655
          mY = (mScreenHeight*0.5f -y1)/mScreenMin;
656
          }
657 a4472437 Leszek Koltunski
        }
658 7695a3be Leszek Koltunski
      else if( index==event.findPointerIndex(mPtrID2) )
659 a4472437 Leszek Koltunski
        {
660
        mPtrID2 = INVALID_POINTER_ID;
661
        }
662 8d50e08d Leszek Koltunski
      }
663
664 e03e0352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
665
666
    void initialize()
667
      {
668
      mPtrID1 = INVALID_POINTER_ID;
669
      mPtrID2 = INVALID_POINTER_ID;
670
      }
671
672 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
673
// PUBLIC API
674
///////////////////////////////////////////////////////////////////////////////////////////////////
675
676
    public RubikSurfaceView(Context context, AttributeSet attrs)
677
      {
678
      super(context,attrs);
679
680
      if(!isInEditMode())
681
        {
682 4e248bcc Leszek Koltunski
        mLastCubitColor = -1;
683 168b6b56 Leszek Koltunski
        mCurrRotSpeed   = 0.0f;
684
685 4c864c68 Leszek Koltunski
        mLastX = new float[NUM_SPEED_PROBES];
686
        mLastY = new float[NUM_SPEED_PROBES];
687
        mLastT = new long[NUM_SPEED_PROBES];
688 168b6b56 Leszek Koltunski
        mFirstIndex =0;
689
        mLastIndex  =0;
690 4e248bcc Leszek Koltunski
691 5a4d4fba Leszek Koltunski
        mRenderer  = new RubikRenderer(this);
692
        mPreRender = new RubikPreRender(this);
693 47ba5ddc Leszek Koltunski
694 cd83d0aa Leszek Koltunski
        RubikActivity act = (RubikActivity)context;
695
        DisplayMetrics dm = new DisplayMetrics();
696
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
697
698
        mDensity = dm.densityDpi;
699
700
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
701
702 4489e68e Leszek Koltunski
        try
703 cd83d0aa Leszek Koltunski
          {
704
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
705
          setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
706
          setRenderer(mRenderer);
707
          }
708 4489e68e Leszek Koltunski
        catch(Exception ex)
709
          {
710
          act.OpenGLError("This device does not support OpenGL ES 3.0");
711
712
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
713
          String version = GLES30.glGetString(GLES30.GL_VERSION);
714
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
715
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
716
717
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
718
          crashlytics.setCustomKey("GLSL Version"  , shading );
719
          crashlytics.setCustomKey("GLversion"     , version );
720
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
721
          crashlytics.setCustomKey("GLSLrenderer"  , renderer);
722
          crashlytics.recordException(ex);
723
          }
724 47ba5ddc Leszek Koltunski
        }
725
      }
726
727
///////////////////////////////////////////////////////////////////////////////////////////////////
728
729
    @Override
730
    public boolean onTouchEvent(MotionEvent event)
731
      {
732 8d50e08d Leszek Koltunski
      int action = event.getActionMasked();
733 47ba5ddc Leszek Koltunski
734
      switch(action)
735
         {
736 8d50e08d Leszek Koltunski
         case MotionEvent.ACTION_DOWN        : actionDown(event) ; break;
737
         case MotionEvent.ACTION_MOVE        : actionMove(event) ; break;
738 0b7e1b05 Leszek Koltunski
         case MotionEvent.ACTION_UP          : actionUp(event)   ; break;
739 8d50e08d Leszek Koltunski
         case MotionEvent.ACTION_POINTER_DOWN: actionDown2(event); break;
740 0b7e1b05 Leszek Koltunski
         case MotionEvent.ACTION_POINTER_UP  : actionUp2(event)  ; break;
741 47ba5ddc Leszek Koltunski
         }
742
743
      return true;
744
      }
745 0c52af30 Leszek Koltunski
}