From 2ba2ce636354885e669e9e3fbb97226025e5af45 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Tue, 11 Feb 2020 23:42:25 +0200 Subject: [PATCH] Code refactoring --- .../sixth/e3d/geometry/Point3D.java | 23 +++++++------- .../TextEditComponent.java | 4 +-- .../composite/ForwardOrientedTextBlock.java | 6 ++-- .../composite/textcanvas/CanvasCharacter.java | 12 ++++---- .../composite/textcanvas/RenderMode.java | 3 +- .../composite/textcanvas/TextCanvas.java | 30 ++++++++++--------- 6 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point3D.java b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point3D.java index ec38ef9..f0f2390 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point3D.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point3D.java @@ -15,9 +15,9 @@ import static java.lang.Math.*; * Used to represent point in a 3D space or vector. */ -public class Point3D extends Point2D implements Cloneable { +public class Point3D implements Cloneable { - public double z; + public double x, y, z; public Point3D() { } @@ -47,12 +47,12 @@ public class Point3D extends Point2D implements Cloneable { } public Point3D add(final Point3D direction) { - super.add(direction); + x += direction.x; + y += direction.y; z += direction.z; return this; } - @Override public Point3D clone() { return new Point3D(this); } @@ -71,7 +71,6 @@ public class Point3D extends Point2D implements Cloneable { return this; } - @Override public boolean isZero() { return (x == 0) && (y == 0) && (z == 0); } @@ -84,6 +83,10 @@ public class Point3D extends Point2D implements Cloneable { return Math.atan2(y - anotherPoint.y, z - anotherPoint.z); } + public double getAngleXY(final Point3D anotherPoint) { + return Math.atan2(x - anotherPoint.x, y - anotherPoint.y); + } + /** * Compute distance to another point. */ @@ -95,12 +98,10 @@ public class Point3D extends Point2D implements Cloneable { return sqrt(((xDelta * xDelta) + (yDelta * yDelta) + (zDelta * zDelta))); } - @Override public double getVectorLength() { return sqrt(((x * x) + (y * y) + (z * z))); } - @Override public Point3D invert() { x = -x; y = -y; @@ -131,7 +132,6 @@ public class Point3D extends Point2D implements Cloneable { z = z2 + center.z; } - @Override public void roundToInteger() { x = (int) x; y = (int) y; @@ -159,7 +159,8 @@ public class Point3D extends Point2D implements Cloneable { } public Point3D subtract(final Point3D direction) { - super.subtract(direction); + x -= direction.x; + y -= direction.y; z -= direction.z; return this; } @@ -196,9 +197,9 @@ public class Point3D extends Point2D implements Cloneable { return false; } - @Override public Point3D zero() { - super.zero(); + x = 0; + y = 0; z = 0; return this; } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/TextEditComponent.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/TextEditComponent.java index 8ebf63a..8e69c95 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/TextEditComponent.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/TextEditComponent.java @@ -47,8 +47,8 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner { // initialize visual panel - final int columns = (int) (size.x / TextCanvas.FONT_CHAR_WIDTH); - final int rows = (int) (size.y / TextCanvas.FONT_CHAR_HEIGHT); + final int columns = (int) (size.x / TextCanvas.FONT_CHAR_WIDTH_PIXELS); + final int rows = (int) (size.y / TextCanvas.FONT_CHAR_HEIGHT_PIXELS); textCanvas = new TextCanvas(new Transform(), new TextPointer(rows, columns), Color.WHITE, colorConfig.normalBack); diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/ForwardOrientedTextBlock.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/ForwardOrientedTextBlock.java index 6671cf0..800c6bd 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/ForwardOrientedTextBlock.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/ForwardOrientedTextBlock.java @@ -28,7 +28,7 @@ public class ForwardOrientedTextBlock extends ForwardOrientedTexture { final eu.svjatoslav.sixth.e3d.renderer.raster.Color textColor) { final Texture texture = new Texture(text.length() - * TextCanvas.FONT_CHAR_WIDTH, TextCanvas.FONT_CHAR_HEIGHT, + * TextCanvas.FONT_CHAR_WIDTH_PIXELS, TextCanvas.FONT_CHAR_HEIGHT_PIXELS, maxUpscaleFactor); // texture.graphics.setColor(Color.BLUE); @@ -40,8 +40,8 @@ public class ForwardOrientedTextBlock extends ForwardOrientedTexture { for (int c = 0; c < text.length(); c++) texture.graphics.drawChars(new char[]{text.charAt(c),}, 0, 1, - (c * TextCanvas.FONT_CHAR_WIDTH) - 0, - (0 * TextCanvas.FONT_CHAR_HEIGHT) + 11); + (c * TextCanvas.FONT_CHAR_WIDTH_PIXELS) - 0, + (0 * TextCanvas.FONT_CHAR_HEIGHT_PIXELS) + 11); return texture; } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/CanvasCharacter.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/CanvasCharacter.java index 7eddb27..9823a72 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/CanvasCharacter.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/CanvasCharacter.java @@ -19,7 +19,6 @@ import java.awt.*; public class CanvasCharacter extends AbstractCoordinateShape { - public static final double SIZE_MULTIPLIER = 0.005; private static final int MAX_FONT_SIZE = 500; private static final Font[] fonts = new Font[MAX_FONT_SIZE]; private String value; @@ -45,8 +44,8 @@ public class CanvasCharacter extends AbstractCoordinateShape { // set corner coordinates (for drawing background) { - final double widthHalf = TextCanvas.FONT_CHAR_WIDTH / 2d; - final double heightHalf = TextCanvas.FONT_CHAR_HEIGHT / 2d; + final double widthHalf = TextCanvas.FONT_CHAR_WIDTH_PIXELS / 2d; + final double heightHalf = TextCanvas.FONT_CHAR_HEIGHT_PIXELS / 2d; // upper left coordinates[1].coordinate = point.clone().translateX(-widthHalf) @@ -131,9 +130,10 @@ public class CanvasCharacter extends AbstractCoordinateShape { 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)); + renderingContext.graphics.drawString( + value, + (int) onScreenLocation.x - (int) (size / 3.2), + (int) onScreenLocation.y + (int) (size / 2.5)); } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/RenderMode.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/RenderMode.java index 1df4654..0dab8e0 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/RenderMode.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/RenderMode.java @@ -10,5 +10,6 @@ package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas; public enum RenderMode { - TEXTURE, CHARACTERS + TEXTURE, // Text is rendered on textured polygon + CHARACTERS // Text is rendered as high quality, anti-aliazed tiles } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/TextCanvas.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/TextCanvas.java index 845fdc5..dfb6e45 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/TextCanvas.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/TextCanvas.java @@ -22,10 +22,13 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; +import static eu.svjatoslav.sixth.e3d.renderer.raster.Color.BLACK; +import static eu.svjatoslav.sixth.e3d.renderer.raster.Color.WHITE; + public class TextCanvas extends TexturedRectangle { - public static final int FONT_CHAR_WIDTH = 8; - public static final int FONT_CHAR_HEIGHT = 16; + public static final int FONT_CHAR_WIDTH_PIXELS = 8; + public static final int FONT_CHAR_HEIGHT_PIXELS = 16; public static final Font FONT = CanvasCharacter.getFont(15); private static final String GROUP_TEXTURE = "texture"; private static final String GROUP_CHARACTERS = "characters"; @@ -33,9 +36,8 @@ public class TextCanvas extends TexturedRectangle { private final TextPointer cursorLocation = new TextPointer(); CanvasCharacter lines[][]; private RenderMode renderMode = null; - private Color backgroundColor = Color.BLACK; - - private Color foregroundColor = Color.WHITE; + private Color backgroundColor = BLACK; + private Color foregroundColor = WHITE; public TextCanvas(final Transform location, final String text, final Color foregroundColor, final Color backgroundColor) { @@ -57,7 +59,7 @@ public class TextCanvas extends TexturedRectangle { this.foregroundColor = foregroundColor; // initialize underlying textured rectangle - initialize(columns * FONT_CHAR_WIDTH, rows * FONT_CHAR_HEIGHT, 0); + initialize(columns * FONT_CHAR_WIDTH_PIXELS, rows * FONT_CHAR_HEIGHT_PIXELS, 0); getTexture().primaryBitmap.fillColor(backgroundColor); getTexture().resetResampledBitmapCache(); @@ -153,15 +155,15 @@ public class TextCanvas extends TexturedRectangle { final char character, final Color foreground) { final Graphics2D graphics = getTexture().graphics; - getTexture().primaryBitmap.drawRectangle(column * FONT_CHAR_WIDTH, row - * FONT_CHAR_HEIGHT, (column * FONT_CHAR_WIDTH) - + FONT_CHAR_WIDTH, (row * FONT_CHAR_HEIGHT) + FONT_CHAR_HEIGHT, + getTexture().primaryBitmap.drawRectangle(column * FONT_CHAR_WIDTH_PIXELS, row + * FONT_CHAR_HEIGHT_PIXELS, (column * FONT_CHAR_WIDTH_PIXELS) + + FONT_CHAR_WIDTH_PIXELS, (row * FONT_CHAR_HEIGHT_PIXELS) + FONT_CHAR_HEIGHT_PIXELS, backgroundColor); graphics.setFont(FONT); graphics.setColor(foreground.toAwtColor()); graphics.drawChars(new char[]{character,}, 0, 1, - (column * FONT_CHAR_WIDTH) - 0, (row * FONT_CHAR_HEIGHT) + 13); + (column * FONT_CHAR_WIDTH_PIXELS) - 0, (row * FONT_CHAR_HEIGHT_PIXELS) + 13); getTexture().resetResampledBitmapCache(); } @@ -169,11 +171,11 @@ public class TextCanvas extends TexturedRectangle { public Point3D getCharLocation(final int row, final int column) { final Point3D coordinate = topLeft.clone(); - coordinate.translateY((row * FONT_CHAR_HEIGHT) - + ((FONT_CHAR_HEIGHT / 2) - 3)); + coordinate.translateY((row * FONT_CHAR_HEIGHT_PIXELS) + + ((FONT_CHAR_HEIGHT_PIXELS / 2) - 3)); - coordinate.translateX((column * FONT_CHAR_WIDTH) - + (FONT_CHAR_WIDTH / 2)); + coordinate.translateX((column * FONT_CHAR_WIDTH_PIXELS) + + (FONT_CHAR_WIDTH_PIXELS / 2)); return coordinate; } -- 2.20.1