Project

General

Profile

Download (28.1 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectControl.java @ cf93ea4e

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