2 * Sixth 3D engine. Copyright ©2012-2020, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 3 of the GNU Lesser General Public License
6 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas;
12 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
13 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
14 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
15 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon;
20 public class CanvasCharacter extends AbstractCoordinateShape {
22 private static final int MAX_FONT_SIZE = 500;
23 private static final Font[] fonts = new Font[MAX_FONT_SIZE];
26 private eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor;
27 private Color foregroundAwtColor;
29 private eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor;
31 public CanvasCharacter(final Point3D point, final char character,
32 final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor,
33 final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
36 coordinates[0].coordinate = point;
38 value = String.valueOf(character);
40 this.foregroundColor = foregroundColor;
41 foregroundAwtColor = foregroundColor.toAwtColor();
43 this.backgroundColor = backgroundColor;
45 // set corner coordinates (for drawing background)
47 final double widthHalf = TextCanvas.FONT_CHAR_WIDTH_PIXELS / 2d;
48 final double heightHalf = TextCanvas.FONT_CHAR_HEIGHT_PIXELS / 2d;
51 coordinates[1].coordinate = point.clone().translateX(-widthHalf)
52 .translateY(-heightHalf);
55 coordinates[2].coordinate = point.clone().translateX(widthHalf)
56 .translateY(-heightHalf);
59 coordinates[3].coordinate = point.clone().translateX(widthHalf)
60 .translateY(heightHalf);
63 coordinates[4].coordinate = point.clone().translateX(-widthHalf)
64 .translateY(heightHalf);
69 public static Font getFont(final int size) {
70 if (fonts[size] != null)
73 final Font font = new Font("Courier", Font.BOLD, size);
78 public eu.svjatoslav.sixth.e3d.renderer.raster.Color getBackgroundColor() {
79 return backgroundColor;
82 public void setBackgroundColor(
83 final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
84 this.backgroundColor = backgroundColor;
87 public eu.svjatoslav.sixth.e3d.renderer.raster.Color getForegroundColor() {
88 return foregroundColor;
91 public void setForegroundColor(
92 final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor) {
93 this.foregroundColor = foregroundColor;
94 foregroundAwtColor = foregroundColor.toAwtColor();
98 public void paint(final RenderingContext renderingContext) {
100 SolidPolygon.drawPolygon(renderingContext,
101 coordinates[1].onScreenCoordinate,
102 coordinates[2].onScreenCoordinate,
103 coordinates[3].onScreenCoordinate, mouseInteractionController,
106 SolidPolygon.drawPolygon(renderingContext,
107 coordinates[1].onScreenCoordinate,
108 coordinates[3].onScreenCoordinate,
109 coordinates[4].onScreenCoordinate, mouseInteractionController,
112 final int size = (int) ((renderingContext.width * 4.5) / onScreenZ);
114 // do not render too large characters
115 if (size >= MAX_FONT_SIZE)
118 final Point2D onScreenLocation = coordinates[0].onScreenCoordinate;
120 // screen borders check
121 if (onScreenLocation.x < 0)
123 if (onScreenLocation.y < 0)
126 if (onScreenLocation.x > renderingContext.width)
128 if (onScreenLocation.y > renderingContext.height)
131 renderingContext.graphics.setFont(getFont(size));
132 renderingContext.graphics.setColor(foregroundAwtColor);
133 renderingContext.graphics.drawString(
135 (int) onScreenLocation.x - (int) (size / 3.2),
136 (int) onScreenLocation.y + (int) (size / 2.5));
140 public void setValue(final char value) {
141 this.value = String.valueOf(value);