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