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