Updated documentation. Added game of life. Refactoring launcher panel.
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / life / Cell.java
1 package eu.svjatoslav.sixth.e3d.examples.life;
2
3 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
4 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController;
5 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
6 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon;
7 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
8
9 /**
10  * This class corresponds to a single cell within matrix.
11  */
12 public class Cell extends AbstractCompositeShape implements
13         MouseInteractionController {
14
15     /**
16      * cell visual size
17      */
18     public static final int SIZE = 20;
19     /**
20      * Color of the active cell (R, G, B, A)
21      */
22     private static final Color ACTIVE_COLOR = new Color("A8FF");
23     /**
24      * Color of the active cell (R, G, B, A) while mouse is over it.
25      */
26     private static final Color ACTIVE_COLOR_MOUSE_OVER = new Color("F9FF");
27     /**
28      * Color of the inactive cell (R, G, B, A)
29      */
30     private static final Color INACTIVE_COLOR = new Color("55F5");
31     /**
32      * Color of the inactive cell (R, G, B, A) while mouse is over it.
33      */
34     private static final Color INACTIVE_COLOR_MOUSE_OVER = new Color("77F7");
35     /**
36      * A placeholder variable to help in next generation computation. Indicates
37      * whether cell is going to survive within next generation.
38      */
39     public boolean survives;
40     /**
41      * Indicates whether cell is currently active
42      */
43     private boolean active;
44     /**
45      * Indicates whether mouse pointer is currently over this cell.
46      */
47     private boolean isMouseOver = false;
48
49     public Cell(final Point3D center) {
50         super(center);
51
52         createCellShape();
53
54         // enable receiving of mouse events
55         setMouseInteractionController(this);
56     }
57
58     private void createCellShape() {
59         final double halfSize = SIZE / 2;
60
61         // define 4 points corresponding to cell borders
62         final Point3D p1 = new Point3D(-halfSize, 0, -halfSize);
63
64         final Point3D p2 = new Point3D(+halfSize, 0, -halfSize);
65
66         final Point3D p3 = new Point3D(+halfSize, 0, +halfSize);
67
68         final Point3D p4 = new Point3D(-halfSize, 0, +halfSize);
69
70         // connect 4 points with 2 polygons
71         addShape(new SolidPolygon(p1, p2, p3, computeCellColor()));
72         addShape(new SolidPolygon(p1, p4, p3, computeCellColor()));
73     }
74
75     /**
76      * Compute cell color depending if cell is active and if mouse is over the
77      * cell.
78      */
79     private Color computeCellColor() {
80         if (active)
81             if (isMouseOver)
82                 return ACTIVE_COLOR_MOUSE_OVER;
83             else
84                 return ACTIVE_COLOR;
85         else if (isMouseOver)
86             return INACTIVE_COLOR_MOUSE_OVER;
87         else
88             return INACTIVE_COLOR;
89     }
90
91     public boolean isActive() {
92         return active;
93     }
94
95     public void setActive(final boolean active) {
96         this.active = active;
97         updateColor();
98     }
99
100     @Override
101     public void mouseClicked() {
102         setActive(!isActive());
103     }
104
105     @Override
106     public void mouseEntered() {
107         setMouseOver(true);
108     }
109
110     @Override
111     public void mouseExited() {
112         setMouseOver(false);
113     }
114
115     private void setMouseOver(final boolean isMouseOver) {
116         this.isMouseOver = isMouseOver;
117         updateColor();
118     }
119
120     /**
121      * This method is called when cell status is changed to update its color
122      * too.
123      */
124     private void updateColor() {
125         setColor(computeCellColor());
126     }
127
128 }