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