c66d5b5b4592f87188b31ca2a147a35ebd28cd9b
[sixth-3d.git] /
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas;
6
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;
12
13 import java.awt.*;
14
15 public class CanvasCharacter extends AbstractCoordinateShape {
16
17     private static final int MAX_FONT_SIZE = 500;
18     private static final Font[] fonts = new Font[MAX_FONT_SIZE];
19     private String value;
20
21     private eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor;
22     private Color foregroundAwtColor;
23
24     private eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor;
25
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) {
29
30         super(5);
31         coordinates[0].coordinate = point;
32
33         value = String.valueOf(character);
34
35         this.foregroundColor = foregroundColor;
36         foregroundAwtColor = foregroundColor.toAwtColor();
37
38         this.backgroundColor = backgroundColor;
39
40         // set corner coordinates (for drawing background)
41         {
42             final double widthHalf = TextCanvas.FONT_CHAR_WIDTH / 2d;
43             final double heightHalf = TextCanvas.FONT_CHAR_HEIGHT / 2d;
44
45             // upper left
46             coordinates[1].coordinate = point.clone().translateX(-widthHalf)
47                     .translateY(-heightHalf);
48
49             // upper right
50             coordinates[2].coordinate = point.clone().translateX(widthHalf)
51                     .translateY(-heightHalf);
52
53             // lower right
54             coordinates[3].coordinate = point.clone().translateX(widthHalf)
55                     .translateY(heightHalf);
56
57             // lower left
58             coordinates[4].coordinate = point.clone().translateX(-widthHalf)
59                     .translateY(heightHalf);
60
61         }
62     }
63
64     public static Font getFont(final int size) {
65         if (fonts[size] != null)
66             return fonts[size];
67
68         final Font font = new Font("Courier", Font.BOLD, size);
69         fonts[size] = font;
70         return font;
71     }
72
73     public eu.svjatoslav.sixth.e3d.renderer.raster.Color getBackgroundColor() {
74         return backgroundColor;
75     }
76
77     public void setBackgroundColor(
78             final eu.svjatoslav.sixth.e3d.renderer.raster.Color backgroundColor) {
79         this.backgroundColor = backgroundColor;
80     }
81
82     public eu.svjatoslav.sixth.e3d.renderer.raster.Color getForegroundColor() {
83         return foregroundColor;
84     }
85
86     public void setForegroundColor(
87             final eu.svjatoslav.sixth.e3d.renderer.raster.Color foregroundColor) {
88         this.foregroundColor = foregroundColor;
89         foregroundAwtColor = foregroundColor.toAwtColor();
90     }
91
92     @Override
93     public void paint(final RenderingContext renderingContext) {
94
95         SolidPolygon.drawPolygon(renderingContext,
96                 coordinates[1].onScreenCoordinate,
97                 coordinates[2].onScreenCoordinate,
98                 coordinates[3].onScreenCoordinate, mouseInteractionController,
99                 backgroundColor);
100
101         SolidPolygon.drawPolygon(renderingContext,
102                 coordinates[1].onScreenCoordinate,
103                 coordinates[3].onScreenCoordinate,
104                 coordinates[4].onScreenCoordinate, mouseInteractionController,
105                 backgroundColor);
106
107         final int size = (int) ((renderingContext.width * 4.5) / onScreenZ);
108
109         // do not render too large characters
110         if (size >= MAX_FONT_SIZE)
111             return;
112
113         final Point2D onScreenLocation = coordinates[0].onScreenCoordinate;
114
115         // screen borders check
116         if (onScreenLocation.x < 0)
117             return;
118         if (onScreenLocation.y < 0)
119             return;
120
121         if (onScreenLocation.x > renderingContext.width)
122             return;
123         if (onScreenLocation.y > renderingContext.height)
124             return;
125
126         renderingContext.graphics.setFont(getFont(size));
127         renderingContext.graphics.setColor(foregroundAwtColor);
128         renderingContext.graphics.drawString(
129                 value,
130                 (int) onScreenLocation.x - (int) (size / 3.2),
131                 (int) onScreenLocation.y + (int) (size / 2.5));
132
133     }
134
135     public void setValue(final char value) {
136         this.value = String.valueOf(value);
137     }
138
139 }