-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
FORMS_NodeGraph.gml
479 lines (422 loc) · 10.3 KB
/
FORMS_NodeGraph.gml
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/// @func FORMS_NodeGraph()
///
/// @desc
///
/// @private
function FORMS_NodeGraph() constructor
{
/// @var {Array<Struct.FORMS_Node>}
/// @readonly
Nodes = [];
/// @private
__evalId = 0;
/// @var {Struct.FORMS_Pin, Undefined}
/// @private
__connectFrom = undefined;
/// @private
__connectToX = 0;
/// @private
__connectToY = 0;
/// @func add_node(_node)
///
/// @desc Adds a node to the graph.
///
/// @param {Struct.FORMS_Node} _node The node to add.
///
/// @return {Struct.FORMS_NodeGraph} Returns `self`.
static add_node = function (_node)
{
forms_assert(_node.Graph == undefined, "Node is already added to a graph!");
array_push(Nodes, _node);
_node.Graph = self;
return self;
}
/// @func eval([_args])
///
/// @desc Evaluates the node graph.
///
/// @param {Any} [_args] Arguments. Defaults to `undefined`.
///
/// @return {Struct} Returns a struct where keys are entry-point nodes and values are the results of their
/// evaluation.
///
/// @see FORMS_Node.IsEntryPoint
static eval = function (_args = undefined)
{
var _evalId = __evalId++;
var _result = {};
for (var i = array_length(Nodes) - 1; i >= 0; --i)
{
var _node = Nodes[i];
_result[$ _node] = _node.eval(_args, _evalId);
}
return _result;
}
/// @func draw()
///
/// @desc Draws the node graph.
///
/// @return {Struct.FORMS_NodeGraph} Returns `self`.
static draw = function ()
{
// Draw pin connections
for (var i = array_length(Nodes) - 1; i >= 0; --i)
{
with(Nodes[i])
{
for (var j = array_length(PinsIn) - 1; j >= 0; --j)
{
var _pinIn = PinsIn[j];
for (var k = array_length(_pinIn.Other) - 1; k >= 0; --k)
{
var _pinOut = _pinIn.Other[k];
// TODO: Draw pin connection
}
}
}
}
// Draw nodes
for (var i = array_length(Nodes) - 1; i >= 0; --i)
{
Nodes[i].draw();
}
return self;
}
}
/// @func FORMS_Node(_name[, _pinsIn[, _pinsOut]])
///
/// @desc
///
/// @param {String} _name The name of the node.
/// @param {Array<Struct.FORMS_Pin>} [_pinsIn] An array of the node's input pins.
/// @param {Array<Struct.FORMS_Pin>} [_pinsOut] An array of the node's output pins.
///
/// @private
function FORMS_Node(_name, _pinsIn = [], _pinsOut = []) constructor
{
/// @var {Struct.NodeGraph, Undefined} The graph to which this node belongs. Default value is `undefined`.
Graph = undefined;
/// @var {String} The name of the node.
Name = _name;
/// @var {Array<Struct.FORMS_Pin>} An array of the node's input pins.
/// @readonly
PinsIn = _pinsIn;
/// @var {Array<Struct.FORMS_Pin>} An array of the node's output pins.
/// @readonly
PinsOut = _pinsOut;
/// @var {Bool} If true then the node is collapsed and its pins are hidden.
Collapsed = false;
/// @var {Bool} If true then the node is an entry point. Default value is false.
/// @readonly
IsEntryPoint = false;
/// @var {Real} The node's X position.
X = 0;
/// @var {Real} The node's Y position.
Y = 0;
/// @var {Real} The node's width.
Width = 0;
/// @var {Real} The node's height.
Height = 0;
/// @var {Function, Undefined} A function executed when the node is evaluated or `undefined` (default). The
/// arguments passed to the function are the pin, args and evalID.
OnEval = undefined;
/// @private
__evalId = undefined;
/// @var {Any} The result of evaluation. Default value is `undefined`.
/// @see FORMS_Node.eval
Result = undefined;
{
for (var i = array_length(PinsIn) - 1; i >= 0; --i)
{
var _pin = PinsIn[i];
forms_assert(_pin.Node == undefined, "Pin is already added to a node!");
_pin.Node = self;
_pin.IsInput = true;
}
for (var i = array_length(PinsOut) - 1; i >= 0; --i)
{
var _pin = PinsOut[i];
forms_assert(_pin.Node == undefined, "Pin is already added to a node!");
_pin.Node = self;
_pin.IsInput = false;
}
}
/// @private
static __eval_pins = function (_args, _evalId)
{
for (var i = array_length(PinsIn) - 1; i >= 0; --i)
{
PinsIn[i].eval(_args, _evalId);
}
for (var i = array_length(PinsOut) - 1; i >= 0; --i)
{
PinsOut[i].eval(_args, _evalId);
}
}
/// @func eval(_args, _evalId)
///
/// @desc Evaluates the node.
///
/// @param {Any} _args
/// @param {Real} _evalId
///
/// @return {Any} The result of evaluation.
static eval = function (_args, _evalId)
{
if (__evalId == _evalId)
{
return Result;
}
__evalId = _evalId;
__eval_pins(_args, _evalId);
if (OnEval != undefined)
{
OnEval(self, _args, _evalId);
}
return Result;
}
/// @func draw()
///
/// @desc Draws the node.
///
/// @return {Struct.FORMS_Node} Returns `self`.
static draw = function ()
{
return self;
}
}
/// @enum
/// @private
enum FORMS_EPinType
{
Real,
String,
Color,
Vec2,
Vec3,
Vec4,
Asset,
};
/// @var {Array<Constant.Color>}
/// @private
global.__formsPinColor = [
0xFF0000, // Real
0x00FF00, // String
0x0000FF, // Color
0x00FFFF, // Vec2
0xFF00FF, // Vec3
0xFFFF00, // Vec4
0xFFFFFF, // Asset
];
/// @func forms_pin_set_color(_pinType, _color)
///
/// @desc Configures color of pins of given type.
///
/// @param {Real} _pinType The type of the pin. Use values from {@link FORMS_EPinType}.
/// @param {Constant.Color} _color The new pin color.
///
/// @private
function forms_pin_set_color(_pinType, _color)
{
gml_pragma("forceinline");
global.__formsPinColor[@ _pinType] = _color;
}
/// @func forms_pin_get_color(_pinType)
///
/// @desc Retrieves color for given pin type.
///
/// @param {Real} _pinType The type of the pin. Use values from {@link FORMS_EPinType}.
///
/// @return {Constant.Color} The pin color.
///
/// @private
function forms_pin_get_color(_pinType)
{
gml_pragma("forceinline");
return global.__formsPinColor[_pinType];
}
/// @func FORMS_Pin(_name, _type[, _assetType])
///
/// @desc
///
/// @param {String} _name The name of the pin.
/// @param {Real} _type The type of the pin. Use values from {@link FORMS_EPinType}.
/// @param {Constant.AssetType, Undefined} [_assetType]
///
/// @private
function FORMS_Pin(_name, _type, _assetType = undefined) constructor
{
/// @var {Struct.FORMS_Node, Undefined} The node to which this pin belongs. Default value is `undefined`.
Node = undefined;
/// @var {String} The name of the pin.
Name = _name;
/// @var {Real} The type of the pin. Use values from {@link FORMS_EPinType}.
/// @readonly
Type = _type;
/// @var {Constant.AssetType, Undefined}
/// @readonly
AssetType = _assetType;
/// @var {Bool} If true then this is an input pin, otherwise it is an output pin. Default value is true.
/// @readonly
IsInput = true;
/// @var {Bool} If true then the pin is disabled and its connection cannot be changed. Default value is false.
Disabled = false;
/// @var {Array<Struct.FORMS_Pin>} An array of pins that this pin is connected to.
Other = [];
/// @var {Real} The pin's X position.
X = 0;
/// @var {Real} The pin's Y position.
Y = 0;
/// @var {Real} The pin's radius.
Radius = 8;
/// @var {Function, Undefined} A function executed when the pin is evaluated or `undefined` (default). The arguments
/// passed to the function are the pin, args and evalID.
OnEval = undefined;
/// @private
__evalId = undefined;
/// @var {Any} The result of evaluation. Default value is `undefined`.
/// @see FORMS_Pin.eval
Result = undefined;
/// @func is_compatible(_pin)
///
/// @desc Checks whether this pin is compatible with other given pin. Incompatible pins cannot be connected.
///
/// @param {Struct.FORMS_Pin} _pin The pin to check compatibility with.
///
/// @return {Bool} Returns true if the pins are compatible.
static is_compatible = function (_pin)
{
return (Node != _pin.Node
&& IsInput != _pin.IsInput);
}
/// @func is_connected(_pin)
///
/// @desc Checks if the pins is connected to other given pin.
///
/// @param {Struct.FORMS_Pin} _pin The pin to check.
///
/// @return {Bool} Returns true if the pins are connected.
static is_connected = function (_pin)
{
for (var i = array_length(Other) - 1; i >= 0; --i)
{
if (Other[i] == _pin)
{
return true;
}
}
return false;
}
/// @func connect(_pin)
///
/// @desc Connects the pin with other given pin.
///
/// @param {Struct.FORMS_Pin} _pin The pin to connect with.
///
/// @return {Struct.FORMS_Pin} Returns `self`.
///
/// @note The pins must be compatible, otherwise this ends with an error!
///
/// @see FORMS_Pin.is_compatible
static connect = function (_pin)
{
forms_assert(is_compatible(_pin), "Pins are not compatible!");
if (!is_connected(_pin))
{
array_push(Other, _pin);
array_push(_pin.Other, self);
}
return self;
}
/// @func disconnect(_pin)
///
/// @desc Disconnects from given pin.
///
/// @param {Struct.FORMS_Pin} _pin The pin to disconnect from.
///
/// @return {Struct.FORMS_Pin} Returns `self`.
static disconnect = function (_pin)
{
for (var i = array_length(Other) - 1; i >= 0; --i)
{
if (Other[i] == _pin)
{
// Remove other pin
array_delete(Other, i, 1);
for (var j = array_length(_pin.Other) - 1; j >= 0; --j)
{
if (_pin.Other[j] == self)
{
// Remove self from other pin
array_delete(_pin.Other, j, 1);
break;
}
}
break;
}
}
return self;
}
/// @func clear_connections()
///
/// @desc Removes all pin connections.
///
/// @return {Struct.FORMS_Pin} Returns `self`.
static clear_connections = function ()
{
for (var i = array_length(Other) - 1; i >= 0; --i)
{
var _pin = Other[i];
for (var j = array_length(_pin.Other) - 1; j >= 0; --j)
{
if (_pin.Other[j] == self)
{
// Remove self from other pin
array_delete(_pin.Other, j, 1);
break;
}
}
}
Other = [];
return self;
}
/// @private
static __eval_others = function (_args, _evalId)
{
for (var i = array_length(Other) - 1; i >= 0; --i)
{
Other[i].Node.eval(_args, _evalId);
}
}
/// @func eval(_args, _evalId)
///
/// @desc Evaluates the pin.
///
/// @param {Any} _args
/// @param {Real} _evalId
///
/// @return {Any} The result of evaluation.
static eval = function (_args, _evalId)
{
if (__evalId == _evalId)
{
return Result;
}
__evalId = _evalId;
__eval_others(_args, _evalId);
if (OnEval != undefined)
{
OnEval(self, _args, _evalId);
}
return Result;
}
/// @func draw()
///
/// @desc Draws the pin.
///
/// @return {Struct.FORMS_Pin} Returns `self`.
static draw = function ()
{
return self;
}
}