Project

General

Profile

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

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

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