Fixed git clone URL
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / composite / textcanvas / CanvasCharacter.java
index dc12fb6..a616dae 100644 (file)
@@ -1,12 +1,7 @@
 /*
- * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
- *
+ * 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;
 
 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
@@ -17,56 +12,81 @@ import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPo
 
 import java.awt.*;
 
+import static eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon.drawPolygon;
+import static eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas.FONT_CHAR_HEIGHT;
+import static eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas.FONT_CHAR_WIDTH;
+import static java.lang.String.valueOf;
+
+/**
+ * Represents a single character on the text canvas.
+ */
 public class CanvasCharacter extends AbstractCoordinateShape {
 
-    public static final double SIZE_MULTIPLIER = 0.005;
     private static final int MAX_FONT_SIZE = 500;
+
+    /**
+     * Cached fonts.
+     */
     private static final Font[] fonts = new Font[MAX_FONT_SIZE];
-    private String value;
 
+    /**
+     * The character to be rendered.
+     */
+    private char value;
+
+    /**
+     * The foreground color of the character.
+     */
     private eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor;
-    private Color foregroundAwtColor;
 
+    /**
+     * The background color of the character.
+     */
     private eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor;
 
-    public CanvasCharacter(final Point3D point, final char character,
+    public CanvasCharacter(final Point3D centerLocation, final char character,
                            final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor,
                            final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
 
+        // There are 5 coordinates: center, upper left, upper right, lower right, lower left
         super(5);
-        coordinates[0].coordinate = point;
-
-        value = String.valueOf(character);
 
+        value = character;
         this.foregroundColor = foregroundColor;
-        foregroundAwtColor = foregroundColor.toAwtColor();
-
         this.backgroundColor = backgroundColor;
 
-        // set corner coordinates (for drawing background)
-        {
-            final double widthHalf = TextCanvas.FONT_CHAR_WIDTH / 2d;
-            final double heightHalf = TextCanvas.FONT_CHAR_HEIGHT / 2d;
 
-            // upper left
-            coordinates[1].coordinate = point.clone().translateX(-widthHalf)
-                    .translateY(-heightHalf);
+        coordinates[0].coordinate = centerLocation;
 
-            // upper right
-            coordinates[2].coordinate = point.clone().translateX(widthHalf)
-                    .translateY(-heightHalf);
+        final double halfWidth = FONT_CHAR_WIDTH / 2d;
+        final double halfHeight = FONT_CHAR_HEIGHT / 2d;
 
-            // lower right
-            coordinates[3].coordinate = point.clone().translateX(widthHalf)
-                    .translateY(heightHalf);
+        // upper left
+        coordinates[1].coordinate = centerLocation.clone().translateX(-halfWidth)
+                .translateY(-halfHeight);
 
-            // lower left
-            coordinates[4].coordinate = point.clone().translateX(-widthHalf)
-                    .translateY(heightHalf);
+        // upper right
+        coordinates[2].coordinate = centerLocation.clone().translateX(halfWidth)
+                .translateY(-halfHeight);
 
-        }
+        // lower right
+        coordinates[3].coordinate = centerLocation.clone().translateX(halfWidth)
+                .translateY(halfHeight);
+
+        // lower left
+        coordinates[4].coordinate = centerLocation.clone().translateX(-halfWidth)
+                .translateY(halfHeight);
     }
 
+    /**
+     * Returns a font of the specified size.
+     * <p>
+     * If the font of the specified size is already cached, it will be
+     * returned. Otherwise, a new font will be created, cached and returned.
+     *
+     * @param size the size of the font
+     * @return the font
+     */
     public static Font getFont(final int size) {
         if (fonts[size] != null)
             return fonts[size];
@@ -76,44 +96,66 @@ public class CanvasCharacter extends AbstractCoordinateShape {
         return font;
     }
 
+    /**
+     * Returns color of the background.
+     */
     public eu.svjatoslav.sixth.e3d.renderer.raster.Color getBackgroundColor() {
         return backgroundColor;
     }
 
+    /**
+     * Sets color of the background.
+     */
     public void setBackgroundColor(
             final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
         this.backgroundColor = backgroundColor;
     }
 
+    /**
+     * Returns color of the foreground.
+     *
+     * @return the color
+     */
     public eu.svjatoslav.sixth.e3d.renderer.raster.Color getForegroundColor() {
         return foregroundColor;
     }
 
+    /**
+     * Sets color of the foreground.
+     *
+     * @param foregroundColor the color
+     */
     public void setForegroundColor(
             final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor) {
         this.foregroundColor = foregroundColor;
-        foregroundAwtColor = foregroundColor.toAwtColor();
     }
 
+    /**
+     * Paints the character on the screen.
+     * @param renderingContext the rendering context
+     */
     @Override
     public void paint(final RenderingContext renderingContext) {
 
-        SolidPolygon.drawPolygon(renderingContext,
+        // Draw background rectangle first. It is composed of two triangles.
+        drawPolygon(renderingContext,
                 coordinates[1].onScreenCoordinate,
                 coordinates[2].onScreenCoordinate,
-                coordinates[3].onScreenCoordinate, mouseInteractionController,
+                coordinates[3].onScreenCoordinate,
+                mouseInteractionController,
                 backgroundColor);
 
-        SolidPolygon.drawPolygon(renderingContext,
+        drawPolygon(renderingContext,
                 coordinates[1].onScreenCoordinate,
                 coordinates[3].onScreenCoordinate,
-                coordinates[4].onScreenCoordinate, mouseInteractionController,
+                coordinates[4].onScreenCoordinate,
+                mouseInteractionController,
                 backgroundColor);
 
-        final int size = (int) ((renderingContext.width * 4.5) / onScreenZ);
+        final int desiredFontSize = (int) ((renderingContext.width * 4.5) / onScreenZ);
 
         // do not render too large characters
-        if (size >= MAX_FONT_SIZE)
+        if (desiredFontSize >= MAX_FONT_SIZE)
             return;
 
         final Point2D onScreenLocation = coordinates[0].onScreenCoordinate;
@@ -129,16 +171,18 @@ public class CanvasCharacter extends AbstractCoordinateShape {
         if (onScreenLocation.y > renderingContext.height)
             return;
 
-        renderingContext.graphics.setFont(getFont(size));
-        renderingContext.graphics.setColor(foregroundAwtColor);
-        renderingContext.graphics.drawString(value, (int) onScreenLocation.x
-                - (int) (size / 3.2), (int) onScreenLocation.y
-                + (int) (size / 2.5));
+        // draw the character
+        renderingContext.graphics.setFont(getFont(desiredFontSize));
+        renderingContext.graphics.setColor(foregroundColor.toAwtColor());
+        renderingContext.graphics.drawString(
+                valueOf(value),
+                (int) onScreenLocation.x - (int) (desiredFontSize / 3.2),
+                (int) onScreenLocation.y + (int) (desiredFontSize / 2.5));
 
     }
 
     public void setValue(final char value) {
-        this.value = String.valueOf(value);
+        this.value = value;
     }
 
 }