2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas;
7 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
8 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
9 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape;
11 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon;
15 import static java.lang.String.valueOf;
18 * Represents a single character on the text canvas.
20 public class CanvasCharacter extends AbstractCoordinateShape {
22 private static final int MAX_FONT_SIZE = 500;
27 private static final Font[] fonts = new Font[MAX_FONT_SIZE];
30 * The character to be rendered.
34 private eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor;
36 private eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor;
38 public CanvasCharacter(final Point3D point, final char character,
39 final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor,
40 final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
43 coordinates[0].coordinate = point;
47 this.foregroundColor = foregroundColor;
49 this.backgroundColor = backgroundColor;
51 // set corner coordinates (for drawing background)
53 final double widthHalf = TextCanvas.FONT_CHAR_WIDTH / 2d;
54 final double heightHalf = TextCanvas.FONT_CHAR_HEIGHT / 2d;
57 coordinates[1].coordinate = point.clone().translateX(-widthHalf)
58 .translateY(-heightHalf);
61 coordinates[2].coordinate = point.clone().translateX(widthHalf)
62 .translateY(-heightHalf);
65 coordinates[3].coordinate = point.clone().translateX(widthHalf)
66 .translateY(heightHalf);
69 coordinates[4].coordinate = point.clone().translateX(-widthHalf)
70 .translateY(heightHalf);
76 * Returns a font of the specified size.
78 * If the font of the specified size is already cached, it will be
79 * returned. Otherwise, a new font will be created, cached and returned.
82 * @param size the size of the font
85 public static Font getFont(final int size) {
86 if (fonts[size] != null)
89 final Font font = new Font("Courier", Font.BOLD, size);
95 * Returns color of the background.
97 public eu.svjatoslav.sixth.e3d.renderer.raster.Color getBackgroundColor() {
98 return backgroundColor;
102 * Sets color of the background.
104 public void setBackgroundColor(
105 final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
106 this.backgroundColor = backgroundColor;
110 * Returns color of the foreground.
113 public eu.svjatoslav.sixth.e3d.renderer.raster.Color getForegroundColor() {
114 return foregroundColor;
118 * Sets color of the foreground.
119 * @param foregroundColor the color
121 public void setForegroundColor(
122 final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor) {
123 this.foregroundColor = foregroundColor;
127 public void paint(final RenderingContext renderingContext) {
129 SolidPolygon.drawPolygon(renderingContext,
130 coordinates[1].onScreenCoordinate,
131 coordinates[2].onScreenCoordinate,
132 coordinates[3].onScreenCoordinate, mouseInteractionController,
135 SolidPolygon.drawPolygon(renderingContext,
136 coordinates[1].onScreenCoordinate,
137 coordinates[3].onScreenCoordinate,
138 coordinates[4].onScreenCoordinate, mouseInteractionController,
141 final int size = (int) ((renderingContext.width * 4.5) / onScreenZ);
143 // do not render too large characters
144 if (size >= MAX_FONT_SIZE)
147 final Point2D onScreenLocation = coordinates[0].onScreenCoordinate;
149 // screen borders check
150 if (onScreenLocation.x < 0)
152 if (onScreenLocation.y < 0)
155 if (onScreenLocation.x > renderingContext.width)
157 if (onScreenLocation.y > renderingContext.height)
160 renderingContext.graphics.setFont(getFont(size));
161 renderingContext.graphics.setColor(foregroundColor.toAwtColor());
162 renderingContext.graphics.drawString(
164 (int) onScreenLocation.x - (int) (size / 3.2),
165 (int) onScreenLocation.y + (int) (size / 2.5));
169 public void setValue(final char value) {