X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fe3d%2Frenderer%2Fraster%2Fshapes%2Fcomposite%2Ftextcanvas%2FTextCanvas.java;h=4ffef134784025ef53d769c2b461587329e4b5c5;hb=HEAD;hp=956cf2c7599f119d195c692d63d359dc7e51e57e;hpb=baab2e2c2ad89695293f3136311c585c9a5afed1;p=sixth-3d.git 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 956cf2c..4ffef13 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 @@ -1,19 +1,14 @@ /* - * 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.Point3D; -import eu.svjatoslav.sixth.e3d.math.Transform; -import eu.svjatoslav.sixth.e3d.math.TransformPipe; 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.TransformsStack; import eu.svjatoslav.sixth.e3d.renderer.raster.Color; import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.TexturedRectangle; @@ -22,20 +17,43 @@ 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; +import static java.lang.Math.PI; +import static java.lang.Math.abs; + public class TextCanvas extends TexturedRectangle { + /** + * 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; - public static final Font FONT = CanvasCharacter.getFont(15); + + /** + * 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)); 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 = 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 +75,12 @@ 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, + rows * FONT_CHAR_HEIGHT, + columns * FONT_CHAR_WIDTH_TEXTURE_PIXELS, + rows * FONT_CHAR_HEIGHT_TEXTURE_PIXELS, + 0); getTexture().primaryBitmap.fillColor(backgroundColor); getTexture().resetResampledBitmapCache(); @@ -109,34 +132,31 @@ public class TextCanvas extends TexturedRectangle { } @Override - public void beforeTransformHook(final TransformPipe 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(' '); @@ -153,16 +173,19 @@ 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_TEXTURE_PIXELS, + row * FONT_CHAR_HEIGHT_TEXTURE_PIXELS, + (column * FONT_CHAR_WIDTH_TEXTURE_PIXELS) + FONT_CHAR_WIDTH_TEXTURE_PIXELS, + (row * FONT_CHAR_HEIGHT_TEXTURE_PIXELS) + FONT_CHAR_HEIGHT_TEXTURE_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); - + graphics.drawChars( + new char[]{character,}, 0, 1, + (column * FONT_CHAR_WIDTH_TEXTURE_PIXELS), + (row * FONT_CHAR_HEIGHT_TEXTURE_PIXELS) + (int) (FONT_CHAR_HEIGHT_TEXTURE_PIXELS / 1.23f)); getTexture().resetResampledBitmapCache(); } @@ -170,7 +193,7 @@ public class TextCanvas extends TexturedRectangle { final Point3D coordinate = topLeft.clone(); coordinate.translateY((row * FONT_CHAR_HEIGHT) - + ((FONT_CHAR_HEIGHT / 2) - 3)); + + (FONT_CHAR_HEIGHT / 3.2)); coordinate.translateX((column * FONT_CHAR_WIDTH) + (FONT_CHAR_WIDTH / 2)); @@ -282,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); - // - // } }