1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 Leszek Koltunski //
|
3
|
// //
|
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.main;
|
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.Static3D;
|
35
|
import org.distorted.library.type.Static4D;
|
36
|
import org.distorted.objects.RubikObject;
|
37
|
import org.distorted.objects.RubikMovementObject;
|
38
|
import org.distorted.solvers.SolverMain;
|
39
|
import org.distorted.states.RubikState;
|
40
|
import org.distorted.states.RubikStatePlay;
|
41
|
import org.distorted.states.RubikStateSolver;
|
42
|
import org.distorted.states.RubikStateSolving;
|
43
|
|
44
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
45
|
|
46
|
public class RubikSurfaceView extends GLSurfaceView
|
47
|
{
|
48
|
private static final int NUM_SPEED_PROBES = 10;
|
49
|
private static final int INVALID_POINTER_ID = -1;
|
50
|
|
51
|
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
|
// 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
|
// Moving the finger by 0.3 of an inch will start a Rotation.
|
59
|
private final static float ROTATION_SENSITIVITY = 0.3f;
|
60
|
|
61
|
// 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
|
|
72
|
private RubikRenderer mRenderer;
|
73
|
private RubikPreRender mPreRender;
|
74
|
private RubikMovementObject mMovement;
|
75
|
private boolean mDragging, mBeginningRotation, mContinuingRotation;
|
76
|
private int mScreenWidth, mScreenHeight, mScreenMin;
|
77
|
|
78
|
private float mRotAngle, mInitDistance;
|
79
|
private int mPtrID1, mPtrID2;
|
80
|
private float mX, mY;
|
81
|
private float mStartRotX, mStartRotY;
|
82
|
private float mAxisX, mAxisY;
|
83
|
private float mRotationFactor;
|
84
|
private int mLastCubitColor, mLastCubitFace, mLastCubit;
|
85
|
private int mCurrentAxis, mCurrentRow;
|
86
|
private float mCurrentAngle, mCurrRotSpeed;
|
87
|
private float[] mLastX;
|
88
|
private float[] mLastY;
|
89
|
private long[] mLastT;
|
90
|
private int mFirstIndex, mLastIndex;
|
91
|
private int mDensity;
|
92
|
|
93
|
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
|
|
96
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
97
|
|
98
|
void setScreenSize(int width, int height)
|
99
|
{
|
100
|
mScreenWidth = width;
|
101
|
mScreenHeight= height;
|
102
|
|
103
|
mScreenMin = Math.min(width, height);
|
104
|
}
|
105
|
|
106
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107
|
|
108
|
boolean isVertical()
|
109
|
{
|
110
|
return mScreenHeight>mScreenWidth;
|
111
|
}
|
112
|
|
113
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
114
|
|
115
|
RubikRenderer getRenderer()
|
116
|
{
|
117
|
return mRenderer;
|
118
|
}
|
119
|
|
120
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
121
|
|
122
|
RubikPreRender getPreRender()
|
123
|
{
|
124
|
return mPreRender;
|
125
|
}
|
126
|
|
127
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
128
|
|
129
|
void setQuat()
|
130
|
{
|
131
|
mQuat.set(mTemp);
|
132
|
}
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135
|
|
136
|
Static4D getQuat()
|
137
|
{
|
138
|
return mQuat;
|
139
|
}
|
140
|
|
141
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142
|
|
143
|
void setMovement(RubikMovementObject movement)
|
144
|
{
|
145
|
mMovement = movement;
|
146
|
}
|
147
|
|
148
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
Static4D result = rotateVectorByQuat(axis4D, mQuat);
|
182
|
|
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
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192
|
// return quat1*quat2
|
193
|
|
194
|
public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
|
195
|
{
|
196
|
float qx = quat1.get0();
|
197
|
float qy = quat1.get1();
|
198
|
float qz = quat1.get2();
|
199
|
float qw = quat1.get3();
|
200
|
|
201
|
float rx = quat2.get0();
|
202
|
float ry = quat2.get1();
|
203
|
float rz = quat2.get2();
|
204
|
float rw = quat2.get3();
|
205
|
|
206
|
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
|
|
211
|
return new Static4D(tx,ty,tz,tw);
|
212
|
}
|
213
|
|
214
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215
|
// rotate 'vector' by quat ( i.e. return quat*vector*(quat^-1) )
|
216
|
|
217
|
public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
|
218
|
{
|
219
|
float qx = quat.get0();
|
220
|
float qy = quat.get1();
|
221
|
float qz = quat.get2();
|
222
|
float qw = quat.get3();
|
223
|
|
224
|
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
|
225
|
Static4D tmp = quatMultiply(quat,vector);
|
226
|
|
227
|
return quatMultiply(tmp,quatInverted);
|
228
|
}
|
229
|
|
230
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
231
|
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat )
|
232
|
|
233
|
public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
|
234
|
{
|
235
|
float qx = quat.get0();
|
236
|
float qy = quat.get1();
|
237
|
float qz = quat.get2();
|
238
|
float qw = quat.get3();
|
239
|
|
240
|
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
|
241
|
Static4D tmp = quatMultiply(quatInverted,vector);
|
242
|
|
243
|
return quatMultiply(tmp,quat);
|
244
|
}
|
245
|
|
246
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
247
|
|
248
|
private void addSpeedProbe(float x, float y)
|
249
|
{
|
250
|
long currTime = System.currentTimeMillis();
|
251
|
boolean theSame = mLastIndex==mFirstIndex;
|
252
|
|
253
|
mLastIndex++;
|
254
|
if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
|
255
|
|
256
|
mLastT[mLastIndex] = currTime;
|
257
|
mLastX[mLastIndex] = x;
|
258
|
mLastY[mLastIndex] = y;
|
259
|
|
260
|
if( mLastIndex==mFirstIndex)
|
261
|
{
|
262
|
mFirstIndex++;
|
263
|
if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
|
264
|
}
|
265
|
|
266
|
if( theSame )
|
267
|
{
|
268
|
mLastT[mFirstIndex] = currTime;
|
269
|
mLastX[mFirstIndex] = x;
|
270
|
mLastY[mFirstIndex] = y;
|
271
|
}
|
272
|
}
|
273
|
|
274
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
275
|
|
276
|
private void computeCurrentSpeedInInchesPerSecond()
|
277
|
{
|
278
|
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
|
|
285
|
long timeDiff = lastTime-firstTime;
|
286
|
|
287
|
mLastIndex = 0;
|
288
|
mFirstIndex= 0;
|
289
|
|
290
|
mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX,fY,lX,lY)/timeDiff : 0;
|
291
|
}
|
292
|
|
293
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
294
|
|
295
|
private float retFingerDragDistanceInInches(float xFrom, float yFrom, float xTo, float yTo)
|
296
|
{
|
297
|
float xDist = mScreenWidth*(xFrom-xTo);
|
298
|
float yDist = mScreenHeight*(yFrom-yTo);
|
299
|
float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
|
300
|
|
301
|
return distInPixels/mDensity;
|
302
|
}
|
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
|
Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuat);
|
320
|
Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
|
321
|
|
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
|
private void drag(MotionEvent event, float x, float y)
|
360
|
{
|
361
|
if( mPtrID1!=INVALID_POINTER_ID && mPtrID2!=INVALID_POINTER_ID)
|
362
|
{
|
363
|
int pointer = event.findPointerIndex(mPtrID2);
|
364
|
float pX,pY;
|
365
|
|
366
|
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
|
|
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
|
|
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
|
}
|
403
|
else
|
404
|
{
|
405
|
Static4D dragQuat = quatMultiply(quatFromDrag(mX-x,y-mY), mQuat);
|
406
|
mTemp.set(dragQuat);
|
407
|
}
|
408
|
|
409
|
mPreRender.setQuatOnNextRender();
|
410
|
mX = x;
|
411
|
mY = y;
|
412
|
}
|
413
|
|
414
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
415
|
|
416
|
private void finishRotation()
|
417
|
{
|
418
|
computeCurrentSpeedInInchesPerSecond();
|
419
|
int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
|
420
|
mPreRender.finishRotation(angle);
|
421
|
|
422
|
if( angle!=0 )
|
423
|
{
|
424
|
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
|
}
|
435
|
|
436
|
mContinuingRotation = false;
|
437
|
mBeginningRotation = false;
|
438
|
mDragging = true;
|
439
|
}
|
440
|
|
441
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
442
|
|
443
|
private void continueRotation(float x, float y)
|
444
|
{
|
445
|
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
|
mCurrentAngle = SWIPING_SENSITIVITY*angle;
|
458
|
mPreRender.getObject().continueRotation(mCurrentAngle);
|
459
|
|
460
|
addSpeedProbe(x2,y2);
|
461
|
}
|
462
|
|
463
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
464
|
|
465
|
private void beginRotation(float x, float y)
|
466
|
{
|
467
|
mStartRotX = x;
|
468
|
mStartRotY = y;
|
469
|
|
470
|
Static4D touchPoint2 = new Static4D(x, y, 0, 0);
|
471
|
Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuat);
|
472
|
|
473
|
Static2D res = mMovement.newRotation(rotatedTouchPoint2);
|
474
|
RubikObject object = mPreRender.getObject();
|
475
|
|
476
|
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
|
|
482
|
object.beginNewRotation( mCurrentAxis, mCurrentRow );
|
483
|
|
484
|
if( RubikState.getCurrentState()==RubikState.READ )
|
485
|
{
|
486
|
RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
|
487
|
solving.resetElapsed();
|
488
|
|
489
|
final RubikActivity act = (RubikActivity)getContext();
|
490
|
|
491
|
act.runOnUiThread(new Runnable()
|
492
|
{
|
493
|
@Override
|
494
|
public void run()
|
495
|
{
|
496
|
RubikState.switchState( act, RubikState.SOLV);
|
497
|
}
|
498
|
});
|
499
|
}
|
500
|
|
501
|
addSpeedProbe(x,y);
|
502
|
|
503
|
mBeginningRotation = false;
|
504
|
mContinuingRotation= true;
|
505
|
}
|
506
|
|
507
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
515
|
|
516
|
private void actionMove(MotionEvent event)
|
517
|
{
|
518
|
int pointer = event.findPointerIndex(mPtrID1 != INVALID_POINTER_ID ? mPtrID1:mPtrID2);
|
519
|
|
520
|
if( pointer<0 ) return;
|
521
|
|
522
|
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
|
|
528
|
if( mBeginningRotation )
|
529
|
{
|
530
|
if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
|
531
|
{
|
532
|
beginRotation(x,y);
|
533
|
}
|
534
|
}
|
535
|
else if( mContinuingRotation )
|
536
|
{
|
537
|
continueRotation(x,y);
|
538
|
}
|
539
|
else if( mDragging )
|
540
|
{
|
541
|
drag(event,x,y);
|
542
|
}
|
543
|
else
|
544
|
{
|
545
|
setUpDragOrRotate(false,x,y);
|
546
|
}
|
547
|
}
|
548
|
|
549
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
550
|
|
551
|
private void actionDown(MotionEvent event)
|
552
|
{
|
553
|
mPtrID1 = event.getPointerId(0);
|
554
|
|
555
|
float x = event.getX();
|
556
|
float y = event.getY();
|
557
|
|
558
|
mX = (x - mScreenWidth*0.5f)/mScreenMin;
|
559
|
mY = (mScreenHeight*0.5f -y)/mScreenMin;
|
560
|
|
561
|
setUpDragOrRotate(true,mX,mY);
|
562
|
}
|
563
|
|
564
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
565
|
|
566
|
private void actionUp(MotionEvent event)
|
567
|
{
|
568
|
mPtrID1 = INVALID_POINTER_ID;
|
569
|
mPtrID2 = INVALID_POINTER_ID;
|
570
|
|
571
|
if( mContinuingRotation )
|
572
|
{
|
573
|
finishRotation();
|
574
|
}
|
575
|
|
576
|
if( mLastCubitColor>=0 )
|
577
|
{
|
578
|
mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
|
579
|
mLastCubitColor = -1;
|
580
|
}
|
581
|
}
|
582
|
|
583
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
584
|
|
585
|
private void actionDown2(MotionEvent event)
|
586
|
{
|
587
|
int index = event.getActionIndex();
|
588
|
|
589
|
if( mPtrID1==INVALID_POINTER_ID )
|
590
|
{
|
591
|
mPtrID1 = event.getPointerId(index);
|
592
|
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
|
mInitDistance = -1;
|
604
|
}
|
605
|
|
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
|
float x = event.getX();
|
614
|
float y = event.getY();
|
615
|
|
616
|
if( mPtrID2 != INVALID_POINTER_ID )
|
617
|
{
|
618
|
int pointer = event.findPointerIndex(mPtrID2);
|
619
|
|
620
|
float x2 = event.getX(pointer);
|
621
|
float y2 = event.getY(pointer);
|
622
|
|
623
|
mRotAngle = getAngle(x,-y,x2,-y2);
|
624
|
mInitDistance = -1;
|
625
|
}
|
626
|
|
627
|
if( mBeginningRotation || mContinuingRotation )
|
628
|
{
|
629
|
mX = (x - mScreenWidth*0.5f)/mScreenMin;
|
630
|
mY = (mScreenHeight*0.5f -y)/mScreenMin;
|
631
|
}
|
632
|
}
|
633
|
|
634
|
if( mBeginningRotation )
|
635
|
{
|
636
|
mContinuingRotation = false;
|
637
|
mBeginningRotation = false;
|
638
|
mDragging = true;
|
639
|
}
|
640
|
else if( mContinuingRotation )
|
641
|
{
|
642
|
finishRotation();
|
643
|
}
|
644
|
}
|
645
|
|
646
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
647
|
|
648
|
private void actionUp2(MotionEvent event)
|
649
|
{
|
650
|
int index = event.getActionIndex();
|
651
|
|
652
|
if( index==event.findPointerIndex(mPtrID1) )
|
653
|
{
|
654
|
mPtrID1 = INVALID_POINTER_ID;
|
655
|
int pointer = event.findPointerIndex(mPtrID2);
|
656
|
|
657
|
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
|
}
|
666
|
else if( index==event.findPointerIndex(mPtrID2) )
|
667
|
{
|
668
|
mPtrID2 = INVALID_POINTER_ID;
|
669
|
}
|
670
|
}
|
671
|
|
672
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
673
|
|
674
|
void initialize()
|
675
|
{
|
676
|
mPtrID1 = INVALID_POINTER_ID;
|
677
|
mPtrID2 = INVALID_POINTER_ID;
|
678
|
}
|
679
|
|
680
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
681
|
// PUBLIC API
|
682
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
683
|
|
684
|
public RubikSurfaceView(Context context, AttributeSet attrs)
|
685
|
{
|
686
|
super(context,attrs);
|
687
|
|
688
|
if(!isInEditMode())
|
689
|
{
|
690
|
mLastCubitColor = -1;
|
691
|
mCurrRotSpeed = 0.0f;
|
692
|
|
693
|
mLastX = new float[NUM_SPEED_PROBES];
|
694
|
mLastY = new float[NUM_SPEED_PROBES];
|
695
|
mLastT = new long[NUM_SPEED_PROBES];
|
696
|
mFirstIndex =0;
|
697
|
mLastIndex =0;
|
698
|
|
699
|
mRenderer = new RubikRenderer(this);
|
700
|
mPreRender = new RubikPreRender(this);
|
701
|
|
702
|
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
|
try
|
711
|
{
|
712
|
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
|
713
|
setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
|
714
|
setRenderer(mRenderer);
|
715
|
}
|
716
|
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
|
}
|
733
|
}
|
734
|
|
735
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
736
|
|
737
|
@Override
|
738
|
public boolean onTouchEvent(MotionEvent event)
|
739
|
{
|
740
|
int action = event.getActionMasked();
|
741
|
|
742
|
switch(action)
|
743
|
{
|
744
|
case MotionEvent.ACTION_DOWN : actionDown(event) ; break;
|
745
|
case MotionEvent.ACTION_MOVE : actionMove(event) ; break;
|
746
|
case MotionEvent.ACTION_UP : actionUp(event) ; break;
|
747
|
case MotionEvent.ACTION_POINTER_DOWN: actionDown2(event); break;
|
748
|
case MotionEvent.ACTION_POINTER_UP : actionUp2(event) ; break;
|
749
|
}
|
750
|
|
751
|
return true;
|
752
|
}
|
753
|
}
|
754
|
|