-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.js
274 lines (222 loc) · 6.04 KB
/
gui.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { Grid } from "./grid.js"
import { Node } from "./node.js"
import { Face } from "./face.js"
import { Manager } from "./manager.js"
import { ContextMenu } from "./contextmenu.js"
export class GUI
{
// Buttons
static hButtonShow = null;
static hButtonNode = null;
static hButtonLine = null;
static hButtonRect = null;
// Input fields
// - Numbers
static hNumberGridSpanX = null;
static hNumberGridSpanY = null;
// Canvas
static canvas = null;
static canvasRect = null;
static ctx = null;
// Helper
static drag = "o";
// General infos
static status = null;
// Debug infos
static debugNodes = null;
static debugFaces = null;
static debugCurrentNodes = null;
static debugCurrentFaces = null;
static create ()
{
document.body.addEventListener ("keydown", GUI.keyDown);
document.body.addEventListener ("keyup", GUI.keyUp);
// Buttons
GUI.hButtonShow = document.getElementById ("navbar-buttons_show");
GUI.hButtonShow.addEventListener ("mousedown", GUI.showAll);
GUI.hButtonShow.addEventListener ("mouseup", GUI.hideAll);
GUI.hButtonShow.addEventListener ("mouseleave", GUI.hideAll);
GUI.hButtonNode = document.getElementById ("sidebar-buttons_add_node");
GUI.hButtonNode.addEventListener ("click", Manager.createNode);
GUI.hButtonLine = document.getElementById ("sidebar-buttons_add_line");
GUI.hButtonLine.addEventListener ("click", Manager.createLine);
GUI.hButtonRect = document.getElementById ("sidebar-buttons_add_rect");
GUI.hButtonRect.addEventListener ("click", Manager.createRectangle);
GUI.hButtonClear = document.getElementById ("sidebar-buttons_clear");
GUI.hButtonClear.addEventListener ("click", GUI.buttonClear);
// Input fields
// - Numbers
GUI.hNumberGridSpanX = document.getElementById ("sidebar-grid_properties_span_x");
GUI.hNumberGridSpanX.value = Grid.getSpanX ();
GUI.hNumberGridSpanX.addEventListener ("change", GUI.gridSpanXChange);
GUI.hNumberGridSpanY = document.getElementById ("sidebar-grid_properties_span_y");
GUI.hNumberGridSpanY.value = Grid.getSpanY ();
GUI.hNumberGridSpanY.addEventListener ("change", GUI.gridSpanYChange);
// Canvas
GUI.canvas = document.getElementsByTagName ("canvas")[0];
GUI.canvas.addEventListener ("click", Manager.mouseClick);
GUI.canvas.addEventListener ("mousedown", GUI.mouseDown);
GUI.canvas.addEventListener ("mouseup", GUI.mouseUp);
GUI.canvas.addEventListener ("mouseleave", GUI.mouseUp);
GUI.canvas.addEventListener ("mousemove", GUI.mouseMove);
GUI.canvas.addEventListener ("contextmenu", ContextMenu.open);
GUI.canvasRect = GUI.canvas.getBoundingClientRect ();
GUI.canvas.width = GUI.canvasRect.width;
GUI.canvas.height = GUI.canvasRect.height;
GUI.ctx = GUI.canvas.getContext ("2d");
Manager.setDimension (GUI.canvas.width, GUI.canvas.height);
Grid.setContext (GUI.ctx, GUI.canvas.width, GUI.canvas.height);
Node.setContext (GUI.ctx, GUI.canvas.width, GUI.canvas.height);
Face.setContext (GUI.ctx);
GUI.debugNodes = document.getElementById ("navbar-message_nodes");
GUI.debugNodes.innerText = Node.getNumNodes().toString ();
GUI.debugFaces = document.getElementById ("navbar-message_faces");
GUI.debugFaces.innerText = Face.getNumFaces().toString ();
GUI.debugCurrentNodes = document.getElementById ("navbar-message_current_nodes");
GUI.debugCurrentNodes.innerText = Node.getNumSelected().toString ();
GUI.debugCurrentFaces = document.getElementById ("navbar-message_current_faces");
GUI.debugCurrentFaces.innerText = Face.getNumSelected().toString ();
GUI.status = document.getElementById ("Multi");
GUI.status.innerText = "Single select";
}
static getContext ()
{
return ctx;
}
static draw ()
{
GUI.ctx.clearRect (0, 0, GUI.canvas.width, GUI.canvas.height);
Grid.draw ();
Face.draw ();
Node.draw ();
requestAnimationFrame (GUI.draw);
// Debug infos
GUI.debugNodes.innerText = Node.getNumNodes().toString ();
GUI.debugFaces.innerText = Face.getNumFaces().toString ();
GUI.debugCurrentNodes.innerText = Node.getNumSelected().toString ();
GUI.debugCurrentFaces.innerText = Face.getNumSelected().toString ();
}
// Event handler
static keyDown (event)
{
if (event.ctrlKey == true && Manager.multipleSelect == false)
{
Manager.multipleSelect = true;
GUI.status.innerText = "Multi select";
}
}
static keyUp (event)
{
if (event.ctrlKey == false)
{
Manager.multipleSelect = false;
GUI.status.innerText = "Single select";
}
}
static showAll (event)
{
Node.showAll ();
}
static hideAll (event)
{
Node.hideAll ();
}
static buttonClear ()
{
Node.clear ();
Node.update ();
Face.update ();
}
static gridSpanXChange (event)
{
Grid.setSpanX (event.target.valueAsNumber);
Grid.update ();
}
static gridSpanYChange (event)
{
Grid.setSpanY (event.target.valueAsNumber);
Grid.update ();
}
static mouseDown (event)
{
const n = Node.getSelected ();
var l = n.length;
if (l > 0)
{
for (var i = 0; i < l; i++)
{
if (n[i].getDistance (event.offsetX, event.offsetY) > 15)
{
continue;
}
GUI.drag = "n";
return;
}
}
else
{
const f = Face.getSelected ();
l = f.length;
if (l > 0)
{
for (var i = 0; i < l; i++)
{
if (f[i].intersect (event.offsetX, event.offsetY) == true)
{
GUI.drag = "f";
return;
}
}
}
}
//Node.showAll ();
GUI.drag = "o";
}
static mouseUp (event)
{
const n = Node.getSelected ();
const l = n.length;
if (l == 0)
{
Node.hideAll ();
}
GUI.drag = "o";
Manager.update ();
}
static mouseMove (event)
{
switch (GUI.drag)
{
case "o":
{
return;
} break;
case "n":
{
const n = Node.getSelected ();
var l = n.length;
if (l > 0)
{
for (var i = 0; i < l; i++)
{
n[i].setRelPos (event.movementX, event.movementY);
}
return;
}
} break;
case "f":
{
const f = Face.getSelected ();
l = f.length;
if (l > 0)
{
for (var i = 0; i < l; i++)
{
f[i].setRelPos (event.movementX, event.movementY);
}
return;
}
} break;
}
}
}