Improved code readability
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / composite / textcanvas / TextCanvas.java
index 854d756..8c3e49f 100644 (file)
@@ -19,15 +19,29 @@ import java.io.StringReader;
 
 import static eu.svjatoslav.sixth.e3d.renderer.raster.Color.BLACK;
 import static eu.svjatoslav.sixth.e3d.renderer.raster.Color.WHITE;
+import static java.lang.Math.PI;
+import static java.lang.Math.abs;
 
 public class TextCanvas extends TexturedRectangle {
 
-    // character size in world coordinates
+    /**
+     * Font character width in world coordinates.
+     */
     public static final int FONT_CHAR_WIDTH = 8;
+
+    /**
+     * Font character height in world coordinates.
+     */
     public static final int FONT_CHAR_HEIGHT = 16;
 
-    // character size on texture
+    /**
+      * Font character width in texture pixels.
+      */
     public static final int FONT_CHAR_WIDTH_TEXTURE_PIXELS = 16;
+
+    /**
+     * Font character height in texture pixels.
+     */
     public static final int FONT_CHAR_HEIGHT_TEXTURE_PIXELS = 32;
 
 
@@ -120,29 +134,26 @@ public class TextCanvas extends TexturedRectangle {
     @Override
     public void beforeTransformHook(final TransformsStack transformPipe,
                                     final RenderingContext context) {
+        ensureOptimalRenderMode(context);
+    }
 
-        final double textRelativeSize = context.width
-                / getRelativityTracker().getDistanceToUser();
+    private void ensureOptimalRenderMode(RenderingContext context) {
 
+        // if the text is too far away, use texture
+        final double textRelativeSize = context.width / getRelativityTracker().getDistanceToUser();
         if (textRelativeSize < 2d) {
-            if (renderMode == RenderMode.CHARACTERS)
-                setRenderMode(RenderMode.TEXTURE);
+            setRenderMode(RenderMode.TEXTURE);
             return;
         }
 
-        final double piHalf = Math.PI / 2;
-
-        final double deviation = Math.abs(getRelativityTracker().getAngleXZ()
+        // if user is looking at the text from the side, use texture
+        final double piHalf = PI / 2;
+        final double deviation = abs(getRelativityTracker().getAngleXZ()
                 + piHalf)
-                + Math.abs(getRelativityTracker().getAngleYZ() + piHalf);
+                + abs(getRelativityTracker().getAngleYZ() + piHalf);
 
         final double maxDeviation = 0.5;
-
-        if (deviation > maxDeviation) {
-            if (renderMode == RenderMode.CHARACTERS)
-                setRenderMode(RenderMode.TEXTURE);
-        } else if (renderMode == RenderMode.TEXTURE)
-            setRenderMode(RenderMode.CHARACTERS);
+        setRenderMode(deviation > maxDeviation ? RenderMode.TEXTURE : RenderMode.CHARACTERS);
     }
 
     public void clear() {