2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
8 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas;
10 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
11 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
12 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
13 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape;
14 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon;
18 public class CanvasCharacter extends AbstractCoordinateShape {
20 private static final int MAX_FONT_SIZE = 500;
21 private static final Font[] fonts = new Font[MAX_FONT_SIZE];
24 private eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor;
25 private Color foregroundAwtColor;
27 private eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor;
29 public CanvasCharacter(final Point3D point, final char character,
30 final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor,
31 final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
34 coordinates[0].coordinate = point;
36 value = String.valueOf(character);
38 this.foregroundColor = foregroundColor;
39 foregroundAwtColor = foregroundColor.toAwtColor();
41 this.backgroundColor = backgroundColor;
43 // set corner coordinates (for drawing background)
45 final double widthHalf = TextCanvas.FONT_CHAR_WIDTH_PIXELS / 2d;
46 final double heightHalf = TextCanvas.FONT_CHAR_HEIGHT_PIXELS / 2d;
49 coordinates[1].coordinate = point.clone().translateX(-widthHalf)
50 .translateY(-heightHalf);
53 coordinates[2].coordinate = point.clone().translateX(widthHalf)
54 .translateY(-heightHalf);
57 coordinates[3].coordinate = point.clone().translateX(widthHalf)
58 .translateY(heightHalf);
61 coordinates[4].coordinate = point.clone().translateX(-widthHalf)
62 .translateY(heightHalf);
67 public static Font getFont(final int size) {
68 if (fonts[size] != null)
71 final Font font = new Font("Courier", Font.BOLD, size);
76 public eu.svjatoslav.sixth.e3d.renderer.raster.Color getBackgroundColor() {
77 return backgroundColor;
80 public void setBackgroundColor(
81 final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
82 this.backgroundColor = backgroundColor;
85 public eu.svjatoslav.sixth.e3d.renderer.raster.Color getForegroundColor() {
86 return foregroundColor;
89 public void setForegroundColor(
90 final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor) {
91 this.foregroundColor = foregroundColor;
92 foregroundAwtColor = foregroundColor.toAwtColor();
96 public void paint(final RenderingContext renderingContext) {
98 SolidPolygon.drawPolygon(renderingContext,
99 coordinates[1].onScreenCoordinate,
100 coordinates[2].onScreenCoordinate,
101 coordinates[3].onScreenCoordinate, mouseInteractionController,
104 SolidPolygon.drawPolygon(renderingContext,
105 coordinates[1].onScreenCoordinate,
106 coordinates[3].onScreenCoordinate,
107 coordinates[4].onScreenCoordinate, mouseInteractionController,
110 final int size = (int) ((renderingContext.width * 4.5) / onScreenZ);
112 // do not render too large characters
113 if (size >= MAX_FONT_SIZE)
116 final Point2D onScreenLocation = coordinates[0].onScreenCoordinate;
118 // screen borders check
119 if (onScreenLocation.x < 0)
121 if (onScreenLocation.y < 0)
124 if (onScreenLocation.x > renderingContext.width)
126 if (onScreenLocation.y > renderingContext.height)
129 renderingContext.graphics.setFont(getFont(size));
130 renderingContext.graphics.setColor(foregroundAwtColor);
131 renderingContext.graphics.drawString(
133 (int) onScreenLocation.x - (int) (size / 3.2),
134 (int) onScreenLocation.y + (int) (size / 2.5));
138 public void setValue(final char value) {
139 this.value = String.valueOf(value);