Updated readability of the code.
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / TextEditorDemo2.java
1 /*
2  * Sixth 3D engine demos. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  *
5 */
6
7 package eu.svjatoslav.sixth.e3d.examples;
8
9 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
10 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
11 import eu.svjatoslav.sixth.e3d.geometry.Rectangle;
12 import eu.svjatoslav.sixth.e3d.gui.Avatar;
13 import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
14 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
15 import eu.svjatoslav.sixth.e3d.gui.textEditorComponent.LookAndFeel;
16 import eu.svjatoslav.sixth.e3d.gui.textEditorComponent.TextEditComponent;
17 import eu.svjatoslav.sixth.e3d.math.Transform;
18 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
19 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
20 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
21 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.wireframe.Grid2D;
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.net.URISyntaxException;
28 import java.util.stream.Collectors;
29
30 public class TextEditorDemo2 {
31
32     public static void main(final String[] args) {
33         try {
34             new TextEditorDemo2().build();
35         } catch (URISyntaxException e) {
36             e.printStackTrace();
37         } catch (IOException e) {
38             e.printStackTrace();
39         }
40     }
41
42     public void build() throws URISyntaxException, IOException {
43         final ViewFrame viewFrame = new ViewFrame();
44         final ViewPanel viewPanel = viewFrame.getViewPanel();
45
46         final ShapeCollection shapeCollection = viewFrame.getViewPanel()
47                 .getRootShapeCollection();
48
49         setAvatarLocation(viewPanel);
50
51         addGrid(shapeCollection);
52
53         addCity(viewPanel, shapeCollection);
54     }
55
56     private void addCity(ViewPanel viewPanel, ShapeCollection shapeCollection) throws URISyntaxException, IOException {
57         int citySize = 4000;
58         for (int z = -citySize; z< citySize; z+= 1000 ){
59             for (int x = -citySize; x< citySize; x+= 1000 ){
60                 addBuilding(viewPanel, shapeCollection, x, z);
61             }
62         }
63     }
64
65
66     private static void addGrid(ShapeCollection shapeCollection) {
67         final Transform transform = new Transform(new Point3D(0, 100, 0), 0,
68                 Math.PI / 2);
69
70         final Rectangle rectangle = new Rectangle(10000);
71         final LineAppearance appearance = new LineAppearance(10, new Color(
72                 "00b3ad"));
73
74         shapeCollection.addShape(new Grid2D(transform, rectangle, 50, 50, appearance));
75     }
76
77
78     private void addBuilding(ViewPanel viewPanel, ShapeCollection shapeCollection, double x, double z) throws URISyntaxException, IOException {
79         Transform transform = new Transform(new Point3D(x, -390, z-200));
80         addTextEditor(viewPanel, shapeCollection, transform);
81
82         transform = new Transform(new Point3D(x, -390, z+200),Math.PI, 0);
83         addTextEditor(viewPanel, shapeCollection, transform);
84
85         transform = new Transform(new Point3D(x-200, -390, z),Math.PI/2, 0);
86         addTextEditor(viewPanel, shapeCollection, transform);
87
88         transform = new Transform(new Point3D(x+200, -390, z),Math.PI/2*3f, 0);
89         addTextEditor(viewPanel, shapeCollection, transform);
90     }
91
92     private void addTextEditor(ViewPanel viewPanel, ShapeCollection shapeCollection, Transform transform) throws IOException {
93         LookAndFeel lookAndFeel = getLookAndFeel();
94
95         final TextEditComponent textEditor = new TextEditComponent(
96                 transform,
97                 viewPanel,
98                 new Point2D(400, 1000),
99                 lookAndFeel
100                 );
101
102         String text = getResourceFileAsString("demo.txt");
103
104         textEditor.setText(text);
105         textEditor.goToLine((int)(Math.random()*200f));
106         shapeCollection.addShape(textEditor);
107     }
108
109     private LookAndFeel getLookAndFeel() {
110         LookAndFeel lookAndFeel = new LookAndFeel();
111         lookAndFeel.background = new Color(20, 20, 50, 150);
112         lookAndFeel.tabStopBackground = lookAndFeel.background;
113         lookAndFeel.foreground = new Color(150, 150, 255,250);
114         return lookAndFeel;
115     }
116
117     static String getResourceFileAsString(String fileName) throws IOException {
118         ClassLoader classLoader = ClassLoader.getSystemClassLoader();
119         try (InputStream is = classLoader.getResourceAsStream(fileName)) {
120             if (is == null) return null;
121             try (InputStreamReader isr = new InputStreamReader(is);
122                  BufferedReader reader = new BufferedReader(isr)) {
123                 return reader.lines().collect(Collectors.joining(System.lineSeparator()));
124             }
125         }
126     }
127
128     private static void setAvatarLocation(ViewPanel viewPanel) {
129         Avatar avatar = viewPanel.getAvatar();
130         avatar.setLocation(new Point3D(500, -300, -800));
131         avatar.setAngleXZ(0.6);
132         avatar.setAngleYZ(-0.5);
133     }
134 }