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