1 package eu.svjatoslav.sixth.e3d.examples.life;
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;
10 * This class corresponds to a single cell within matrix.
12 public class Cell extends AbstractCompositeShape implements
13 MouseInteractionController {
18 public static final int SIZE = 20;
20 * Color of the active cell (R, G, B, A)
22 private static final Color ACTIVE_COLOR = new Color("A8FF");
24 * Color of the active cell (R, G, B, A) while mouse is over it.
26 private static final Color ACTIVE_COLOR_MOUSE_OVER = new Color("F9FF");
28 * Color of the inactive cell (R, G, B, A)
30 private static final Color INACTIVE_COLOR = new Color("55F5");
32 * Color of the inactive cell (R, G, B, A) while mouse is over it.
34 private static final Color INACTIVE_COLOR_MOUSE_OVER = new Color("77F7");
36 * A placeholder variable to help in next generation computation. Indicates
37 * whether cell is going to survive within next generation.
39 public boolean survives;
41 * Indicates whether cell is currently active
43 private boolean active;
45 * Indicates whether mouse pointer is currently over this cell.
47 private boolean isMouseOver = false;
49 public Cell(final Point3D center) {
54 // enable receiving of mouse events
55 setMouseInteractionController(this);
58 private void createCellShape() {
59 final double halfSize = SIZE / 2;
61 // define 4 points corresponding to cell borders
62 final Point3D p1 = new Point3D(-halfSize, 0, -halfSize);
64 final Point3D p2 = new Point3D(+halfSize, 0, -halfSize);
66 final Point3D p3 = new Point3D(+halfSize, 0, +halfSize);
68 final Point3D p4 = new Point3D(-halfSize, 0, +halfSize);
70 // connect 4 points with 2 polygons
71 addShape(new SolidPolygon(p1, p2, p3, computeCellColor()));
72 addShape(new SolidPolygon(p1, p4, p3, computeCellColor()));
76 * Compute cell color depending if cell is active and if mouse is over the
79 private Color computeCellColor() {
82 return ACTIVE_COLOR_MOUSE_OVER;
86 return INACTIVE_COLOR_MOUSE_OVER;
88 return INACTIVE_COLOR;
91 public boolean isActive() {
95 public void setActive(final boolean active) {
101 public void mouseClicked() {
102 setActive(!isActive());
106 public void mouseEntered() {
111 public void mouseExited() {
115 private void setMouseOver(final boolean isMouseOver) {
116 this.isMouseOver = isMouseOver;
121 * This method is called when cell status is changed to update its color
124 private void updateColor() {
125 setColor(computeCellColor());