commit 18709ae03410c24e4fe81432d176c09c4ad3cfa3
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Sat Apr 6 17:08:52 2019 +0100

    Improve the architecture of the Rubik App.

diff --git a/src/main/java/org/distorted/examples/rubik/RubikRenderer.java b/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
index 589947e..d6fa3b7 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikRenderer.java
@@ -37,19 +37,18 @@ class RubikRenderer implements GLSurfaceView.Renderer
     static final int NUM_CUBES = 4;
     private static final float CUBE_SCREEN_RATIO = 0.5f;
 
-    private GLSurfaceView mView;
+    private RubikSurfaceView mView;
     private DistortedScreen mScreen;
     private Static3D mMove, mScale;
     private Static4D mQuatCurrent, mQuatAccumulated;
     private Static4D mTempCurrent, mTempAccumulated;
     private float mScaleFactor;
-    private int mScreenWidth, mScreenHeight;
     private boolean mFinishRotation, mFinishDragCurrent, mFinishDragAccumulated;
     private RubikCube mCube;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    RubikRenderer(GLSurfaceView v)
+    RubikRenderer(RubikSurfaceView v)
       {
       mView = v;
 
@@ -101,8 +100,8 @@ class RubikRenderer implements GLSurfaceView.Renderer
       {
       mScreen.setProjection( width>height ? 60.0f : 90.0f, 0.1f);
 
-      mScreenWidth = width;
-      mScreenHeight= height;
+      mView.setScreenSize(width,height);
+      mView.recomputeCameraDistance();
 
       float w = mCube.getWidth();
       float h = mCube.getHeight();
@@ -146,13 +145,9 @@ class RubikRenderer implements GLSurfaceView.Renderer
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    float returnCameraDistance()
+    float getFOVInRadians()
       {
-      float fov = mScreen.getFOV();
-      double fovInRadians = (fov*Math.PI) / 180.0;
-      double distance = (mScreenHeight*0.5f)/Math.tan(fovInRadians*0.5);
-
-      return (float)distance;
+      return (float)((mScreen.getFOV()*Math.PI) / 180.0);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -163,20 +158,6 @@ class RubikRenderer implements GLSurfaceView.Renderer
       return mScaleFactor*NUM_CUBES*RubikCube.TEXTURE_SIZE;
       }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    float getScreenWidth()
-      {
-      return mScreenWidth;
-      }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    float getScreenHeight()
-      {
-      return mScreenHeight;
-      }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
     RubikCube getCube()
@@ -184,13 +165,6 @@ class RubikRenderer implements GLSurfaceView.Renderer
       return mCube;
       }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-    int getScreenMin()
-      {
-      return mScreenWidth<mScreenHeight ? mScreenWidth:mScreenHeight;
-      }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Initial rotation of the cube. Something semi-random that looks good.
 
diff --git a/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java b/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
index d797cdd..1930b42 100644
--- a/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
+++ b/src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
@@ -59,6 +59,8 @@ class RubikSurfaceView extends GLSurfaceView
     private float mPoiX, mPoiY, mPoiZ, mCamX, mCamY, mCamZ;
     private float mStartX, mStartY, mStartZ;
     private int mLastTouchedFace;
+    private int mScreenWidth, mScreenHeight, mScreenMin;
+    private float mCameraDistance;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -70,6 +72,8 @@ class RubikSurfaceView extends GLSurfaceView
       mRotating = false;
       mRotationVect = VECTN;
 
+      mScreenWidth = mScreenHeight = mScreenMin = 0;
+
       mRenderer = new RubikRenderer(this);
       mCube = mRenderer.getCube();
 
@@ -115,8 +119,7 @@ class RubikSurfaceView extends GLSurfaceView
                                          }
                                        else if( mRotating )
                                          {
-                                         int screenMin = mRenderer.getScreenMin();
-                                         int minimumToRotate = (screenMin*screenMin)/64;
+                                         int minimumToRotate = (mScreenMin*mScreenMin)/64;
 
                                          if( (mX-x)*(mX-x)+(mY-y)*(mY-y)>minimumToRotate )
                                            {
@@ -244,7 +247,7 @@ class RubikSurfaceView extends GLSurfaceView
       float dY = mY-y;
       float calibration = (float)Math.sqrt(dX*dX+dY*dY);
 
-      mCube.continueRotation(calibration*sign, mRenderer.getScreenMin());
+      mCube.continueRotation(calibration*sign, mScreenMin);
       }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -341,7 +344,7 @@ class RubikSurfaceView extends GLSurfaceView
         axisY /= axisL;
         axisZ /= axisL;
 
-        float cosA = (float)Math.cos(axisL*Math.PI/mRenderer.getScreenMin());
+        float cosA = (float)Math.cos(axisL*Math.PI/mScreenMin);
         float sinA = (float)Math.sqrt(1-cosA*cosA);
 
         return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
@@ -354,10 +357,9 @@ class RubikSurfaceView extends GLSurfaceView
 
     private boolean faceIsVisible(int face)
       {
-      float cameraDistance = mRenderer.returnCameraDistance();
       float cubeHalfSize   = mRenderer.returnCubeSize()*0.5f;
 
-      Static4D rotated = rotateVectorByInvertedQuat(new Static4D(0,0,cameraDistance,0), mQuatAccumulated);
+      Static4D rotated = rotateVectorByInvertedQuat(new Static4D(0,0,mCameraDistance,0), mQuatAccumulated);
 
       switch(face)
         {
@@ -406,8 +408,8 @@ class RubikSurfaceView extends GLSurfaceView
 
     void fillTouchPoint(int x, int y)
       {
-      float halfScrWidth  = mRenderer.getScreenWidth() *0.5f;
-      float halfScrHeight = mRenderer.getScreenHeight()*0.5f;
+      float halfScrWidth  = mScreenWidth *0.5f;
+      float halfScrHeight = mScreenHeight*0.5f;
       Static4D touchPoint = new Static4D(x-halfScrWidth, halfScrHeight-y, 0, 0);
       Static4D rotatedTouchPoint= rotateVectorByInvertedQuat(touchPoint, mQuatAccumulated);
 
@@ -420,9 +422,7 @@ class RubikSurfaceView extends GLSurfaceView
 
     void fillCamera()
       {
-      float cameraDistance = mRenderer.returnCameraDistance();
-
-      Static4D cameraPoint = new Static4D(0, 0, cameraDistance, 0);
+      Static4D cameraPoint = new Static4D(0, 0, mCameraDistance, 0);
       Static4D rotatedCamera= rotateVectorByInvertedQuat(cameraPoint, mQuatAccumulated);
 
       mCamX = rotatedCamera.get1();
@@ -463,5 +463,23 @@ class RubikSurfaceView extends GLSurfaceView
 
       return false;
       }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    void setScreenSize(int width, int height)
+      {
+      mScreenWidth = width;
+      mScreenHeight= height;
+
+      mScreenMin = width<height ? width:height;
+      }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+    void recomputeCameraDistance()
+      {
+      double fovInRadians = mRenderer.getFOVInRadians();
+      mCameraDistance = (float)((mScreenHeight*0.5f)/Math.tan(fovInRadians*0.5));
+      }
 }
 
