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