1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.objectlib.main;
|
11
|
|
12
|
import static org.distorted.objectlib.helpers.OperatingSystemInterface.*;
|
13
|
|
14
|
import org.distorted.library.helpers.QuatHelper;
|
15
|
import org.distorted.library.type.Static4D;
|
16
|
|
17
|
import org.distorted.objectlib.effects.BaseEffect;
|
18
|
import org.distorted.objectlib.helpers.BlockController;
|
19
|
import org.distorted.objectlib.helpers.MovesFinished;
|
20
|
import org.distorted.objectlib.helpers.ObjectLibInterface;
|
21
|
import org.distorted.objectlib.helpers.OperatingSystemInterface;
|
22
|
import org.distorted.objectlib.touchcontrol.TouchControl;
|
23
|
import org.distorted.objectlib.touchcontrol.TouchControlShapeChanging;
|
24
|
|
25
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26
|
|
27
|
public class ObjectControl
|
28
|
{
|
29
|
public static final int MAX_MOVING_PARTS = 242; // Gigaminx
|
30
|
public static final int MAX_QUATS = 60; // Gigaminx: 60 quats group.
|
31
|
|
32
|
public static final int NUM_SPEED_PROBES = 10;
|
33
|
|
34
|
public static final int MODE_ROTATE = 0;
|
35
|
public static final int MODE_DRAG = 1;
|
36
|
public static final int MODE_REPLACE = 2;
|
37
|
public static final int MODE_NOTHING = 3;
|
38
|
|
39
|
public static final int ROT_AXIS_NOT_NORMAL = 0;
|
40
|
public static final int ROT_AXIS_NORMAL_OPP = 1;
|
41
|
public static final int ROT_AXIS_NORMAL_AGR = 2;
|
42
|
|
43
|
// Moving the finger from the middle of the vertical screen to the right edge will rotate a
|
44
|
// given face by SWIPING_SENSITIVITY/2 degrees.
|
45
|
public final static int SWIPING_SENSITIVITY = 240;
|
46
|
// Moving the finger by 0.3 of an inch will start a Rotation.
|
47
|
public final static float ROTATION_SENSITIVITY = 0.3f;
|
48
|
|
49
|
private final Static4D CAMERA_POINT = new Static4D(0, 0, 0, 0);
|
50
|
|
51
|
private final ObjectLibInterface mInterface;
|
52
|
private final OperatingSystemInterface mOS;
|
53
|
private final ObjectPreRender mPreRender;
|
54
|
private TouchControl mTouchControl;
|
55
|
private TwistyObjectNode mObjectNode;
|
56
|
private boolean mDragging, mBeginningRotation, mContinuingRotation;
|
57
|
private int mScreenWidth, mScreenHeight, mScalingSize;
|
58
|
private float mMoveX, mMoveY;
|
59
|
private int mLastMode;
|
60
|
|
61
|
private float mRotAngle, mInitDistance;
|
62
|
private float mStartRotX, mStartRotY;
|
63
|
private float mRotationFactor;
|
64
|
private int mCurrentAxis, mCurrentRow;
|
65
|
private float mCurrentAngle, mAvgRotSpeed;
|
66
|
private final float[] mLastA;
|
67
|
private final long[] mLastT;
|
68
|
private int mFirstIndex, mLastIndex;
|
69
|
private final int mDensity;
|
70
|
|
71
|
private float mX1, mY1, mX2, mY2, mX, mY;
|
72
|
private final boolean mIsAutomatic;
|
73
|
|
74
|
private boolean mIsLocked, mRemLocked;
|
75
|
private final int[] mBuffer;
|
76
|
private final float[] mAxis;
|
77
|
private int mMeshState, mIconMode;
|
78
|
private boolean mRotateOnCreation;
|
79
|
private final Static4D mQuat,mTemp;
|
80
|
private int mRotAxisType;
|
81
|
private final float[] mNormalTouchPoint;
|
82
|
private float mNormalRotAngle;
|
83
|
|
84
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85
|
|
86
|
private void addSpeedProbe(float angle)
|
87
|
{
|
88
|
long currTime = System.currentTimeMillis();
|
89
|
boolean theSame = mLastIndex==mFirstIndex;
|
90
|
|
91
|
mLastIndex++;
|
92
|
if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
|
93
|
|
94
|
mLastT[mLastIndex] = currTime;
|
95
|
mLastA[mLastIndex] = angle;
|
96
|
|
97
|
if( mLastIndex==mFirstIndex)
|
98
|
{
|
99
|
mFirstIndex++;
|
100
|
if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
|
101
|
}
|
102
|
|
103
|
if( theSame )
|
104
|
{
|
105
|
mLastT[mFirstIndex] = currTime;
|
106
|
mLastA[mFirstIndex] = angle;
|
107
|
}
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
private void computeCurrentSpeedInInchesPerSecond()
|
113
|
{
|
114
|
long timeDiff = mLastT[mLastIndex]-mLastT[mFirstIndex];
|
115
|
float sum = 0;
|
116
|
for(int i=mFirstIndex; i<=mLastIndex; i++) sum += mLastA[i];
|
117
|
|
118
|
mLastIndex = 0;
|
119
|
mFirstIndex= 0;
|
120
|
|
121
|
mAvgRotSpeed = timeDiff>0 ? sum/timeDiff : 0;
|
122
|
if( mAvgRotSpeed<0 ) mAvgRotSpeed = -mAvgRotSpeed;
|
123
|
}
|
124
|
|
125
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126
|
|
127
|
private float retFingerDragDistanceInInches(float xd, float yd)
|
128
|
{
|
129
|
float xDist = mScreenWidth*xd;
|
130
|
float yDist = mScreenHeight*yd;
|
131
|
float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
|
132
|
|
133
|
return distInPixels/mDensity;
|
134
|
}
|
135
|
|
136
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
137
|
|
138
|
private void replaceMode(boolean down)
|
139
|
{
|
140
|
mBeginningRotation= false;
|
141
|
|
142
|
if( down )
|
143
|
{
|
144
|
int cubit = mTouchControl.getTouchedCubit();
|
145
|
int face = mTouchControl.getTouchedCubitFace();
|
146
|
mInterface.onReplaceModeDown(cubit,face);
|
147
|
}
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
private void setUpDragOrRotate(boolean down, float x, float y)
|
153
|
{
|
154
|
if( mLastMode==MODE_DRAG )
|
155
|
{
|
156
|
mDragging = true;
|
157
|
mBeginningRotation = false;
|
158
|
mContinuingRotation = false;
|
159
|
}
|
160
|
else
|
161
|
{
|
162
|
CAMERA_POINT.set2( mObjectNode.getCameraDist() );
|
163
|
Static4D touchPoint = new Static4D(x, y, 0, 0);
|
164
|
Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, mQuat);
|
165
|
Static4D rotatedCamera= QuatHelper.rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
|
166
|
|
167
|
if( mTouchControl!=null && mTouchControl.objectTouched(rotatedTouchPoint,rotatedCamera) )
|
168
|
{
|
169
|
mDragging = false;
|
170
|
mContinuingRotation = false;
|
171
|
|
172
|
if( mLastMode==MODE_ROTATE ) mBeginningRotation = !mPreRender.isRotationBlocked();
|
173
|
else if( mLastMode==MODE_REPLACE ) replaceMode(down);
|
174
|
}
|
175
|
else
|
176
|
{
|
177
|
mDragging = (!mIsLocked || mIsAutomatic);
|
178
|
mBeginningRotation = false;
|
179
|
mContinuingRotation = false;
|
180
|
if( !mDragging ) mInterface.failedToDrag();
|
181
|
}
|
182
|
}
|
183
|
}
|
184
|
|
185
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
186
|
|
187
|
private void drag(float x, float y)
|
188
|
{
|
189
|
if( mOS.isFirstPressed() && mOS.isSecondPressed() )
|
190
|
{
|
191
|
float x2 = (mX2 - mScreenWidth*0.5f)/ mScalingSize;
|
192
|
float y2 = (mScreenHeight*0.5f - mY2)/ mScalingSize;
|
193
|
|
194
|
float angleNow = getAngle(x,y,x2,y2);
|
195
|
float angleDiff = angleNow-mRotAngle;
|
196
|
float sinA =-(float)Math.sin(angleDiff);
|
197
|
float cosA = (float)Math.cos(angleDiff);
|
198
|
|
199
|
Static4D dragQuat = QuatHelper.quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
|
200
|
mTemp.set(dragQuat);
|
201
|
|
202
|
mRotAngle = angleNow;
|
203
|
|
204
|
float distNow = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
|
205
|
float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
|
206
|
mInitDistance = distNow;
|
207
|
TwistyObject object = mPreRender.getObject();
|
208
|
if( object!=null )
|
209
|
{
|
210
|
object.setObjectRatio(distQuot, mScalingSize );
|
211
|
float ratio = object.getObjectRatio();
|
212
|
if( mLastMode==MODE_REPLACE ) mTouchControl.setObjectRatio(ratio);
|
213
|
}
|
214
|
}
|
215
|
else
|
216
|
{
|
217
|
Static4D dragQuat = QuatHelper.quatMultiply(QuatHelper.quatFromDrag(mX-x,y-mY), mQuat);
|
218
|
mTemp.set(dragQuat);
|
219
|
}
|
220
|
|
221
|
mPreRender.setQuatOnNextRender();
|
222
|
mX = x;
|
223
|
mY = y;
|
224
|
}
|
225
|
|
226
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
227
|
|
228
|
private void finishRotation()
|
229
|
{
|
230
|
TwistyObject object = mPreRender.getObject();
|
231
|
int[][] angles = object.getBasicAngles();
|
232
|
|
233
|
if( mCurrentAxis<angles.length && mCurrentRow<angles[mCurrentAxis].length )
|
234
|
{
|
235
|
computeCurrentSpeedInInchesPerSecond();
|
236
|
int basic = angles[mCurrentAxis][mCurrentRow];
|
237
|
int angle = object.computeNearestAngle(basic,mCurrentAngle, mAvgRotSpeed);
|
238
|
mPreRender.finishRotation(angle);
|
239
|
mPreRender.rememberMove(mCurrentAxis,mCurrentRow,angle);
|
240
|
|
241
|
if( angle!=0 )
|
242
|
{
|
243
|
int realAngle = (angle*basic)/360;
|
244
|
mInterface.onFinishRotation(mCurrentAxis,mCurrentRow,realAngle);
|
245
|
}
|
246
|
|
247
|
mContinuingRotation = false;
|
248
|
mBeginningRotation = false;
|
249
|
mDragging = true;
|
250
|
}
|
251
|
}
|
252
|
|
253
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
254
|
|
255
|
private void continueRotation(float x, float y)
|
256
|
{
|
257
|
if( mRotAxisType == ROT_AXIS_NOT_NORMAL )
|
258
|
{
|
259
|
float dx = x-mStartRotX;
|
260
|
float dy = y-mStartRotY;
|
261
|
float alpha = dx*mAxis[0] + dy*mAxis[1];
|
262
|
float x2 = dx - alpha*mAxis[0];
|
263
|
float y2 = dy - alpha*mAxis[1];
|
264
|
|
265
|
float len = (float)Math.sqrt(x2*x2 + y2*y2);
|
266
|
|
267
|
// we have the length of 1D vector 'angle', now the direction:
|
268
|
float tmp = mAxis[1]==0 ? -mAxis[0]*y2 : mAxis[1]*x2;
|
269
|
|
270
|
float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
|
271
|
mCurrentAngle = SWIPING_SENSITIVITY*angle;
|
272
|
mPreRender.getObject().continueRotation(mCurrentAngle);
|
273
|
addSpeedProbe(mCurrentAngle);
|
274
|
}
|
275
|
else
|
276
|
{
|
277
|
float rotAngle = computeNormalRotAngle(mNormalTouchPoint[0]-x,mNormalTouchPoint[1]-y);
|
278
|
mCurrentAngle = mNormalRotAngle - rotAngle;
|
279
|
if( mRotAxisType==ROT_AXIS_NORMAL_AGR ) mCurrentAngle = -mCurrentAngle;
|
280
|
mPreRender.getObject().continueRotation(mCurrentAngle);
|
281
|
addSpeedProbe(mCurrentAngle);
|
282
|
}
|
283
|
}
|
284
|
|
285
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
286
|
|
287
|
private void correctRotationAxis()
|
288
|
{
|
289
|
if( mAxis[0]==0 && mAxis[1]==0 )
|
290
|
{
|
291
|
// 0. get face center in object space
|
292
|
// 1. multiply it by mQuat
|
293
|
// 2. multiply if by mObjectRatio. Let the result be (x,y,z)
|
294
|
// 3. let D be CAMERA_POINT.Z
|
295
|
// 4. then compute the point on the screen (X,Y) the face center maps to:
|
296
|
// (X,Y) = (Ax,Ay) where A = D/(D-z)
|
297
|
|
298
|
float[] faceCenter = mTouchControl.getTouchedPuzzleCenter();
|
299
|
float D = CAMERA_POINT.get2();
|
300
|
float[] rotated = new float[4];
|
301
|
QuatHelper.rotateVectorByQuat(rotated, faceCenter[0],faceCenter[1],faceCenter[2],faceCenter[3],mQuat);
|
302
|
TwistyObject object = getObject();
|
303
|
float ratio = object.getObjectRatio();
|
304
|
|
305
|
float x = rotated[0] * ratio;
|
306
|
float y = rotated[1] * ratio;
|
307
|
float z = rotated[2] * ratio;
|
308
|
|
309
|
float A = D / (D-z);
|
310
|
mNormalTouchPoint[0] = A*x;
|
311
|
mNormalTouchPoint[1] = A*y;
|
312
|
|
313
|
boolean agree = mTouchControl.axisAndFaceAgree(mCurrentAxis);
|
314
|
mRotAxisType = (agree ? ROT_AXIS_NORMAL_AGR : ROT_AXIS_NORMAL_OPP);
|
315
|
}
|
316
|
else
|
317
|
{
|
318
|
mRotAxisType = ROT_AXIS_NOT_NORMAL;
|
319
|
}
|
320
|
}
|
321
|
|
322
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
323
|
// in degrees
|
324
|
|
325
|
private float computeNormalRotAngle(float vx, float vy)
|
326
|
{
|
327
|
return (float)((180*Math.atan2(vy,vx))/Math.PI);
|
328
|
}
|
329
|
|
330
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
331
|
|
332
|
private void beginRotation(float x, float y)
|
333
|
{
|
334
|
mStartRotX = x;
|
335
|
mStartRotY = y;
|
336
|
|
337
|
TwistyObject object = mPreRender.getObject();
|
338
|
int[] numLayers = object.getNumLayers();
|
339
|
Static4D touchPoint = new Static4D(x, y, 0, 0);
|
340
|
Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, mQuat);
|
341
|
|
342
|
mTouchControl.newRotation(mBuffer,rotatedTouchPoint,mQuat);
|
343
|
|
344
|
mCurrentAxis = mBuffer[0];
|
345
|
mCurrentRow = mBuffer[1];
|
346
|
|
347
|
mTouchControl.getCastedRotAxis(mAxis,mQuat,mCurrentAxis);
|
348
|
correctRotationAxis();
|
349
|
mRotationFactor = mTouchControl.returnRotationFactor(numLayers,mCurrentRow);
|
350
|
if( mRotAxisType != ROT_AXIS_NOT_NORMAL ) mNormalRotAngle = computeNormalRotAngle(mNormalTouchPoint[0]-x,mNormalTouchPoint[1]-y);
|
351
|
|
352
|
if( object.beginNewRotation( mCurrentAxis, mCurrentRow ) )
|
353
|
{
|
354
|
mInterface.onBeginRotation();
|
355
|
addSpeedProbe(0);
|
356
|
mBeginningRotation = false;
|
357
|
mContinuingRotation= true;
|
358
|
}
|
359
|
}
|
360
|
|
361
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
362
|
|
363
|
private float getAngle(float x1, float y1, float x2, float y2)
|
364
|
{
|
365
|
return (float) Math.atan2(y1-y2, x1-x2);
|
366
|
}
|
367
|
|
368
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
369
|
|
370
|
private void prepareDown()
|
371
|
{
|
372
|
mOS.pressFirst();
|
373
|
mOS.unpressSecond();
|
374
|
|
375
|
mX1 = mOS.getFirstX() - mMoveX;
|
376
|
mY1 = mOS.getFirstY() + mMoveY;
|
377
|
}
|
378
|
|
379
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
380
|
|
381
|
private void prepareMove()
|
382
|
{
|
383
|
int index1 = mOS.getFirstPointerIndex();
|
384
|
|
385
|
if( index1>=0 )
|
386
|
{
|
387
|
mX1 = mOS.getX(index1) - mMoveX;
|
388
|
mY1 = mOS.getY(index1) + mMoveY;
|
389
|
}
|
390
|
|
391
|
int index2 = mOS.getSecondPointerIndex();
|
392
|
|
393
|
if( index2>=0 )
|
394
|
{
|
395
|
mX2 = mOS.getX(index2) - mMoveX;
|
396
|
mY2 = mOS.getY(index2) + mMoveY;
|
397
|
}
|
398
|
}
|
399
|
|
400
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
401
|
|
402
|
private void prepareUp()
|
403
|
{
|
404
|
mOS.unpressFirst();
|
405
|
mOS.unpressSecond();
|
406
|
}
|
407
|
|
408
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
409
|
|
410
|
private void prepareDown2()
|
411
|
{
|
412
|
if( !mOS.isFirstPressed() )
|
413
|
{
|
414
|
mOS.pressFirst();
|
415
|
mX1 = mOS.getFirstX() - mMoveX;
|
416
|
mY1 = mOS.getFirstY() + mMoveY;
|
417
|
}
|
418
|
else if( !mOS.isSecondPressed() )
|
419
|
{
|
420
|
mOS.pressSecond();
|
421
|
mX2 = mOS.getSecondX() - mMoveX;
|
422
|
mY2 = mOS.getSecondY() + mMoveY;
|
423
|
}
|
424
|
}
|
425
|
|
426
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
427
|
|
428
|
private void prepareUp2()
|
429
|
{
|
430
|
mOS.upOneOfThem();
|
431
|
}
|
432
|
|
433
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
434
|
|
435
|
private void actionMove(float x1, float y1, float x2, float y2)
|
436
|
{
|
437
|
float pX,pY;
|
438
|
|
439
|
if( mOS.isFirstPressed() ) { pX = x1; pY=y1; }
|
440
|
else { pX = x2; pY=y2; }
|
441
|
|
442
|
float x = (pX - mScreenWidth*0.5f)/ mScalingSize;
|
443
|
float y = (mScreenHeight*0.5f -pY)/ mScalingSize;
|
444
|
|
445
|
if( mBeginningRotation )
|
446
|
{
|
447
|
if( retFingerDragDistanceInInches(mX-x,mY-y) > ROTATION_SENSITIVITY )
|
448
|
{
|
449
|
beginRotation(x,y);
|
450
|
}
|
451
|
}
|
452
|
else if( mContinuingRotation )
|
453
|
{
|
454
|
continueRotation(x,y);
|
455
|
}
|
456
|
else if( mDragging )
|
457
|
{
|
458
|
drag(x,y);
|
459
|
}
|
460
|
else
|
461
|
{
|
462
|
setUpDragOrRotate(false,x,y);
|
463
|
}
|
464
|
}
|
465
|
|
466
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
467
|
|
468
|
private void actionDown(float x, float y)
|
469
|
{
|
470
|
mX = (x - mScreenWidth*0.5f)/ mScalingSize;
|
471
|
mY = (mScreenHeight*0.5f - y)/ mScalingSize;
|
472
|
|
473
|
setUpDragOrRotate(true,mX,mY);
|
474
|
}
|
475
|
|
476
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
477
|
|
478
|
private void actionUp()
|
479
|
{
|
480
|
if( mContinuingRotation )
|
481
|
{
|
482
|
finishRotation();
|
483
|
}
|
484
|
|
485
|
if( mLastMode==MODE_REPLACE ) mInterface.onReplaceModeUp();
|
486
|
}
|
487
|
|
488
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
489
|
|
490
|
private void actionDown2(float x1, float y1, float x2, float y2)
|
491
|
{
|
492
|
mRotAngle = getAngle(x1,-y1, x2,-y2);
|
493
|
mInitDistance = -1;
|
494
|
|
495
|
mX = (x1 - mScreenWidth*0.5f )/ mScalingSize;
|
496
|
mY = (mScreenHeight*0.5f - y1)/ mScalingSize;
|
497
|
|
498
|
if( mBeginningRotation )
|
499
|
{
|
500
|
mContinuingRotation = false;
|
501
|
mBeginningRotation = false;
|
502
|
mDragging = true;
|
503
|
}
|
504
|
else if( mContinuingRotation )
|
505
|
{
|
506
|
finishRotation();
|
507
|
}
|
508
|
}
|
509
|
|
510
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
511
|
|
512
|
private void actionUp2(boolean p1isUp, float x1, float y1, boolean p2isUp, float x2, float y2)
|
513
|
{
|
514
|
if( p1isUp )
|
515
|
{
|
516
|
mX = (x2 - mScreenWidth*0.5f)/ mScalingSize;
|
517
|
mY = (mScreenHeight*0.5f - y2)/ mScalingSize;
|
518
|
}
|
519
|
if( p2isUp )
|
520
|
{
|
521
|
mX = (x1 - mScreenWidth*0.5f)/ mScalingSize;
|
522
|
mY = (mScreenHeight*0.5f - y1)/ mScalingSize;
|
523
|
}
|
524
|
}
|
525
|
|
526
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
527
|
|
528
|
int getScalingSize()
|
529
|
{
|
530
|
return mScalingSize;
|
531
|
}
|
532
|
|
533
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
534
|
|
535
|
void setTouchControl(TwistyObject object)
|
536
|
{
|
537
|
if( mLastMode!=MODE_REPLACE ) mTouchControl = object.getTouchControl();
|
538
|
else mTouchControl = new TouchControlShapeChanging(object);
|
539
|
}
|
540
|
|
541
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
542
|
// INTERNAL API (for AutomaticControl)
|
543
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
544
|
|
545
|
public ObjectPreRender getPreRender()
|
546
|
{
|
547
|
return mPreRender;
|
548
|
}
|
549
|
|
550
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
551
|
|
552
|
public ObjectLibInterface getInterface()
|
553
|
{
|
554
|
return mInterface;
|
555
|
}
|
556
|
|
557
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
558
|
// PUBLIC API
|
559
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
560
|
|
561
|
public ObjectControl(OperatingSystemInterface os)
|
562
|
{
|
563
|
mIsAutomatic = false;
|
564
|
|
565
|
mBuffer = new int[2];
|
566
|
mAxis = new float[2];
|
567
|
|
568
|
mQuat= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
|
569
|
mTemp= new Static4D(0,0,0,1);
|
570
|
|
571
|
mAvgRotSpeed = 0.0f;
|
572
|
mLastMode = -1;
|
573
|
mRotateOnCreation = false;
|
574
|
|
575
|
mNormalTouchPoint = new float[2];
|
576
|
|
577
|
mLastA = new float[NUM_SPEED_PROBES];
|
578
|
mLastT = new long[NUM_SPEED_PROBES];
|
579
|
mFirstIndex= 0;
|
580
|
mLastIndex = 0;
|
581
|
mMeshState =-1;
|
582
|
mIconMode =-1;
|
583
|
|
584
|
mInterface = os.getInterface();
|
585
|
mOS = os;
|
586
|
|
587
|
mDensity = mOS.getScreenDensity();
|
588
|
mPreRender = new ObjectPreRender(this,mInterface);
|
589
|
}
|
590
|
|
591
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
592
|
|
593
|
public void setRotateOnCreation(boolean rotate)
|
594
|
{
|
595
|
mRotateOnCreation = rotate;
|
596
|
}
|
597
|
|
598
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
599
|
|
600
|
public boolean getRotateOnCreation()
|
601
|
{
|
602
|
return mRotateOnCreation;
|
603
|
}
|
604
|
|
605
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
606
|
|
607
|
public TwistyObjectNode getNode()
|
608
|
{
|
609
|
return mObjectNode;
|
610
|
}
|
611
|
|
612
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
613
|
|
614
|
public void createNode(int width, int height)
|
615
|
{
|
616
|
if( mObjectNode==null ) mObjectNode = new TwistyObjectNode(width,height);
|
617
|
}
|
618
|
|
619
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
620
|
|
621
|
public void setScreenSizeAndScaling(int width, int height, int scaling)
|
622
|
{
|
623
|
mScreenWidth = width;
|
624
|
mScreenHeight= height;
|
625
|
mScalingSize = scaling;
|
626
|
|
627
|
if( mObjectNode!=null ) mObjectNode.setSize(width,height);
|
628
|
|
629
|
TwistyObject object = mPreRender.getObject();
|
630
|
|
631
|
if( object!=null )
|
632
|
{
|
633
|
object.setTexture();
|
634
|
object.setNodeSize(mScalingSize);
|
635
|
}
|
636
|
}
|
637
|
|
638
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
639
|
|
640
|
public void setObjectMove(int xmove, int ymove)
|
641
|
{
|
642
|
mMoveX = xmove;
|
643
|
mMoveY = ymove;
|
644
|
|
645
|
mPreRender.setMove(xmove,ymove);
|
646
|
}
|
647
|
|
648
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
649
|
|
650
|
public void setObjectScale(float scale)
|
651
|
{
|
652
|
mPreRender.setScale(scale);
|
653
|
}
|
654
|
|
655
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
656
|
|
657
|
public void onPause()
|
658
|
{
|
659
|
BlockController.onPause();
|
660
|
}
|
661
|
|
662
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
663
|
|
664
|
public void onResume()
|
665
|
{
|
666
|
mOS.unpressFirst();
|
667
|
mOS.unpressSecond();
|
668
|
|
669
|
unlock();
|
670
|
|
671
|
BlockController.onResume();
|
672
|
}
|
673
|
|
674
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
675
|
|
676
|
public void rotateNow(Static4D quat)
|
677
|
{
|
678
|
mTemp.set(quat);
|
679
|
mQuat.set(mTemp);
|
680
|
}
|
681
|
|
682
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
683
|
|
684
|
public void scaleNow(float scale)
|
685
|
{
|
686
|
mPreRender.getObject().setObjectRatioNow(scale,mScalingSize );
|
687
|
}
|
688
|
|
689
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
690
|
|
691
|
public void setQuat()
|
692
|
{
|
693
|
mQuat.set(mTemp);
|
694
|
}
|
695
|
|
696
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
697
|
|
698
|
public Static4D getQuat()
|
699
|
{
|
700
|
return mQuat;
|
701
|
}
|
702
|
|
703
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
704
|
|
705
|
public void preRender()
|
706
|
{
|
707
|
mPreRender.preRender();
|
708
|
}
|
709
|
|
710
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
711
|
|
712
|
public void blockTouch(int place)
|
713
|
{
|
714
|
setLock(true);
|
715
|
mPreRender.blockRotation(place);
|
716
|
}
|
717
|
|
718
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
719
|
|
720
|
public void unblockRotation()
|
721
|
{
|
722
|
unsetLock();
|
723
|
mPreRender.unblockRotation();
|
724
|
}
|
725
|
|
726
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
727
|
|
728
|
public void unblockEverything()
|
729
|
{
|
730
|
mPreRender.unblockEverything();
|
731
|
}
|
732
|
|
733
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
734
|
|
735
|
public boolean isScramblingAndSolvingNotBlocked()
|
736
|
{
|
737
|
return mPreRender.isScramblingAndSolvingNotBlocked();
|
738
|
}
|
739
|
|
740
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
741
|
|
742
|
public void initializeObject(int[][] moves)
|
743
|
{
|
744
|
mPreRender.initializeObject(moves);
|
745
|
}
|
746
|
|
747
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
748
|
|
749
|
public void changeObject(int ordinal, int meshState, int iconMode, InitAssets asset)
|
750
|
{
|
751
|
mPreRender.changeObject(ordinal, meshState, iconMode, asset);
|
752
|
}
|
753
|
|
754
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
755
|
|
756
|
public void changeIfDifferent(int ordinal, String newName, int meshState, int iconMode, InitAssets asset)
|
757
|
{
|
758
|
TwistyObject object = mPreRender.getObject();
|
759
|
String oldName = object==null ? "" : object.getShortName();
|
760
|
|
761
|
if( !oldName.equals(newName) || mMeshState!=meshState || mIconMode!=iconMode )
|
762
|
{
|
763
|
mMeshState = meshState;
|
764
|
mIconMode = iconMode;
|
765
|
mPreRender.changeObject(ordinal, meshState, iconMode, asset);
|
766
|
}
|
767
|
}
|
768
|
|
769
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
770
|
// if one or more fingers currently touch the screen, and we just pressed the 'scramble' button, do
|
771
|
// not scramble - otherwise a kind of a cheat is possible where user touches the screen, starts
|
772
|
// scrambling, then lifts the finger and the act of lifting screws the scrambling - no further
|
773
|
// scrambles take any effect!
|
774
|
|
775
|
public boolean scrambleObject(int num)
|
776
|
{
|
777
|
if( !mBeginningRotation && !mContinuingRotation )
|
778
|
{
|
779
|
return mPreRender.scrambleObject(num);
|
780
|
}
|
781
|
return false;
|
782
|
}
|
783
|
|
784
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
785
|
// ditto
|
786
|
|
787
|
public boolean fastScrambleObject(int duration, int num)
|
788
|
{
|
789
|
if( !mBeginningRotation && !mContinuingRotation )
|
790
|
{
|
791
|
return mPreRender.fastScrambleObject(duration,num);
|
792
|
}
|
793
|
return false;
|
794
|
}
|
795
|
|
796
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
797
|
|
798
|
public void presentObject(int num, int duration)
|
799
|
{
|
800
|
mPreRender.presentObject(num,duration);
|
801
|
}
|
802
|
|
803
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
804
|
|
805
|
public void solveObject()
|
806
|
{
|
807
|
mPreRender.solveObject();
|
808
|
}
|
809
|
|
810
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
811
|
|
812
|
public void solveOnly()
|
813
|
{
|
814
|
mPreRender.solveOnly();
|
815
|
}
|
816
|
|
817
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
818
|
|
819
|
public void resetTextureMapsEffect(int duration)
|
820
|
{
|
821
|
mPreRender.resetTextureMapsEffect(duration);
|
822
|
}
|
823
|
|
824
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
825
|
|
826
|
public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, int duration)
|
827
|
{
|
828
|
mPreRender.addRotation(listener,axis,rowBitmap,angle,duration);
|
829
|
}
|
830
|
|
831
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
832
|
|
833
|
public void resetAllTextureMaps()
|
834
|
{
|
835
|
mPreRender.resetAllTextureMaps();
|
836
|
}
|
837
|
|
838
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
839
|
|
840
|
public TwistyObject getObject()
|
841
|
{
|
842
|
return mPreRender.getObject();
|
843
|
}
|
844
|
|
845
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
846
|
|
847
|
public OperatingSystemInterface getOS()
|
848
|
{
|
849
|
return mOS;
|
850
|
}
|
851
|
|
852
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
853
|
|
854
|
public void savePreferences()
|
855
|
{
|
856
|
mPreRender.savePreferences(mOS);
|
857
|
|
858
|
for( int i=0; i< BaseEffect.Type.LENGTH; i++ )
|
859
|
{
|
860
|
BaseEffect.Type.getType(i).savePreferences(mOS);
|
861
|
}
|
862
|
}
|
863
|
|
864
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
865
|
|
866
|
public void restorePreferences()
|
867
|
{
|
868
|
mPreRender.restorePreferences(mOS);
|
869
|
|
870
|
for (int i=0; i<BaseEffect.Type.LENGTH; i++)
|
871
|
{
|
872
|
BaseEffect.Type.getType(i).restorePreferences(mOS);
|
873
|
}
|
874
|
}
|
875
|
|
876
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
877
|
|
878
|
public boolean retLocked()
|
879
|
{
|
880
|
return mIsLocked;
|
881
|
}
|
882
|
|
883
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
884
|
|
885
|
public void toggleLock()
|
886
|
{
|
887
|
mIsLocked = !mIsLocked;
|
888
|
}
|
889
|
|
890
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
891
|
|
892
|
public void unlock()
|
893
|
{
|
894
|
mIsLocked = false;
|
895
|
}
|
896
|
|
897
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
898
|
|
899
|
public void setLock(boolean value)
|
900
|
{
|
901
|
mRemLocked = mIsLocked;
|
902
|
mIsLocked = value;
|
903
|
}
|
904
|
|
905
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
906
|
|
907
|
public void unsetLock()
|
908
|
{
|
909
|
mIsLocked = mRemLocked;
|
910
|
}
|
911
|
|
912
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
913
|
|
914
|
public void setTextureMap(int cubit, int face, int newColor)
|
915
|
{
|
916
|
mPreRender.setTextureMap(cubit,face,newColor);
|
917
|
}
|
918
|
|
919
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
920
|
|
921
|
public boolean onTouchEvent(int mode)
|
922
|
{
|
923
|
if( mode!=MODE_NOTHING )
|
924
|
{
|
925
|
if( mObjectNode==null ) return true;
|
926
|
|
927
|
if( mode!=mLastMode )
|
928
|
{
|
929
|
TwistyObject object = getObject();
|
930
|
|
931
|
if( object!=null )
|
932
|
{
|
933
|
mLastMode = mode;
|
934
|
setTouchControl(object);
|
935
|
}
|
936
|
else return true;
|
937
|
}
|
938
|
|
939
|
int action = mOS.getAction();
|
940
|
|
941
|
switch(action)
|
942
|
{
|
943
|
case ACTION_DOWN_1: prepareDown();
|
944
|
actionDown(mX1, mY1);
|
945
|
break;
|
946
|
case ACTION_MOVE : prepareMove();
|
947
|
actionMove(mX1, mY1, mX2, mY2);
|
948
|
break;
|
949
|
case ACTION_UP_1 : prepareUp();
|
950
|
actionUp();
|
951
|
break;
|
952
|
case ACTION_DOWN_2: prepareDown2();
|
953
|
actionDown2(mX1, mY1, mX2, mY2);
|
954
|
break;
|
955
|
case ACTION_UP_2 : prepareUp2();
|
956
|
boolean p1isUp = !mOS.isFirstPressed();
|
957
|
boolean p2isUp = !mOS.isSecondPressed();
|
958
|
actionUp2(p1isUp, mX1, mY1, p2isUp, mX2, mY2);
|
959
|
break;
|
960
|
}
|
961
|
}
|
962
|
|
963
|
return true;
|
964
|
}
|
965
|
}
|
966
|
|