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