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