Refactoring.
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / RainingNumbersDemo.java
1 /*
2  * Sixth 3D engine demos. 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 package eu.svjatoslav.sixth.e3d.examples;
10
11 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
12 import eu.svjatoslav.sixth.e3d.math.Transform;
13 import eu.svjatoslav.sixth.e3d.gui.ViewContext;
14 import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
15 import eu.svjatoslav.sixth.e3d.gui.ViewRenderListener;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
17 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
18 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape;
19 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas;
20
21 import java.io.IOException;
22 import java.util.Collection;
23 import java.util.Random;
24
25 public class RainingNumbersDemo implements ViewRenderListener {
26
27     private static final int NUMBERS_COUNT = 1000;
28     private final static int AREA = 600;
29     private final static int AREA_HALF = AREA / 2;
30
31     public static void main(final String[] args) throws IOException {
32         new RainingNumbersDemo().run();
33     }
34
35     @Override
36     public boolean beforeRender(final ViewContext viewContext,
37                                 final int millisecondsSinceLastFrame) {
38
39         final Collection<AbstractShape> shapes = viewContext
40                 .getRootShapeCollection().getShapes();
41
42         final double translateAmount = millisecondsSinceLastFrame / 50d;
43
44         shapes.stream().filter(shape -> shape instanceof TextCanvas).forEach(shape -> {
45             final TextCanvas block = (TextCanvas) shape;
46             final Point3D location = block.getLocation();
47             location.translateY(translateAmount);
48
49             if (location.y > AREA_HALF)
50                 location.y -= AREA;
51         });
52
53         return true;
54     }
55
56     private void run() throws IOException {
57         final ViewFrame viewFrame = new ViewFrame();
58
59         final ShapeCollection geometryCollection = viewFrame.getView()
60                 .getContext().getRootShapeCollection();
61
62         Random random = new Random();
63
64         for (int i = 0; i < NUMBERS_COUNT; i++) {
65             final Point3D location = new Point3D((Math.random() * AREA)
66                     - AREA_HALF, (Math.random() * AREA) - AREA_HALF,
67                     (Math.random() * AREA) - AREA_HALF);
68
69             final Color color = new Color(Math.random(), Math.random(),
70                     Math.random(), Math.random());
71
72             final TextCanvas textCanvas = new TextCanvas(
73                     new Transform(location), String.valueOf(random.nextInt(10)), color,
74                     Color.TRANSPARENT);
75
76             geometryCollection.addShape(textCanvas);
77         }
78
79         viewFrame.getView().addViewUpdateListener(this);
80     }
81 }