-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRayCasting.pde
158 lines (137 loc) · 5.12 KB
/
RayCasting.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
BoxBoundary canvasBoundary;
ArrayList<Boundary> boundaries;
ButtonContainer buttonContainer1;
ButtonContainer buttonContainer2;
CasterContainer caster;
Config config;
void saveBoundariesToFile() {
JSONArray list = new JSONArray();
for (int i = 0; i < boundaries.size(); i++) {
Boundary b = boundaries.get(i);
JSONArray pointList = new JSONArray();
for (int j = 0; j < b.points.size(); j++) {
Point p = b.points.get(j);
JSONArray pt = new JSONArray();
pt.setFloat(0, (float) p.x);
pt.setFloat(1, (float) p.y);
pointList.setJSONArray(j, pt);
}
list.setJSONArray(i, pointList);
}
saveJSONArray(list, "data/boundaries.json");
}
ArrayList<Boundary> createInitialLayout() {
JSONArray jsonBoundaries = loadJSONArray("boundaries.json");
ArrayList<Boundary> boundaryList = new ArrayList<Boundary>();
for (int i = 0; i < jsonBoundaries.size(); i++) {
JSONArray jsonPoints = jsonBoundaries.getJSONArray(i);
ArrayList<Point> boundaryPoints = new ArrayList<Point>();
for (int j = 0; j < jsonPoints.size(); j++) {
JSONArray jsonPoint = jsonPoints.getJSONArray(j);
boundaryPoints.add(new Point(jsonPoint.getFloat(0), jsonPoint.getFloat(1)));
}
boundaryList.add(new Boundary(boundaryPoints));
}
return boundaryList;
}
void setup() {
// Define the canvas width and height
//size(1900, 1000);
fullScreen();
noiseDetail(2, 0.7);
ArrayList<Button> buttons = new ArrayList<Button>();
buttons.add(new Button("Edit Boundaries", () -> { config.editing = !config.editing; }));
buttons.add(new Button("Automatic Movement", () -> { config.autoMove = !config.autoMove; }));
buttons.add(new Button("Snap Node On Edit", () -> { config.snapNodes = !config.snapNodes; }));
buttons.add(new Button("Show Rays", () -> { config.casting = !config.casting; }));
buttons.add(new Button("Show Cast Background", () -> { config.background = !config.background; }));
buttonContainer1 = new ButtonContainer(buttons, 30, 30);
ArrayList<Button> addButtons = new ArrayList<Button>();
addButtons.add(new Button("Clear Boundaries", () -> { boundaries = new ArrayList<Boundary>(); }));
addButtons.add(new Button("+2 Boundary", () -> { boundaries.add(new Boundary(2)); }));
addButtons.add(new Button("+3", () -> { boundaries.add(new Boundary(3)); }));
addButtons.add(new Button("+4", () -> { boundaries.add(new Boundary(4)); }));
addButtons.add(new Button("+5", () -> { boundaries.add(new Boundary(5)); }));
buttonContainer2 = new ButtonContainer(addButtons, 30, 30 + 55);
config = new Config(width / 2, height / 2);
caster = new CasterContainer(config.centerPosition, #33a1fd);
canvasBoundary = new BoxBoundary(new Point(0, 0), new Point(width, 0), new Point(width, height), new Point(0, height), 255, 0);
boundaries = createInitialLayout();
for (String font : PFont.list())
println(font);
PFont myFont = createFont("Monospaced", 32);
textFont(myFont);
}
void draw() {
background(#f4d06f);
if (config.autoMove) {
caster.setPosition(noise(config.xoff) * width, noise(config.yoff) * height);
config.xoff += 0.01;
config.yoff += 0.01;
} else {
caster.setPosition(mouseX, mouseY);
}
ArrayList<LineBoundary> tempArray = new ArrayList<LineBoundary>();
canvasBoundary.getLines().forEach((line) ->
tempArray.add(line));
boundaries.forEach((boundary) ->
boundary.lines.forEach((line) ->
tempArray.add(line)));
buttonContainer1.buttons.forEach((boundary) ->
boundary.lines.forEach((line) ->
tempArray.add(line)));
buttonContainer2.buttons.forEach((boundary) ->
boundary.lines.forEach((line) ->
tempArray.add(line)));
if (!config.clicking) {
if (config.background) caster.castBackground(tempArray);
if (config.casting) caster.castRays(tempArray);
}
buttonContainer1.show();
buttonContainer2.show();
boundaries.forEach((b) ->
b.show(config.editing));
}
void mouseReleased() {
config.clicking = false;
for (Boundary b : boundaries) {
for (Point pt : b.points) {
if (pt.getMoving()) {
pt.setMoving(false);
}
}
}
}
void mousePressed() {
config.clicking = true;
for (int i = boundaries.size(); i > 0; i--) {
for (int j = boundaries.get(i - 1).points.size(); j > 0; j--) {
if (boundaries.get(i - 1).points.get(j - 1).hovering()) {
boundaries.get(i - 1).points.get(j - 1).setMoving(true);
if (!config.snapNodes) return;
}
}
}
PVector mouse = new PVector(mouseX, mouseY);
for (Button button : buttonContainer1.buttons)
if (button.collides(mouse)) button.onClick();
for (Button button : buttonContainer2.buttons)
if (button.collides(mouse)) button.onClick();
}
void keyPressed() {
if (keyCode == 69) {
config.editing = !config.editing;
} else if (keyCode == 65) {
boundaries.add(new Boundary(3));
} else if (keyCode == 83) {
config.snapNodes = !config.snapNodes;
} else if (keyCode == 77) {
config.autoMove = !config.autoMove;
} else if (keyCode == 67) {
config.casting = !config.casting;
} else if (keyCode == 66) {
config.background = !config.background;
} else if (keyCode == 70) {
saveBoundariesToFile();
}
}