Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / ObjectControl.java @ 880beeea

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
import java.lang.ref.WeakReference;
23
24
import android.util.DisplayMetrics;
25
import android.view.MotionEvent;
26
27
import org.distorted.library.main.QuatHelper;
28
import org.distorted.library.type.Static2D;
29
import org.distorted.library.type.Static4D;
30
31
import org.distorted.objectlib.helpers.ObjectStateActioner;
32
import org.distorted.objectlib.helpers.ObjectSurfaceView;
33
import org.distorted.objectlib.helpers.TwistyActivity;
34
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
37
public class ObjectControl
38
{
39
    public static final int NUM_SPEED_PROBES = 10;
40
    public static final int INVALID_POINTER_ID = -1;
41
42
    public static final int MODE_ROTATE  = 0;
43
    public static final int MODE_DRAG    = 1;
44
    public static final int MODE_REPLACE = 2;
45
46
    // Moving the finger from the middle of the vertical screen to the right edge will rotate a
47
    // given face by SWIPING_SENSITIVITY/2 degrees.
48
    public final static int SWIPING_SENSITIVITY  = 240;
49
    // Moving the finger by 0.3 of an inch will start a Rotation.
50
    public final static float ROTATION_SENSITIVITY = 0.3f;
51
52
    private final Static4D CAMERA_POINT = new Static4D(0, 0, 0, 0);
53
54
    private final WeakReference<TwistyActivity> mAct;
55
    private final ObjectStateActioner mActioner;
56
    private final ObjectPreRender mPreRender;
57
    private Movement mMovement;
58
    private boolean mDragging, mBeginningRotation, mContinuingRotation;
59
    private int mScreenWidth, mScreenHeight, mScreenMin;
60
61
    private float mRotAngle, mInitDistance;
62
    private float mStartRotX, mStartRotY;
63
    private float mAxisX, mAxisY;
64
    private float mRotationFactor;
65
    private int mLastCubitColor, mLastCubitFace, mLastCubit;
66
    private int mCurrentAxis, mCurrentRow;
67
    private float mCurrentAngle, mCurrRotSpeed;
68
    private final float[] mLastX;
69
    private final float[] mLastY;
70
    private final long[] mLastT;
71
    private int mFirstIndex, mLastIndex;
72
    private final int mDensity;
73
74
    private int mPointer1, mPointer2;
75
    private float mX1, mY1, mX2, mY2, mX, mY;
76
    private boolean mIsAutomatic;
77
78
    private static final Static4D mQuat= new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
79
    private static final Static4D mTemp= new Static4D(0,0,0,1);
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
// cast the 3D axis we are currently rotating along (which is already casted to the surface of the
83
// currently touched face AND converted into a 4D vector - fourth 0) to a 2D in-screen-surface axis
84
85
    private void computeCurrentAxis(Static4D axis)
86
      {
87
      Static4D result = QuatHelper.rotateVectorByQuat(axis, mQuat);
88
89
      mAxisX =result.get0();
90
      mAxisY =result.get1();
91
92
      float len = (float)Math.sqrt(mAxisX*mAxisX + mAxisY*mAxisY);
93
      mAxisX /= len;
94
      mAxisY /= len;
95
      }
96
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99
    private void addSpeedProbe(float x, float y)
100
      {
101
      long currTime = System.currentTimeMillis();
102
      boolean theSame = mLastIndex==mFirstIndex;
103
104
      mLastIndex++;
105
      if( mLastIndex>=NUM_SPEED_PROBES ) mLastIndex=0;
106
107
      mLastT[mLastIndex] = currTime;
108
      mLastX[mLastIndex] = x;
109
      mLastY[mLastIndex] = y;
110
111
      if( mLastIndex==mFirstIndex)
112
        {
113
        mFirstIndex++;
114
        if( mFirstIndex>=NUM_SPEED_PROBES ) mFirstIndex=0;
115
        }
116
117
      if( theSame )
118
        {
119
        mLastT[mFirstIndex] = currTime;
120
        mLastX[mFirstIndex] = x;
121
        mLastY[mFirstIndex] = y;
122
        }
123
      }
124
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
127
    private void computeCurrentSpeedInInchesPerSecond()
128
      {
129
      long firstTime = mLastT[mFirstIndex];
130
      long lastTime  = mLastT[mLastIndex];
131
      float fX = mLastX[mFirstIndex];
132
      float fY = mLastY[mFirstIndex];
133
      float lX = mLastX[mLastIndex];
134
      float lY = mLastY[mLastIndex];
135
136
      long timeDiff = lastTime-firstTime;
137
138
      mLastIndex = 0;
139
      mFirstIndex= 0;
140
141
      mCurrRotSpeed = timeDiff>0 ? 1000*retFingerDragDistanceInInches(fX,fY,lX,lY)/timeDiff : 0;
142
      }
143
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146
    private float retFingerDragDistanceInInches(float xFrom, float yFrom, float xTo, float yTo)
147
      {
148
      float xDist = mScreenWidth*(xFrom-xTo);
149
      float yDist = mScreenHeight*(yFrom-yTo);
150
      float distInPixels = (float)Math.sqrt(xDist*xDist + yDist*yDist);
151
152
      return distInPixels/mDensity;
153
      }
154
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
157
    private void setUpDragOrRotate(boolean down, float x, float y, int mode)
158
      {
159
      if( mode==MODE_DRAG )
160
        {
161
        mDragging           = true;
162
        mBeginningRotation  = false;
163
        mContinuingRotation = false;
164
        }
165
      else
166
        {
167
        TwistyObject object = mPreRender.getObject();
168
        CAMERA_POINT.set2( object==null ? 1.21f : object.getCameraDist() );
169
170
        Static4D touchPoint = new Static4D(x, y, 0, 0);
171
        Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, mQuat);
172
        Static4D rotatedCamera= QuatHelper.rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
173
174
        if( object!=null && mMovement!=null && mMovement.faceTouched(rotatedTouchPoint,rotatedCamera,object.getObjectRatio() ) )
175
          {
176
          mDragging           = false;
177
          mContinuingRotation = false;
178
179
          if( mode==MODE_ROTATE )
180
            {
181
            mBeginningRotation= !mPreRender.isTouchBlocked();
182
            }
183
          else if( mode==MODE_REPLACE )
184
            {
185
            mBeginningRotation= false;
186
187
            if( down )
188
              {
189
              int color = mActioner.getCurrentColor();
190
              mLastCubitFace = mMovement.getTouchedFace();
191
              float[] point = mMovement.getTouchedPoint3D();
192
              mLastCubit = object.getCubit(point);
193
              mPreRender.setTextureMap( mLastCubit, mLastCubitFace, color );
194
              mLastCubitColor = mActioner.cubitIsLocked(object.getObjectType(),mLastCubit);
195
              }
196
            }
197
          }
198
        else
199
          {
200
          final TwistyActivity act = mAct.get();
201
          final boolean locked= act.isLocked();
202
          mDragging           = (!locked || mIsAutomatic);
203
          mBeginningRotation  = false;
204
          mContinuingRotation = false;
205
          if( !mDragging ) mActioner.failedToDrag(act);
206
          }
207
        }
208
      }
209
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212
    private void drag(float x, float y)
213
      {
214
      if( mPointer1!=INVALID_POINTER_ID && mPointer2!=INVALID_POINTER_ID)
215
        {
216
        float x2 = (mX2 - mScreenWidth*0.5f)/mScreenMin;
217
        float y2 = (mScreenHeight*0.5f - mY2)/mScreenMin;
218
219
        float angleNow = getAngle(x,y,x2,y2);
220
        float angleDiff = angleNow-mRotAngle;
221
        float sinA =-(float)Math.sin(angleDiff);
222
        float cosA = (float)Math.cos(angleDiff);
223
224
        Static4D dragQuat = QuatHelper.quatMultiply(new Static4D(0,0,sinA,cosA), mQuat);
225
        mTemp.set(dragQuat);
226
227
        mRotAngle = angleNow;
228
229
        float distNow  = (float)Math.sqrt( (x-x2)*(x-x2) + (y-y2)*(y-y2) );
230
        float distQuot = mInitDistance<0 ? 1.0f : distNow/ mInitDistance;
231
        mInitDistance = distNow;
232
        TwistyObject object = mPreRender.getObject();
233
        if( object!=null ) object.setObjectRatio(distQuot);
234
        }
235
      else
236
        {
237
        Static4D dragQuat = QuatHelper.quatMultiply(QuatHelper.quatFromDrag(mX-x,y-mY), mQuat);
238
        mTemp.set(dragQuat);
239
        }
240
241
      mPreRender.setQuatOnNextRender();
242
      mX = x;
243
      mY = y;
244
      }
245
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
248
    private void finishRotation()
249
      {
250
      computeCurrentSpeedInInchesPerSecond();
251
      int angle = mPreRender.getObject().computeNearestAngle(mCurrentAxis,mCurrentAngle, mCurrRotSpeed);
252
      mPreRender.finishRotation(angle);
253
      mPreRender.rememberMove(mCurrentAxis,mCurrentRow,angle);
254
255
      if( angle!=0 )
256
        {
257
        TwistyActivity act = mAct.get();
258
        mActioner.onFinishRotation(act,mCurrentAxis,mCurrentRow,angle);
259
        }
260
261
      mContinuingRotation = false;
262
      mBeginningRotation  = false;
263
      mDragging           = true;
264
      }
265
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268
    private void continueRotation(float x, float y)
269
      {
270
      float dx = x-mStartRotX;
271
      float dy = y-mStartRotY;
272
      float alpha = dx*mAxisX + dy*mAxisY;
273
      float x2 = dx - alpha*mAxisX;
274
      float y2 = dy - alpha*mAxisY;
275
276
      float len = (float)Math.sqrt(x2*x2 + y2*y2);
277
278
      // we have the length of 1D vector 'angle', now the direction:
279
      float tmp = mAxisY==0 ? -mAxisX*y2 : mAxisY*x2;
280
281
      float angle = (tmp>0 ? 1:-1)*len*mRotationFactor;
282
      mCurrentAngle = SWIPING_SENSITIVITY*angle;
283
      mPreRender.getObject().continueRotation(mCurrentAngle);
284
285
      addSpeedProbe(x2,y2);
286
      }
287
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
290
    private void beginRotation(float x, float y)
291
      {
292
      mStartRotX = x;
293
      mStartRotY = y;
294
295
      TwistyObject object = mPreRender.getObject();
296
      int numLayers = object.getNumLayers();
297
298
      Static4D touchPoint2 = new Static4D(x, y, 0, 0);
299
      Static4D rotatedTouchPoint2= QuatHelper.rotateVectorByInvertedQuat(touchPoint2, mQuat);
300
      Static2D res = mMovement.newRotation(rotatedTouchPoint2,object.getObjectRatio());
301
302
      mCurrentAxis = (int)res.get0();
303
      mCurrentRow  = (int)res.get1();
304
305
      computeCurrentAxis( mMovement.getCastedRotAxis(mCurrentAxis) );
306
      mRotationFactor = mMovement.returnRotationFactor(numLayers,mCurrentRow);
307
308
      object.beginNewRotation( mCurrentAxis, mCurrentRow );
309
310
      TwistyActivity act = mAct.get();
311
      mActioner.onBeginRotation(act);
312
313
      addSpeedProbe(x,y);
314
315
      mBeginningRotation = false;
316
      mContinuingRotation= true;
317
      }
318
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320
321
    private float getAngle(float x1, float y1, float x2, float y2)
322
      {
323
      return (float) Math.atan2(y1-y2, x1-x2);
324
      }
325
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327
328
    private void prepareDown(MotionEvent event)
329
      {
330
      mPointer1 = event.getPointerId(0);
331
      mX1 = event.getX();
332
      mY1 = event.getY();
333
      mPointer2 = INVALID_POINTER_ID;
334
      }
335
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337
338
    private void prepareMove(MotionEvent event)
339
      {
340
      int index1 = event.findPointerIndex(mPointer1);
341
342
      if( index1>=0 )
343
        {
344
        mX1 = event.getX(index1);
345
        mY1 = event.getY(index1);
346
        }
347
348
      int index2 = event.findPointerIndex(mPointer2);
349
350
      if( index2>=0 )
351
        {
352
        mX2 = event.getX(index2);
353
        mY2 = event.getY(index2);
354
        }
355
      }
356
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358
359
    private void prepareUp(MotionEvent event)
360
      {
361
      mPointer1 = INVALID_POINTER_ID;
362
      mPointer2 = INVALID_POINTER_ID;
363
      }
364
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366
367
    private void prepareDown2(MotionEvent event)
368
      {
369
      int index = event.getActionIndex();
370
371
      if( mPointer1==INVALID_POINTER_ID )
372
        {
373
        mPointer1 = event.getPointerId(index);
374
        mX1 = event.getX(index);
375
        mY1 = event.getY(index);
376
        }
377
      else if( mPointer2==INVALID_POINTER_ID )
378
        {
379
        mPointer2 = event.getPointerId(index);
380
        mX2 = event.getX(index);
381
        mY2 = event.getY(index);
382
        }
383
      }
384
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386
387
    private void prepareUp2(MotionEvent event)
388
      {
389
      int index = event.getActionIndex();
390
391
           if( index==event.findPointerIndex(mPointer1) ) mPointer1 = INVALID_POINTER_ID;
392
      else if( index==event.findPointerIndex(mPointer2) ) mPointer2 = INVALID_POINTER_ID;
393
      }
394
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396
397
    private void actionMove(float x1, float y1, float x2, float y2, int mode)
398
      {
399
      float pX = mPointer1 != INVALID_POINTER_ID ? x1 : x2;
400
      float pY = mPointer1 != INVALID_POINTER_ID ? y1 : y2;
401
402
      float x = (pX - mScreenWidth*0.5f)/mScreenMin;
403
      float y = (mScreenHeight*0.5f -pY)/mScreenMin;
404
405
      if( mBeginningRotation )
406
        {
407
        if( retFingerDragDistanceInInches(mX,mY,x,y) > ROTATION_SENSITIVITY )
408
          {
409
          beginRotation(x,y);
410
          }
411
        }
412
      else if( mContinuingRotation )
413
        {
414
        continueRotation(x,y);
415
        }
416
      else if( mDragging )
417
        {
418
        drag(x,y);
419
        }
420
      else
421
        {
422
        setUpDragOrRotate(false,x,y,mode);
423
        }
424
      }
425
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427
428
    private void actionDown(float x, float y, int mode)
429
      {
430
      mX = (x -  mScreenWidth*0.5f)/mScreenMin;
431
      mY = (mScreenHeight*0.5f - y)/mScreenMin;
432
433
      setUpDragOrRotate(true,mX,mY,mode);
434
      }
435
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437
438
    private void actionUp()
439
      {
440
      if( mContinuingRotation )
441
        {
442
        finishRotation();
443
        }
444
445
      if( mLastCubitColor>=0 )
446
        {
447
        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
448
        mLastCubitColor = -1;
449
        }
450
      }
451
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453
454
    private void actionDown2(float x1, float y1, float x2, float y2)
455
      {
456
      mRotAngle = getAngle(x1,-y1, x2,-y2);
457
      mInitDistance = -1;
458
459
      mX = (x1 - mScreenWidth*0.5f )/mScreenMin;
460
      mY = (mScreenHeight*0.5f - y1)/mScreenMin;
461
462
      if( mBeginningRotation )
463
        {
464
        mContinuingRotation = false;
465
        mBeginningRotation  = false;
466
        mDragging           = true;
467
        }
468
      else if( mContinuingRotation )
469
        {
470
        finishRotation();
471
        }
472
      }
473
474
///////////////////////////////////////////////////////////////////////////////////////////////////
475
476
    private void actionUp2(boolean p1isUp, float x1, float y1, boolean p2isUp, float x2, float y2)
477
      {
478
      if( p1isUp )
479
        {
480
        mX = (x2 -  mScreenWidth*0.5f)/mScreenMin;
481
        mY = (mScreenHeight*0.5f - y2)/mScreenMin;
482
        }
483
      if( p2isUp )
484
        {
485
        mX = (x1 -  mScreenWidth*0.5f)/mScreenMin;
486
        mY = (mScreenHeight*0.5f - y1)/mScreenMin;
487
        }
488
      }
489
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491
// PUBLIC API
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493
494
    public ObjectControl(TwistyActivity act, ObjectSurfaceView view, ObjectStateActioner actioner)
495
      {
496
      mIsAutomatic = false;
497
498
      mLastCubitColor = -1;
499
      mCurrRotSpeed   = 0.0f;
500
501
      mLastX = new float[NUM_SPEED_PROBES];
502
      mLastY = new float[NUM_SPEED_PROBES];
503
      mLastT = new long[NUM_SPEED_PROBES];
504
      mFirstIndex =0;
505
      mLastIndex  =0;
506
507
      DisplayMetrics dm = new DisplayMetrics();
508
      act.getWindowManager().getDefaultDisplay().getMetrics(dm);
509
510
      mDensity = dm.densityDpi;
511
512
      mPreRender = new ObjectPreRender(view,actioner);
513
      mAct = new WeakReference<>(act);
514
      mActioner = actioner;
515
      }
516
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518
519
    public void setScreenSize(int width, int height)
520
      {
521
      mScreenWidth = width;
522
      mScreenHeight= height;
523
524
      mScreenMin = Math.min(width, height);
525
      }
526
527
///////////////////////////////////////////////////////////////////////////////////////////////////
528
529
    public void initialize()
530
      {
531
      mPointer1 = INVALID_POINTER_ID;
532
      mPointer2 = INVALID_POINTER_ID;
533
      }
534
535
///////////////////////////////////////////////////////////////////////////////////////////////////
536
537
    public void setQuat()
538
      {
539
      mQuat.set(mTemp);
540
      }
541
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543
544
    public Static4D getQuat()
545
      {
546
      return mQuat;
547
      }
548
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
551
    public void setMovement(Movement movement)
552
      {
553
      mMovement = movement;
554
      }
555
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557
558
    public ObjectPreRender getPreRender()
559
      {
560
      return mPreRender;
561
      }
562
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564
565
    public void prepareDown()
566
      {
567
      mIsAutomatic = true;
568
      mPointer1 = 0;
569
      mPointer2 = INVALID_POINTER_ID;
570
      }
571
572
///////////////////////////////////////////////////////////////////////////////////////////////////
573
574
    public void prepareDown2()
575
      {
576
      mPointer2 = 0;
577
      }
578
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580
581
    public void prepareUp()
582
      {
583
      mIsAutomatic = false;
584
      mPointer1 = INVALID_POINTER_ID;
585
      mPointer2 = INVALID_POINTER_ID;
586
      }
587
588
///////////////////////////////////////////////////////////////////////////////////////////////////
589
590
    public void prepareMove(float x1, float y1, float x2, float y2)
591
      {
592
      mX1 = x1;
593
      mY1 = y1;
594
      mX2 = x2;
595
      mY2 = y2;
596
      }
597
598
///////////////////////////////////////////////////////////////////////////////////////////////////
599
600
    public boolean onTouchEvent(MotionEvent event, int mode)
601
      {
602
      int action = event.getActionMasked();
603
604
      switch(action)
605
         {
606
         case MotionEvent.ACTION_DOWN        : prepareDown(event);
607
                                               actionDown(mX1, mY1, mode);
608
                                               break;
609
         case MotionEvent.ACTION_MOVE        : prepareMove(event);
610
                                               actionMove(mX1, mY1, mX2, mY2, mode);
611
                                               break;
612
         case MotionEvent.ACTION_UP          : prepareUp(event);
613
                                               actionUp();
614
                                               break;
615
         case MotionEvent.ACTION_POINTER_DOWN: prepareDown2(event);
616
                                               actionDown2(mX1, mY1, mX2, mY2);
617
                                               break;
618
         case MotionEvent.ACTION_POINTER_UP  : prepareUp2(event);
619
                                               boolean p1isUp = mPointer1==INVALID_POINTER_ID;
620
                                               boolean p2isUp = mPointer2==INVALID_POINTER_ID;
621
                                               actionUp2(p1isUp, mX1, mY1, p2isUp, mX2, mY2);
622
                                               break;
623
         }
624
625
      return true;
626
      }
627
}