Removed ViewContext.
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / life / Main.java
1 package eu.svjatoslav.sixth.e3d.examples.life;
2
3 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
4 import eu.svjatoslav.sixth.e3d.geometry.Rectangle;
5 import eu.svjatoslav.sixth.e3d.gui.Avatar;
6 import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
7 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
8 import eu.svjatoslav.sixth.e3d.gui.humaninput.WorldNavigationTracker;
9 import eu.svjatoslav.sixth.e3d.math.Transform;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
11 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
12 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
13 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.wireframe.Grid2D;
14
15 import java.awt.event.KeyEvent;
16
17
18 public class Main extends WorldNavigationTracker {
19
20     private static final Matrix MATRIX = new Matrix(
21             new Point3D() // position matrix in the center of the scene
22     );
23
24     public static void main(final String[] args) {
25         new Main().run();
26     }
27
28     /**
29      * Handle keyboard input.
30      */
31     @Override
32     public void keyPressed(final KeyEvent event, final ViewPanel viewPanel) {
33         switch (event.getKeyChar()) {
34             case ' ': // space key
35                 MATRIX.evolve(false);
36                 break;
37             case 10: // ENTER
38                 MATRIX.evolve(true);
39                 break;
40             case 'c': // reset matrix
41                 MATRIX.clear();
42                 break;
43             default:
44                 super.keyPressed(event, viewPanel);
45         }
46     }
47
48     private void run() {
49
50         // create application frame visible to the user
51         final ViewFrame viewFrame = new ViewFrame();
52
53         final ShapeCollection shapeCollection = viewFrame.getViewPanel()
54                 .getRootShapeCollection();
55
56         // add matrix
57         shapeCollection.addShape(MATRIX);
58
59         // add wire-frame grid (optional)
60         shapeCollection.addShape(createGrid());
61
62         final ViewPanel viewPanel = viewFrame.getViewPanel();
63
64         setAvatarOrientation(viewPanel.getAvatar());
65
66         // enable receiving of keyboard events
67         viewPanel.getKeyboardFocusTracker().setFocusOwner(this);
68
69         // Done! World is built. So ensure screen is updated too.
70         viewPanel.repaintDuringNextViewUpdate();
71     }
72
73     /**
74      * Create pink wire-frame grid below (for decorative purposes).
75      */
76     private Grid2D createGrid() {
77         return new Grid2D(
78                 new Transform(
79                         new Point3D( // Grid positioning:
80                                 0, // center
81                                 100, // below the main scene
82                                 0), // center
83
84                         // Grid orientation:
85                         0, // no rotation along XZ axis
86                         Math.PI / 2), // face down
87
88                 new Rectangle(800), // large enough, square grid
89
90                 5, 5, // grid will be divided to 5x5 segments
91
92                 new LineAppearance(3, // line thickness
93                         new Color("FF000050") // red and quite transparent
94                 )
95         );
96     }
97
98     /**
99      * Set Avatar/Camera initial position in the world.
100      */
101     private void setAvatarOrientation(final Avatar avatar) {
102         avatar.setLocation(new Point3D(100, -50, -200));
103         avatar.setAngleXZ(0.2f);
104         avatar.setAngleYZ(-0.7f);
105     }
106
107 }