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 public class CanvasCharacter extends AbstractCoordinateShape {
17 private static final int MAX_FONT_SIZE = 500;
18 private static final Font[] fonts = new Font[MAX_FONT_SIZE];
21 private eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor;
22 private Color foregroundAwtColor;
24 private eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor;
26 public CanvasCharacter(final Point3D point, final char character,
27 final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor,
28 final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
31 coordinates[0].coordinate = point;
33 value = String.valueOf(character);
35 this.foregroundColor = foregroundColor;
36 foregroundAwtColor = foregroundColor.toAwtColor();
38 this.backgroundColor = backgroundColor;
40 // set corner coordinates (for drawing background)
42 final double widthHalf = TextCanvas.FONT_CHAR_WIDTH / 2d;
43 final double heightHalf = TextCanvas.FONT_CHAR_HEIGHT / 2d;
46 coordinates[1].coordinate = point.clone().translateX(-widthHalf)
47 .translateY(-heightHalf);
50 coordinates[2].coordinate = point.clone().translateX(widthHalf)
51 .translateY(-heightHalf);
54 coordinates[3].coordinate = point.clone().translateX(widthHalf)
55 .translateY(heightHalf);
58 coordinates[4].coordinate = point.clone().translateX(-widthHalf)
59 .translateY(heightHalf);
64 public static Font getFont(final int size) {
65 if (fonts[size] != null)
68 final Font font = new Font("Courier", Font.BOLD, size);
73 public eu.svjatoslav.sixth.e3d.renderer.raster.Color getBackgroundColor() {
74 return backgroundColor;
77 public void setBackgroundColor(
78 final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
79 this.backgroundColor = backgroundColor;
82 public eu.svjatoslav.sixth.e3d.renderer.raster.Color getForegroundColor() {
83 return foregroundColor;
86 public void setForegroundColor(
87 final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor) {
88 this.foregroundColor = foregroundColor;
89 foregroundAwtColor = foregroundColor.toAwtColor();
93 public void paint(final RenderingContext renderingContext) {
95 SolidPolygon.drawPolygon(renderingContext,
96 coordinates[1].onScreenCoordinate,
97 coordinates[2].onScreenCoordinate,
98 coordinates[3].onScreenCoordinate, mouseInteractionController,
101 SolidPolygon.drawPolygon(renderingContext,
102 coordinates[1].onScreenCoordinate,
103 coordinates[3].onScreenCoordinate,
104 coordinates[4].onScreenCoordinate, mouseInteractionController,
107 final int size = (int) ((renderingContext.width * 4.5) / onScreenZ);
109 // do not render too large characters
110 if (size >= MAX_FONT_SIZE)
113 final Point2D onScreenLocation = coordinates[0].onScreenCoordinate;
115 // screen borders check
116 if (onScreenLocation.x < 0)
118 if (onScreenLocation.y < 0)
121 if (onScreenLocation.x > renderingContext.width)
123 if (onScreenLocation.y > renderingContext.height)
126 renderingContext.graphics.setFont(getFont(size));
127 renderingContext.graphics.setColor(foregroundAwtColor);
128 renderingContext.graphics.drawString(
130 (int) onScreenLocation.x - (int) (size / 3.2),
131 (int) onScreenLocation.y + (int) (size / 2.5));
135 public void setValue(final char value) {
136 this.value = String.valueOf(value);