Project

General

Profile

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

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

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