Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikSurfaceView.java @ 82f42eeb

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