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.GLSurfaceView;
|
26
|
import android.util.AttributeSet;
|
27
|
import android.view.MotionEvent;
|
28
|
|
29
|
import org.distorted.library.type.Static2D;
|
30
|
import org.distorted.library.type.Static3D;
|
31
|
import org.distorted.library.type.Static4D;
|
32
|
import org.distorted.objects.RubikObject;
|
33
|
import org.distorted.objects.RubikObjectMovement;
|
34
|
import org.distorted.solvers.SolverMain;
|
35
|
import org.distorted.states.RubikState;
|
36
|
import org.distorted.states.RubikStateSolver;
|
37
|
import org.distorted.states.RubikStateSolving;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
public class RubikSurfaceView extends GLSurfaceView
|
42
|
{
|
43
|
public static final int MODE_ROTATE = 0;
|
44
|
public static final int MODE_DRAG = 1;
|
45
|
public static final int MODE_REPLACE = 2;
|
46
|
|
47
|
// Moving the finger from the middle of the vertical screen to the right edge will rotate a
|
48
|
// given face by SWIPING_SENSITIVITY/2 degrees.
|
49
|
private final static int SWIPING_SENSITIVITY = 240;
|
50
|
// Moving the finger by 1/15 the distance of min(scrWidth,scrHeight) will start a Rotation.
|
51
|
private final static int ROTATION_SENSITIVITY = 15;
|
52
|
// Every 1/12 the distance of min(scrWidth,scrHeight) the direction of cube rotation will reset.
|
53
|
private final static int DIRECTION_SENSITIVITY= 12;
|
54
|
|
55
|
// Where did we get this sqrt(3)/2 ? From the (default, i.e. 60 degrees - see InternalOutputSurface!)
|
56
|
// FOV of the projection matrix of the Node onto the Screen.
|
57
|
// Take a look how the CAMERA_POINT is used in onTouchEvent - (x,y) there are expressed in sort of
|
58
|
// 'half-NDC' coordinates i.e. they range from +0.5 to -0.5; thus CAMERA_POINT also needs to be
|
59
|
// in 'half-NDC'. Since in this coordinate system the height of the screen is equal to 1, then the
|
60
|
// Z-distance from the center of the object to the camera is equal to (scrHeight/2)/tan(FOV/2) =
|
61
|
// 0.5/tan(30) = sqrt(3)/2.
|
62
|
// Why is the Z-distance between the camera and the object equal to (scrHeight/2)/tan(FOV/2)?
|
63
|
// Because of the way the View part of the ModelView matrix is constructed in EffectQueueMatrix.send().
|
64
|
private final Static4D CAMERA_POINT = new Static4D(0, 0, (float)Math.sqrt(3)*0.5f, 0);
|
65
|
|
66
|
private RubikRenderer mRenderer;
|
67
|
private RubikPostRender mPostRender;
|
68
|
private RubikObjectMovement mMovement;
|
69
|
private boolean mDragging, mBeginningRotation, mContinuingRotation;
|
70
|
private int mScreenWidth, mScreenHeight, mScreenMin;
|
71
|
|
72
|
private float mX, mY;
|
73
|
private float mStartRotX, mStartRotY;
|
74
|
private float mAxisX, mAxisY;
|
75
|
private float mRotationFactor;
|
76
|
private int mLastCubitColor, mLastCubitFace, mLastCubit;
|
77
|
private int mCurrentAxis, mCurrentRow;
|
78
|
private float mCurrentAngle;
|
79
|
|
80
|
private static Static4D mQuatCurrent = new Static4D(0,0,0,1);
|
81
|
private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
|
82
|
private static Static4D mTempCurrent = new Static4D(0,0,0,1);
|
83
|
private static Static4D mTempAccumulated= new Static4D(0,0,0,1);
|
84
|
|
85
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86
|
|
87
|
void setScreenSize(int width, int height)
|
88
|
{
|
89
|
mScreenWidth = width;
|
90
|
mScreenHeight= height;
|
91
|
|
92
|
mScreenMin = Math.min(width, height);
|
93
|
}
|
94
|
|
95
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
96
|
|
97
|
boolean isVertical()
|
98
|
{
|
99
|
return mScreenHeight>mScreenWidth;
|
100
|
}
|
101
|
|
102
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
103
|
|
104
|
RubikRenderer getRenderer()
|
105
|
{
|
106
|
return mRenderer;
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
|
111
|
RubikPostRender getPostRender()
|
112
|
{
|
113
|
return mPostRender;
|
114
|
}
|
115
|
|
116
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
|
118
|
void setQuatAccumulated()
|
119
|
{
|
120
|
mQuatAccumulated.set(mTempAccumulated);
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
void setQuatCurrent()
|
126
|
{
|
127
|
mQuatCurrent.set(mTempCurrent);
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
Static4D getQuatAccumulated()
|
133
|
{
|
134
|
return mQuatAccumulated;
|
135
|
}
|
136
|
|
137
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138
|
|
139
|
Static4D getQuatCurrent()
|
140
|
{
|
141
|
return mQuatCurrent;
|
142
|
}
|
143
|
|
144
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
145
|
|
146
|
void setMovement(RubikObjectMovement movement)
|
147
|
{
|
148
|
mMovement = movement;
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
|
153
|
private Static4D quatFromDrag(float dragX, float dragY)
|
154
|
{
|
155
|
float axisX = dragY; // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
|
156
|
float axisY = dragX; // Why not (-dragY, dragX) ? because Y axis is also inverted!
|
157
|
float axisZ = 0;
|
158
|
float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
|
159
|
|
160
|
if( axisL>0 )
|
161
|
{
|
162
|
axisX /= axisL;
|
163
|
axisY /= axisL;
|
164
|
axisZ /= axisL;
|
165
|
|
166
|
float ratio = axisL;
|
167
|
ratio = ratio - (int)ratio; // the cos() is only valid in (0,Pi)
|
168
|
|
169
|
float cosA = (float)Math.cos(Math.PI*ratio);
|
170
|
float sinA = (float)Math.sqrt(1-cosA*cosA);
|
171
|
|
172
|
return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
|
173
|
}
|
174
|
|
175
|
return new Static4D(0f, 0f, 0f, 1f);
|
176
|
}
|
177
|
|
178
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179
|
|
180
|
private void setUpDragOrRotate(boolean down, float x, float y)
|
181
|
{
|
182
|
int mode = RubikState.getMode();
|
183
|
|
184
|
if( mode==MODE_DRAG )
|
185
|
{
|
186
|
mDragging = true;
|
187
|
mBeginningRotation = false;
|
188
|
mContinuingRotation = false;
|
189
|
}
|
190
|
else
|
191
|
{
|
192
|
Static4D touchPoint1 = new Static4D(x, y, 0, 0);
|
193
|
Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
|
194
|
Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
|
195
|
|
196
|
if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
|
197
|
{
|
198
|
mDragging = false;
|
199
|
mContinuingRotation = false;
|
200
|
|
201
|
if( mode==MODE_ROTATE )
|
202
|
{
|
203
|
mBeginningRotation= mPostRender.canRotate();
|
204
|
}
|
205
|
else if( mode==MODE_REPLACE )
|
206
|
{
|
207
|
mBeginningRotation= false;
|
208
|
|
209
|
if( down )
|
210
|
{
|
211
|
RubikStateSolver solver = (RubikStateSolver) RubikState.SVER.getStateClass();
|
212
|
mLastCubitFace = mMovement.getTouchedFace();
|
213
|
float[] point = mMovement.getTouchedPoint3D();
|
214
|
int color = solver.getCurrentColor();
|
215
|
RubikObject object = mPostRender.getObject();
|
216
|
mLastCubit = object.getCubit(point);
|
217
|
mPostRender.setTextureMap( mLastCubit, mLastCubitFace, color );
|
218
|
mLastCubitColor = SolverMain.cubitIsLocked(object.getObjectList(), object.getSize(), mLastCubit);
|
219
|
}
|
220
|
}
|
221
|
}
|
222
|
else
|
223
|
{
|
224
|
mDragging = true;
|
225
|
mBeginningRotation = false;
|
226
|
mContinuingRotation = false;
|
227
|
}
|
228
|
}
|
229
|
}
|
230
|
|
231
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
232
|
// cast the 3D axis we are currently rotating along to the 2D in-screen-surface axis
|
233
|
|
234
|
private void computeCurrentAxis(Static3D axis)
|
235
|
{
|
236
|
Static4D axis4D = new Static4D(axis.get0(), axis.get1(), axis.get2(), 0);
|
237
|
Static4D result = rotateVectorByQuat(axis4D, mQuatAccumulated);
|
238
|
|
239
|
mAxisX =result.get0();
|
240
|
mAxisY =result.get1();
|
241
|
|
242
|
float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
|
243
|
mAxisX /= len;
|
244
|
mAxisY /= len;
|
245
|
}
|
246
|
|
247
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
248
|
|
249
|
private float continueRotation(float dx, float dy)
|
250
|
{
|
251
|
float alpha = dx*mAxisX + dy*mAxisY;
|
252
|
float x = dx - alpha*mAxisX;
|
253
|
float y = dy - alpha*mAxisY;
|
254
|
|
255
|
float len = (float)Math.sqrt(x*x + y*y);
|
256
|
|
257
|
// we have the length of 1D vector 'angle', now the direction:
|
258
|
float tmp = mAxisY==0 ? -mAxisX*y : mAxisY*x;
|
259
|
|
260
|
return (tmp>0 ? 1:-1)*len*mRotationFactor;
|
261
|
}
|
262
|
|
263
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
264
|
// return quat1*quat2
|
265
|
|
266
|
public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
|
267
|
{
|
268
|
float qx = quat1.get0();
|
269
|
float qy = quat1.get1();
|
270
|
float qz = quat1.get2();
|
271
|
float qw = quat1.get3();
|
272
|
|
273
|
float rx = quat2.get0();
|
274
|
float ry = quat2.get1();
|
275
|
float rz = quat2.get2();
|
276
|
float rw = quat2.get3();
|
277
|
|
278
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
279
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
280
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
281
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
282
|
|
283
|
return new Static4D(tx,ty,tz,tw);
|
284
|
}
|
285
|
|
286
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
287
|
// rotate 'vector' by quat ( i.e. return quat*vector*(quat^-1) )
|
288
|
|
289
|
public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
|
290
|
{
|
291
|
float qx = quat.get0();
|
292
|
float qy = quat.get1();
|
293
|
float qz = quat.get2();
|
294
|
float qw = quat.get3();
|
295
|
|
296
|
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
|
297
|
Static4D tmp = quatMultiply(quat,vector);
|
298
|
|
299
|
return quatMultiply(tmp,quatInverted);
|
300
|
}
|
301
|
|
302
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
303
|
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat )
|
304
|
|
305
|
public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
|
306
|
{
|
307
|
float qx = quat.get0();
|
308
|
float qy = quat.get1();
|
309
|
float qz = quat.get2();
|
310
|
float qw = quat.get3();
|
311
|
|
312
|
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
|
313
|
Static4D tmp = quatMultiply(quatInverted,vector);
|
314
|
|
315
|
return quatMultiply(tmp,quat);
|
316
|
}
|
317
|
|
318
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
319
|
// PUBLIC API
|
320
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
321
|
|
322
|
public RubikSurfaceView(Context context, AttributeSet attrs)
|
323
|
{
|
324
|
super(context,attrs);
|
325
|
|
326
|
if(!isInEditMode())
|
327
|
{
|
328
|
mLastCubitColor = -1;
|
329
|
|
330
|
mRenderer = new RubikRenderer(this);
|
331
|
mPostRender = new RubikPostRender(this);
|
332
|
|
333
|
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
334
|
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
|
335
|
setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
|
336
|
setRenderer(mRenderer);
|
337
|
}
|
338
|
}
|
339
|
|
340
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
341
|
|
342
|
@Override
|
343
|
public boolean onTouchEvent(MotionEvent event)
|
344
|
{
|
345
|
int action = event.getAction();
|
346
|
float x = (event.getX() - mScreenWidth*0.5f)/mScreenMin;
|
347
|
float y = (mScreenHeight*0.5f -event.getY())/mScreenMin;
|
348
|
|
349
|
switch(action)
|
350
|
{
|
351
|
case MotionEvent.ACTION_DOWN: mX = x;
|
352
|
mY = y;
|
353
|
setUpDragOrRotate(true,x,y);
|
354
|
break;
|
355
|
case MotionEvent.ACTION_MOVE: if( mBeginningRotation )
|
356
|
{
|
357
|
if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
|
358
|
{
|
359
|
mStartRotX = x;
|
360
|
mStartRotY = y;
|
361
|
|
362
|
Static4D touchPoint2 = new Static4D(x, y, 0, 0);
|
363
|
Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
|
364
|
|
365
|
Static2D res = mMovement.newRotation(rotatedTouchPoint2);
|
366
|
RubikObject object = mPostRender.getObject();
|
367
|
|
368
|
mCurrentAxis = (int)res.get0();
|
369
|
float offset = res.get1();
|
370
|
mCurrentRow = (int)(object.returnMultiplier()*offset);
|
371
|
computeCurrentAxis( object.getRotationAxis()[mCurrentAxis] );
|
372
|
mRotationFactor = object.returnRotationFactor(offset);
|
373
|
|
374
|
object.beginNewRotation( mCurrentAxis, mCurrentRow );
|
375
|
|
376
|
if( RubikState.getCurrentState()==RubikState.SOLV )
|
377
|
{
|
378
|
RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
|
379
|
solving.startCounting( (RubikActivity)getContext() );
|
380
|
}
|
381
|
|
382
|
mBeginningRotation = false;
|
383
|
mContinuingRotation= true;
|
384
|
}
|
385
|
}
|
386
|
else if( mContinuingRotation )
|
387
|
{
|
388
|
float angle = continueRotation(x-mStartRotX,y-mStartRotY);
|
389
|
mCurrentAngle = SWIPING_SENSITIVITY*angle;
|
390
|
mPostRender.getObject().continueRotation(mCurrentAngle);
|
391
|
}
|
392
|
else if( mDragging )
|
393
|
{
|
394
|
mTempCurrent.set(quatFromDrag(mX-x,y-mY));
|
395
|
mPostRender.setQuatCurrentOnNextRender();
|
396
|
|
397
|
if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
|
398
|
{
|
399
|
mX = x;
|
400
|
mY = y;
|
401
|
mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
|
402
|
mTempCurrent.set(0f, 0f, 0f, 1f);
|
403
|
mPostRender.setQuatCurrentOnNextRender();
|
404
|
mPostRender.setQuatAccumulatedOnNextRender();
|
405
|
}
|
406
|
}
|
407
|
else
|
408
|
{
|
409
|
setUpDragOrRotate(false,x,y);
|
410
|
}
|
411
|
break;
|
412
|
case MotionEvent.ACTION_UP : if( mDragging )
|
413
|
{
|
414
|
mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
|
415
|
mTempCurrent.set(0f, 0f, 0f, 1f);
|
416
|
mPostRender.setQuatCurrentOnNextRender();
|
417
|
mPostRender.setQuatAccumulatedOnNextRender();
|
418
|
}
|
419
|
|
420
|
if( mContinuingRotation )
|
421
|
{
|
422
|
mPostRender.finishRotation();
|
423
|
|
424
|
if( RubikState.getCurrentState()==RubikState.SOLV )
|
425
|
{
|
426
|
RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
|
427
|
|
428
|
int angle = mPostRender.getObject().computeNearestAngle(mCurrentAngle);
|
429
|
|
430
|
if( angle!=0 )
|
431
|
solving.addMove(mCurrentAxis, mCurrentRow, angle);
|
432
|
}
|
433
|
}
|
434
|
if( mLastCubitColor>=0 )
|
435
|
{
|
436
|
mPostRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
|
437
|
}
|
438
|
break;
|
439
|
}
|
440
|
|
441
|
return true;
|
442
|
}
|
443
|
}
|
444
|
|