Fixed git clone URL
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / composite / textcanvas / TextCanvas.java
index fd78bf9..4ffef13 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sixth 3D engine. Author: Svjatoslav Agejenko. 
+ * Sixth 3D engine. Author: Svjatoslav Agejenko.
  * This project is released under Creative Commons Zero (CC0) license.
  */
 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas;
@@ -8,7 +8,7 @@ import eu.svjatoslav.sixth.e3d.geometry.Point3D;
 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
 import eu.svjatoslav.sixth.e3d.gui.TextPointer;
 import eu.svjatoslav.sixth.e3d.math.Transform;
-import eu.svjatoslav.sixth.e3d.math.TransformsPipeline;
+import eu.svjatoslav.sixth.e3d.math.TransformsStack;
 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.TexturedRectangle;
 
@@ -19,24 +19,38 @@ 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 coordiates
+    /**
+     * 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;
 
 
-    public static final Font FONT = CanvasCharacter.getFont((int)(FONT_CHAR_HEIGHT_TEXTURE_PIXELS / 1.066));
+    public static final Font FONT = CanvasCharacter.getFont((int) (FONT_CHAR_HEIGHT_TEXTURE_PIXELS / 1.066));
     private static final String GROUP_TEXTURE = "texture";
     private static final String GROUP_CHARACTERS = "characters";
     private final TextPointer size;
     private final TextPointer cursorLocation = new TextPointer();
-    CanvasCharacter lines[][];
+    CanvasCharacter[][] lines;
     private RenderMode renderMode = null;
     private Color backgroundColor = BLACK;
     private Color foregroundColor = WHITE;
@@ -118,34 +132,31 @@ public class TextCanvas extends TexturedRectangle {
     }
 
     @Override
-    public void beforeTransformHook(final TransformsPipeline transformPipe,
+    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 cls() {
+    public void clear() {
         for (final CanvasCharacter[] line : lines)
             for (final CanvasCharacter character : line) {
                 character.setValue(' ');
@@ -173,9 +184,8 @@ public class TextCanvas extends TexturedRectangle {
         graphics.setColor(foreground.toAwtColor());
         graphics.drawChars(
                 new char[]{character,}, 0, 1,
-                (column * FONT_CHAR_WIDTH_TEXTURE_PIXELS) - 0,
-                (row * FONT_CHAR_HEIGHT_TEXTURE_PIXELS) + (int)(FONT_CHAR_HEIGHT_TEXTURE_PIXELS / 1.23f));
-
+                (column * FONT_CHAR_WIDTH_TEXTURE_PIXELS),
+                (row * FONT_CHAR_HEIGHT_TEXTURE_PIXELS) + (int) (FONT_CHAR_HEIGHT_TEXTURE_PIXELS / 1.23f));
         getTexture().resetResampledBitmapCache();
     }
 
@@ -295,11 +305,4 @@ public class TextCanvas extends TexturedRectangle {
                 character.setForegroundColor(color);
     }
 
-    // @Override
-    // public void transform(final TransformPipe transformPipe,
-    // final RenderAggregator aggregator, final RenderingContext buffer) {
-    //
-    // super.transform(transformPipe, aggregator, buffer);
-    //
-    // }
 }