Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ 1cd323dd

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