2 * Sixth 3D engine. Copyright ©2012-2019, 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 public static final double SIZE_MULTIPLIER = 0.005;
23 private static final int MAX_FONT_SIZE = 500;
24 private static final Font[] fonts = new Font[MAX_FONT_SIZE];
27 private eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor;
28 private Color foregroundAwtColor;
30 private eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor;
32 public CanvasCharacter(final Point3D point, final char character,
33 final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor,
34 final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
37 coordinates[0].coordinate = point;
39 value = String.valueOf(character);
41 this.foregroundColor = foregroundColor;
42 foregroundAwtColor = foregroundColor.toAwtColor();
44 this.backgroundColor = backgroundColor;
46 // set corner coordinates (for drawing background)
48 final double widthHalf = TextCanvas.FONT_CHAR_WIDTH / 2d;
49 final double heightHalf = TextCanvas.FONT_CHAR_HEIGHT / 2d;
52 coordinates[1].coordinate = point.clone().translateX(-widthHalf)
53 .translateY(-heightHalf);
56 coordinates[2].coordinate = point.clone().translateX(widthHalf)
57 .translateY(-heightHalf);
60 coordinates[3].coordinate = point.clone().translateX(widthHalf)
61 .translateY(heightHalf);
64 coordinates[4].coordinate = point.clone().translateX(-widthHalf)
65 .translateY(heightHalf);
70 public static Font getFont(final int size) {
71 if (fonts[size] != null)
74 final Font font = new Font("Courier", Font.BOLD, size);
79 public eu.svjatoslav.sixth.e3d.renderer.raster.Color getBackgroundColor() {
80 return backgroundColor;
83 public void setBackgroundColor(
84 final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
85 this.backgroundColor = backgroundColor;
88 public eu.svjatoslav.sixth.e3d.renderer.raster.Color getForegroundColor() {
89 return foregroundColor;
92 public void setForegroundColor(
93 final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor) {
94 this.foregroundColor = foregroundColor;
95 foregroundAwtColor = foregroundColor.toAwtColor();
99 public void paint(final RenderingContext renderingContext) {
101 SolidPolygon.drawPolygon(renderingContext,
102 coordinates[1].onScreenCoordinate,
103 coordinates[2].onScreenCoordinate,
104 coordinates[3].onScreenCoordinate, mouseInteractionController,
107 SolidPolygon.drawPolygon(renderingContext,
108 coordinates[1].onScreenCoordinate,
109 coordinates[3].onScreenCoordinate,
110 coordinates[4].onScreenCoordinate, mouseInteractionController,
113 final int size = (int) ((renderingContext.width * 4.5) / onScreenZ);
115 // do not render too large characters
116 if (size >= MAX_FONT_SIZE)
119 final Point2D onScreenLocation = coordinates[0].onScreenCoordinate;
121 // screen borders check
122 if (onScreenLocation.x < 0)
124 if (onScreenLocation.y < 0)
127 if (onScreenLocation.x > renderingContext.width)
129 if (onScreenLocation.y > renderingContext.height)
132 renderingContext.graphics.setFont(getFont(size));
133 renderingContext.graphics.setColor(foregroundAwtColor);
134 renderingContext.graphics.drawString(value, (int) onScreenLocation.x
135 - (int) (size / 3.2), (int) onScreenLocation.y
136 + (int) (size / 2.5));
140 public void setValue(final char value) {
141 this.value = String.valueOf(value);