Changed license to Creative Commons Zero (CC0).
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / GuiComponent.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.gui;
9
10 import eu.svjatoslav.sixth.e3d.geometry.Box;
11 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
12 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController;
13 import eu.svjatoslav.sixth.e3d.gui.humaninput.UserInputHandler;
14 import eu.svjatoslav.sixth.e3d.gui.textEditorComponent.KeyboardHelper;
15 import eu.svjatoslav.sixth.e3d.math.Transform;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
17 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
18 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.wireframe.WireframeBox;
19
20 import java.awt.event.KeyEvent;
21
22 public class GuiComponent extends AbstractCompositeShape implements
23         UserInputHandler, MouseInteractionController {
24
25     private static final String GROUP_GUI_FOCUS = "gui.focus";
26     public final ViewPanel viewPanel;
27     Box containingBox = new Box();
28     private WireframeBox borders = null;
29
30     private boolean borderShown = false;
31
32     public GuiComponent(final Transform transform,
33                         final ViewPanel viewPanel, final Point3D size) {
34         super(transform);
35         this.viewPanel = viewPanel;
36         setDimensions(size);
37     }
38
39     @Override
40     public boolean beforeRender(final ViewPanel viewPanel,
41                                 final int millisecondsSinceLastFrame) {
42         return false;
43     }
44
45     private WireframeBox createBorder() {
46         final LineAppearance appearance = new LineAppearance(10,
47                 new eu.svjatoslav.sixth.e3d.renderer.raster.Color(255, 0, 0, 100));
48
49         final double borderSize = 10;
50
51         final Box borderArea = containingBox.clone().addBorder(borderSize);
52
53         return new WireframeBox(borderArea, appearance);
54     }
55
56     @Override
57     public boolean focusLost(final ViewPanel viewPanel) {
58         hideBorder();
59         return true;
60     }
61
62     @Override
63     public boolean focusReceived(final ViewPanel viewPanel) {
64         showBorder();
65         return true;
66     }
67
68     public WireframeBox getBorders() {
69         if (borders == null)
70             borders = createBorder();
71         return borders;
72     }
73
74     public int getDepth() {
75         return (int) containingBox.getDepth();
76     }
77
78     public int getHeight() {
79         return (int) containingBox.getHeight();
80     }
81
82     public int getWidth() {
83         return (int) containingBox.getWidth();
84     }
85
86     public void hideBorder() {
87         if (!borderShown)
88             return;
89         borderShown = false;
90         removeGroup(GROUP_GUI_FOCUS);
91     }
92
93     @Override
94     public boolean keyPressed(final KeyEvent event, final ViewPanel viewPanel) {
95         if (event.getKeyChar() == KeyboardHelper.ESC)
96             viewPanel.getKeyboardFocusStack().popFocusOwner();
97         return true;
98     }
99
100     @Override
101     public boolean keyReleased(final KeyEvent event, final ViewPanel viewPanel) {
102         return false;
103     }
104
105     @Override
106     public boolean mouseClicked() {
107         return viewPanel.getKeyboardFocusStack().pushFocusOwner(this);
108     }
109
110     @Override
111     public boolean mouseEntered() {
112         return false;
113     }
114
115     @Override
116     public boolean mouseExited() {
117         return false;
118     }
119
120     private void setDimensions(final Point3D size) {
121         containingBox.setSizeCentered(size);
122     }
123
124     private void showBorder() {
125         if (borderShown)
126             return;
127         borderShown = true;
128         addShape(getBorders(), GROUP_GUI_FOCUS);
129     }
130
131 }