Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ 4946b635

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