Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectControl.java @ 45e0065d

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