Project

General

Profile

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

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

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