Project

General

Profile

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

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

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 fcd5b990 Leszek Koltunski
import org.distorted.screens.ScreenList;
39
import org.distorted.screens.RubikScreenPlay;
40
import org.distorted.screens.RubikScreenSolver;
41
import org.distorted.screens.RubikScreenSolving;
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 30bc2d91 Leszek Koltunski
    private final Static4D CAMERA_POINT = new Static4D(0, 0, 0, 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 fcd5b990 Leszek Koltunski
      int mode = ScreenList.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 30bc2d91 Leszek Koltunski
        TwistyObject object = mPreRender.getObject();
309 80fe0ef7 Leszek Koltunski
        CAMERA_POINT.set2( object==null ? 1.21f : object.getCameraDist() );
310 30bc2d91 Leszek Koltunski
311 d99f3a48 Leszek Koltunski
        Static4D touchPoint = new Static4D(x, y, 0, 0);
312
        Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuat);
313 4da7d87a Leszek Koltunski
        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
314 f0533889 Leszek Koltunski
315 d99f3a48 Leszek Koltunski
        if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint,rotatedCamera) )
316 f0533889 Leszek Koltunski
          {
317
          mDragging           = false;
318
          mContinuingRotation = false;
319
320
          if( mode==MODE_ROTATE )
321
            {
322
            mBeginningRotation= mPreRender.canRotate();
323
            }
324
          else if( mode==MODE_REPLACE )
325
            {
326
            mBeginningRotation= false;
327
328
            if( down )
329
              {
330 fcd5b990 Leszek Koltunski
              RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getStateClass();
331 f0533889 Leszek Koltunski
              mLastCubitFace = mMovement.getTouchedFace();
332
              float[] point = mMovement.getTouchedPoint3D();
333
              int color = solver.getCurrentColor();
334
              mLastCubit = object.getCubit(point);
335
              mPreRender.setTextureMap( mLastCubit, mLastCubitFace, color );
336 d99f3a48 Leszek Koltunski
              mLastCubitColor = SolverMain.cubitIsLocked(object.getObjectList(), object.getNumLayers(), mLastCubit);
337 f0533889 Leszek Koltunski
              }
338
            }
339
          }
340
        else
341
          {
342 46405bb4 Leszek Koltunski
          final RubikActivity act = (RubikActivity)getContext();
343
344
          mDragging           = !act.isLocked();
345 f0533889 Leszek Koltunski
          mBeginningRotation  = false;
346
          mContinuingRotation = false;
347
          }
348
        }
349
      }
350
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352
353 7695a3be Leszek Koltunski
    private void drag(MotionEvent event, float x, float y)
354 f0533889 Leszek Koltunski
      {
355 7695a3be Leszek Koltunski
      if( mPtrID1!=INVALID_POINTER_ID && mPtrID2!=INVALID_POINTER_ID)
356
        {
357
        int pointer = event.findPointerIndex(mPtrID2);
358 b96a20a4 Leszek Koltunski
        float pX,pY;
359 7695a3be Leszek Koltunski
360 b96a20a4 Leszek Koltunski
        try
361
          {
362
          pX = event.getX(pointer);
363
          pY = event.getY(pointer);
364
          }
365
        catch(IllegalArgumentException ex)
366
          {
367
          mPtrID1=INVALID_POINTER_ID;
368
          mPtrID2=INVALID_POINTER_ID;
369
370
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
371
          crashlytics.setCustomKey("DragError", "pointer="+pointer );
372
          crashlytics.recordException(ex);
373
374
          return;
375
          }
376 7695a3be Leszek Koltunski
377
        float x2 = (pX - mScreenWidth*0.5f)/mScreenMin;
378
        float y2 = (mScreenHeight*0.5f -pY)/mScreenMin;
379
380
        float angleNow = getAngle(x,y,x2,y2);
381
        float angleDiff = angleNow-mRotAngle;
382
        float sinA =-(float)Math.sin(angleDiff);
383
        float cosA = (float)Math.cos(angleDiff);
384
385
        Static4D dragQuat = quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
386
        mTemp.set(dragQuat);
387
388
        mRotAngle = angleNow;
389 c7b00dfb Leszek Koltunski
390
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
391
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
392
        mInitDistance = distNow;
393
394 9c2f0c91 Leszek Koltunski
        TwistyObject object = mPreRender.getObject();
395 49595e4b Leszek Koltunski
        if( object!=null ) object.setObjectRatio(distQuot);
396 7695a3be Leszek Koltunski
        }
397
      else
398
        {
399
        Static4D dragQuat = quatMultiply(quatFromDrag(mX-x,y-mY), mQuat);
400
        mTemp.set(dragQuat);
401
        }
402
403 4da7d87a Leszek Koltunski
      mPreRender.setQuatOnNextRender();
404
      mX = x;
405
      mY = y;
406 0b7e1b05 Leszek Koltunski
      }
407
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409
410
    private void finishRotation()
411
      {
412
      computeCurrentSpeedInInchesPerSecond();
413
      int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
414
      mPreRender.finishRotation(angle);
415 ac722a43 Leszek Koltunski
      mPreRender.rememberMove(mCurrentAxis,mCurrentRow,angle);
416 0b7e1b05 Leszek Koltunski
417 15846fe4 Leszek Koltunski
      if( angle!=0 )
418 0b7e1b05 Leszek Koltunski
        {
419 fcd5b990 Leszek Koltunski
        if( ScreenList.getCurrentState()== ScreenList.SOLV )
420 15846fe4 Leszek Koltunski
          {
421 fcd5b990 Leszek Koltunski
          RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getStateClass();
422 15846fe4 Leszek Koltunski
          solving.addMove(mCurrentAxis, mCurrentRow, angle);
423
          }
424 fcd5b990 Leszek Koltunski
        if( ScreenList.getCurrentState()== ScreenList.PLAY )
425 15846fe4 Leszek Koltunski
          {
426 fcd5b990 Leszek Koltunski
          RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getStateClass();
427 15846fe4 Leszek Koltunski
          play.addMove(mCurrentAxis, mCurrentRow, angle);
428
          }
429 0b7e1b05 Leszek Koltunski
        }
430
431
      mContinuingRotation = false;
432
      mBeginningRotation  = false;
433
      mDragging           = true;
434 f0533889 Leszek Koltunski
      }
435
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437
438 0b7e1b05 Leszek Koltunski
    private void continueRotation(float x, float y)
439 f0533889 Leszek Koltunski
      {
440 0b7e1b05 Leszek Koltunski
      float dx = x-mStartRotX;
441
      float dy = y-mStartRotY;
442
      float alpha = dx*mAxisX + dy*mAxisY;
443
      float x2 = dx - alpha*mAxisX;
444
      float y2 = dy - alpha*mAxisY;
445
446
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
447
448
      // we have the length of 1D vector 'angle', now the direction:
449
      float tmp = mAxisY==0 ? -mAxisX*y2 : mAxisY*x2;
450
451
      float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
452 8d50e08d Leszek Koltunski
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
453
      mPreRender.getObject().continueRotation(mCurrentAngle);
454 f0533889 Leszek Koltunski
455 0b7e1b05 Leszek Koltunski
      addSpeedProbe(x2,y2);
456 8d50e08d Leszek Koltunski
      }
457 f0533889 Leszek Koltunski
458 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
459 f0533889 Leszek Koltunski
460 0b7e1b05 Leszek Koltunski
    private void beginRotation(float x, float y)
461 8d50e08d Leszek Koltunski
      {
462
      mStartRotX = x;
463
      mStartRotY = y;
464 f0533889 Leszek Koltunski
465 ee39837d Leszek Koltunski
      TwistyObject object = mPreRender.getObject();
466 d99f3a48 Leszek Koltunski
      int numLayers = object.getNumLayers();
467 ee39837d Leszek Koltunski
468 8d50e08d Leszek Koltunski
      Static4D touchPoint2 = new Static4D(x, y, 0, 0);
469 4da7d87a Leszek Koltunski
      Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuat);
470 d99f3a48 Leszek Koltunski
      Static2D res = mMovement.newRotation(numLayers,rotatedTouchPoint2);
471 f0533889 Leszek Koltunski
472 8d50e08d Leszek Koltunski
      mCurrentAxis = (int)res.get0();
473 ee39837d Leszek Koltunski
      mCurrentRow  = (int)res.get1();
474 fb377dae Leszek Koltunski
475 935f3663 Leszek Koltunski
      computeCurrentAxis( mMovement.getCastedRotAxis(mCurrentAxis) );
476 d99f3a48 Leszek Koltunski
      mRotationFactor = mMovement.returnRotationFactor(numLayers,mCurrentRow);
477 f0533889 Leszek Koltunski
478 8d50e08d Leszek Koltunski
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
479 f0533889 Leszek Koltunski
480 fcd5b990 Leszek Koltunski
      if( ScreenList.getCurrentState()== ScreenList.READ )
481 f0533889 Leszek Koltunski
        {
482 fcd5b990 Leszek Koltunski
        RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getStateClass();
483 8d50e08d Leszek Koltunski
        solving.resetElapsed();
484
485
        final RubikActivity act = (RubikActivity)getContext();
486 f0533889 Leszek Koltunski
487 8d50e08d Leszek Koltunski
        act.runOnUiThread(new Runnable()
488
          {
489
          @Override
490
          public void run()
491
            {
492 fcd5b990 Leszek Koltunski
            ScreenList.switchState( act, ScreenList.SOLV);
493 8d50e08d Leszek Koltunski
            }
494
          });
495 f0533889 Leszek Koltunski
        }
496 8d50e08d Leszek Koltunski
497
      addSpeedProbe(x,y);
498
499
      mBeginningRotation = false;
500
      mContinuingRotation= true;
501
      }
502
503 7695a3be Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
504
505
    private float getAngle(float x1, float y1, float x2, float y2)
506
      {
507
      return (float) Math.atan2(y1-y2, x1-x2);
508
      }
509
510 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
511
512
    private void actionMove(MotionEvent event)
513
      {
514 a4472437 Leszek Koltunski
      int pointer = event.findPointerIndex(mPtrID1 != INVALID_POINTER_ID ? mPtrID1:mPtrID2);
515 f0533889 Leszek Koltunski
516 7695a3be Leszek Koltunski
      if( pointer<0 ) return;
517
518 a4472437 Leszek Koltunski
      float pX = event.getX(pointer);
519
      float pY = event.getY(pointer);
520
521
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
522
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
523 0b7e1b05 Leszek Koltunski
524
      if( mBeginningRotation )
525
        {
526
        if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
527 8d50e08d Leszek Koltunski
          {
528 0b7e1b05 Leszek Koltunski
          beginRotation(x,y);
529 f0533889 Leszek Koltunski
          }
530
        }
531 0b7e1b05 Leszek Koltunski
      else if( mContinuingRotation )
532
        {
533
        continueRotation(x,y);
534
        }
535
      else if( mDragging )
536
        {
537 7695a3be Leszek Koltunski
        drag(event,x,y);
538 0b7e1b05 Leszek Koltunski
        }
539 f0533889 Leszek Koltunski
      else
540
        {
541 0b7e1b05 Leszek Koltunski
        setUpDragOrRotate(false,x,y);
542 f0533889 Leszek Koltunski
        }
543
      }
544
545 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
546
547
    private void actionDown(MotionEvent event)
548
      {
549 a4472437 Leszek Koltunski
      mPtrID1 = event.getPointerId(0);
550 0b7e1b05 Leszek Koltunski
551 4da7d87a Leszek Koltunski
      float x = event.getX();
552
      float y = event.getY();
553 8d50e08d Leszek Koltunski
554 4da7d87a Leszek Koltunski
      mX = (x - mScreenWidth*0.5f)/mScreenMin;
555
      mY = (mScreenHeight*0.5f -y)/mScreenMin;
556 8d50e08d Leszek Koltunski
557
      setUpDragOrRotate(true,mX,mY);
558
      }
559
560 f0533889 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
561
562 0b7e1b05 Leszek Koltunski
    private void actionUp(MotionEvent event)
563 f0533889 Leszek Koltunski
      {
564 a4472437 Leszek Koltunski
      mPtrID1 = INVALID_POINTER_ID;
565
      mPtrID2 = INVALID_POINTER_ID;
566 f0533889 Leszek Koltunski
567
      if( mContinuingRotation )
568
        {
569 0b7e1b05 Leszek Koltunski
        finishRotation();
570 f0533889 Leszek Koltunski
        }
571
572
      if( mLastCubitColor>=0 )
573
        {
574
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
575 c558f011 Leszek Koltunski
        mLastCubitColor = -1;
576 f0533889 Leszek Koltunski
        }
577
      }
578
579 8d50e08d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
580
581
    private void actionDown2(MotionEvent event)
582
      {
583 a4472437 Leszek Koltunski
      int index = event.getActionIndex();
584
585
      if( mPtrID1==INVALID_POINTER_ID )
586
        {
587
        mPtrID1 = event.getPointerId(index);
588 7695a3be Leszek Koltunski
        float x = event.getX();
589
        float y = event.getY();
590
591
        if( mPtrID2 != INVALID_POINTER_ID )
592
          {
593
          int pointer = event.findPointerIndex(mPtrID2);
594
595 2f6da3f4 Leszek Koltunski
          try
596
            {
597
            float x2 = event.getX(pointer);
598
            float y2 = event.getY(pointer);
599
600
            mRotAngle = getAngle(x,-y,x2,-y2);
601
            mInitDistance = -1;
602
            }
603
          catch(IllegalArgumentException ex)
604
            {
605
            mPtrID1=INVALID_POINTER_ID;
606
            mPtrID2=INVALID_POINTER_ID;
607
608
            FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
609
            crashlytics.setCustomKey("DragError", "pointer="+pointer );
610
            crashlytics.recordException(ex);
611 7695a3be Leszek Koltunski
612 2f6da3f4 Leszek Koltunski
            return;
613
            }
614 7695a3be Leszek Koltunski
          }
615 a4472437 Leszek Koltunski
616
        mX = (x - mScreenWidth*0.5f)/mScreenMin;
617
        mY = (mScreenHeight*0.5f -y)/mScreenMin;
618
        }
619
      else if( mPtrID2==INVALID_POINTER_ID )
620
        {
621
        mPtrID2 = event.getPointerId(index);
622
623 7695a3be Leszek Koltunski
        float x = event.getX();
624
        float y = event.getY();
625
626
        if( mPtrID2 != INVALID_POINTER_ID )
627 a4472437 Leszek Koltunski
          {
628 7695a3be Leszek Koltunski
          int pointer = event.findPointerIndex(mPtrID2);
629
630 2f6da3f4 Leszek Koltunski
          try
631
            {
632
            float x2 = event.getX(pointer);
633
            float y2 = event.getY(pointer);
634
635
            mRotAngle = getAngle(x,-y,x2,-y2);
636
            mInitDistance = -1;
637
            }
638
          catch(IllegalArgumentException ex)
639
            {
640
            mPtrID1=INVALID_POINTER_ID;
641
            mPtrID2=INVALID_POINTER_ID;
642
643
            FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
644
            crashlytics.setCustomKey("DragError", "pointer="+pointer );
645
            crashlytics.recordException(ex);
646 a4472437 Leszek Koltunski
647 2f6da3f4 Leszek Koltunski
            return;
648
            }
649 7695a3be Leszek Koltunski
          }
650
651
        if( mBeginningRotation || mContinuingRotation )
652
          {
653 a4472437 Leszek Koltunski
          mX = (x - mScreenWidth*0.5f)/mScreenMin;
654
          mY = (mScreenHeight*0.5f -y)/mScreenMin;
655
          }
656
        }
657
658 0b7e1b05 Leszek Koltunski
      if( mBeginningRotation )
659
        {
660
        mContinuingRotation = false;
661
        mBeginningRotation  = false;
662
        mDragging           = true;
663
        }
664
      else if( mContinuingRotation )
665
        {
666
        finishRotation();
667
        }
668 8d50e08d Leszek Koltunski
      }
669
670
///////////////////////////////////////////////////////////////////////////////////////////////////
671
672 0b7e1b05 Leszek Koltunski
    private void actionUp2(MotionEvent event)
673 8d50e08d Leszek Koltunski
      {
674 4da7d87a Leszek Koltunski
      int index = event.getActionIndex();
675
676 7695a3be Leszek Koltunski
      if( index==event.findPointerIndex(mPtrID1) )
677 a4472437 Leszek Koltunski
        {
678
        mPtrID1 = INVALID_POINTER_ID;
679
        int pointer = event.findPointerIndex(mPtrID2);
680
681 7695a3be Leszek Koltunski
        if( pointer>=0 )
682
          {
683
          float x1 = event.getX(pointer);
684
          float y1 = event.getY(pointer);
685
686
          mX = (x1 - mScreenWidth*0.5f)/mScreenMin;
687
          mY = (mScreenHeight*0.5f -y1)/mScreenMin;
688
          }
689 a4472437 Leszek Koltunski
        }
690 7695a3be Leszek Koltunski
      else if( index==event.findPointerIndex(mPtrID2) )
691 a4472437 Leszek Koltunski
        {
692
        mPtrID2 = INVALID_POINTER_ID;
693
        }
694 8d50e08d Leszek Koltunski
      }
695
696 e03e0352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
697
698
    void initialize()
699
      {
700
      mPtrID1 = INVALID_POINTER_ID;
701
      mPtrID2 = INVALID_POINTER_ID;
702
      }
703
704 47ba5ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
705
// PUBLIC API
706
///////////////////////////////////////////////////////////////////////////////////////////////////
707
708
    public RubikSurfaceView(Context context, AttributeSet attrs)
709
      {
710
      super(context,attrs);
711
712
      if(!isInEditMode())
713
        {
714 4e248bcc Leszek Koltunski
        mLastCubitColor = -1;
715 168b6b56 Leszek Koltunski
        mCurrRotSpeed   = 0.0f;
716
717 4c864c68 Leszek Koltunski
        mLastX = new float[NUM_SPEED_PROBES];
718
        mLastY = new float[NUM_SPEED_PROBES];
719
        mLastT = new long[NUM_SPEED_PROBES];
720 168b6b56 Leszek Koltunski
        mFirstIndex =0;
721
        mLastIndex  =0;
722 4e248bcc Leszek Koltunski
723 5a4d4fba Leszek Koltunski
        mRenderer  = new RubikRenderer(this);
724
        mPreRender = new RubikPreRender(this);
725 47ba5ddc Leszek Koltunski
726 cd83d0aa Leszek Koltunski
        RubikActivity act = (RubikActivity)context;
727
        DisplayMetrics dm = new DisplayMetrics();
728
        act.getWindowManager().getDefaultDisplay().getMetrics(dm);
729
730
        mDensity = dm.densityDpi;
731
732
        final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
733
734 4489e68e Leszek Koltunski
        try
735 cd83d0aa Leszek Koltunski
          {
736
          final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
737 e7e0a94d Leszek Koltunski
          int esVersion = configurationInfo.reqGlEsVersion>>16;
738
          setEGLContextClientVersion(esVersion);
739 cd83d0aa Leszek Koltunski
          setRenderer(mRenderer);
740
          }
741 4489e68e Leszek Koltunski
        catch(Exception ex)
742
          {
743 e7e0a94d Leszek Koltunski
          act.OpenGLError();
744 4489e68e Leszek Koltunski
745
          String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
746
          String version = GLES30.glGetString(GLES30.GL_VERSION);
747
          String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
748
          String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
749
750
          FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
751
          crashlytics.setCustomKey("GLSL Version"  , shading );
752
          crashlytics.setCustomKey("GLversion"     , version );
753
          crashlytics.setCustomKey("GL Vendor "    , vendor  );
754
          crashlytics.setCustomKey("GLSLrenderer"  , renderer);
755
          crashlytics.recordException(ex);
756
          }
757 47ba5ddc Leszek Koltunski
        }
758
      }
759
760
///////////////////////////////////////////////////////////////////////////////////////////////////
761
762
    @Override
763
    public boolean onTouchEvent(MotionEvent event)
764
      {
765 8d50e08d Leszek Koltunski
      int action = event.getActionMasked();
766 47ba5ddc Leszek Koltunski
767
      switch(action)
768
         {
769 8d50e08d Leszek Koltunski
         case MotionEvent.ACTION_DOWN        : actionDown(event) ; break;
770
         case MotionEvent.ACTION_MOVE        : actionMove(event) ; break;
771 0b7e1b05 Leszek Koltunski
         case MotionEvent.ACTION_UP          : actionUp(event)   ; break;
772 8d50e08d Leszek Koltunski
         case MotionEvent.ACTION_POINTER_DOWN: actionDown2(event); break;
773 0b7e1b05 Leszek Koltunski
         case MotionEvent.ACTION_POINTER_UP  : actionUp2(event)  ; break;
774 47ba5ddc Leszek Koltunski
         }
775
776
      return true;
777
      }
778 0c52af30 Leszek Koltunski
}