commit af8b42cc0f8c2646f98febbf606a55a73719f9b9
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Mon Apr 29 15:04:35 2019 +0100

    Port changes from the 'distorted-cube' target.

diff --git a/src/main/java/org/distorted/examples/rubik/RubikActivity.java b/src/main/java/org/distorted/examples/rubik/RubikActivity.java
index 0cf221d..f1cfb63 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikActivity.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikActivity.java
@@ -35,7 +35,7 @@ import org.distorted.library.main.Distorted;
 public class RubikActivity extends Activity
 {
             static final int DEFAULT_SIZE  = 3;
-    private static final int STARTING_SIZE = 2;
+    private static final int SMALLEST_SIZE = 2;
     private static final int[] button_ids  = {R.id.rubikSize2, R.id.rubikSize3, R.id.rubikSize4};
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -103,7 +103,7 @@ public class RubikActivity extends Activity
       for(int b=0; b<button_ids.length; b++)
         if( button_ids[b] == id )
           {
-          size = b+STARTING_SIZE;
+          size = b+SMALLEST_SIZE;
           break;
           }
 
@@ -121,7 +121,7 @@ public class RubikActivity extends Activity
        {
        Drawable d = findViewById(button_ids[b]).getBackground();
 
-       if( size == b+STARTING_SIZE )
+       if( size == b+SMALLEST_SIZE )
          {
          d.setColorFilter(ContextCompat.getColor(this,R.color.red), PorterDuff.Mode.MULTIPLY);
          }
diff --git a/src/main/java/org/distorted/examples/rubik/RubikCube.java b/src/main/java/org/distorted/examples/rubik/RubikCube.java
index 8f43700..9062912 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikCube.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikCube.java
@@ -393,7 +393,7 @@ class RubikCube
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    float getSize()
+    int getSize()
       {
       return mSize;
       }
diff --git a/src/main/java/org/distorted/examples/rubik/RubikRenderer.java b/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
index dddc91f..e5e5cdb 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
@@ -49,6 +49,8 @@ class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
     private boolean mCanRotate;
     private RubikCube mCube;
 
+    private int mScreenWidth, mScreenHeight;
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     RubikRenderer(RubikSurfaceView v)
@@ -62,6 +64,8 @@ class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
       mQuatCurrent     = new Static4D(0,0,0,1);
       mQuatAccumulated = initializeQuat();
 
+      mScreenWidth = mScreenHeight = 0;
+
       mMove  = new Static3D(0,0,0);
       mScale = new Static3D(1,1,1);
 
@@ -71,8 +75,6 @@ class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
       mFinishDragAccumulated = false;
 
       mCanRotate = true;
-
-      mCube = new RubikCube( RubikActivity.DEFAULT_SIZE, mMove, mScale, mQuatCurrent, mQuatAccumulated);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -131,15 +133,12 @@ class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
       mScreen.setProjection( fovInDegrees, 0.1f);
       mView.setScreenSize(width,height);
       mView.setCameraDist(cameraDistance);
+      mScreen.resize(width, height);
 
-      mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(width>height ? height:width);
-      float texSize = mCube.getTextureSize();
-      float scaleFactor = mCubeSizeInScreenSpace/(texSize*mCube.getSize());
-
-      mMove.set( (width-scaleFactor*texSize)/2 , (height-scaleFactor*texSize)/2 , -scaleFactor*texSize/2 );
-      mScale.set(scaleFactor,scaleFactor,scaleFactor);
+      recomputeScaleFactor(width,height);
 
-      mScreen.resize(width, height);
+      mScreenHeight = height;
+      mScreenWidth  = width;
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -164,62 +163,102 @@ class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    private float computeFOV(float cameraDistance, int screenHeight)
-      {
-      double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
-      return (float)(2*halfFOVInRadians*(180/Math.PI));
-      }
+   private float computeFOV(float cameraDistance, int screenHeight)
+     {
+     double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
+     return (float)(2*halfFOVInRadians*(180/Math.PI));
+     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // no this will not race with onDrawFrame
 
-    void finishRotation()
-      {
-      mFinishRotation = true;
-      }
+   void finishRotation()
+     {
+     mFinishRotation = true;
+     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    float returnCubeSizeInScreenSpace()
-      {
-      return mCubeSizeInScreenSpace;
-      }
+   void createCube(int newSize)
+     {
+     int oldSize = mCube==null ? 0 : mCube.getSize();
+
+     if( oldSize!=newSize )
+       {
+       mCube = new RubikCube(newSize, mMove, mScale, mQuatCurrent, mQuatAccumulated);
+       mCube.createTexture();
+
+       if( mScreenWidth!=0 )
+         {
+         recomputeScaleFactor(mScreenWidth,mScreenHeight);
+         }
+
+       mScreen.detachAll();
+       mCube.attachToScreen(mScreen);
+       }
+     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    boolean canRotate()
-      {
-      return mCanRotate;
-      }
+   void recomputeScaleFactor(int screenWidth, int screenHeight)
+     {
+     mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth);
+     float texSize = mCube.getTextureSize();
+     float scaleFactor = mCubeSizeInScreenSpace/(texSize*mCube.getSize());
+
+     mMove.set( (screenWidth-scaleFactor*texSize)/2 , (screenHeight-scaleFactor*texSize)/2 , -scaleFactor*texSize/2 );
+     mScale.set(scaleFactor,scaleFactor,scaleFactor);
+     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    RubikCube getCube()
-      {
-      return mCube;
-      }
+   void scrambleCube()
+     {
+
+     }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+   float returnCubeSizeInScreenSpace()
+     {
+     return mCubeSizeInScreenSpace;
+     }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+   boolean canRotate()
+     {
+     return mCanRotate;
+     }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+   RubikCube getCube()
+     {
+     return mCube;
+     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Initial rotation of the cube. Something semi-random that looks good.
 
-    Static4D initializeQuat()
-      {
-      return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
-      }
+   Static4D initializeQuat()
+     {
+     return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
+     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    void setQuatCurrent(Static4D current)
-      {
-      mTempCurrent.set(current);
-      mFinishDragCurrent = true;
-      }
+   void setQuatCurrent(Static4D current)
+     {
+     mTempCurrent.set(current);
+     mFinishDragCurrent = true;
+     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    void setQuatAccumulated(Static4D accumulated)
-      {
-      mTempAccumulated.set(accumulated);
-      mFinishDragAccumulated = true;
-      }
+   void setQuatAccumulated(Static4D accumulated)
+     {
+     mTempAccumulated.set(accumulated);
+     mFinishDragAccumulated = true;
+     }
 }
diff --git a/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java b/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
index 23664ba..5ccbf00 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
@@ -32,6 +32,10 @@ import org.distorted.library.type.Static4D;
 
 class RubikSurfaceView extends GLSurfaceView
 {
+    // Moving the finger from the middle of the vertical screen to the right edge will rotate a
+    // given face by SWIPING_SENSITIVITY/2 degrees.
+    private final static int SWIPING_SENSITIVITY = 240;
+
     private final static int NONE   =-1;
     private final static int FRONT  = 0;  // has to be 6 consecutive ints
     private final static int BACK   = 1;  // FRONT ... BOTTOM
@@ -41,7 +45,7 @@ class RubikSurfaceView extends GLSurfaceView
     private final static int BOTTOM = 5;  //
 
     static final int VECTX = 0;  //
-    static final int VECTY = 1;  // dont change this
+    static final int VECTY = 1;  // don't change this
     static final int VECTZ = 2;  //
 
     private static final int[] VECT = {VECTX,VECTY,VECTZ};
@@ -51,7 +55,6 @@ class RubikSurfaceView extends GLSurfaceView
     private Static4D mQuatCurrent, mQuatAccumulated;
     private int mRotationVect;
     private RubikRenderer mRenderer;
-    private RubikCube mCube;
 
     private float[] mPoint, mCamera, mTouchPointCastOntoFace, mDiff, mTouchPoint; // all in screen space
     private int mLastTouchedFace;
@@ -77,7 +80,7 @@ class RubikSurfaceView extends GLSurfaceView
         mScreenWidth = mScreenHeight = mScreenMin = 0;
 
         mRenderer = new RubikRenderer(this);
-        mCube = mRenderer.getCube();
+        mRenderer.createCube(RubikActivity.DEFAULT_SIZE);
 
         mQuatCurrent     = new Static4D(0,0,0,1);
         mQuatAccumulated = mRenderer.initializeQuat();
@@ -89,13 +92,6 @@ class RubikSurfaceView extends GLSurfaceView
         }
       }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    public RubikRenderer getRenderer()
-      {
-      return mRenderer;
-      }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     @Override
@@ -168,14 +164,14 @@ class RubikSurfaceView extends GLSurfaceView
 
     void setNewCubeSize(int newCubeSize)
       {
-      android.util.Log.e("view", "new size="+newCubeSize);
+      mRenderer.createCube(newCubeSize);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     void scrambleCube()
       {
-      android.util.Log.e("view", "scrambling...");
+      mRenderer.scrambleCube();
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -243,7 +239,7 @@ class RubikSurfaceView extends GLSurfaceView
       mTouchPoint[1] = mPoint[1];
       mTouchPoint[2] = mPoint[2];
 
-      mCube.addNewRotation(mRotationVect,offset);
+      mRenderer.getCube().addNewRotation(mRotationVect,offset);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -254,8 +250,6 @@ class RubikSurfaceView extends GLSurfaceView
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-// 240 --> moving finger from the middle of the vertical screen to the right edge will rotate a
-// given face by 240/2 = 120 degrees.
 
     private void continueRotation(int x, int y)
       {
@@ -270,7 +264,7 @@ class RubikSurfaceView extends GLSurfaceView
       int sign = retFaceRotationSign(mLastTouchedFace);
       float angle = (mRotationVect==xAxis ? mDiff[yAxis] : -mDiff[xAxis]);
 
-      mCube.continueRotation(240.0f*sign*angle/mScreenMin);
+      mRenderer.getCube().continueRotation(SWIPING_SENSITIVITY*sign*angle/mScreenMin);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
