Removed ViewListener interface. Renamed View to ViewPanel.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / GuiComponent.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2018, 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.gui;
11
12 import eu.svjatoslav.sixth.e3d.geometry.Box;
13 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
14 import eu.svjatoslav.sixth.e3d.math.Transform;
15 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController;
16 import eu.svjatoslav.sixth.e3d.gui.humaninput.UserInputHandler;
17 import eu.svjatoslav.sixth.e3d.gui.textEditorComponent.KeyboardHelper;
18 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
19 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
20 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.wireframe.WireframeBox;
21
22 import java.awt.event.KeyEvent;
23
24 public class GuiComponent extends AbstractCompositeShape implements
25         UserInputHandler, MouseInteractionController {
26
27     private static final String GROUP_GUI_FOCUS = "gui.focus";
28     public final ViewContext viewContext;
29     Box containingBox = new Box();
30     private WireframeBox borders = null;
31
32     private boolean borderShown = false;
33
34     public GuiComponent(final Transform transform,
35                         final ViewContext viewContext, final Point3D size) {
36         super(transform);
37         this.viewContext = viewContext;
38         setDimensions(size);
39     }
40
41     @Override
42     public boolean beforeRender(final ViewContext viewContext,
43                                 final int millisecondsSinceLastFrame) {
44         return false;
45     }
46
47     private WireframeBox createBorder() {
48         final LineAppearance appearance = new LineAppearance(10,
49                 new eu.svjatoslav.sixth.e3d.renderer.raster.Color(255, 0, 0, 100));
50
51         final double borderSize = 10;
52
53         final Box borderArea = containingBox.clone().addBorder(borderSize);
54
55         return new WireframeBox(borderArea, appearance);
56     }
57
58     @Override
59     public void focusLost(final ViewContext viewContext) {
60         hideBorder();
61     }
62
63     @Override
64     public void focusReceived(final ViewContext viewContext) {
65         showBorder();
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 void keyPressed(final KeyEvent event, final ViewContext viewContext) {
95         if (event.getKeyChar() == KeyboardHelper.ESC)
96             viewContext.getKeyboardFocusTracker().popFocusOwner();
97     }
98
99     @Override
100     public void keyReleased(final KeyEvent event, final ViewContext viewContext) {
101     }
102
103     @Override
104     public void mouseClicked() {
105         viewContext.getKeyboardFocusTracker().setFocusOwner(this);
106     }
107
108     @Override
109     public void mouseEntered() {
110     }
111
112     @Override
113     public void mouseExited() {
114     }
115
116     private void setDimensions(final Point3D size) {
117         containingBox.setSizeCentered(size);
118     }
119
120     public void showBorder() {
121         if (borderShown)
122             return;
123         borderShown = true;
124         addShape(getBorders(), GROUP_GUI_FOCUS);
125     }
126
127 }