initial commit
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / RainingNumbersDemo.java
1 /*
2  * Sixth - System for data storage, computation, exploration and interaction.
3  * Copyright ©2012-2016, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.sixth.e3d.examples;
11
12 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
13 import eu.svjatoslav.sixth.e3d.geometry.Transform;
14 import eu.svjatoslav.sixth.e3d.gui.ViewContext;
15 import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
16 import eu.svjatoslav.sixth.e3d.gui.ViewUpdateListener;
17 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
18 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
19 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape;
20 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas;
21
22 import java.io.IOException;
23 import java.util.Collection;
24 import java.util.Random;
25
26 public class RainingNumbersDemo implements ViewUpdateListener {
27
28     private static final int NUMBERS_COUNT = 1000;
29     private final static int AREA = 600;
30     private final static int AREA_HALF = AREA / 2;
31
32     public static void main(final String[] args) throws IOException {
33         new RainingNumbersDemo().run();
34     }
35
36     @Override
37     public boolean beforeViewUpdate(final ViewContext viewContext,
38                                     final int millisecondsSinceLastFrame) {
39
40         final Collection<AbstractShape> shapes = viewContext
41                 .getRootShapeCollection().getShapes();
42
43         final double translateAmount = millisecondsSinceLastFrame / 50d;
44
45         shapes.stream().filter(shape -> shape instanceof TextCanvas).forEach(shape -> {
46             final TextCanvas block = (TextCanvas) shape;
47             final Point3D location = block.getLocation();
48             location.translateY(translateAmount);
49
50             if (location.y > AREA_HALF)
51                 location.y -= AREA;
52         });
53
54         return true;
55     }
56
57     private void run() throws IOException {
58         final ViewFrame viewFrame = new ViewFrame();
59
60         final ShapeCollection geometryCollection = viewFrame.getView()
61                 .getContext().getRootShapeCollection();
62
63         Random random = new Random();
64
65         for (int i = 0; i < NUMBERS_COUNT; i++) {
66             final Point3D location = new Point3D((Math.random() * AREA)
67                     - AREA_HALF, (Math.random() * AREA) - AREA_HALF,
68                     (Math.random() * AREA) - AREA_HALF);
69
70             final Color color = new Color(Math.random(), Math.random(),
71                     Math.random(), Math.random());
72
73             final TextCanvas textCanvas = new TextCanvas(
74                     new Transform(location), String.valueOf(random.nextInt(10)), color,
75                     Color.TRANSPARENT);
76
77             geometryCollection.addShape(textCanvas);
78         }
79
80         viewFrame.getView().addViewUpdateListener(this);
81     }
82 }