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 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
|
computeCurrentSpeedInInchesPerSecond();
|
231
|
mPreRender.finishRotation(mCurrentAxis,mCurrentRow,mCurrentAngle,mAvgRotSpeed);
|
232
|
mContinuingRotation = false;
|
233
|
mBeginningRotation = false;
|
234
|
mDragging = true;
|
235
|
}
|
236
|
|
237
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
238
|
|
239
|
private void continueRotation(float x, float y)
|
240
|
{
|
241
|
if( mRotAxisType == ROT_AXIS_NOT_NORMAL )
|
242
|
{
|
243
|
float dx = x-mStartRotX;
|
244
|
float dy = y-mStartRotY;
|
245
|
float alpha = dx*mAxis[0] + dy*mAxis[1];
|
246
|
float x2 = dx - alpha*mAxis[0];
|
247
|
float y2 = dy - alpha*mAxis[1];
|
248
|
|
249
|
float len = (float)Math.sqrt(x2*x2 + y2*y2);
|
250
|
|
251
|
// we have the length of 1D vector 'angle', now the direction:
|
252
|
float tmp = mAxis[1]==0 ? -mAxis[0]*y2 : mAxis[1]*x2;
|
253
|
|
254
|
float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
|
255
|
mCurrentAngle = SWIPING_SENSITIVITY*angle;
|
256
|
mPreRender.getObject().continueRotation(mCurrentAngle);
|
257
|
addSpeedProbe(mCurrentAngle);
|
258
|
}
|
259
|
else
|
260
|
{
|
261
|
float rotAngle = computeNormalRotAngle(mNormalTouchPoint[0]-x,mNormalTouchPoint[1]-y);
|
262
|
mCurrentAngle = mNormalRotAngle - rotAngle;
|
263
|
if( mRotAxisType==ROT_AXIS_NORMAL_AGR ) mCurrentAngle = -mCurrentAngle;
|
264
|
mPreRender.getObject().continueRotation(mCurrentAngle);
|
265
|
addSpeedProbe(mCurrentAngle);
|
266
|
}
|
267
|
}
|
268
|
|
269
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
270
|
|
271
|
private void correctRotationAxis()
|
272
|
{
|
273
|
if( mAxis[0]==0 && mAxis[1]==0 )
|
274
|
{
|
275
|
// 0. get face center in object space
|
276
|
// 1. multiply it by mQuat
|
277
|
// 2. multiply if by mObjectRatio. Let the result be (x,y,z)
|
278
|
// 3. let D be CAMERA_POINT.Z
|
279
|
// 4. then compute the point on the screen (X,Y) the face center maps to:
|
280
|
// (X,Y) = (Ax,Ay) where A = D/(D-z)
|
281
|
|
282
|
float[] faceCenter = mTouchControl.getTouchedPuzzleCenter();
|
283
|
float D = CAMERA_POINT.get2();
|
284
|
float[] rotated = new float[4];
|
285
|
QuatHelper.rotateVectorByQuat(rotated, faceCenter[0],faceCenter[1],faceCenter[2],faceCenter[3],mQuat);
|
286
|
TwistyObject object = getObject();
|
287
|
float ratio = object.getObjectRatio();
|
288
|
|
289
|
float x = rotated[0] * ratio;
|
290
|
float y = rotated[1] * ratio;
|
291
|
float z = rotated[2] * ratio;
|
292
|
|
293
|
float A = D / (D-z);
|
294
|
mNormalTouchPoint[0] = A*x;
|
295
|
mNormalTouchPoint[1] = A*y;
|
296
|
|
297
|
boolean agree = mTouchControl.axisAndFaceAgree(mCurrentAxis);
|
298
|
mRotAxisType = (agree ? ROT_AXIS_NORMAL_AGR : ROT_AXIS_NORMAL_OPP);
|
299
|
}
|
300
|
else
|
301
|
{
|
302
|
mRotAxisType = ROT_AXIS_NOT_NORMAL;
|
303
|
}
|
304
|
}
|
305
|
|
306
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
307
|
// in degrees
|
308
|
|
309
|
private float computeNormalRotAngle(float vx, float vy)
|
310
|
{
|
311
|
return (float)((180*Math.atan2(vy,vx))/Math.PI);
|
312
|
}
|
313
|
|
314
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
315
|
|
316
|
private void beginRotation(float x, float y)
|
317
|
{
|
318
|
Static4D touchPoint = new Static4D(x, y, 0, 0);
|
319
|
Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, mQuat);
|
320
|
mTouchControl.newRotation(mBuffer,rotatedTouchPoint,mQuat);
|
321
|
|
322
|
if( mBuffer[0]>=0 && mBuffer[1]>=0 )
|
323
|
{
|
324
|
mCurrentAxis = mBuffer[0];
|
325
|
mCurrentRow = mBuffer[1];
|
326
|
mStartRotX = x;
|
327
|
mStartRotY = y;
|
328
|
|
329
|
mTouchControl.getCastedRotAxis(mAxis, mQuat, mCurrentAxis);
|
330
|
correctRotationAxis();
|
331
|
mRotationFactor = mTouchControl.returnRotationFactor(mCurrentAxis, mCurrentRow);
|
332
|
if( mRotAxisType!=ROT_AXIS_NOT_NORMAL ) mNormalRotAngle = computeNormalRotAngle(mNormalTouchPoint[0]-x, mNormalTouchPoint[1]-y);
|
333
|
|
334
|
if( mPreRender.getObject().beginRotation(mCurrentAxis, mCurrentRow) )
|
335
|
{
|
336
|
mInterface.onBeginRotation();
|
337
|
addSpeedProbe(0);
|
338
|
mBeginningRotation = false;
|
339
|
mContinuingRotation = true;
|
340
|
}
|
341
|
}
|
342
|
}
|
343
|
|
344
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
345
|
|
346
|
private float getAngle(float x1, float y1, float x2, float y2)
|
347
|
{
|
348
|
return (float) Math.atan2(y1-y2, x1-x2);
|
349
|
}
|
350
|
|
351
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
352
|
|
353
|
private void prepareDown()
|
354
|
{
|
355
|
mOS.pressFirst();
|
356
|
mOS.unpressSecond();
|
357
|
|
358
|
mX1 = mOS.getFirstX() - mMoveX;
|
359
|
mY1 = mOS.getFirstY() + mMoveY;
|
360
|
}
|
361
|
|
362
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
363
|
|
364
|
private void prepareMove()
|
365
|
{
|
366
|
int index1 = mOS.getFirstPointerIndex();
|
367
|
|
368
|
if( index1>=0 )
|
369
|
{
|
370
|
mX1 = mOS.getX(index1) - mMoveX;
|
371
|
mY1 = mOS.getY(index1) + mMoveY;
|
372
|
}
|
373
|
|
374
|
int index2 = mOS.getSecondPointerIndex();
|
375
|
|
376
|
if( index2>=0 )
|
377
|
{
|
378
|
mX2 = mOS.getX(index2) - mMoveX;
|
379
|
mY2 = mOS.getY(index2) + mMoveY;
|
380
|
}
|
381
|
}
|
382
|
|
383
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
384
|
|
385
|
private void prepareUp()
|
386
|
{
|
387
|
mOS.unpressFirst();
|
388
|
mOS.unpressSecond();
|
389
|
}
|
390
|
|
391
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
392
|
|
393
|
private void prepareDown2()
|
394
|
{
|
395
|
if( !mOS.isFirstPressed() )
|
396
|
{
|
397
|
mOS.pressFirst();
|
398
|
mX1 = mOS.getFirstX() - mMoveX;
|
399
|
mY1 = mOS.getFirstY() + mMoveY;
|
400
|
}
|
401
|
else if( !mOS.isSecondPressed() )
|
402
|
{
|
403
|
mOS.pressSecond();
|
404
|
mX2 = mOS.getSecondX() - mMoveX;
|
405
|
mY2 = mOS.getSecondY() + mMoveY;
|
406
|
}
|
407
|
}
|
408
|
|
409
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
410
|
|
411
|
private void prepareUp2()
|
412
|
{
|
413
|
mOS.upOneOfThem();
|
414
|
}
|
415
|
|
416
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
417
|
|
418
|
private void actionMove(float x1, float y1, float x2, float y2)
|
419
|
{
|
420
|
float pX,pY;
|
421
|
|
422
|
if( mOS.isFirstPressed() ) { pX = x1; pY=y1; }
|
423
|
else { pX = x2; pY=y2; }
|
424
|
|
425
|
float x = (pX - mScreenWidth*0.5f)/ mScalingSize;
|
426
|
float y = (mScreenHeight*0.5f -pY)/ mScalingSize;
|
427
|
|
428
|
if( mBeginningRotation )
|
429
|
{
|
430
|
if( retFingerDragDistanceInInches(mX-x,mY-y) > ROTATION_SENSITIVITY )
|
431
|
{
|
432
|
beginRotation(x,y);
|
433
|
}
|
434
|
}
|
435
|
else if( mContinuingRotation )
|
436
|
{
|
437
|
continueRotation(x,y);
|
438
|
}
|
439
|
else if( mDragging )
|
440
|
{
|
441
|
drag(x,y);
|
442
|
}
|
443
|
else
|
444
|
{
|
445
|
setUpDragOrRotate(false,x,y);
|
446
|
}
|
447
|
}
|
448
|
|
449
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
450
|
|
451
|
private void actionDown(float x, float y)
|
452
|
{
|
453
|
mX = (x - mScreenWidth*0.5f)/ mScalingSize;
|
454
|
mY = (mScreenHeight*0.5f - y)/ mScalingSize;
|
455
|
|
456
|
setUpDragOrRotate(true,mX,mY);
|
457
|
}
|
458
|
|
459
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
460
|
|
461
|
private void actionUp()
|
462
|
{
|
463
|
if( mContinuingRotation )
|
464
|
{
|
465
|
finishRotation();
|
466
|
}
|
467
|
|
468
|
if( mLastMode==MODE_REPLACE ) mInterface.onReplaceModeUp();
|
469
|
}
|
470
|
|
471
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
472
|
|
473
|
private void actionDown2(float x1, float y1, float x2, float y2)
|
474
|
{
|
475
|
mRotAngle = getAngle(x1,-y1, x2,-y2);
|
476
|
mInitDistance = -1;
|
477
|
|
478
|
mX = (x1 - mScreenWidth*0.5f )/ mScalingSize;
|
479
|
mY = (mScreenHeight*0.5f - y1)/ mScalingSize;
|
480
|
|
481
|
if( mBeginningRotation )
|
482
|
{
|
483
|
mContinuingRotation = false;
|
484
|
mBeginningRotation = false;
|
485
|
mDragging = true;
|
486
|
}
|
487
|
else if( mContinuingRotation )
|
488
|
{
|
489
|
finishRotation();
|
490
|
}
|
491
|
}
|
492
|
|
493
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
494
|
|
495
|
private void actionUp2(boolean p1isUp, float x1, float y1, boolean p2isUp, float x2, float y2)
|
496
|
{
|
497
|
if( p1isUp )
|
498
|
{
|
499
|
mX = (x2 - mScreenWidth*0.5f)/ mScalingSize;
|
500
|
mY = (mScreenHeight*0.5f - y2)/ mScalingSize;
|
501
|
}
|
502
|
if( p2isUp )
|
503
|
{
|
504
|
mX = (x1 - mScreenWidth*0.5f)/ mScalingSize;
|
505
|
mY = (mScreenHeight*0.5f - y1)/ mScalingSize;
|
506
|
}
|
507
|
}
|
508
|
|
509
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
510
|
|
511
|
int getScalingSize()
|
512
|
{
|
513
|
return mScalingSize;
|
514
|
}
|
515
|
|
516
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
517
|
|
518
|
void setTouchControl(TwistyObject object)
|
519
|
{
|
520
|
if( mLastMode!=MODE_REPLACE ) mTouchControl = object.getTouchControl();
|
521
|
else mTouchControl = new TouchControlShapeChanging(object);
|
522
|
}
|
523
|
|
524
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
525
|
// INTERNAL API (for AutomaticControl)
|
526
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
527
|
|
528
|
public ObjectPreRender getPreRender()
|
529
|
{
|
530
|
return mPreRender;
|
531
|
}
|
532
|
|
533
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
534
|
|
535
|
public ObjectLibInterface getInterface()
|
536
|
{
|
537
|
return mInterface;
|
538
|
}
|
539
|
|
540
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
541
|
// PUBLIC API
|
542
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
543
|
|
544
|
public ObjectControl(OperatingSystemInterface os)
|
545
|
{
|
546
|
mIsAutomatic = false;
|
547
|
|
548
|
mBuffer = new int[2];
|
549
|
mAxis = new float[2];
|
550
|
|
551
|
mQuat= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
|
552
|
mTemp= new Static4D(0,0,0,1);
|
553
|
|
554
|
mAvgRotSpeed = 0.0f;
|
555
|
mLastMode = -1;
|
556
|
mRotateOnCreation = false;
|
557
|
|
558
|
mNormalTouchPoint = new float[2];
|
559
|
|
560
|
mLastA = new float[NUM_SPEED_PROBES];
|
561
|
mLastT = new long[NUM_SPEED_PROBES];
|
562
|
mFirstIndex= 0;
|
563
|
mLastIndex = 0;
|
564
|
mIconMode =-1;
|
565
|
|
566
|
mInterface = os.getInterface();
|
567
|
mOS = os;
|
568
|
|
569
|
mDensity = mOS.getScreenDensity();
|
570
|
mPreRender = new ObjectPreRender(this,mInterface);
|
571
|
}
|
572
|
|
573
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
574
|
|
575
|
public void setRotateOnCreation(boolean rotate)
|
576
|
{
|
577
|
mRotateOnCreation = rotate;
|
578
|
}
|
579
|
|
580
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
581
|
|
582
|
public boolean getRotateOnCreation()
|
583
|
{
|
584
|
return mRotateOnCreation;
|
585
|
}
|
586
|
|
587
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
588
|
|
589
|
public TwistyObjectNode getNode()
|
590
|
{
|
591
|
return mObjectNode;
|
592
|
}
|
593
|
|
594
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
595
|
|
596
|
public void createNode(int width, int height)
|
597
|
{
|
598
|
if( mObjectNode==null ) mObjectNode = new TwistyObjectNode(width,height);
|
599
|
}
|
600
|
|
601
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
602
|
|
603
|
public void setScreenSizeAndScaling(int width, int height, int scaling)
|
604
|
{
|
605
|
mScreenWidth = width;
|
606
|
mScreenHeight= height;
|
607
|
mScalingSize = scaling;
|
608
|
|
609
|
if( mObjectNode!=null ) mObjectNode.setSize(width,height);
|
610
|
|
611
|
TwistyObject object = mPreRender.getObject();
|
612
|
|
613
|
if( object!=null )
|
614
|
{
|
615
|
object.setTexture();
|
616
|
object.setNodeSize(mScalingSize);
|
617
|
}
|
618
|
}
|
619
|
|
620
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
621
|
|
622
|
public void setObjectMove(int xmove, int ymove)
|
623
|
{
|
624
|
mMoveX = xmove;
|
625
|
mMoveY = ymove;
|
626
|
|
627
|
mPreRender.setMove(xmove,ymove);
|
628
|
}
|
629
|
|
630
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
631
|
|
632
|
public void setObjectScale(float scale)
|
633
|
{
|
634
|
mPreRender.setScale(scale);
|
635
|
}
|
636
|
|
637
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
638
|
|
639
|
public void onPause()
|
640
|
{
|
641
|
// BlockController.onPause();
|
642
|
}
|
643
|
|
644
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
645
|
|
646
|
public void onResume()
|
647
|
{
|
648
|
mOS.unpressFirst();
|
649
|
mOS.unpressSecond();
|
650
|
|
651
|
unlock();
|
652
|
|
653
|
// BlockController.onResume();
|
654
|
}
|
655
|
|
656
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
657
|
|
658
|
public void rotateNow(Static4D quat)
|
659
|
{
|
660
|
mTemp.set(quat);
|
661
|
mQuat.set(mTemp);
|
662
|
}
|
663
|
|
664
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
665
|
|
666
|
public void scaleNow(float scale)
|
667
|
{
|
668
|
mPreRender.getObject().setObjectRatioNow(scale,mScalingSize );
|
669
|
}
|
670
|
|
671
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
672
|
|
673
|
public void setQuat()
|
674
|
{
|
675
|
mQuat.set(mTemp);
|
676
|
}
|
677
|
|
678
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
679
|
|
680
|
public Static4D getQuat()
|
681
|
{
|
682
|
return mQuat;
|
683
|
}
|
684
|
|
685
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
686
|
|
687
|
public void preRender()
|
688
|
{
|
689
|
mPreRender.preRender();
|
690
|
}
|
691
|
|
692
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
693
|
|
694
|
public void blockTouch(int place)
|
695
|
{
|
696
|
setLock(true);
|
697
|
mPreRender.blockRotation(place);
|
698
|
}
|
699
|
|
700
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
701
|
|
702
|
public void unblockRotation()
|
703
|
{
|
704
|
unsetLock();
|
705
|
mPreRender.unblockRotation();
|
706
|
}
|
707
|
|
708
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
709
|
|
710
|
public void unblockEverything()
|
711
|
{
|
712
|
mPreRender.unblockEverything();
|
713
|
}
|
714
|
|
715
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
716
|
|
717
|
public boolean isScramblingAndSolvingNotBlocked()
|
718
|
{
|
719
|
return mPreRender.isScramblingAndSolvingNotBlocked();
|
720
|
}
|
721
|
|
722
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
723
|
|
724
|
public void initializeObject(int[][] moves)
|
725
|
{
|
726
|
mPreRender.initializeObject(moves);
|
727
|
}
|
728
|
|
729
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
730
|
|
731
|
public void changeObject(int ordinal, int iconMode, InitAssets asset)
|
732
|
{
|
733
|
mPreRender.changeObject(ordinal, iconMode, asset);
|
734
|
}
|
735
|
|
736
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
737
|
|
738
|
public void changeIfDifferent(int ordinal, String newName, int iconMode, InitAssets asset)
|
739
|
{
|
740
|
TwistyObject object = mPreRender.getObject();
|
741
|
String oldName = object==null ? "" : object.getShortName();
|
742
|
|
743
|
if( !oldName.equals(newName.toUpperCase()) || mIconMode!=iconMode )
|
744
|
{
|
745
|
mIconMode = iconMode;
|
746
|
mPreRender.changeObject(ordinal, iconMode, asset);
|
747
|
}
|
748
|
}
|
749
|
|
750
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
751
|
// if one or more fingers currently touch the screen, and we just pressed the 'scramble' button, do
|
752
|
// not scramble - otherwise a kind of a cheat is possible where user touches the screen, starts
|
753
|
// scrambling, then lifts the finger and the act of lifting screws the scrambling - no further
|
754
|
// scrambles take any effect!
|
755
|
|
756
|
public boolean scrambleObject(int num)
|
757
|
{
|
758
|
if( !mBeginningRotation && !mContinuingRotation )
|
759
|
{
|
760
|
return mPreRender.scrambleObject(num);
|
761
|
}
|
762
|
return false;
|
763
|
}
|
764
|
|
765
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
766
|
// ditto
|
767
|
|
768
|
public boolean fastScrambleObject(int duration, int num)
|
769
|
{
|
770
|
if( !mBeginningRotation && !mContinuingRotation )
|
771
|
{
|
772
|
return mPreRender.fastScrambleObject(duration,num);
|
773
|
}
|
774
|
return false;
|
775
|
}
|
776
|
|
777
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
778
|
|
779
|
public void presentObject(int num, int duration)
|
780
|
{
|
781
|
mPreRender.presentObject(num,duration);
|
782
|
}
|
783
|
|
784
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
785
|
|
786
|
public void solveObject()
|
787
|
{
|
788
|
mPreRender.solveObject();
|
789
|
}
|
790
|
|
791
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
792
|
|
793
|
public void solveOnly()
|
794
|
{
|
795
|
mPreRender.solveOnly();
|
796
|
}
|
797
|
|
798
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
799
|
|
800
|
public void resetTextureMapsEffect(int duration)
|
801
|
{
|
802
|
mPreRender.resetTextureMapsEffect(duration);
|
803
|
}
|
804
|
|
805
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
806
|
|
807
|
public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, int duration)
|
808
|
{
|
809
|
mPreRender.addRotation(listener,axis,rowBitmap,angle,duration);
|
810
|
}
|
811
|
|
812
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
813
|
|
814
|
public void resetAllTextureMaps()
|
815
|
{
|
816
|
mPreRender.resetAllTextureMaps();
|
817
|
}
|
818
|
|
819
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
820
|
|
821
|
public void recreateSticker(float borders, float corners)
|
822
|
{
|
823
|
TwistyObject object = mPreRender.getObject();
|
824
|
object.createTexture(borders,corners);
|
825
|
object.setTexture();
|
826
|
}
|
827
|
|
828
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
829
|
|
830
|
public TwistyObject getObject()
|
831
|
{
|
832
|
return mPreRender.getObject();
|
833
|
}
|
834
|
|
835
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
836
|
|
837
|
public void savePreferences()
|
838
|
{
|
839
|
mPreRender.savePreferences(mOS);
|
840
|
|
841
|
for( int i=0; i< BaseEffect.Type.LENGTH; i++ )
|
842
|
{
|
843
|
BaseEffect.Type.getType(i).savePreferences(mOS);
|
844
|
}
|
845
|
}
|
846
|
|
847
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
848
|
|
849
|
public void restorePreferences()
|
850
|
{
|
851
|
mPreRender.restorePreferences(mOS);
|
852
|
|
853
|
for (int i=0; i<BaseEffect.Type.LENGTH; i++)
|
854
|
{
|
855
|
BaseEffect.Type.getType(i).restorePreferences(mOS);
|
856
|
}
|
857
|
}
|
858
|
|
859
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
860
|
|
861
|
public boolean retLocked()
|
862
|
{
|
863
|
return mIsLocked;
|
864
|
}
|
865
|
|
866
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
867
|
|
868
|
public void toggleLock()
|
869
|
{
|
870
|
mIsLocked = !mIsLocked;
|
871
|
}
|
872
|
|
873
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
874
|
|
875
|
public void unlock()
|
876
|
{
|
877
|
mIsLocked = false;
|
878
|
}
|
879
|
|
880
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
881
|
|
882
|
public void setLock(boolean value)
|
883
|
{
|
884
|
mRemLocked = mIsLocked;
|
885
|
mIsLocked = value;
|
886
|
}
|
887
|
|
888
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
889
|
|
890
|
public void unsetLock()
|
891
|
{
|
892
|
mIsLocked = mRemLocked;
|
893
|
}
|
894
|
|
895
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
896
|
|
897
|
public void setTextureMap(int cubit, int face, int newColor)
|
898
|
{
|
899
|
mPreRender.setTextureMap(cubit,face,newColor);
|
900
|
}
|
901
|
|
902
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
903
|
|
904
|
public void repaintPuzzleFace(int cubit, int face, int color)
|
905
|
{
|
906
|
mPreRender.repaintPuzzleFace(cubit,face,color);
|
907
|
}
|
908
|
|
909
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
910
|
|
911
|
public boolean onTouchEvent(int mode)
|
912
|
{
|
913
|
if( mode!=MODE_NOTHING )
|
914
|
{
|
915
|
if( mObjectNode==null ) return true;
|
916
|
|
917
|
if( mode!=mLastMode )
|
918
|
{
|
919
|
TwistyObject object = getObject();
|
920
|
|
921
|
if( object!=null )
|
922
|
{
|
923
|
mLastMode = mode;
|
924
|
setTouchControl(object);
|
925
|
}
|
926
|
else return true;
|
927
|
}
|
928
|
|
929
|
int action = mOS.getAction();
|
930
|
|
931
|
switch(action)
|
932
|
{
|
933
|
case ACTION_DOWN_1: prepareDown();
|
934
|
actionDown(mX1, mY1);
|
935
|
break;
|
936
|
case ACTION_MOVE : prepareMove();
|
937
|
actionMove(mX1, mY1, mX2, mY2);
|
938
|
break;
|
939
|
case ACTION_UP_1 : prepareUp();
|
940
|
actionUp();
|
941
|
break;
|
942
|
case ACTION_DOWN_2: prepareDown2();
|
943
|
actionDown2(mX1, mY1, mX2, mY2);
|
944
|
break;
|
945
|
case ACTION_UP_2 : prepareUp2();
|
946
|
boolean p1isUp = !mOS.isFirstPressed();
|
947
|
boolean p2isUp = !mOS.isSecondPressed();
|
948
|
actionUp2(p1isUp, mX1, mY1, p2isUp, mX2, mY2);
|
949
|
break;
|
950
|
}
|
951
|
}
|
952
|
|
953
|
return true;
|
954
|
}
|
955
|
}
|
956
|
|