commit f0533889d3b931653701e249bc46134d105cce71
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Wed Jun 17 21:57:12 2020 +0100

    Split up the onTouch() part of the View.

diff --git a/src/main/java/org/distorted/main/RubikSurfaceView.java b/src/main/java/org/distorted/main/RubikSurfaceView.java
index 84b5a446..c0fd8ab3 100644
--- a/src/main/java/org/distorted/main/RubikSurfaceView.java
+++ b/src/main/java/org/distorted/main/RubikSurfaceView.java
@@ -180,59 +180,6 @@ public class RubikSurfaceView extends GLSurfaceView
       return new Static4D(0f, 0f, 0f, 1f);
       }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    private void setUpDragOrRotate(boolean down, float x, float y)
-      {
-      int mode = RubikState.getMode();
-
-      if( mode==MODE_DRAG )
-        {
-        mDragging           = true;
-        mBeginningRotation  = false;
-        mContinuingRotation = false;
-        }
-      else
-        {
-        Static4D touchPoint1 = new Static4D(x, y, 0, 0);
-        Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
-        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
-
-        if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
-          {
-          mDragging           = false;
-          mContinuingRotation = false;
-
-          if( mode==MODE_ROTATE )
-            {
-            mBeginningRotation= mPreRender.canRotate();
-            }
-          else if( mode==MODE_REPLACE )
-            {
-            mBeginningRotation= false;
-
-            if( down )
-              {
-              RubikStateSolver solver = (RubikStateSolver) RubikState.SVER.getStateClass();
-              mLastCubitFace = mMovement.getTouchedFace();
-              float[] point = mMovement.getTouchedPoint3D();
-              int color = solver.getCurrentColor();
-              RubikObject object = mPreRender.getObject();
-              mLastCubit = object.getCubit(point);
-              mPreRender.setTextureMap( mLastCubit, mLastCubitFace, color );
-              mLastCubitColor = SolverMain.cubitIsLocked(object.getObjectList(), object.getSize(), mLastCubit);
-              }
-            }
-          }
-        else
-          {
-          mDragging           = true;
-          mBeginningRotation  = false;
-          mContinuingRotation = false;
-          }
-        }
-      }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // cast the 3D axis we are currently rotating along to the 2D in-screen-surface axis
 
@@ -363,6 +310,190 @@ public class RubikSurfaceView extends GLSurfaceView
       mCurrRotSpeed = timeDiff>0 ? (lastAngle-firstAngle)/timeDiff : 0;
       }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    private boolean canBeginRotate(float x, float y)
+      {
+      return (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    private boolean shouldChangeDirection(float x, float y)
+      {
+      return (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    private void setUpDragOrRotate(boolean down, float x, float y)
+      {
+      int mode = RubikState.getMode();
+
+      if( mode==MODE_DRAG )
+        {
+        mDragging           = true;
+        mBeginningRotation  = false;
+        mContinuingRotation = false;
+        }
+      else
+        {
+        Static4D touchPoint1 = new Static4D(x, y, 0, 0);
+        Static4D rotatedTouchPoint1= rotateVectorByInvertedQuat(touchPoint1, mQuatAccumulated);
+        Static4D rotatedCamera= rotateVectorByInvertedQuat(CAMERA_POINT, mQuatAccumulated);
+
+        if( mMovement!=null && mMovement.faceTouched(rotatedTouchPoint1,rotatedCamera) )
+          {
+          mDragging           = false;
+          mContinuingRotation = false;
+
+          if( mode==MODE_ROTATE )
+            {
+            mBeginningRotation= mPreRender.canRotate();
+            }
+          else if( mode==MODE_REPLACE )
+            {
+            mBeginningRotation= false;
+
+            if( down )
+              {
+              RubikStateSolver solver = (RubikStateSolver) RubikState.SVER.getStateClass();
+              mLastCubitFace = mMovement.getTouchedFace();
+              float[] point = mMovement.getTouchedPoint3D();
+              int color = solver.getCurrentColor();
+              RubikObject object = mPreRender.getObject();
+              mLastCubit = object.getCubit(point);
+              mPreRender.setTextureMap( mLastCubit, mLastCubitFace, color );
+              mLastCubitColor = SolverMain.cubitIsLocked(object.getObjectList(), object.getSize(), mLastCubit);
+              }
+            }
+          }
+        else
+          {
+          mDragging           = true;
+          mBeginningRotation  = false;
+          mContinuingRotation = false;
+          }
+        }
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    private void actionDown(float x, float y)
+      {
+      mX = x;
+      mY = y;
+      setUpDragOrRotate(true,x,y);
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    private void actionMove(float x, float y)
+      {
+      if( mBeginningRotation )
+        {
+        if( canBeginRotate(x,y) )
+          {
+          mStartRotX = x;
+          mStartRotY = y;
+
+          Static4D touchPoint2 = new Static4D(x, y, 0, 0);
+          Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
+
+          Static2D res = mMovement.newRotation(rotatedTouchPoint2);
+          RubikObject object = mPreRender.getObject();
+
+          mCurrentAxis = (int)res.get0();
+          float offset = res.get1();
+          mCurrentRow = (int)(object.returnMultiplier()*offset);
+          computeCurrentAxis( object.getRotationAxis()[mCurrentAxis] );
+          mRotationFactor = object.returnRotationFactor(offset);
+
+          object.beginNewRotation( mCurrentAxis, mCurrentRow );
+
+          if( RubikState.getCurrentState()==RubikState.READ )
+            {
+            RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
+            solving.resetElapsed();
+
+            final RubikActivity act = (RubikActivity)getContext();
+
+            act.runOnUiThread(new Runnable()
+              {
+              @Override
+              public void run()
+                {
+                RubikState.switchState( act, RubikState.SOLV);
+                }
+              });
+            }
+
+          addSpeedProbe(0.0f);
+
+          mBeginningRotation = false;
+          mContinuingRotation= true;
+          }
+        }
+      else if( mContinuingRotation )
+        {
+        float angle = continueRotation(x-mStartRotX,y-mStartRotY);
+        mCurrentAngle = SWIPING_SENSITIVITY*angle;
+        mPreRender.getObject().continueRotation(mCurrentAngle);
+
+        addSpeedProbe(mCurrentAngle);
+        }
+      else if( mDragging )
+        {
+        mTempCurrent.set(quatFromDrag(mX-x,y-mY));
+        mPreRender.setQuatCurrentOnNextRender();
+
+        if( shouldChangeDirection(x,y) )
+          {
+          mX = x;
+          mY = y;
+          mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
+          mTempCurrent.set(0f, 0f, 0f, 1f);
+          mPreRender.setQuatCurrentOnNextRender();
+          mPreRender.setQuatAccumulatedOnNextRender();
+          }
+        }
+      else
+        {
+        setUpDragOrRotate(false,x,y);
+        }
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    private void actionUp()
+      {
+      if( mDragging )
+        {
+        mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
+        mTempCurrent.set(0f, 0f, 0f, 1f);
+        mPreRender.setQuatCurrentOnNextRender();
+        mPreRender.setQuatAccumulatedOnNextRender();
+        }
+
+      if( mContinuingRotation )
+        {
+        computeCurrentSpeed();
+        int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
+        mPreRender.finishRotation(angle);
+
+        if( RubikState.getCurrentState()==RubikState.SOLV && angle!=0 )
+          {
+          RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
+          solving.addMove(mCurrentAxis, mCurrentRow, angle);
+          }
+        }
+
+      if( mLastCubitColor>=0 )
+        {
+        mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
+        }
+      }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -402,109 +533,9 @@ public class RubikSurfaceView extends GLSurfaceView
 
       switch(action)
          {
-         case MotionEvent.ACTION_DOWN: mX = x;
-                                       mY = y;
-                                       setUpDragOrRotate(true,x,y);
-                                       break;
-         case MotionEvent.ACTION_MOVE: if( mBeginningRotation )
-                                         {
-                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > 1.0f/(ROTATION_SENSITIVITY*ROTATION_SENSITIVITY) )
-                                           {
-                                           mStartRotX = x;
-                                           mStartRotY = y;
-
-                                           Static4D touchPoint2 = new Static4D(x, y, 0, 0);
-                                           Static4D rotatedTouchPoint2= rotateVectorByInvertedQuat(touchPoint2, mQuatAccumulated);
-
-                                           Static2D res = mMovement.newRotation(rotatedTouchPoint2);
-                                           RubikObject object = mPreRender.getObject();
-
-                                           mCurrentAxis = (int)res.get0();
-                                           float offset = res.get1();
-                                           mCurrentRow = (int)(object.returnMultiplier()*offset);
-                                           computeCurrentAxis( object.getRotationAxis()[mCurrentAxis] );
-                                           mRotationFactor = object.returnRotationFactor(offset);
-
-                                           object.beginNewRotation( mCurrentAxis, mCurrentRow );
-
-                                           if( RubikState.getCurrentState()==RubikState.READ )
-                                             {
-                                             RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
-                                             solving.resetElapsed();
-
-                                             final RubikActivity act = (RubikActivity)getContext();
-
-                                             act.runOnUiThread(new Runnable()
-                                               {
-                                               @Override
-                                               public void run()
-                                                 {
-                                                 RubikState.switchState( act, RubikState.SOLV);
-                                                 }
-                                               });
-                                             }
-
-                                           addSpeedProbe(0.0f);
-
-                                           mBeginningRotation = false;
-                                           mContinuingRotation= true;
-                                           }
-                                         }
-                                       else if( mContinuingRotation )
-                                         {
-                                         float angle = continueRotation(x-mStartRotX,y-mStartRotY);
-                                         mCurrentAngle = SWIPING_SENSITIVITY*angle;
-                                         mPreRender.getObject().continueRotation(mCurrentAngle);
-
-                                         addSpeedProbe(mCurrentAngle);
-                                         }
-                                       else if( mDragging )
-                                         {
-                                         mTempCurrent.set(quatFromDrag(mX-x,y-mY));
-                                         mPreRender.setQuatCurrentOnNextRender();
-
-                                         if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > 1.0f/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
-                                           {
-                                           mX = x;
-                                           mY = y;
-                                           mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
-                                           mTempCurrent.set(0f, 0f, 0f, 1f);
-                                           mPreRender.setQuatCurrentOnNextRender();
-                                           mPreRender.setQuatAccumulatedOnNextRender();
-                                           }
-                                         }
-                                       else
-                                         {
-                                         setUpDragOrRotate(false,x,y);
-                                         }
-                                       break;
-         case MotionEvent.ACTION_UP  : if( mDragging )
-                                         {
-                                         mTempAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
-                                         mTempCurrent.set(0f, 0f, 0f, 1f);
-                                         mPreRender.setQuatCurrentOnNextRender();
-                                         mPreRender.setQuatAccumulatedOnNextRender();
-                                         }
-
-                                       if( mContinuingRotation )
-                                         {
-                                         computeCurrentSpeed();
-                                         int angle = mPreRender.getObject().computeNearestAngle(mCurrentAngle, mCurrRotSpeed);
-                                         mPreRender.finishRotation(angle);
-
-                                         if( RubikState.getCurrentState()==RubikState.SOLV )
-                                           {
-                                           RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
-
-                                           if( angle!=0 )
-                                             solving.addMove(mCurrentAxis, mCurrentRow, angle);
-                                           }
-                                         }
-                                       if( mLastCubitColor>=0 )
-                                         {
-                                          mPreRender.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
-                                         }
-                                       break;
+         case MotionEvent.ACTION_DOWN: actionDown(x,y); break;
+         case MotionEvent.ACTION_MOVE: actionMove(x,y); break;
+         case MotionEvent.ACTION_UP  : actionUp()     ; break;
          }
 
       return true;
