1 |
af88bf2e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
2971588c
|
Leszek Koltunski
|
// Copyright 2020 Leszek Koltunski //
|
3 |
af88bf2e
|
Leszek Koltunski
|
// //
|
4 |
|
|
// This file is part of Magic Cube. //
|
5 |
|
|
// //
|
6 |
|
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7 |
|
|
// 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 |
|
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12 |
|
|
// 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 |
|
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
package org.distorted.tutorial;
|
21 |
|
|
|
22 |
|
|
import android.app.ActivityManager;
|
23 |
|
|
import android.content.Context;
|
24 |
|
|
import android.content.pm.ConfigurationInfo;
|
25 |
|
|
import android.opengl.GLES30;
|
26 |
|
|
import android.opengl.GLSurfaceView;
|
27 |
|
|
import android.util.AttributeSet;
|
28 |
|
|
import android.util.DisplayMetrics;
|
29 |
|
|
import android.view.MotionEvent;
|
30 |
|
|
|
31 |
|
|
import com.google.firebase.crashlytics.FirebaseCrashlytics;
|
32 |
|
|
|
33 |
|
|
import org.distorted.library.type.Static2D;
|
34 |
|
|
import org.distorted.library.type.Static4D;
|
35 |
|
|
import org.distorted.objects.Movement;
|
36 |
|
|
import org.distorted.objects.TwistyObject;
|
37 |
|
|
|
38 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
39 |
|
|
|
40 |
|
|
public class TutorialSurfaceView extends GLSurfaceView
|
41 |
|
|
{
|
42 |
|
|
private static final int NUM_SPEED_PROBES = 10;
|
43 |
|
|
private static final int INVALID_POINTER_ID = -1;
|
44 |
|
|
|
45 |
|
|
// Moving the finger from the middle of the vertical screen to the right edge will rotate a
|
46 |
|
|
// given face by SWIPING_SENSITIVITY/2 degrees.
|
47 |
|
|
private final static int SWIPING_SENSITIVITY = 240;
|
48 |
|
|
// Moving the finger by 0.3 of an inch will start a Rotation.
|
49 |
|
|
private final static float ROTATION_SENSITIVITY = 0.3f;
|
50 |
|
|
|
51 |
|
|
private final Static4D CAMERA_POINT = new Static4D(0, 0, 1, 0);
|
52 |
|
|
|
53 |
|
|
private TutorialRenderer mRenderer;
|
54 |
|
|
private TutorialPreRender mPreRender;
|
55 |
|
|
private Movement mMovement;
|
56 |
|
|
private boolean mDragging, mBeginningRotation, mContinuingRotation;
|
57 |
|
|
private int mScreenWidth, mScreenHeight, mScreenMin;
|
58 |
|
|
|
59 |
|
|
private float mRotAngle, mInitDistance;
|
60 |
|
|
private int mPtrID1, mPtrID2;
|
61 |
|
|
private float mX, mY;
|
62 |
|
|
private float mStartRotX, mStartRotY;
|
63 |
|
|
private float mAxisX, mAxisY;
|
64 |
|
|
private float mRotationFactor;
|
65 |
|
|
private int mCurrentAxis, mCurrentRow;
|
66 |
|
|
private float mCurrentAngle, mCurrRotSpeed;
|
67 |
|
|
private float[] mLastX;
|
68 |
|
|
private float[] mLastY;
|
69 |
|
|
private long[] mLastT;
|
70 |
|
|
private int mFirstIndex, mLastIndex;
|
71 |
|
|
private int mDensity;
|
72 |
|
|
|
73 |
|
|
private static Static4D mQuat= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
|
74 |
|
|
private static Static4D mTemp= new Static4D(0,0,0,1);
|
75 |
|
|
|
76 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77 |
|
|
|
78 |
|
|
void setScreenSize(int width, int height)
|
79 |
|
|
{
|
80 |
|
|
mScreenWidth = width;
|
81 |
|
|
mScreenHeight= height;
|
82 |
|
|
|
83 |
|
|
mScreenMin = Math.min(width, height);
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
87 |
|
|
|
88 |
|
|
boolean isVertical()
|
89 |
|
|
{
|
90 |
|
|
return mScreenHeight>mScreenWidth;
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94 |
|
|
|
95 |
|
|
TutorialRenderer getRenderer()
|
96 |
|
|
{
|
97 |
|
|
return mRenderer;
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101 |
|
|
|
102 |
|
|
TutorialPreRender getPreRender()
|
103 |
|
|
{
|
104 |
|
|
return mPreRender;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
108 |
|
|
|
109 |
|
|
void setQuat()
|
110 |
|
|
{
|
111 |
|
|
mQuat.set(mTemp);
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
115 |
|
|
|
116 |
|
|
Static4D getQuat()
|
117 |
|
|
{
|
118 |
|
|
return mQuat;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122 |
|
|
|
123 |
|
|
void setMovement(Movement movement)
|
124 |
|
|
{
|
125 |
|
|
mMovement = movement;
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
129 |
|
|
|
130 |
|
|
private Static4D quatFromDrag(float dragX, float dragY)
|
131 |
|
|
{
|
132 |
|
|
float axisX = dragY; // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
|
133 |
|
|
float axisY = dragX; // Why not (-dragY, dragX) ? because Y axis is also inverted!
|
134 |
|
|
float axisZ = 0;
|
135 |
|
|
float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
|
136 |
|
|
|
137 |
|
|
if( axisL>0 )
|
138 |
|
|
{
|
139 |
|
|
axisX /= axisL;
|
140 |
|
|
axisY /= axisL;
|
141 |
|
|
axisZ /= axisL;
|
142 |
|
|
|
143 |
|
|
float ratio = axisL;
|
144 |
|
|
ratio = ratio - (int)ratio; // the cos() is only valid in (0,Pi)
|
145 |
|
|
|
146 |
|
|
float cosA = (float)Math.cos(Math.PI*ratio);
|
147 |
|
|
float sinA = (float)Math.sqrt(1-cosA*cosA);
|
148 |
|
|
|
149 |
|
|
return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
return new Static4D(0f, 0f, 0f, 1f);
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
156 |
|
|
// cast the 3D axis we are currently rotating along (which is already casted to the surface of the
|
157 |
|
|
// currently touched face AND converted into a 4D vector - fourth 0) to a 2D in-screen-surface axis
|
158 |
|
|
|
159 |
|
|
private void computeCurrentAxis(Static4D axis)
|
160 |
|
|
{
|
161 |
|
|
Static4D result = rotateVectorByQuat(axis, mQuat);
|
162 |
|
|
|
163 |
|
|
mAxisX =result.get0();
|
164 |
|
|
mAxisY =result.get1();
|
165 |
|
|
|
166 |
|
|
float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
|
167 |
|
|
mAxisX /= len;
|
168 |
|
|
mAxisY /= len;
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
172 |
|
|
// return quat1*quat2
|
173 |
|
|
|
174 |
|
|
public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
|
175 |
|
|
{
|
176 |
|
|
float qx = quat1.get0();
|
177 |
|
|
float qy = quat1.get1();
|
178 |
|
|
float qz = quat1.get2();
|
179 |
|
|
float qw = quat1.get3();
|
180 |
|
|
|
181 |
|
|
float rx = quat2.get0();
|
182 |
|
|
float ry = quat2.get1();
|
183 |
|
|
float rz = quat2.get2();
|
184 |
|
|
float rw = quat2.get3();
|
185 |
|
|
|
186 |
|
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
187 |
|
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
188 |
|
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
189 |
|
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
190 |
|
|
|
191 |
|
|
return new Static4D(tx,ty,tz,tw);
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
195 |
|
|
// rotate 'vector' by quat ( i.e. return quat*vector*(quat^-1) )
|
196 |
|
|
|
197 |
|
|
public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
|
198 |
|
|
{
|
199 |
|
|
float qx = quat.get0();
|
200 |
|
|
float qy = quat.get1();
|
201 |
|
|
float qz = quat.get2();
|
202 |
|
|
float qw = quat.get3();
|
203 |
|
|
|
204 |
|
|
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
|
205 |
|
|
Static4D tmp = quatMultiply(quat,vector);
|
206 |
|
|
|
207 |
|
|
return quatMultiply(tmp,quatInverted);
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211 |
|
|
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat )
|
212 |
|
|
|
213 |
|
|
public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
|
214 |
|
|
{
|
215 |
|
|
float qx = quat.get0();
|
216 |
|
|
float qy = quat.get1();
|
217 |
|
|
float qz = quat.get2();
|
218 |
|
|
float qw = quat.get3();
|
219 |
|
|
|
220 |
|
|
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
|
221 |
|
|
Static4D tmp = quatMultiply(quatInverted,vector);
|
222 |
|
|
|
223 |
|
|
return quatMultiply(tmp,quat);
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
227 |
|
|
|
228 |
|
|
private void addSpeedProbe(float x, float y)
|
229 |
|
|
{
|
230 |
|
|
long currTime = System.currentTimeMillis();
|
231 |
|
|
boolean theSame = mLastIndex==mFirstIndex;
|
232 |
|
|
|
233 |
|
|
mLastIndex++;
|
234 |
|
|
if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
|
235 |
|
|
|
236 |
|
|
mLastT[mLastIndex] = currTime;
|
237 |
|
|
mLastX[mLastIndex] = x;
|
238 |
|
|
mLastY[mLastIndex] = y;
|
239 |
|
|
|
240 |
|
|
if( mLastIndex==mFirstIndex)
|
241 |
|
|
{
|
242 |
|
|
mFirstIndex++;
|
243 |
|
|
if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
if( theSame )
|
247 |
|
|
{
|
248 |
|
|
mLastT[mFirstIndex] = currTime;
|
249 |
|
|
mLastX[mFirstIndex] = x;
|
250 |
|
|
mLastY[mFirstIndex] = y;
|
251 |
|
|
}
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
255 |
|
|
|
256 |
|
|
private void computeCurrentSpeedInInchesPerSecond()
|
257 |
|
|
{
|
258 |
|
|
long firstTime = mLastT[mFirstIndex];
|
259 |
|
|
long lastTime = mLastT[mLastIndex];
|
260 |
|
|
float fX = mLastX[mFirstIndex];
|
261 |
|
|
float fY = mLastY[mFirstIndex];
|
262 |
|
|
float lX = mLastX[mLastIndex];
|
263 |
|
|
float lY = mLastY[mLastIndex];
|
264 |
|
|
|
265 |
|
|
long timeDiff = lastTime-firstTime;
|
266 |
|
|
|
267 |
|
|
mLastIndex = 0;
|
268 |
|
|
mFirstIndex= 0;
|
269 |
|
|
|
270 |
|
|
mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX,fY,lX,lY)/timeDiff : 0;
|
271 |
|
|
}
|
272 |
|
|
|
273 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
274 |
|
|
|
275 |
|
|
private float retFingerDragDistanceInInches(float xFrom, float yFrom, float xTo, float yTo)
|
276 |
|
|
{
|
277 |
|
|
float xDist = mScreenWidth*(xFrom-xTo);
|
278 |
|
|
float yDist = mScreenHeight*(yFrom-yTo);
|
279 |
|
|
float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
|
280 |
|
|
|
281 |
|
|
return distInPixels/mDensity;
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
285 |
|
|
|
286 |
|
|
private void setUpDragOrRotate(float x, float y)
|
287 |
|
|
{
|
288 |
|
|
Static4D touchPoint = new Static4D(x, y, 0, 0);
|
289 |
|
|
Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuat);
|
290 |
|
|
Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
|
291 |
|
|
|
292 |
|
|
if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint,rotatedCamera) )
|
293 |
|
|
{
|
294 |
|
|
mDragging = false;
|
295 |
|
|
mContinuingRotation = false;
|
296 |
|
|
mBeginningRotation = true;
|
297 |
|
|
}
|
298 |
|
|
else
|
299 |
|
|
{
|
300 |
|
|
final TutorialActivity act = (TutorialActivity)getContext();
|
301 |
|
|
mDragging = !act.isLocked();
|
302 |
|
|
mContinuingRotation = false;
|
303 |
|
|
mBeginningRotation = false;
|
304 |
|
|
}
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
308 |
|
|
|
309 |
|
|
private void drag(MotionEvent event, float x, float y)
|
310 |
|
|
{
|
311 |
|
|
if( mPtrID1!=INVALID_POINTER_ID && mPtrID2!=INVALID_POINTER_ID)
|
312 |
|
|
{
|
313 |
|
|
int pointer = event.findPointerIndex(mPtrID2);
|
314 |
|
|
float pX,pY;
|
315 |
|
|
|
316 |
|
|
try
|
317 |
|
|
{
|
318 |
|
|
pX = event.getX(pointer);
|
319 |
|
|
pY = event.getY(pointer);
|
320 |
|
|
}
|
321 |
|
|
catch(IllegalArgumentException ex)
|
322 |
|
|
{
|
323 |
|
|
mPtrID1=INVALID_POINTER_ID;
|
324 |
|
|
mPtrID2=INVALID_POINTER_ID;
|
325 |
|
|
|
326 |
|
|
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
|
327 |
|
|
crashlytics.setCustomKey("DragError", "pointer="+pointer );
|
328 |
|
|
crashlytics.recordException(ex);
|
329 |
|
|
|
330 |
|
|
return;
|
331 |
|
|
}
|
332 |
|
|
|
333 |
|
|
float x2 = (pX - mScreenWidth*0.5f)/mScreenMin;
|
334 |
|
|
float y2 = (mScreenHeight*0.5f -pY)/mScreenMin;
|
335 |
|
|
|
336 |
|
|
float angleNow = getAngle(x,y,x2,y2);
|
337 |
|
|
float angleDiff = angleNow-mRotAngle;
|
338 |
|
|
float sinA =-(float)Math.sin(angleDiff);
|
339 |
|
|
float cosA = (float)Math.cos(angleDiff);
|
340 |
|
|
|
341 |
|
|
Static4D dragQuat = quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
|
342 |
|
|
mTemp.set(dragQuat);
|
343 |
|
|
|
344 |
|
|
mRotAngle = angleNow;
|
345 |
|
|
|
346 |
|
|
float distNow = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
|
347 |
|
|
float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
|
348 |
|
|
mInitDistance = distNow;
|
349 |
|
|
|
350 |
|
|
TwistyObject object = mPreRender.getObject();
|
351 |
|
|
if( object!=null ) object.setObjectRatio(distQuot);
|
352 |
|
|
}
|
353 |
|
|
else
|
354 |
|
|
{
|
355 |
|
|
Static4D dragQuat = quatMultiply(quatFromDrag(mX-x,y-mY), mQuat);
|
356 |
|
|
mTemp.set(dragQuat);
|
357 |
|
|
}
|
358 |
|
|
|
359 |
|
|
mPreRender.setQuatOnNextRender();
|
360 |
|
|
mX = x;
|
361 |
|
|
mY = y;
|
362 |
|
|
}
|
363 |
|
|
|
364 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
365 |
|
|
|
366 |
|
|
private void finishRotation()
|
367 |
|
|
{
|
368 |
|
|
computeCurrentSpeedInInchesPerSecond();
|
369 |
|
|
int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
|
370 |
|
|
mPreRender.finishRotation(angle);
|
371 |
|
|
|
372 |
|
|
if( angle!=0 )
|
373 |
|
|
{
|
374 |
344f290c
|
Leszek Koltunski
|
final TutorialActivity act = (TutorialActivity)getContext();
|
375 |
|
|
TutorialState state = act.getState();
|
376 |
|
|
state.addMove(mCurrentAxis, mCurrentRow, angle);
|
377 |
af88bf2e
|
Leszek Koltunski
|
}
|
378 |
|
|
|
379 |
|
|
mContinuingRotation = false;
|
380 |
|
|
mBeginningRotation = false;
|
381 |
|
|
mDragging = true;
|
382 |
|
|
}
|
383 |
|
|
|
384 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
385 |
|
|
|
386 |
|
|
private void continueRotation(float x, float y)
|
387 |
|
|
{
|
388 |
|
|
float dx = x-mStartRotX;
|
389 |
|
|
float dy = y-mStartRotY;
|
390 |
|
|
float alpha = dx*mAxisX + dy*mAxisY;
|
391 |
|
|
float x2 = dx - alpha*mAxisX;
|
392 |
|
|
float y2 = dy - alpha*mAxisY;
|
393 |
|
|
|
394 |
|
|
float len = (float)Math.sqrt(x2*x2 + y2*y2);
|
395 |
|
|
|
396 |
|
|
// we have the length of 1D vector 'angle', now the direction:
|
397 |
|
|
float tmp = mAxisY==0 ? -mAxisX*y2 : mAxisY*x2;
|
398 |
|
|
|
399 |
|
|
float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
|
400 |
|
|
mCurrentAngle = SWIPING_SENSITIVITY*angle;
|
401 |
|
|
mPreRender.getObject().continueRotation(mCurrentAngle);
|
402 |
|
|
|
403 |
|
|
addSpeedProbe(x2,y2);
|
404 |
|
|
}
|
405 |
|
|
|
406 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
407 |
|
|
|
408 |
|
|
private void beginRotation(float x, float y)
|
409 |
|
|
{
|
410 |
|
|
mStartRotX = x;
|
411 |
|
|
mStartRotY = y;
|
412 |
|
|
|
413 |
|
|
TwistyObject object = mPreRender.getObject();
|
414 |
|
|
int numLayers = object.getNumLayers();
|
415 |
|
|
|
416 |
|
|
Static4D touchPoint2 = new Static4D(x, y, 0, 0);
|
417 |
|
|
Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuat);
|
418 |
|
|
Static2D res = mMovement.newRotation(numLayers,rotatedTouchPoint2);
|
419 |
|
|
|
420 |
|
|
mCurrentAxis = (int)res.get0();
|
421 |
|
|
mCurrentRow = (int)res.get1();
|
422 |
|
|
|
423 |
|
|
computeCurrentAxis( mMovement.getCastedRotAxis(mCurrentAxis) );
|
424 |
|
|
mRotationFactor = mMovement.returnRotationFactor(numLayers,mCurrentRow);
|
425 |
|
|
|
426 |
|
|
object.beginNewRotation( mCurrentAxis, mCurrentRow );
|
427 |
|
|
|
428 |
|
|
addSpeedProbe(x,y);
|
429 |
|
|
|
430 |
|
|
mBeginningRotation = false;
|
431 |
|
|
mContinuingRotation= true;
|
432 |
|
|
}
|
433 |
|
|
|
434 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
435 |
|
|
|
436 |
|
|
private float getAngle(float x1, float y1, float x2, float y2)
|
437 |
|
|
{
|
438 |
|
|
return (float) Math.atan2(y1-y2, x1-x2);
|
439 |
|
|
}
|
440 |
|
|
|
441 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
442 |
|
|
|
443 |
|
|
private void actionMove(MotionEvent event)
|
444 |
|
|
{
|
445 |
|
|
int pointer = event.findPointerIndex(mPtrID1 != INVALID_POINTER_ID ? mPtrID1:mPtrID2);
|
446 |
|
|
|
447 |
|
|
if( pointer<0 ) return;
|
448 |
|
|
|
449 |
|
|
float pX = event.getX(pointer);
|
450 |
|
|
float pY = event.getY(pointer);
|
451 |
|
|
|
452 |
|
|
float x = (pX - mScreenWidth*0.5f)/mScreenMin;
|
453 |
|
|
float y = (mScreenHeight*0.5f -pY)/mScreenMin;
|
454 |
|
|
|
455 |
|
|
if( mBeginningRotation )
|
456 |
|
|
{
|
457 |
|
|
if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
|
458 |
|
|
{
|
459 |
|
|
beginRotation(x,y);
|
460 |
|
|
}
|
461 |
|
|
}
|
462 |
|
|
else if( mContinuingRotation )
|
463 |
|
|
{
|
464 |
|
|
continueRotation(x,y);
|
465 |
|
|
}
|
466 |
|
|
else if( mDragging )
|
467 |
|
|
{
|
468 |
|
|
drag(event,x,y);
|
469 |
|
|
}
|
470 |
|
|
else
|
471 |
|
|
{
|
472 |
|
|
setUpDragOrRotate(x,y);
|
473 |
|
|
}
|
474 |
|
|
}
|
475 |
|
|
|
476 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
477 |
|
|
|
478 |
|
|
private void actionDown(MotionEvent event)
|
479 |
|
|
{
|
480 |
|
|
mPtrID1 = event.getPointerId(0);
|
481 |
|
|
|
482 |
|
|
float x = event.getX();
|
483 |
|
|
float y = event.getY();
|
484 |
|
|
|
485 |
|
|
mX = (x - mScreenWidth*0.5f)/mScreenMin;
|
486 |
|
|
mY = (mScreenHeight*0.5f -y)/mScreenMin;
|
487 |
|
|
|
488 |
|
|
setUpDragOrRotate(mX,mY);
|
489 |
|
|
}
|
490 |
|
|
|
491 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
492 |
|
|
|
493 |
|
|
private void actionUp(MotionEvent event)
|
494 |
|
|
{
|
495 |
|
|
mPtrID1 = INVALID_POINTER_ID;
|
496 |
|
|
mPtrID2 = INVALID_POINTER_ID;
|
497 |
|
|
|
498 |
|
|
if( mContinuingRotation )
|
499 |
|
|
{
|
500 |
|
|
finishRotation();
|
501 |
|
|
}
|
502 |
|
|
}
|
503 |
|
|
|
504 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
505 |
|
|
|
506 |
|
|
private void actionDown2(MotionEvent event)
|
507 |
|
|
{
|
508 |
|
|
int index = event.getActionIndex();
|
509 |
|
|
|
510 |
|
|
if( mPtrID1==INVALID_POINTER_ID )
|
511 |
|
|
{
|
512 |
|
|
mPtrID1 = event.getPointerId(index);
|
513 |
|
|
float x = event.getX();
|
514 |
|
|
float y = event.getY();
|
515 |
|
|
|
516 |
|
|
if( mPtrID2 != INVALID_POINTER_ID )
|
517 |
|
|
{
|
518 |
|
|
int pointer = event.findPointerIndex(mPtrID2);
|
519 |
|
|
|
520 |
|
|
try
|
521 |
|
|
{
|
522 |
|
|
float x2 = event.getX(pointer);
|
523 |
|
|
float y2 = event.getY(pointer);
|
524 |
|
|
|
525 |
|
|
mRotAngle = getAngle(x,-y,x2,-y2);
|
526 |
|
|
mInitDistance = -1;
|
527 |
|
|
}
|
528 |
|
|
catch(IllegalArgumentException ex)
|
529 |
|
|
{
|
530 |
|
|
mPtrID1=INVALID_POINTER_ID;
|
531 |
|
|
mPtrID2=INVALID_POINTER_ID;
|
532 |
|
|
|
533 |
|
|
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
|
534 |
|
|
crashlytics.setCustomKey("DragError", "pointer="+pointer );
|
535 |
|
|
crashlytics.recordException(ex);
|
536 |
|
|
|
537 |
|
|
return;
|
538 |
|
|
}
|
539 |
|
|
}
|
540 |
|
|
|
541 |
|
|
mX = (x - mScreenWidth*0.5f)/mScreenMin;
|
542 |
|
|
mY = (mScreenHeight*0.5f -y)/mScreenMin;
|
543 |
|
|
}
|
544 |
|
|
else if( mPtrID2==INVALID_POINTER_ID )
|
545 |
|
|
{
|
546 |
|
|
mPtrID2 = event.getPointerId(index);
|
547 |
|
|
|
548 |
|
|
float x = event.getX();
|
549 |
|
|
float y = event.getY();
|
550 |
|
|
|
551 |
|
|
if( mPtrID2 != INVALID_POINTER_ID )
|
552 |
|
|
{
|
553 |
|
|
int pointer = event.findPointerIndex(mPtrID2);
|
554 |
|
|
|
555 |
|
|
try
|
556 |
|
|
{
|
557 |
|
|
float x2 = event.getX(pointer);
|
558 |
|
|
float y2 = event.getY(pointer);
|
559 |
|
|
|
560 |
|
|
mRotAngle = getAngle(x,-y,x2,-y2);
|
561 |
|
|
mInitDistance = -1;
|
562 |
|
|
}
|
563 |
|
|
catch(IllegalArgumentException ex)
|
564 |
|
|
{
|
565 |
|
|
mPtrID1=INVALID_POINTER_ID;
|
566 |
|
|
mPtrID2=INVALID_POINTER_ID;
|
567 |
|
|
|
568 |
|
|
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
|
569 |
|
|
crashlytics.setCustomKey("DragError", "pointer="+pointer );
|
570 |
|
|
crashlytics.recordException(ex);
|
571 |
|
|
|
572 |
|
|
return;
|
573 |
|
|
}
|
574 |
|
|
}
|
575 |
|
|
|
576 |
|
|
if( mBeginningRotation || mContinuingRotation )
|
577 |
|
|
{
|
578 |
|
|
mX = (x - mScreenWidth*0.5f)/mScreenMin;
|
579 |
|
|
mY = (mScreenHeight*0.5f -y)/mScreenMin;
|
580 |
|
|
}
|
581 |
|
|
}
|
582 |
|
|
|
583 |
|
|
if( mBeginningRotation )
|
584 |
|
|
{
|
585 |
|
|
mContinuingRotation = false;
|
586 |
|
|
mBeginningRotation = false;
|
587 |
|
|
mDragging = true;
|
588 |
|
|
}
|
589 |
|
|
else if( mContinuingRotation )
|
590 |
|
|
{
|
591 |
|
|
finishRotation();
|
592 |
|
|
}
|
593 |
|
|
}
|
594 |
|
|
|
595 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
596 |
|
|
|
597 |
|
|
private void actionUp2(MotionEvent event)
|
598 |
|
|
{
|
599 |
|
|
int index = event.getActionIndex();
|
600 |
|
|
|
601 |
|
|
if( index==event.findPointerIndex(mPtrID1) )
|
602 |
|
|
{
|
603 |
|
|
mPtrID1 = INVALID_POINTER_ID;
|
604 |
|
|
int pointer = event.findPointerIndex(mPtrID2);
|
605 |
|
|
|
606 |
|
|
if( pointer>=0 )
|
607 |
|
|
{
|
608 |
|
|
float x1 = event.getX(pointer);
|
609 |
|
|
float y1 = event.getY(pointer);
|
610 |
|
|
|
611 |
|
|
mX = (x1 - mScreenWidth*0.5f)/mScreenMin;
|
612 |
|
|
mY = (mScreenHeight*0.5f -y1)/mScreenMin;
|
613 |
|
|
}
|
614 |
|
|
}
|
615 |
|
|
else if( index==event.findPointerIndex(mPtrID2) )
|
616 |
|
|
{
|
617 |
|
|
mPtrID2 = INVALID_POINTER_ID;
|
618 |
|
|
}
|
619 |
|
|
}
|
620 |
|
|
|
621 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
622 |
|
|
|
623 |
|
|
void initialize()
|
624 |
|
|
{
|
625 |
|
|
mPtrID1 = INVALID_POINTER_ID;
|
626 |
|
|
mPtrID2 = INVALID_POINTER_ID;
|
627 |
|
|
}
|
628 |
|
|
|
629 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
630 |
|
|
// PUBLIC API
|
631 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
632 |
|
|
|
633 |
|
|
public TutorialSurfaceView(Context context, AttributeSet attrs)
|
634 |
|
|
{
|
635 |
|
|
super(context,attrs);
|
636 |
|
|
|
637 |
|
|
if(!isInEditMode())
|
638 |
|
|
{
|
639 |
|
|
mCurrRotSpeed= 0.0f;
|
640 |
|
|
|
641 |
|
|
mLastX = new float[NUM_SPEED_PROBES];
|
642 |
|
|
mLastY = new float[NUM_SPEED_PROBES];
|
643 |
|
|
mLastT = new long[NUM_SPEED_PROBES];
|
644 |
|
|
mFirstIndex =0;
|
645 |
|
|
mLastIndex =0;
|
646 |
|
|
|
647 |
|
|
mRenderer = new TutorialRenderer(this);
|
648 |
|
|
mPreRender = new TutorialPreRender(this);
|
649 |
|
|
|
650 |
|
|
TutorialActivity act = (TutorialActivity)context;
|
651 |
|
|
DisplayMetrics dm = new DisplayMetrics();
|
652 |
|
|
act.getWindowManager().getDefaultDisplay().getMetrics(dm);
|
653 |
|
|
|
654 |
|
|
mDensity = dm.densityDpi;
|
655 |
|
|
|
656 |
|
|
final ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
657 |
|
|
|
658 |
|
|
try
|
659 |
|
|
{
|
660 |
|
|
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
|
661 |
|
|
int esVersion = configurationInfo.reqGlEsVersion>>16;
|
662 |
|
|
setEGLContextClientVersion(esVersion);
|
663 |
|
|
setRenderer(mRenderer);
|
664 |
|
|
}
|
665 |
|
|
catch(Exception ex)
|
666 |
|
|
{
|
667 |
|
|
act.OpenGLError();
|
668 |
|
|
|
669 |
|
|
String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
|
670 |
|
|
String version = GLES30.glGetString(GLES30.GL_VERSION);
|
671 |
|
|
String vendor = GLES30.glGetString(GLES30.GL_VENDOR);
|
672 |
|
|
String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
|
673 |
|
|
|
674 |
|
|
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
|
675 |
|
|
crashlytics.setCustomKey("GLSL Version" , shading );
|
676 |
|
|
crashlytics.setCustomKey("GLversion" , version );
|
677 |
|
|
crashlytics.setCustomKey("GL Vendor " , vendor );
|
678 |
|
|
crashlytics.setCustomKey("GLSLrenderer" , renderer);
|
679 |
|
|
crashlytics.recordException(ex);
|
680 |
|
|
}
|
681 |
|
|
}
|
682 |
|
|
}
|
683 |
|
|
|
684 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
685 |
|
|
|
686 |
|
|
@Override
|
687 |
|
|
public boolean onTouchEvent(MotionEvent event)
|
688 |
|
|
{
|
689 |
|
|
int action = event.getActionMasked();
|
690 |
|
|
|
691 |
|
|
switch(action)
|
692 |
|
|
{
|
693 |
|
|
case MotionEvent.ACTION_DOWN : actionDown(event) ; break;
|
694 |
|
|
case MotionEvent.ACTION_MOVE : actionMove(event) ; break;
|
695 |
|
|
case MotionEvent.ACTION_UP : actionUp(event) ; break;
|
696 |
|
|
case MotionEvent.ACTION_POINTER_DOWN: actionDown2(event); break;
|
697 |
|
|
case MotionEvent.ACTION_POINTER_UP : actionUp2(event) ; break;
|
698 |
|
|
}
|
699 |
|
|
|
700 |
|
|
return true;
|
701 |
|
|
}
|
702 |
|
|
}
|