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.magic;
|
21
|
|
22
|
import android.app.ActivityManager;
|
23
|
import android.content.Context;
|
24
|
import android.content.pm.ConfigurationInfo;
|
25
|
import android.graphics.PorterDuff;
|
26
|
import android.graphics.drawable.Drawable;
|
27
|
import android.opengl.GLSurfaceView;
|
28
|
import android.support.v4.content.ContextCompat;
|
29
|
import android.util.AttributeSet;
|
30
|
import android.util.DisplayMetrics;
|
31
|
import android.view.MotionEvent;
|
32
|
import android.view.ViewGroup;
|
33
|
import android.widget.Button;
|
34
|
import android.widget.ImageButton;
|
35
|
import android.widget.LinearLayout;
|
36
|
|
37
|
import org.distorted.library.type.Static2D;
|
38
|
import org.distorted.library.type.Static4D;
|
39
|
import org.distorted.object.RubikCube;
|
40
|
import org.distorted.object.RubikCubeMovement;
|
41
|
|
42
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43
|
|
44
|
public class RubikSurfaceView extends GLSurfaceView
|
45
|
{
|
46
|
// Moving the finger from the middle of the vertical screen to the right edge will rotate a
|
47
|
// given face by SWIPING_SENSITIVITY/2 degrees.
|
48
|
private final static int SWIPING_SENSITIVITY = 240;
|
49
|
// Moving the finger by 1/12 the distance of min(scrWidth,scrHeight) will start a Rotation.
|
50
|
private final static int ROTATION_SENSITIVITY = 12;
|
51
|
// Every 1/12 the distance of min(scrWidth,scrHeight) the direction of cube rotation will reset.
|
52
|
private final static int DIRECTION_SENSITIVITY= 12;
|
53
|
|
54
|
private final Static4D CAMERA_POINT = new Static4D(0, 0, RubikRenderer.CAMERA_DISTANCE, 0);
|
55
|
|
56
|
private RubikRenderer mRenderer;
|
57
|
private RubikCubeMovement mMovement;
|
58
|
|
59
|
private boolean mInScrambleMode;
|
60
|
|
61
|
private boolean mDragging, mBeginningRotation, mContinuingRotation;
|
62
|
private float mX, mY;
|
63
|
private int mScreenWidth, mScreenHeight, mScreenMin;
|
64
|
|
65
|
private static int mButton = RubikSize.SIZE3.ordinal();
|
66
|
private static Static4D mQuatCurrent = new Static4D(0,0,0,1);
|
67
|
private static Static4D mQuatAccumulated= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
|
68
|
private static Static4D mTempCurrent = new Static4D(0,0,0,1);
|
69
|
private static Static4D mTempAccumulated= new Static4D(0,0,0,1);
|
70
|
|
71
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
72
|
// return quat1*quat2
|
73
|
|
74
|
public static Static4D quatMultiply( Static4D quat1, Static4D quat2 )
|
75
|
{
|
76
|
float qx = quat1.get0();
|
77
|
float qy = quat1.get1();
|
78
|
float qz = quat1.get2();
|
79
|
float qw = quat1.get3();
|
80
|
|
81
|
float rx = quat2.get0();
|
82
|
float ry = quat2.get1();
|
83
|
float rz = quat2.get2();
|
84
|
float rw = quat2.get3();
|
85
|
|
86
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
87
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
88
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
89
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
90
|
|
91
|
return new Static4D(tx,ty,tz,tw);
|
92
|
}
|
93
|
|
94
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
95
|
// rotate 'vector' by quat ( i.e. return quat*vector*(quat^-1) )
|
96
|
|
97
|
public static Static4D rotateVectorByQuat(Static4D vector, Static4D quat)
|
98
|
{
|
99
|
float qx = quat.get0();
|
100
|
float qy = quat.get1();
|
101
|
float qz = quat.get2();
|
102
|
float qw = quat.get3();
|
103
|
|
104
|
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
|
105
|
Static4D tmp = quatMultiply(quat,vector);
|
106
|
|
107
|
return quatMultiply(tmp,quatInverted);
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
// rotate 'vector' by quat^(-1) ( i.e. return (quat^-1)*vector*quat )
|
112
|
|
113
|
public static Static4D rotateVectorByInvertedQuat(Static4D vector, Static4D quat)
|
114
|
{
|
115
|
float qx = quat.get0();
|
116
|
float qy = quat.get1();
|
117
|
float qz = quat.get2();
|
118
|
float qw = quat.get3();
|
119
|
|
120
|
Static4D quatInverted= new Static4D(-qx,-qy,-qz,qw);
|
121
|
Static4D tmp = quatMultiply(quatInverted,vector);
|
122
|
|
123
|
return quatMultiply(tmp,quat);
|
124
|
}
|
125
|
|
126
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
127
|
|
128
|
void setScreenSize(int width, int height)
|
129
|
{
|
130
|
mScreenWidth = width;
|
131
|
mScreenHeight= height;
|
132
|
|
133
|
mScreenMin = width<height ? width:height;
|
134
|
}
|
135
|
|
136
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
137
|
|
138
|
RubikRenderer getRenderer()
|
139
|
{
|
140
|
return mRenderer;
|
141
|
}
|
142
|
|
143
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144
|
|
145
|
static int getRedButton()
|
146
|
{
|
147
|
return mButton;
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
void markButton(int button)
|
153
|
{
|
154
|
mButton = button;
|
155
|
RubikActivity act = (RubikActivity)getContext();
|
156
|
|
157
|
for(int b=0; b<RubikSize.LENGTH; b++)
|
158
|
{
|
159
|
Drawable d = act.findViewById(b).getBackground();
|
160
|
|
161
|
if( b==button )
|
162
|
{
|
163
|
d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
|
164
|
}
|
165
|
else
|
166
|
{
|
167
|
d.clearColorFilter();
|
168
|
}
|
169
|
}
|
170
|
}
|
171
|
|
172
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
173
|
|
174
|
void addSizeButtons(RubikActivity act)
|
175
|
{
|
176
|
LinearLayout layout = act.findViewById(R.id.sizeLayout);
|
177
|
DisplayMetrics metrics = getResources().getDisplayMetrics();
|
178
|
float scale = metrics.density;
|
179
|
int size = (int)(64*scale +0.5f);
|
180
|
int padding = (int)(3*scale + 0.5f);
|
181
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(size,size);
|
182
|
|
183
|
for(int i=0; i<RubikSize.LENGTH; i++)
|
184
|
{
|
185
|
ImageButton button = new ImageButton(act);
|
186
|
button.setLayoutParams(params);
|
187
|
button.setId(i);
|
188
|
button.setPadding(padding,0,padding,0);
|
189
|
int iconID = RubikSize.getSize(i).getIconID();
|
190
|
button.setImageResource(iconID);
|
191
|
button.setOnClickListener(act);
|
192
|
layout.addView(button);
|
193
|
}
|
194
|
}
|
195
|
|
196
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
197
|
|
198
|
void enterScrambleMode()
|
199
|
{
|
200
|
if( !mInScrambleMode )
|
201
|
{
|
202
|
mInScrambleMode = true;
|
203
|
|
204
|
android.util.Log.e("view", "entering scramble mode");
|
205
|
|
206
|
RubikActivity act = (RubikActivity)getContext();
|
207
|
|
208
|
Button scrambleButt = act.findViewById(R.id.rubikScramble);
|
209
|
|
210
|
if( scrambleButt!=null )
|
211
|
{
|
212
|
scrambleButt.setClickable(false);
|
213
|
scrambleButt.setVisibility(INVISIBLE);
|
214
|
}
|
215
|
else
|
216
|
{
|
217
|
android.util.Log.e("view", "button null!");
|
218
|
}
|
219
|
}
|
220
|
}
|
221
|
|
222
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223
|
|
224
|
void leaveScrambleMode()
|
225
|
{
|
226
|
if( mInScrambleMode )
|
227
|
{
|
228
|
mInScrambleMode = false;
|
229
|
|
230
|
android.util.Log.e("view", "leaving scramble mode");
|
231
|
|
232
|
RubikActivity act = (RubikActivity)getContext();
|
233
|
|
234
|
Button scrambleButt = act.findViewById(R.id.rubikScramble);
|
235
|
|
236
|
if( scrambleButt!=null )
|
237
|
{
|
238
|
scrambleButt.setClickable(true);
|
239
|
scrambleButt.setVisibility(VISIBLE);
|
240
|
}
|
241
|
else
|
242
|
{
|
243
|
android.util.Log.e("view", "button null!");
|
244
|
}
|
245
|
}
|
246
|
}
|
247
|
|
248
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
249
|
|
250
|
void leaveScrambleModeNonUI()
|
251
|
{
|
252
|
RubikActivity act = (RubikActivity)getContext();
|
253
|
|
254
|
act.runOnUiThread(new Runnable()
|
255
|
{
|
256
|
@Override
|
257
|
public void run()
|
258
|
{
|
259
|
leaveScrambleMode();
|
260
|
}
|
261
|
});
|
262
|
}
|
263
|
|
264
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
265
|
|
266
|
void setQuatAccumulated()
|
267
|
{
|
268
|
mQuatAccumulated.set(mTempAccumulated);
|
269
|
}
|
270
|
|
271
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
272
|
|
273
|
void setQuatCurrent()
|
274
|
{
|
275
|
mQuatCurrent.set(mTempCurrent);
|
276
|
}
|
277
|
|
278
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
279
|
|
280
|
Static4D getQuatAccumulated()
|
281
|
{
|
282
|
return mQuatAccumulated;
|
283
|
}
|
284
|
|
285
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
286
|
|
287
|
Static4D getQuatCurrent()
|
288
|
{
|
289
|
return mQuatCurrent;
|
290
|
}
|
291
|
|
292
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
293
|
|
294
|
private Static4D quatFromDrag(float dragX, float dragY)
|
295
|
{
|
296
|
float axisX = dragY; // inverted X and Y - rotation axis is perpendicular to (dragX,dragY)
|
297
|
float axisY = dragX; // Why not (-dragY, dragX) ? because Y axis is also inverted!
|
298
|
float axisZ = 0;
|
299
|
float axisL = (float)Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);
|
300
|
|
301
|
if( axisL>0 )
|
302
|
{
|
303
|
axisX /= axisL;
|
304
|
axisY /= axisL;
|
305
|
axisZ /= axisL;
|
306
|
|
307
|
float ratio = axisL;
|
308
|
ratio = ratio - (int)ratio; // the cos() is only valid in (0,Pi)
|
309
|
|
310
|
float cosA = (float)Math.cos(Math.PI*ratio);
|
311
|
float sinA = (float)Math.sqrt(1-cosA*cosA);
|
312
|
|
313
|
return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
|
314
|
}
|
315
|
|
316
|
return new Static4D(0f, 0f, 0f, 1f);
|
317
|
}
|
318
|
|
319
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
320
|
// PUBLIC API
|
321
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
322
|
|
323
|
public RubikSurfaceView(Context context, AttributeSet attrs)
|
324
|
{
|
325
|
super(context,attrs);
|
326
|
|
327
|
if(!isInEditMode())
|
328
|
{
|
329
|
mInScrambleMode = false;
|
330
|
mRenderer = new RubikRenderer(this);
|
331
|
mMovement = new RubikCubeMovement();
|
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
|
|
354
|
Static4D touchPoint1 = new Static4D(x, y, 0, 0);
|
355
|
Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
|
356
|
Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
|
357
|
|
358
|
if( mMovement.faceTouched(rotatedTouchPoint1, rotatedCamera) )
|
359
|
{
|
360
|
mDragging = false;
|
361
|
mBeginningRotation = mRenderer.canRotate();
|
362
|
mContinuingRotation = false;
|
363
|
}
|
364
|
else
|
365
|
{
|
366
|
mDragging = mRenderer.canDrag();
|
367
|
mBeginningRotation = false;
|
368
|
mContinuingRotation = false;
|
369
|
}
|
370
|
break;
|
371
|
case MotionEvent.ACTION_MOVE: if( mDragging )
|
372
|
{
|
373
|
mTempCurrent.set(quatFromDrag(mX-x,y-mY));
|
374
|
mRenderer.setQuatCurrentOnNextRender();
|
375
|
|
376
|
if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
|
377
|
{
|
378
|
mX = x;
|
379
|
mY = y;
|
380
|
mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
|
381
|
mTempCurrent.set(0f, 0f, 0f, 1f);
|
382
|
mRenderer.setQuatCurrentOnNextRender();
|
383
|
mRenderer.setQuatAccumulatedOnNextRender();
|
384
|
}
|
385
|
}
|
386
|
if( mBeginningRotation )
|
387
|
{
|
388
|
if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
|
389
|
{
|
390
|
Static4D touchPoint2 = new Static4D(x, y, 0, 0);
|
391
|
Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
|
392
|
|
393
|
Static2D rot = mMovement.newRotation(rotatedTouchPoint2);
|
394
|
RubikCube cube = mRenderer.getCube();
|
395
|
|
396
|
cube.addNewRotation( (int)rot.get0(), (int)(cube.getSize()*rot.get1()) );
|
397
|
|
398
|
mBeginningRotation = false;
|
399
|
mContinuingRotation= true;
|
400
|
}
|
401
|
}
|
402
|
else if( mContinuingRotation )
|
403
|
{
|
404
|
Static4D touchPoint3 = new Static4D(x, y, 0, 0);
|
405
|
Static4D rotatedTouchPoint3= rotateVectorByInvertedQuat(touchPoint3, mQuatAccumulated);
|
406
|
|
407
|
float angle = mMovement.continueRotation(rotatedTouchPoint3);
|
408
|
mRenderer.getCube().continueRotation(SWIPING_SENSITIVITY*angle);
|
409
|
}
|
410
|
break;
|
411
|
case MotionEvent.ACTION_UP : if( mDragging )
|
412
|
{
|
413
|
mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
|
414
|
mTempCurrent.set(0f, 0f, 0f, 1f);
|
415
|
mRenderer.setQuatCurrentOnNextRender();
|
416
|
mRenderer.setQuatAccumulatedOnNextRender();
|
417
|
}
|
418
|
|
419
|
if( mContinuingRotation )
|
420
|
{
|
421
|
mRenderer.finishRotation();
|
422
|
}
|
423
|
break;
|
424
|
}
|
425
|
|
426
|
return true;
|
427
|
}
|
428
|
}
|
429
|
|