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