commit f843630bb049048617256b889e51bd590d085b52
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Sat Apr 6 19:15:54 2019 +0100

    RubikApp: simplify SurfaceView

diff --git a/src/main/java/org/distorted/examples/rubik/RubikCube.java b/src/main/java/org/distorted/examples/rubik/RubikCube.java
index ca3b063..efc10e9 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikCube.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikCube.java
@@ -41,7 +41,7 @@ import org.distorted.library.type.Static4D;
 
 class RubikCube
 {
-    static final int TEXTURE_SIZE = 200;
+    static final int TEXTURE_SIZE = 100;
 
     private static final Static3D VectX = new Static3D(1,0,0);
     private static final Static3D VectY = new Static3D(0,1,0);
@@ -192,9 +192,9 @@ class RubikCube
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    void continueRotation(float angle, int screenMin)
+    void continueRotation(float angle)
       {
-      mRotationAngleStatic.set1(200.0f*angle/screenMin);
+      mRotationAngleStatic.set1(angle);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java b/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
index 1930b42..e8bd324 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
@@ -39,11 +39,6 @@ class RubikSurfaceView extends GLSurfaceView
     private final static int TOP    = 4;  //
     private final static int BOTTOM = 5;  //
 
-    private static final int DIR_UP   =0;
-    private static final int DIR_DOWN =1;
-    private static final int DIR_LEFT =2;
-    private static final int DIR_RIGHT=3;
-
     static final int VECTX = 0;
     static final int VECTY = 1;
     static final int VECTZ = 2;
@@ -119,7 +114,7 @@ class RubikSurfaceView extends GLSurfaceView
                                          }
                                        else if( mRotating )
                                          {
-                                         int minimumToRotate = (mScreenMin*mScreenMin)/64;
+                                         int minimumToRotate = (mScreenMin*mScreenMin)/100;
 
                                          if( (mX-x)*(mX-x)+(mY-y)*(mY-y)>minimumToRotate )
                                            {
@@ -156,57 +151,52 @@ class RubikSurfaceView extends GLSurfaceView
       fillTouchPoint(x,y);
 
       float cubeHalfSize= mRenderer.returnCubeSize()*0.5f;
-      float tmp = RubikRenderer.NUM_CUBES/(2*cubeHalfSize);
       float A=retA(mLastTouchedFace,cubeHalfSize);
 
       float diffX = (mPoiX-mCamX)*A + mCamX - mStartX;
       float diffY = (mPoiY-mCamY)*A + mCamY - mStartY;
       float diffZ = (mPoiZ-mCamZ)*A + mCamZ - mStartZ;
 
-      int dir, row=1;
+      mStartX = diffX + mStartX;
+      mStartY = diffY + mStartY;
+      mStartZ = diffZ + mStartZ;
 
       switch(mLastTouchedFace)
         {
-        case FRONT : dir = retDirection( diffX, diffY);
-                     mRotationVect = (dir==DIR_UP || dir==DIR_DOWN) ? VECTX:VECTY;
-                     row = (int)( tmp*((mRotationVect==VECTX?mStartX:mStartY)+cubeHalfSize) );
-                     break;
-        case BACK  : dir = retDirection(-diffX, diffY);
-                     mRotationVect = (dir==DIR_UP || dir==DIR_DOWN) ? VECTX:VECTY;
-                     row = (int)( tmp*((mRotationVect==VECTX?mStartX:mStartY)+cubeHalfSize) );
-                     break;
-        case LEFT  : dir = retDirection( diffZ, diffY);
-                     mRotationVect = (dir==DIR_UP || dir==DIR_DOWN) ? VECTZ:VECTY;
-                     row = (int)( tmp*((mRotationVect==VECTZ?mStartZ:mStartY)+cubeHalfSize) );
-                     break;
-        case RIGHT : dir = retDirection(-diffZ, diffY);
-                     mRotationVect = (dir==DIR_UP || dir==DIR_DOWN) ? VECTZ:VECTY;
-                     row = (int)( tmp*((mRotationVect==VECTZ?mStartZ:mStartY)+cubeHalfSize) );
-                     break;
-        case TOP   : dir = retDirection( diffX,-diffZ);
-                     mRotationVect = (dir==DIR_UP || dir==DIR_DOWN) ? VECTX:VECTZ;
-                     row = (int)( tmp*((mRotationVect==VECTX?mStartX:mStartZ)+cubeHalfSize) );
-                     break;
-        case BOTTOM: dir = retDirection( diffX, diffZ);
-                     mRotationVect = (dir==DIR_UP || dir==DIR_DOWN) ? VECTX:VECTZ;
-                     row = (int)( tmp*((mRotationVect==VECTX?mStartX:mStartZ)+cubeHalfSize) );
-                     break;
+        case FRONT :
+        case BACK  : mRotationVect = (isVertical(diffX, diffY) ? VECTX:VECTY); break;
+        case LEFT  :
+        case RIGHT : mRotationVect = (isVertical(diffZ, diffY) ? VECTZ:VECTY); break;
+        case TOP   :
+        case BOTTOM: mRotationVect = (isVertical(diffX, diffZ) ? VECTX:VECTZ); break;
         }
 
-      mStartX = diffX + mStartX;
-      mStartY = diffY + mStartY;
-      mStartZ = diffZ + mStartZ;
+      int row=0;
+      float tmp = RubikRenderer.NUM_CUBES/(2*cubeHalfSize);
+
+      switch(mRotationVect)
+        {
+        case VECTX: row = (int)(tmp*(mStartX+cubeHalfSize)); break;
+        case VECTY: row = (int)(tmp*(mStartY+cubeHalfSize)); break;
+        case VECTZ: row = (int)(tmp*(mStartZ+cubeHalfSize)); break;
+        }
 
       mCube.addNewRotation(mRotationVect,row);
       }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    private boolean isVertical(float x, float y)
+      {
+      return (y>x) ? (y>=-x) : (y< -x);
+      }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     private void continueRotation(int x, int y)
       {
       fillTouchPoint(x,y);
 
-      int sign=1;
       float cubeHalfSize= mRenderer.returnCubeSize()*0.5f;
       float A=retA(mLastTouchedFace,cubeHalfSize);
 
@@ -214,40 +204,38 @@ class RubikSurfaceView extends GLSurfaceView
       float diffY = (mPoiY-mCamY)*A + mCamY - mStartY;
       float diffZ = (mPoiZ-mCamZ)*A + mCamZ - mStartZ;
 
+      float angle=0.0f;
+
       switch(mRotationVect)
         {
         case VECTX: switch(mLastTouchedFace)
                       {
-                      case FRONT : sign = returnSign(-diffY); break;
-                      case BACK  : sign = returnSign( diffY); break;
-                      case TOP   : sign = returnSign( diffZ); break;
-                      case BOTTOM: sign = returnSign(-diffZ); break;
+                      case FRONT : angle = -diffY; break;
+                      case BACK  : angle =  diffY; break;
+                      case TOP   : angle =  diffZ; break;
+                      case BOTTOM: angle = -diffZ; break;
                       }
                     break;
         case VECTY: switch(mLastTouchedFace)
                       {
-                      case FRONT : sign = returnSign( diffX); break;
-                      case BACK  : sign = returnSign(-diffX); break;
-                      case LEFT  : sign = returnSign( diffZ); break;
-                      case RIGHT : sign = returnSign(-diffZ); break;
+                      case FRONT : angle =  diffX; break;
+                      case BACK  : angle = -diffX; break;
+                      case LEFT  : angle =  diffZ; break;
+                      case RIGHT : angle = -diffZ; break;
                       }
                     break;
         case VECTZ: switch(mLastTouchedFace)
                       {
-                      case TOP   : sign = returnSign(-diffX); break;
-                      case BOTTOM: sign = returnSign( diffX); break;
-                      case LEFT  : sign = returnSign(-diffY); break;
-                      case RIGHT : sign = returnSign( diffY); break;
+                      case TOP   : angle = -diffX; break;
+                      case BOTTOM: angle =  diffX; break;
+                      case LEFT  : angle = -diffY; break;
+                      case RIGHT : angle =  diffY; break;
                       }
                     break;
         default   : android.util.Log.e("View", "impossible vector: "+mRotationVect);
         }
 
-      float dX = mX-x;
-      float dY = mY-y;
-      float calibration = (float)Math.sqrt(dX*dX+dY*dY);
-
-      mCube.continueRotation(calibration*sign, mScreenMin);
+      mCube.continueRotation(200.0f*angle/mScreenMin);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -258,21 +246,6 @@ class RubikSurfaceView extends GLSurfaceView
       mRenderer.finishRotation();
       }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    private int returnSign(float diff)
-      {
-      return diff>0 ? 1 : -1;
-      }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    private int retDirection(float a, float b)
-      {
-      if( b>a ) return a>-b ? DIR_UP   :DIR_LEFT;
-      else      return a>-b ? DIR_RIGHT:DIR_DOWN;
-      }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // return quat1*quat2
 
@@ -312,7 +285,6 @@ class RubikSurfaceView extends GLSurfaceView
       return quatMultiply(tmp,quat);
       }
 
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // rotate 'vector' by quat  ( i.e. return quat*vector*(quat^-1) )
 
