Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectControl.java @ 23afe4c4

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