Project

General

Profile

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

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

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 0c52af30 Leszek Koltunski
import org.distorted.library.type.Static4D;
35 9c2f0c91 Leszek Koltunski
import org.distorted.objects.TwistyObject;
36 ca3300c3 Leszek Koltunski
import org.distorted.objects.Movement;
37 46a961fd Leszek Koltunski
import org.distorted.solvers.SolverMain;
38 be576d14 Leszek Koltunski
import org.distorted.states.StateList;
39 15846fe4 Leszek Koltunski
import org.distorted.states.RubikStatePlay;
40 473611ee Leszek Koltunski
import org.distorted.states.RubikStateSolver;
41 1f9772f3 Leszek Koltunski
import org.distorted.states.RubikStateSolving;
42 0c52af30 Leszek Koltunski
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45 beb325a0 Leszek Koltunski
public class RubikSurfaceView extends GLSurfaceView
46 0c52af30 Leszek Koltunski
{
47 168b6b56 Leszek Koltunski
    private static final int NUM_SPEED_PROBES = 10;
48 a4472437 Leszek Koltunski
    private static final int INVALID_POINTER_ID = -1;
49 168b6b56 Leszek Koltunski
50 473611ee Leszek Koltunski
    public static final int MODE_ROTATE  = 0;
51
    public static final int MODE_DRAG    = 1;
52
    public static final int MODE_REPLACE = 2;
53
54 775e675d Leszek Koltunski
    // Moving the finger from the middle of the vertical screen to the right edge will rotate a
55
    // given face by SWIPING_SENSITIVITY/2 degrees.
56
    private final static int SWIPING_SENSITIVITY  = 240;
57 4da7d87a Leszek Koltunski
    // Moving the finger by 0.3 of an inch will start a Rotation.
58
    private final static float ROTATION_SENSITIVITY = 0.3f;
59 eac805bd Leszek Koltunski
60 c7e79b69 Leszek Koltunski
    private final Static4D CAMERA_POINT = new Static4D(0, 0, 1, 0);
61 86c73a69 Leszek Koltunski
62 beb325a0 Leszek Koltunski
    private RubikRenderer mRenderer;
63 5a4d4fba Leszek Koltunski
    private RubikPreRender mPreRender;
64 ca3300c3 Leszek Koltunski
    private Movement mMovement;
65 0c52af30 Leszek Koltunski
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
66 beb325a0 Leszek Koltunski
    private int mScreenWidth, mScreenHeight, mScreenMin;
67 0c52af30 Leszek Koltunski
68 c7b00dfb Leszek Koltunski
    private float mRotAngle, mInitDistance;
69 a4472437 Leszek Koltunski
    private int mPtrID1, mPtrID2;
70 0b7e1b05 Leszek Koltunski
    private float mX, mY;
71 12ad3fca Leszek Koltunski
    private float mStartRotX, mStartRotY;
72
    private float mAxisX, mAxisY;
73 e46e17fb Leszek Koltunski
    private float mRotationFactor;
74 46a961fd Leszek Koltunski
    private int mLastCubitColor, mLastCubitFace, mLastCubit;
75 0e5ad27c Leszek Koltunski
    private int mCurrentAxis, mCurrentRow;
76 168b6b56 Leszek Koltunski
    private float mCurrentAngle, mCurrRotSpeed;
77 4c864c68 Leszek Koltunski
    private float[] mLastX;
78
    private float[] mLastY;
79
    private long[] mLastT;
80 168b6b56 Leszek Koltunski
    private int mFirstIndex, mLastIndex;
81 cd83d0aa Leszek Koltunski
    private int mDensity;
82 12ad3fca Leszek Koltunski
83 a64e07d0 Leszek Koltunski
    private static final Static4D mQuat= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
84
    private static final Static4D mTemp= new Static4D(0,0,0,1);
85 ee5c2ae1 Leszek Koltunski
86 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
87
88
    void setScreenSize(int width, int height)
89
      {
90
      mScreenWidth = width;
91
      mScreenHeight= height;
92
93 12ad3fca Leszek Koltunski
      mScreenMin = Math.min(width, height);
94 0fd3ac1f Leszek Koltunski
      }
95
96 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
97
98
    boolean isVertical()
99
      {
100
      return mScreenHeight>mScreenWidth;
101
      }
102
103 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
104
105
    RubikRenderer getRenderer()
106
      {
107
      return mRenderer;
108
      }
109
110 8becce57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112 5a4d4fba Leszek Koltunski
    RubikPreRender getPreRender()
113 8becce57 Leszek Koltunski
      {
114 5a4d4fba Leszek Koltunski
      return mPreRender;
115 8becce57 Leszek Koltunski
      }
116
117 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
118
119 4da7d87a Leszek Koltunski
    void setQuat()
120 0fd3ac1f Leszek Koltunski
      {
121 4da7d87a Leszek Koltunski
      mQuat.set(mTemp);
122 0fd3ac1f Leszek Koltunski
      }
123
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126 4da7d87a Leszek Koltunski
    Static4D getQuat()
127 0fd3ac1f Leszek Koltunski
      {
128 4da7d87a Leszek Koltunski
      return mQuat;
129 0fd3ac1f Leszek Koltunski
      }
130
131 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
132
133 ca3300c3 Leszek Koltunski
    void setMovement(Movement movement)
134 27a70eae Leszek Koltunski
      {
135
      mMovement = movement;
136
      }
137
138 0fd3ac1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140
    private Static4D quatFromDrag(float dragX, float dragY)
141
      {
142
      float axisX = dragY;  // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
143
      float axisY = dragX;  // Why not (-dragY, dragX) ? because Y axis is also inverted!
144
      float axisZ = 0;
145
      float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
146
147
      if( axisL>0 )
148
        {
149
        axisX /= axisL;
150
        axisY /= axisL;
151
        axisZ /= axisL;
152
153
        float ratio = axisL;
154
        ratio = ratio - (int)ratio;     // the cos() is only valid in (0,Pi)
155
156
        float cosA = (float)Math.cos(Math.PI*ratio);
157
        float sinA = (float)Math.sqrt(1-cosA*cosA);
158
159
        return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
160
        }
161
162
      return new Static4D(0f, 0f, 0f, 1f);
163
      }
164
165 12ad3fca Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
166 935f3663 Leszek Koltunski
// cast the 3D axis we are currently rotating along (which is already casted to the surface of the
167
// currently touched face AND converted into a 4D vector - fourth 0) to a 2D in-screen-surface axis
168 12ad3fca Leszek Koltunski
169 935f3663 Leszek Koltunski
    private void computeCurrentAxis(Static4D axis)
170 12ad3fca Leszek Koltunski
      {
171 935f3663 Leszek Koltunski
      Static4D result = rotateVectorByQuat(axis, mQuat);
172 12ad3fca Leszek Koltunski
173
      mAxisX =result.get0();
174
      mAxisY =result.get1();
175
176
      float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
177
      mAxisX /= len;
178
      mAxisY /= len;
179
      }
180
181 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
182 47ba5ddc Leszek Koltunski
// return quat1*quat2
183 0c52af30 Leszek Koltunski
184 beb325a0 Leszek Koltunski
    public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
185 45686da2 Leszek Koltunski
      {
186 348dfe69 Leszek Koltunski
      float qx = quat1.get0();
187
      float qy = quat1.get1();
188
      float qz = quat1.get2();
189
      float qw = quat1.get3();
190 45686da2 Leszek Koltunski
191 348dfe69 Leszek Koltunski
      float rx = quat2.get0();
192
      float ry = quat2.get1();
193
      float rz = quat2.get2();
194
      float rw = quat2.get3();
195 45686da2 Leszek Koltunski
196 47ba5ddc Leszek Koltunski
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
197
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
198
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
199
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
200 45686da2 Leszek Koltunski
201 47ba5ddc Leszek Koltunski
      return new Static4D(tx,ty,tz,tw);
202 45686da2 Leszek Koltunski
      }
203
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205 beb325a0 Leszek Koltunski
// rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
206 45686da2 Leszek Koltunski
207 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
208 45686da2 Leszek Koltunski
      {
209 348dfe69 Leszek Koltunski
      float qx = quat.get0();
210
      float qy = quat.get1();
211
      float qz = quat.get2();
212
      float qw = quat.get3();
213 45686da2 Leszek Koltunski
214 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
215 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quat,vector);
216 0c52af30 Leszek Koltunski
217 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quatInverted);
218 0c52af30 Leszek Koltunski
      }
219
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221 beb325a0 Leszek Koltunski
// rotate 'vector' by quat^(-1)  ( i.e. return (quat^-1)*vector*quat )
222 0c52af30 Leszek Koltunski
223 beb325a0 Leszek Koltunski
    public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
224 0c52af30 Leszek Koltunski
      {
225 348dfe69 Leszek Koltunski
      float qx = quat.get0();
226
      float qy = quat.get1();
227
      float qz = quat.get2();
228
      float qw = quat.get3();
229 0c52af30 Leszek Koltunski
230 47ba5ddc Leszek Koltunski
      Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
231 beb325a0 Leszek Koltunski
      Static4D tmp = quatMultiply(quatInverted,vector);
232 0c52af30 Leszek Koltunski
233 beb325a0 Leszek Koltunski
      return quatMultiply(tmp,quat);
234 0c52af30 Leszek Koltunski
      }
235
236 168b6b56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
237
238 4c864c68 Leszek Koltunski
    private void addSpeedProbe(float x, float y)
239 168b6b56 Leszek Koltunski
      {
240
      long currTime = System.currentTimeMillis();
241
      boolean theSame = mLastIndex==mFirstIndex;
242
243
      mLastIndex++;
244
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
245
246 4c864c68 Leszek Koltunski
      mLastT[mLastIndex] = currTime;
247
      mLastX[mLastIndex] = x;
248
      mLastY[mLastIndex] = y;
249 168b6b56 Leszek Koltunski
250
      if( mLastIndex==mFirstIndex)
251
        {
252
        mFirstIndex++;
253
        if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
254
        }
255
256
      if( theSame )
257
        {
258 4c864c68 Leszek Koltunski
        mLastT[mFirstIndex] = currTime;
259
        mLastX[mFirstIndex] = x;
260
        mLastY[mFirstIndex] = y;
261 168b6b56 Leszek Koltunski
        }
262
      }
263
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
266 4c864c68 Leszek Koltunski
    private void computeCurrentSpeedInInchesPerSecond()
267 168b6b56 Leszek Koltunski
      {
268 4c864c68 Leszek Koltunski
      long firstTime = mLastT[mFirstIndex];
269
      long lastTime  = mLastT[mLastIndex];
270
      float fX = mLastX[mFirstIndex];
271
      float fY = mLastY[mFirstIndex];
272
      float lX = mLastX[mLastIndex];
273
      float lY = mLastY[mLastIndex];
274 168b6b56 Leszek Koltunski
275
      long timeDiff = lastTime-firstTime;
276
277
      mLastIndex = 0;
278
      mFirstIndex= 0;
279
280 4c864c68 Leszek Koltunski
      mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX,fY,lX,lY)/timeDiff : 0;
281 168b6b56 Leszek Koltunski
      }
282
283 f0533889 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
284
285 cd83d0aa Leszek Koltunski
    private float retFingerDragDistanceInInches(float xFrom, float yFrom, float xTo, float yTo)
286 f0533889 Leszek Koltunski
      {
287 cd83d0aa Leszek Koltunski
      float xDist = mScreenWidth*(xFrom-xTo);
288
      float yDist = mScreenHeight*(yFrom-yTo);
289
      float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
290 f0533889 Leszek Koltunski
291 cd83d0aa Leszek Koltunski
      return distInPixels/mDensity;
292 f0533889 Leszek Koltunski
      }
293
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
296
    private void setUpDragOrRotate(boolean down, float x, float y)
297
      {
298 be576d14 Leszek Koltunski
      int mode = StateList.getMode();
299 f0533889 Leszek Koltunski
300
      if( mode==MODE_DRAG )
301
        {
302
        mDragging           = true;
303
        mBeginningRotation  = false;
304
        mContinuingRotation = false;
305
        }
306
      else
307
        {
308 d99f3a48 Leszek Koltunski
        Static4D touchPoint = new Static4D(x, y, 0, 0);
309
        Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuat);
310 4da7d87a Leszek Koltunski
        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
311 f0533889 Leszek Koltunski
312 d99f3a48 Leszek Koltunski
        if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint,rotatedCamera) )
313 f0533889 Leszek Koltunski
          {
314
          mDragging           = false;
315
          mContinuingRotation = false;
316
317
          if( mode==MODE_ROTATE )
318
            {
319
            mBeginningRotation= mPreRender.canRotate();
320
            }
321
          else if( mode==MODE_REPLACE )
322
            {
323
            mBeginningRotation= false;
324
325
            if( down )
326
              {
327 be576d14 Leszek Koltunski
              RubikStateSolver solver = (RubikStateSolver) StateList.SVER.getStateClass();
328 f0533889 Leszek Koltunski
              mLastCubitFace = mMovement.getTouchedFace();
329
              float[] point = mMovement.getTouchedPoint3D();
330
              int color = solver.getCurrentColor();
331 9c2f0c91 Leszek Koltunski
              TwistyObject object = mPreRender.getObject();
332 f0533889 Leszek Koltunski
              mLastCubit = object.getCubit(point);
333
              mPreRender.setTextureMap( mLastCubit, mLastCubitFace, color );
334 d99f3a48 Leszek Koltunski
              mLastCubitColor = SolverMain.cubitIsLocked(object.getObjectList(), object.getNumLayers(), mLastCubit);
335 f0533889 Leszek Koltunski
              }
336
            }
337
          }
338
        else
339
          {
340 46405bb4 Leszek Koltunski
          final RubikActivity act = (RubikActivity)getContext();
341
342
          mDragging           = !act.isLocked();
343 f0533889 Leszek Koltunski
          mBeginningRotation  = false;
344
          mContinuingRotation = false;
345
          }
346
        }
347
      }
348
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350
351 7695a3be Leszek Koltunski
    private void drag(MotionEvent event, float x, float y)
352 f0533889 Leszek Koltunski
      {
353 7695a3be Leszek Koltunski
      if( mPtrID1!=INVALID_POINTER_ID && mPtrID2!=INVALID_POINTER_ID)
354
        {
355
        int pointer = event.findPointerIndex(mPtrID2);
356 b96a20a4 Leszek Koltunski
        float pX,pY;
357 7695a3be Leszek Koltunski
358 b96a20a4 Leszek Koltunski
        try
359
          {
360
          pX = event.getX(pointer);
361
          pY = event.getY(pointer);
362
          }
363
        catch(IllegalArgumentException ex)
364
          {
365
          mPtrID1=INVALID_POINTER_ID;
366
          mPtrID2=INVALID_POINTER_ID;
367
368
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
369
          crashlytics.setCustomKey("DragError", "pointer="+pointer );
370
          crashlytics.recordException(ex);
371
372
          return;
373
          }
374 7695a3be Leszek Koltunski
375
        float x2 = (pX - mScreenWidth*0.5f)/mScreenMin;
376
        float y2 = (mScreenHeight*0.5f -pY)/mScreenMin;
377
378
        float angleNow = getAngle(x,y,x2,y2);
379
        float angleDiff = angleNow-mRotAngle;
380
        float sinA =-(float)Math.sin(angleDiff);
381
        float cosA = (float)Math.cos(angleDiff);
382
383
        Static4D dragQuat = quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
384
        mTemp.set(dragQuat);
385
386
        mRotAngle = angleNow;
387 c7b00dfb Leszek Koltunski
388
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
389
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
390
        mInitDistance = distNow;
391
392 9c2f0c91 Leszek Koltunski
        TwistyObject object = mPreRender.getObject();
393 49595e4b Leszek Koltunski
        if( object!=null ) object.setObjectRatio(distQuot);
394 7695a3be Leszek Koltunski
        }
395
      else
396
        {
397
        Static4D dragQuat = quatMultiply(quatFromDrag(mX-x,y-mY), mQuat);
398
        mTemp.set(dragQuat);
399
        }
400
401 4da7d87a Leszek Koltunski
      mPreRender.setQuatOnNextRender();
402
      mX = x;
403
      mY = y;
404 0b7e1b05 Leszek Koltunski
      }
405
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407
408
    private void finishRotation()
409
      {
410
      computeCurrentSpeedInInchesPerSecond();
411
      int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
412
      mPreRender.finishRotation(angle);
413 ac722a43 Leszek Koltunski
      mPreRender.rememberMove(mCurrentAxis,mCurrentRow,angle);
414 0b7e1b05 Leszek Koltunski
415 15846fe4 Leszek Koltunski
      if( angle!=0 )
416 0b7e1b05 Leszek Koltunski
        {
417 be576d14 Leszek Koltunski
        if( StateList.getCurrentState()== StateList.SOLV )
418 15846fe4 Leszek Koltunski
          {
419 be576d14 Leszek Koltunski
          RubikStateSolving solving = (RubikStateSolving) StateList.SOLV.getStateClass();
420 15846fe4 Leszek Koltunski
          solving.addMove(mCurrentAxis, mCurrentRow, angle);
421
          }
422 be576d14 Leszek Koltunski
        if( StateList.getCurrentState()== StateList.PLAY )
423 15846fe4 Leszek Koltunski
          {
424 be576d14 Leszek Koltunski
          RubikStatePlay play = (RubikStatePlay) StateList.PLAY.getStateClass();
425 15846fe4 Leszek Koltunski
          play.addMove(mCurrentAxis, mCurrentRow, angle);
426
          }
427 0b7e1b05 Leszek Koltunski
        }
428
429
      mContinuingRotation = false;
430
      mBeginningRotation  = false;
431
      mDragging           = true;
432 f0533889 Leszek Koltunski
      }
433
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435
436 0b7e1b05 Leszek Koltunski
    private void continueRotation(float x, float y)
437 f0533889 Leszek Koltunski
      {
438 0b7e1b05 Leszek Koltunski
      float dx = x-mStartRotX;
439
      float dy = y-mStartRotY;
440
      float alpha = dx*mAxisX + dy*mAxisY;
441
      float x2 = dx - alpha*mAxisX;
442
      float y2 = dy - alpha*mAxisY;
443
444
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
445
446
      // we have the length of 1D vector 'angle', now the direction:
447
      float tmp = mAxisY==0 ? -mAxisX*y2 : mAxisY*x2;
448
449
      float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
450 8d50e08d Leszek Koltunski
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
451
      mPreRender.getObject().continueRotation(mCurrentAngle);
452 f0533889 Leszek Koltunski
453 0b7e1b05 Leszek Koltunski
      addSpeedProbe(x2,y2);
454 8d50e08d Leszek Koltunski
      }
455 f0533889 Leszek Koltunski
456 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
457 f0533889 Leszek Koltunski
458 0b7e1b05 Leszek Koltunski
    private void beginRotation(float x, float y)
459 8d50e08d Leszek Koltunski
      {
460
      mStartRotX = x;
461
      mStartRotY = y;
462 f0533889 Leszek Koltunski
463 ee39837d Leszek Koltunski
      TwistyObject object = mPreRender.getObject();
464 d99f3a48 Leszek Koltunski
      int numLayers = object.getNumLayers();
465 ee39837d Leszek Koltunski
466 8d50e08d Leszek Koltunski
      Static4D touchPoint2 = new Static4D(x, y, 0, 0);
467 4da7d87a Leszek Koltunski
      Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuat);
468 d99f3a48 Leszek Koltunski
      Static2D res = mMovement.newRotation(numLayers,rotatedTouchPoint2);
469 f0533889 Leszek Koltunski
470 8d50e08d Leszek Koltunski
      mCurrentAxis = (int)res.get0();
471 ee39837d Leszek Koltunski
      mCurrentRow  = (int)res.get1();
472 fb377dae Leszek Koltunski
473 935f3663 Leszek Koltunski
      computeCurrentAxis( mMovement.getCastedRotAxis(mCurrentAxis) );
474 d99f3a48 Leszek Koltunski
      mRotationFactor = mMovement.returnRotationFactor(numLayers,mCurrentRow);
475 f0533889 Leszek Koltunski
476 8d50e08d Leszek Koltunski
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
477 f0533889 Leszek Koltunski
478 be576d14 Leszek Koltunski
      if( StateList.getCurrentState()== StateList.READ )
479 f0533889 Leszek Koltunski
        {
480 be576d14 Leszek Koltunski
        RubikStateSolving solving = (RubikStateSolving) StateList.SOLV.getStateClass();
481 8d50e08d Leszek Koltunski
        solving.resetElapsed();
482
483
        final RubikActivity act = (RubikActivity)getContext();
484 f0533889 Leszek Koltunski
485 8d50e08d Leszek Koltunski
        act.runOnUiThread(new Runnable()
486
          {
487
          @Override
488
          public void run()
489
            {
490 be576d14 Leszek Koltunski
            StateList.switchState( act, StateList.SOLV);
491 8d50e08d Leszek Koltunski
            }
492
          });
493 f0533889 Leszek Koltunski
        }
494 8d50e08d Leszek Koltunski
495
      addSpeedProbe(x,y);
496
497
      mBeginningRotation = false;
498
      mContinuingRotation= true;
499
      }
500
501 7695a3be Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
502
503
    private float getAngle(float x1, float y1, float x2, float y2)
504
      {
505
      return (float) Math.atan2(y1-y2, x1-x2);
506
      }
507
508 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
509
510
    private void actionMove(MotionEvent event)
511
      {
512 a4472437 Leszek Koltunski
      int pointer = event.findPointerIndex(mPtrID1 != INVALID_POINTER_ID ? mPtrID1:mPtrID2);
513 f0533889 Leszek Koltunski
514 7695a3be Leszek Koltunski
      if( pointer<0 ) return;
515
516 a4472437 Leszek Koltunski
      float pX = event.getX(pointer);
517
      float pY = event.getY(pointer);
518
519
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
520
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
521 0b7e1b05 Leszek Koltunski
522
      if( mBeginningRotation )
523
        {
524
        if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
525 8d50e08d Leszek Koltunski
          {
526 0b7e1b05 Leszek Koltunski
          beginRotation(x,y);
527 f0533889 Leszek Koltunski
          }
528
        }
529 0b7e1b05 Leszek Koltunski
      else if( mContinuingRotation )
530
        {
531
        continueRotation(x,y);
532
        }
533
      else if( mDragging )
534
        {
535 7695a3be Leszek Koltunski
        drag(event,x,y);
536 0b7e1b05 Leszek Koltunski
        }
537 f0533889 Leszek Koltunski
      else
538
        {
539 0b7e1b05 Leszek Koltunski
        setUpDragOrRotate(false,x,y);
540 f0533889 Leszek Koltunski
        }
541
      }
542
543 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
544
545
    private void actionDown(MotionEvent event)
546
      {
547 a4472437 Leszek Koltunski
      mPtrID1 = event.getPointerId(0);
548 0b7e1b05 Leszek Koltunski
549 4da7d87a Leszek Koltunski
      float x = event.getX();
550
      float y = event.getY();
551 8d50e08d Leszek Koltunski
552 4da7d87a Leszek Koltunski
      mX = (x - mScreenWidth*0.5f)/mScreenMin;
553
      mY = (mScreenHeight*0.5f -y)/mScreenMin;
554 8d50e08d Leszek Koltunski
555
      setUpDragOrRotate(true,mX,mY);
556
      }
557
558 f0533889 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
559
560 0b7e1b05 Leszek Koltunski
    private void actionUp(MotionEvent event)
561 f0533889 Leszek Koltunski
      {
562 a4472437 Leszek Koltunski
      mPtrID1 = INVALID_POINTER_ID;
563
      mPtrID2 = INVALID_POINTER_ID;
564 f0533889 Leszek Koltunski
565
      if( mContinuingRotation )
566
        {
567 0b7e1b05 Leszek Koltunski
        finishRotation();
568 f0533889 Leszek Koltunski
        }
569
570
      if( mLastCubitColor>=0 )
571
        {
572
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
573 c558f011 Leszek Koltunski
        mLastCubitColor = -1;
574 f0533889 Leszek Koltunski
        }
575
      }
576
577 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
578
579
    private void actionDown2(MotionEvent event)
580
      {
581 a4472437 Leszek Koltunski
      int index = event.getActionIndex();
582
583
      if( mPtrID1==INVALID_POINTER_ID )
584
        {
585
        mPtrID1 = event.getPointerId(index);
586 7695a3be Leszek Koltunski
        float x = event.getX();
587
        float y = event.getY();
588
589
        if( mPtrID2 != INVALID_POINTER_ID )
590
          {
591
          int pointer = event.findPointerIndex(mPtrID2);
592
593 2f6da3f4 Leszek Koltunski
          try
594
            {
595
            float x2 = event.getX(pointer);
596
            float y2 = event.getY(pointer);
597
598
            mRotAngle = getAngle(x,-y,x2,-y2);
599
            mInitDistance = -1;
600
            }
601
          catch(IllegalArgumentException ex)
602
            {
603
            mPtrID1=INVALID_POINTER_ID;
604
            mPtrID2=INVALID_POINTER_ID;
605
606
            FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
607
            crashlytics.setCustomKey("DragError", "pointer="+pointer );
608
            crashlytics.recordException(ex);
609 7695a3be Leszek Koltunski
610 2f6da3f4 Leszek Koltunski
            return;
611
            }
612 7695a3be Leszek Koltunski
          }
613 a4472437 Leszek Koltunski
614
        mX = (x - mScreenWidth*0.5f)/mScreenMin;
615
        mY = (mScreenHeight*0.5f -y)/mScreenMin;
616
        }
617
      else if( mPtrID2==INVALID_POINTER_ID )
618
        {
619
        mPtrID2 = event.getPointerId(index);
620
621 7695a3be Leszek Koltunski
        float x = event.getX();
622
        float y = event.getY();
623
624
        if( mPtrID2 != INVALID_POINTER_ID )
625 a4472437 Leszek Koltunski
          {
626 7695a3be Leszek Koltunski
          int pointer = event.findPointerIndex(mPtrID2);
627
628 2f6da3f4 Leszek Koltunski
          try
629
            {
630
            float x2 = event.getX(pointer);
631
            float y2 = event.getY(pointer);
632
633
            mRotAngle = getAngle(x,-y,x2,-y2);
634
            mInitDistance = -1;
635
            }
636
          catch(IllegalArgumentException ex)
637
            {
638
            mPtrID1=INVALID_POINTER_ID;
639
            mPtrID2=INVALID_POINTER_ID;
640
641
            FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
642
            crashlytics.setCustomKey("DragError", "pointer="+pointer );
643
            crashlytics.recordException(ex);
644 a4472437 Leszek Koltunski
645 2f6da3f4 Leszek Koltunski
            return;
646
            }
647 7695a3be Leszek Koltunski
          }
648
649
        if( mBeginningRotation || mContinuingRotation )
650
          {
651 a4472437 Leszek Koltunski
          mX = (x - mScreenWidth*0.5f)/mScreenMin;
652
          mY = (mScreenHeight*0.5f -y)/mScreenMin;
653
          }
654
        }
655
656 0b7e1b05 Leszek Koltunski
      if( mBeginningRotation )
657
        {
658
        mContinuingRotation = false;
659
        mBeginningRotation  = false;
660
        mDragging           = true;
661
        }
662
      else if( mContinuingRotation )
663
        {
664
        finishRotation();
665
        }
666 8d50e08d Leszek Koltunski
      }
667
668
///////////////////////////////////////////////////////////////////////////////////////////////////
669
670 0b7e1b05 Leszek Koltunski
    private void actionUp2(MotionEvent event)
671 8d50e08d Leszek Koltunski
      {
672 4da7d87a Leszek Koltunski
      int index = event.getActionIndex();
673
674 7695a3be Leszek Koltunski
      if( index==event.findPointerIndex(mPtrID1) )
675 a4472437 Leszek Koltunski
        {
676
        mPtrID1 = INVALID_POINTER_ID;
677
        int pointer = event.findPointerIndex(mPtrID2);
678
679 7695a3be Leszek Koltunski
        if( pointer>=0 )
680
          {
681
          float x1 = event.getX(pointer);
682
          float y1 = event.getY(pointer);
683
684
          mX = (x1 - mScreenWidth*0.5f)/mScreenMin;
685
          mY = (mScreenHeight*0.5f -y1)/mScreenMin;
686
          }
687 a4472437 Leszek Koltunski
        }
688 7695a3be Leszek Koltunski
      else if( index==event.findPointerIndex(mPtrID2) )
689 a4472437 Leszek Koltunski
        {
690
        mPtrID2 = INVALID_POINTER_ID;
691
        }
692 8d50e08d Leszek Koltunski
      }
693
694 e03e0352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
695
696
    void initialize()
697
      {
698
      mPtrID1 = INVALID_POINTER_ID;
699
      mPtrID2 = INVALID_POINTER_ID;
700
      }
701
702 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
703
// PUBLIC API
704
///////////////////////////////////////////////////////////////////////////////////////////////////
705
706
    public RubikSurfaceView(Context context, AttributeSet attrs)
707
      {
708
      super(context,attrs);
709
710
      if(!isInEditMode())
711
        {
712 4e248bcc Leszek Koltunski
        mLastCubitColor = -1;
713 168b6b56 Leszek Koltunski
        mCurrRotSpeed   = 0.0f;
714
715 4c864c68 Leszek Koltunski
        mLastX = new float[NUM_SPEED_PROBES];
716
        mLastY = new float[NUM_SPEED_PROBES];
717
        mLastT = new long[NUM_SPEED_PROBES];
718 168b6b56 Leszek Koltunski
        mFirstIndex =0;
719
        mLastIndex  =0;
720 4e248bcc Leszek Koltunski
721 5a4d4fba Leszek Koltunski
        mRenderer  = new RubikRenderer(this);
722
        mPreRender = new RubikPreRender(this);
723 47ba5ddc Leszek Koltunski
724 cd83d0aa Leszek Koltunski
        RubikActivity act = (RubikActivity)context;
725
        DisplayMetrics dm = new DisplayMetrics();
726
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
727
728
        mDensity = dm.densityDpi;
729
730
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
731
732 4489e68e Leszek Koltunski
        try
733 cd83d0aa Leszek Koltunski
          {
734
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
735 e7e0a94d Leszek Koltunski
          int esVersion = configurationInfo.reqGlEsVersion>>16;
736
          setEGLContextClientVersion(esVersion);
737 cd83d0aa Leszek Koltunski
          setRenderer(mRenderer);
738
          }
739 4489e68e Leszek Koltunski
        catch(Exception ex)
740
          {
741 e7e0a94d Leszek Koltunski
          act.OpenGLError();
742 4489e68e Leszek Koltunski
743
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
744
          String version = GLES30.glGetString(GLES30.GL_VERSION);
745
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
746
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
747
748
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
749
          crashlytics.setCustomKey("GLSL Version"  , shading );
750
          crashlytics.setCustomKey("GLversion"     , version );
751
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
752
          crashlytics.setCustomKey("GLSLrenderer"  , renderer);
753
          crashlytics.recordException(ex);
754
          }
755 47ba5ddc Leszek Koltunski
        }
756
      }
757
758
///////////////////////////////////////////////////////////////////////////////////////////////////
759
760
    @Override
761
    public boolean onTouchEvent(MotionEvent event)
762
      {
763 8d50e08d Leszek Koltunski
      int action = event.getActionMasked();
764 47ba5ddc Leszek Koltunski
765
      switch(action)
766
         {
767 8d50e08d Leszek Koltunski
         case MotionEvent.ACTION_DOWN        : actionDown(event) ; break;
768
         case MotionEvent.ACTION_MOVE        : actionMove(event) ; break;
769 0b7e1b05 Leszek Koltunski
         case MotionEvent.ACTION_UP          : actionUp(event)   ; break;
770 8d50e08d Leszek Koltunski
         case MotionEvent.ACTION_POINTER_DOWN: actionDown2(event); break;
771 0b7e1b05 Leszek Koltunski
         case MotionEvent.ACTION_POINTER_UP  : actionUp2(event)  ; break;
772 47ba5ddc Leszek Koltunski
         }
773
774
      return true;
775
      }
776 0c52af30 Leszek Koltunski
}