forked from jagenjo/litegraph.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiteGraph.cs
604 lines (528 loc) · 20.2 KB
/
LiteGraph.cs
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
using System;
using System.Collections;
using System.Collections.Generic;
//using System.Diagnostics;
using UnityEngine; //for debug messages
using SimpleJSON; //to parse the JSON
namespace LiteGraph
{
public enum DataType { NONE, ENUM, NUMBER, STRING, BOOL, VEC2, VEC3 };
public struct vec2 {
float x;
float y;
};
public struct vec3
{
float x;
float y;
float z;
};
//not used yet...
public class MutableType {
public DataType type;
public bool data_bool;
public float data_number;
public string data_string;
public vec2 data_vec2;
public vec3 data_vec3;
public bool AsBool { get { return data_bool; } }
public int AsEnum { get { return (int)data_number; } }
public float AsFloat { get { return data_number; } }
public string AsString { get { return data_string; } }
public vec2 AsVec2 { get { return data_vec2; } }
public vec3 AsVec3 { get { return data_vec3; } }
public void Set(bool v) { type = DataType.BOOL; data_bool = v; }
public void Set(int v) { type = DataType.ENUM; data_number = v; }
public void Set(float v) { type = DataType.NUMBER; data_number = v; }
public void Set(string v) { type = DataType.STRING; data_string = v; }
public void Set(vec2 v) { type = DataType.VEC2; data_vec2 = v; }
public void Set(vec3 v) { type = DataType.VEC3; data_vec3 = v; }
public override string ToString() {
switch (type)
{
case DataType.NONE: return "";
case DataType.ENUM: return data_number.ToString();
case DataType.BOOL: return data_bool.ToString();
case DataType.NUMBER: return data_number.ToString();
case DataType.STRING: return data_string;
case DataType.VEC2: return data_vec2.ToString();
case DataType.VEC3: return data_vec3.ToString();
}
return "";
}
};
//to store connection info
public class LLink {
public int id;
public int origin_id;
public int origin_slot;
public int target_id;
public int target_slot;
public DataType data_type;
public bool data_bool;
public float data_float;
public string data_string;
public vec2 data_vec2;
public vec3 data_vec3;
public LLink(int id, DataType type, int origin_id, int origin_slot, int target_id, int target_slot)
{
this.id = id;
this.data_type = type;
this.origin_id = origin_id;
this.origin_slot = origin_slot;
this.target_id = target_id;
this.target_slot = target_slot;
}
public void setData(bool data)
{
data_type = DataType.BOOL;
data_bool = data;
}
public void setData(float data)
{
data_type = DataType.NUMBER;
data_float = data;
}
public void setData(string data)
{
data_type = DataType.STRING;
data_string = data;
}
public void setData(vec2 data)
{
data_type = DataType.VEC2;
data_vec2 = data;
}
public void setData(vec3 data)
{
data_type = DataType.VEC3;
data_vec3 = data;
}
}
//to store slot info
public class LSlot
{
public int num;
public string name;
public DataType type;
public LLink link = null; //for input slots
public List<LLink> links = new List<LLink>(); //for output slots
public LSlot(string name, DataType type)
{
this.name = name;
this.type = type;
}
}
//the node base class
public class LGraphNode {
public int id = -1;
public LGraph graph = null;
public int order = -1;
public List<LSlot> inputs = new List<LSlot>();
public List<LSlot> outputs = new List<LSlot>();
public LGraphNode()
{
}
public virtual LSlot addInput(string name, DataType type = DataType.NONE)
{
LSlot slot = new LSlot(name, type);
slot.num = inputs.Count;
inputs.Add(slot);
return slot;
}
public virtual LSlot addOutput(string name, DataType type = DataType.NONE)
{
LSlot slot = new LSlot(name, type);
slot.num = outputs.Count;
outputs.Add(slot);
return slot;
}
public virtual bool getInputData(int slot_num, bool default_value)
{
LLink link = inputs[slot_num].link;
if (link != null)
return link.data_bool;
return default_value;
}
public virtual float getInputData(int slot_num, float default_value )
{
LLink link = inputs[slot_num].link;
if (link != null)
return link.data_float;
return default_value;
}
public virtual string getInputData(int slot_num, string default_value)
{
LLink link = inputs[slot_num].link;
if (link != null)
return link.data_string;
return default_value;
}
public virtual vec2 getInputData(int slot_num, vec2 default_value)
{
LLink link = inputs[slot_num].link;
if (link != null)
return link.data_vec2;
return default_value;
}
public virtual vec3 getInputData(int slot_num, vec3 default_value)
{
LLink link = inputs[slot_num].link;
if (link != null)
return link.data_vec3;
return default_value;
}
public virtual void setOutputData(int slot_num, bool v)
{
if (inputs.Count <= slot_num)
return;
LSlot slot = outputs[slot_num];
if (slot == null)
return;
for (int i = 0; i < slot.links.Count; ++i)
{
LLink link = slot.links[i];
if (link == null)
return;
link.setData(v);
}
}
public virtual void setOutputData(int slot_num, float v)
{
if (outputs.Count <= slot_num)
return;
LSlot slot = outputs[slot_num];
if (slot == null)
return;
for (int i = 0; i < slot.links.Count; ++i)
{
LLink link = slot.links[i];
if (link == null)
return;
link.setData(v);
}
}
public virtual void setOutputData(int slot_num, string v)
{
if (outputs.Count <= slot_num)
return;
LSlot slot = outputs[slot_num];
if (slot == null)
return;
for (int i = 0; i < slot.links.Count; ++i)
{
LLink link = slot.links[i];
if (link == null)
return;
link.setData(v);
}
}
public virtual void setOutputData(int slot_num, vec2 v)
{
if (outputs.Count <= slot_num)
return;
LSlot slot = outputs[slot_num];
if (slot == null)
return;
for (int i = 0; i < slot.links.Count; ++i)
{
LLink link = slot.links[i];
if (link == null)
return;
link.setData(v);
}
}
public virtual void setOutputData(int slot_num, vec3 v)
{
if (outputs.Count <= slot_num)
return;
LSlot slot = outputs[slot_num];
if (slot == null)
return;
for (int i = 0; i < slot.links.Count; ++i)
{
LLink link = slot.links[i];
if (link == null)
return;
link.setData(v);
}
}
public virtual void transferData(int input_slot_num, int output_slot_num)
{
LLink input_link = inputs[input_slot_num].link;
if (input_link == null)
return;
if (outputs.Count <= output_slot_num)
return;
LSlot slot = outputs[output_slot_num];
if (slot == null)
return;
for (int i = 0; i < slot.links.Count; ++i)
{
LLink link = slot.links[i];
if (link == null)
return;
switch(input_link.data_type)
{
case DataType.BOOL: link.setData(input_link.data_bool); break;
case DataType.NUMBER: link.setData(input_link.data_float); break;
case DataType.STRING: link.setData(input_link.data_string); break;
case DataType.VEC2: link.setData(input_link.data_vec2); break;
case DataType.VEC3: link.setData(input_link.data_vec3); break;
}
}
}
public virtual bool connect(int origin_slot, LGraphNode target, int target_slot)
{
if(graph == null)
throw (new Exception("node does not belong to a graph"));
if (graph != target.graph)
throw (new Exception("nodes do not belong to same graph") );
LSlot origin_slot_info = this.outputs[origin_slot];
LSlot target_slot_info = target.inputs[target_slot];
if (origin_slot_info == null || target_slot_info == null)
return false;
if(origin_slot_info.type != target_slot_info.type && (origin_slot_info.type != DataType.NONE && target_slot_info.type != DataType.NONE) )
throw (new Exception("connecting incompatible types"));
int id = graph.last_link_id++;
LLink link = new LLink(id, origin_slot_info.type, this.id, origin_slot, target.id, target_slot);
graph.links.Add(link);
origin_slot_info.links.Add(link);
target_slot_info.link = link;
graph.sortByExecutionOrder();
return true;
}
public virtual void onExecute()
{
}
public virtual void configure(JSONNode json_node)
{
this.id = json_node["id"].AsInt;
this.order = json_node["order"].AsInt;
//inputs
var json_inputs = json_node["inputs"];
if (json_inputs != null)
{
JSONNode.Enumerator it = json_inputs.GetEnumerator();
int i = 0;
while(it.MoveNext())
{
JSONNode json_slot = it.Current;
string str_type = json_slot["type"];
DataType type = DataType.NONE;
if (str_type != null && Globals.stringToDataType.ContainsKey(str_type))
type = Globals.stringToDataType[str_type];
LSlot slot = null;
if (inputs.Count > i)
slot = inputs[i];
if(slot == null)
slot = this.addInput( json_slot["name"], type );
JSONNode json_link = json_slot["link"];
if (json_link != null)
slot.link = graph.links_by_id[json_link.AsInt];
++i;
}
}
//outputs
var json_outputs = json_node["outputs"];
if (json_outputs != null)
{
JSONNode.Enumerator it = json_outputs.GetEnumerator();
int i = 0;
while (it.MoveNext())
{
JSONNode json_slot = it.Current;
string str_type = json_slot["type"];
DataType type = DataType.NONE;
if (str_type != null && Globals.stringToDataType.ContainsKey(str_type))
type = Globals.stringToDataType[str_type];
LSlot slot = null;
if (outputs.Count > i)
slot = outputs[i];
if (slot == null)
slot = this.addOutput(json_slot["name"], type);
JSONNode json_links = json_slot["links"];
if(json_links != null)
{
JSONNode.Enumerator it2 = json_links.GetEnumerator();
while (it2.MoveNext())
{
JSONNode json_link_id = it2.Current;
LLink link = graph.links_by_id[json_link_id.AsInt];
if (link != null)
slot.links.Add(link);
else
Debug.LogError("Link ID not found!: " + json_link_id);
}
}
++i;
}
}
//custom data (properties)
this.onConfigure(json_node);
}
public virtual void onConfigure(JSONNode json_node)
{
//overwrite this one
}
}
//namespace to store global litegraph data
public class Globals
{
static public Dictionary<string, DataType> stringToDataType = new Dictionary<string, DataType> { { "NONE", DataType.NONE }, { "", DataType.NONE }, { "ENUM", DataType.ENUM }, {"NUMBER",DataType.NUMBER }, { "number", DataType.NUMBER }, { "BOOLEAN", DataType.BOOL }, { "boolean", DataType.BOOL }, { "STRING", DataType.STRING }, { "string", DataType.STRING }, { "VEC2", DataType.VEC2 } };
static public Dictionary<string, Func<LGraphNode>> node_types = new Dictionary<string, Func<LGraphNode>>();
static public void registerType(string name, Func<LGraphNode> ctor )
{
node_types.Add(name, ctor);
}
static public LGraphNode createNodeType(string name)
{
if (!node_types.ContainsKey(name))
{
Debug.Log("Node type not found: " + name);
return null;
}
Func<LGraphNode> ctor = node_types[name];
return ctor();
}
};
//one graph
public class LGraph
{
public List<LGraphNode> nodes = new List<LGraphNode>();
public Dictionary<int, LGraphNode> nodes_by_id = new Dictionary<int, LGraphNode>();
public List<LGraphNode> nodes_in_execution_order = new List<LGraphNode>();
public List<LLink> links = new List<LLink>();
public Dictionary<int,LLink> links_by_id = new Dictionary<int, LLink>();
public bool has_errors = false;
public int last_node_id = 0;
public int last_link_id = 0;
public double time = 0; //time in seconds
public Dictionary<string, float> outputs = new Dictionary<string, float>();
// Start is called before the first frame update
public LGraph()
{
}
public void add(LGraphNode node)
{
if (node.graph != null)
throw ( new Exception("already has graph") );
node.graph = this;
node.id = last_node_id++;
node.order = node.id;
nodes.Add(node);
nodes_by_id.Add(node.id, node);
}
public void clear()
{
has_errors = false;
nodes.Clear();
nodes_by_id.Clear();
links.Clear();
links_by_id.Clear();
nodes_in_execution_order.Clear();
last_link_id = 0;
last_node_id = 0;
outputs.Clear();
}
// Update is called once per frame
public void runStep(float dt = 0)
{
for (int i = 0; i < nodes_in_execution_order.Count; ++i)
{
LGraphNode node = nodes_in_execution_order[i];
node.onExecute();
}
time += dt;
}
public void configure(string data)
{
sortByExecutionOrder();
}
public void sortByExecutionOrder()
{
nodes_in_execution_order = nodes.GetRange(0, nodes.Count);
nodes_in_execution_order.Sort(delegate (LGraphNode a, LGraphNode b)
{
return a.order - b.order;
});
}
public void fromJSONText(string text)
{
clear();
var root = JSON.Parse(text);
last_node_id = root["last_node_id"].AsInt;
last_link_id = root["last_link_id"].AsInt;
var json_links = root["links"];
for (int i = 0; i < json_links.Count; ++i)
{
var json_node = json_links[i];
int id = json_node[0].AsInt;
int origin_id = json_node[1].AsInt;
int origin_slot = json_node[2].AsInt;
int target_id = json_node[3].AsInt;
int target_slot = json_node[4].AsInt;
JSONNode json_type = json_node[5];
DataType type = DataType.NONE;
if(json_type != null && json_type.Value != "0" && Globals.stringToDataType.ContainsKey(json_type) )
type = Globals.stringToDataType[ json_type ];
LLink link = new LLink(id, type, origin_id, origin_slot, target_id, target_slot);
links.Add(link);
links_by_id[link.id] = link;
}
var json_nodes = root["nodes"];
for (int i = 0; i < json_nodes.Count; ++i)
{
var json_node = json_nodes[i];
string node_type = json_node["type"];
Debug.Log(node_type);
LGraphNode node = LiteGraph.Globals.createNodeType(node_type);
if (node == null)
{
Debug.Log("Error: node type not found: " + node_type);
has_errors = true;
continue;
}
node.graph = this;
nodes.Add(node);
node.configure(json_node);
}
sortByExecutionOrder();
}
public float getOutput(string name, float def_value)
{
if (!outputs.ContainsKey(name))
return def_value;
return outputs[name];
}
}
public class Main
{
public static void Init()
{
Main.loadNodes();
}
public static void loadNodes()
{
Globals.registerType(WatchNode.type, () => new WatchNode() );
Globals.registerType(RandomNumberNode.type, () => new RandomNumberNode());
Globals.registerType(ConstNumberNode.type, () => new ConstNumberNode());
Globals.registerType(TimeNode.type, () => new TimeNode());
Globals.registerType(ConditionNode.type, () => new ConditionNode());
Globals.registerType(GraphOutputNode.type, () => new GraphOutputNode());
Globals.registerType(GateNode.type, () => new GateNode());
}
public static void test()
{
Debug.Log("Testing Graph...");
LGraph graph = new LGraph();
LGraphNode node1 = LiteGraph.Globals.createNodeType("math/rand");
graph.add(node1);
LGraphNode node2 = LiteGraph.Globals.createNodeType("basic/watch");
graph.add(node2);
node1.connect(0,node2,0);
for(int i = 0; i < 100; ++i)
graph.runStep();
}
}
}