forked from riicchhaarrd/nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.js
343 lines (319 loc) · 7.7 KB
/
nodes.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
export var ctx = null;
export var canvas = null;
export var dummy=true;
export var nodes = [];
export var main_node = null;
export var node_candidate = null;
export var signal_candidate = null;
export var moving = false;
export var selected_node = null;
export var selected_signal = null;
export var mouse_x = 0;
export var mouse_y = 0;
import signal_proxy from "./signal_proxy.js";
import node_0 from "./node_0.js";
import node_1 from "./node_1.js";
import node_text from "./node_text.js";
import node_color from "./node_color.js";
import node_constant from "./node_constant.js";
import node_entry from "./node_entry.js";
import node_cast_vec3 from "./node_cast_vec3.js";
import node_characters from "./node_characters.js";
import node_print from "./node_print.js";
import node_printbold from "./node_printbold.js";
import node_repeat from "./node_repeat.js";
import node_reverse from "./node_reverse.js";
import node_sort from "./node_sort.js";
import node_stringjoin from "./node_stringjoin.js";
import node_branch from "./node_branch.js";
import node_delay from "./node_delay.js";
import node_exit from "./node_exit.js";
import node_color2 from "./node_color2.js";
import node_extract_vec3 from "./node_extract_vec3.js";
import {
node_mathop,
node_mathop_mul,
node_mathop_sub,
node_mathop_div,
node_mathop_add
} from "./node_mathops.js";
import node_normalize from "./node_normalize.js";
import node_rand from "./node_rand.js";
import node_t from "./node_t.js";
import node_tally from "./node_tally.js";
import node_vec3 from "./node_vec3.js";
export function prompt_proxy()
{
if(dummy)
return;
return prompt();
}
var app = new Vue({
el: "#app",
data: function()
{
return {
registered_nodes: []
};
},
mounted: function()
{
},
methods:
{
load_nodes: function()
{
let tmp = window.localStorage.getItem("nodes");
if(tmp != null)
{
let list = JSON.parse(tmp);
for(let i = 0; i < list.length; ++i)
{
let inst = eval("new "+list[i]._classname+"()");
//console.log(inst.constructor.name);
inst.set_initial_value();
inst.thaw(list[i]);
nodes.push(inst);
}
return true;
}
return false;
},
add_node: function(n)
{
this.closeMenu();
let o = new (n.constructor)();
o.set_position(mouse_x, mouse_y);
o.set_initial_value();
nodes.push(o);
this.save_nodes();
},
save_nodes: function()
{
let serializable_nodes = [];
for(let i = 0; i < nodes.length; ++i)
{
let n = nodes[i];
let serializable_node_data = n.freeze();
serializable_nodes.push(serializable_node_data);
}
window.localStorage.setItem("nodes", JSON.stringify(serializable_nodes));
},
register_node: function(n)
{
this.registered_nodes.push(n);
},
closeMenu: function()
{
let menu = document.getElementById("menu");
menu.style.display="none";
},
openMenu: function(ev)
{
let menu = document.getElementById("menu");
menu.style.left = ev.clientX;
menu.style.top = ev.clientY;
menu.style.display="block";
}
}
});
function get_selected_node()
{
for(let itx in nodes)
{
let node = nodes[itx];
if(node.in_bounds(mouse_x,mouse_y))
return node;
}
return null;
}
function draw()
{
if(ctx==null)return;
if(selected_node != null && moving)
{
selected_node.move_absolute(mouse_x,mouse_y);
}
ctx.clearRect(0,0,canvas.width,canvas.height);
for(let itx in nodes)
{
let node = nodes[itx];
let selected = node.in_bounds(mouse_x,mouse_y);
node.draw(node_candidate == node || selected || node == selected_node);
if(node.link != null)
{
ctx.beginPath();
ctx.moveTo(node.x,node.y);
ctx.lineTo(node.link.x,node.link.y);
ctx.closePath();
ctx.stroke();
}
}
window.requestAnimationFrame(draw);
}
function init()
{
//https://stackoverflow.com/questions/1255512/how-to-draw-a-rounded-rectangle-on-html-canvas
CanvasRenderingContext2D.prototype.roundRect = function (x, y, w, h, r) {
if (w < 2 * r) r = w / 2;
if (h < 2 * r) r = h / 2;
this.beginPath();
this.moveTo(x+r, y);
this.arcTo(x+w, y, x+w, y+h, r);
this.arcTo(x+w, y+h, x, y+h, r);
this.arcTo(x, y+h, x, y, r);
this.arcTo(x, y, x+w, y, r);
this.closePath();
return this;
}
canvas = document.getElementsByTagName("canvas")[0];
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx = canvas.getContext("2d");
window.onmouseup=(ev)=>
{
moving=false;
selected_node=null;
};
window.oncontextmenu=(ev)=>
{
return false;
}
window.onkeyup=(ev)=>
{
if(ev.key==" ")
{
app.openMenu({
clientX: mouse_x,
clientY: mouse_y
});
} else if(ev.key == "q")
{
let gsc="main()\n{";
//walk backwards from main_node
let cur = main_node;
for(;;)
{
console.log(cur);
if(typeof cur.get_script==="function")
gsc+=cur.get_script()+"\n";
if(cur.outputs.length==0)
break;
if(cur.outputs[0].link==null)
break;
cur = cur.outputs[0].link.node;
}
gsc+="\n}";
console.log(gsc);
} else if(ev.key == "s")
{
app.save_nodes();
}
return false;
};
window.onmousedown=(ev)=>
{
if(ev.buttons!=1)
{
let node_index = null;
for(let itx in nodes)
{
let node = nodes[itx];
if(node.in_bounds(ev.clientX,ev.clientY))
{
node_index = itx;
break;
}
}
if(node_index != null)
{
nodes[node_index].remove();
nodes.splice(node_index, 1);
}
return false;
}
let node = get_selected_node();
selected_node=node;
if(node!=null)
{
let sig = node.get_selected_signal(mouse_x, mouse_y);
if(sig == null)
moving=true;
}
};
window.onmousemove=(ev)=>
{
mouse_x = ev.clientX; mouse_y = ev.clientY;
//get current elements under mouse
node_candidate = get_selected_node();
};
window.onkeydown=(ev)=>
{
};
window.onclick=(ev)=>
{
app.closeMenu();
let node = get_selected_node();
if(node!=null)
{
let inp = node.get_selected_signal(ev.clientX, ev.clientY);
if(inp != null)
{
if(selected_signal != null && inp != selected_signal && inp.type != selected_signal.type)
{
if(selected_signal.link!=null&&selected_signal.type!="output")
selected_signal.link.link=null;
if(inp.link!=null&&inp.link.link!=null)
inp.link.link=null;
selected_signal.link = inp;
inp.link=selected_signal;
selected_signal=null;
node.value_changed();
} else
selected_signal = inp;
}
}
};
app.register_node(new node_rand());
app.register_node(new node_color());
app.register_node(new node_0());
app.register_node(new node_1());
app.register_node(new node_mathop_mul());
app.register_node(new node_mathop_div());
app.register_node(new node_mathop_add());
app.register_node(new node_mathop_sub());
app.register_node(new node_normalize());
app.register_node(new node_extract_vec3());
app.register_node(new node_constant());
app.register_node(new node_vec3());
app.register_node(new node_text());
app.register_node(new node_color2());
app.register_node(new node_cast_vec3());
app.register_node(new node_printbold());
app.register_node(new node_print());
app.register_node(new node_delay());
app.register_node(new node_exit());
app.register_node(new node_branch());
app.register_node(new node_entry());
app.register_node(new node_repeat());
app.register_node(new node_reverse());
app.register_node(new node_sort());
app.register_node(new node_stringjoin());
app.register_node(new node_tally());
app.register_node(new node_characters());
main_node = new node_entry();
if(!app.load_nodes())
nodes.push(main_node);
}
window.requestAnimationFrame(()=>
{
//ctx = document.getElementsByTagName("canvas")[0].getContext("2d");
init();
window.requestAnimationFrame(draw);
dummy=false;
});
export default {
a: 1,
b: 2,
c: 3
};