-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
node_manager.js
197 lines (166 loc) · 6.7 KB
/
node_manager.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
import { PolyNode } from "./nodes/node.js";
function ImageParameterNode() {
// this.addInput(inputName, nodeData.inputs[inputName].type);
this.addOutput("Value", "image.Image");
// this.properties = { precision: 1 };
this.title = "Image";
this.color = "#233";
this.bgcolor = "#355";
const H = LiteGraph.NODE_WIDGET_HEIGHT;
const imgWidget = this.addWidget("image", "Image", true, { property: "surname" }); //this will modify the node.properties
this.imgWidget = imgWidget;
const margin = 15;
this.imgWidget.draw = (ctx, node, widget_width, y, H) => {
if (!imgWidget.image) {
return;
}
const adjustedWidth = widget_width - margin * 2
ctx.drawImage(
imgWidget.image,
margin,
y,
adjustedWidth,
(adjustedWidth / imgWidget.image.width) * imgWidget.image.height
);
}
this.imgWidget.computeSize = (width) => {
if (!!imgWidget.image) {
const adjustedWidth = width - margin * 2
const newH = (adjustedWidth / imgWidget.image.width) * imgWidget.image.height;
return [width, newH]
}
return [width, 0];
}
// this.imgWidget.mouse = (event, pos, node) => {
// if (event.type == LiteGraph.pointerevents_method + "down") {
// w.value = !w.value;
// setTimeout(function () {
// inner_value_change(w, w.value);
// }, 20);
// }
// }
}
function ColorParameterNode() {
// this.addInput(inputName, nodeData.inputs[inputName].type);
this.addOutput("Value", "github.com/EliCDavis/polyform/drawing/coloring.WebColor");
// this.properties = { precision: 1 };
this.title = "Image";
this.color = "#233";
this.bgcolor = "#355";
const imgWidget = this.addWidget("color", "Color", "#00FF00", { color: "surname" }); //this will modify the node.properties
this.imgWidget = imgWidget;
const margin = 15;
this.imgWidget.draw = (ctx, node, widget_width, y, H) => {
const adjustedWidth = widget_width - margin * 2
ctx.beginPath(); // Start a new path
ctx.rect(margin, y, adjustedWidth, H); // Add a rectangle to the current path
ctx.fillStyle = this.imgWidget.value;
ctx.fill(); // Render the path
}
// this.imgWidget.mouse = (event, pos, node) => {
// if (event.type == LiteGraph.pointerevents_method + "down") {
// w.value = !w.value;
// setTimeout(function () {
// inner_value_change(w, w.value);
// }, 20);
// }
// }
}
export class NodeManager {
constructor(app) {
this.app = app;
this.guiFolderData = {};
this.nodeIdToNode = new Map();
this.subscribers = [];
this.registerPolyformNodes();
}
registerPolyformNodes() {
function Vector3ParameterNode() {
// this.addInput(inputName, nodeData.inputs[inputName].type);
this.addOutput("Value", "github.com/EliCDavis/vector/vector3.Vector[float64]");
// this.properties = { precision: 1 };
this.title = "Vector3";
this.color = "#233";
this.bgcolor = "#355";
}
function Vector3ArrayParameterNode() {
// this.addInput(inputName, nodeData.inputs[inputName].type);
this.addOutput("Value", "[]github.com/EliCDavis/vector/vector3.Vector[float64]");
// this.properties = { precision: 1 };
this.title = "Vector3 Array";
this.color = "#233";
this.bgcolor = "#355";
}
function AABBParameterNode() {
// this.addInput(inputName, nodeData.inputs[inputName].type);
this.addOutput("Value", "github.com/EliCDavis/polyform/math/geometry.AABB");
// this.properties = { precision: 1 };
this.title = "AABB";
this.color = "#233";
this.bgcolor = "#355";
}
LiteGraph.registerNodeType("polyform/aabb", AABBParameterNode);
LiteGraph.registerNodeType("polyform/vector3", Vector3ParameterNode);
LiteGraph.registerNodeType("polyform/vector3[]", Vector3ArrayParameterNode);
LiteGraph.registerNodeType("polyform/color", ColorParameterNode);
LiteGraph.registerNodeType("polyform/Image", ImageParameterNode);
}
sortNodesByName(nodesToSort) {
let sortable = [];
for (var nodeID in nodesToSort) {
sortable.push([nodeID, nodesToSort[nodeID]]);
}
sortable.sort((a, b) => {
var textA = a[1].name.toUpperCase();
var textB = b[1].name.toUpperCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});
return sortable;
}
updateNodeConnections(nodes) {
for (let node of nodes) {
const nodeID = node[0];
const nodeData = node[1];
const source = this.nodeIdToNode.get(nodeID);
for (let i = 0; i < nodeData.dependencies.length; i++) {
const dep = nodeData.dependencies[i];
const target = this.nodeIdToNode.get(dep.dependencyID);
let sourceInput = -1;
for (let sourceInputIndex = 0; sourceInputIndex < source.lightNode.inputs.length; sourceInputIndex++) {
if (source.lightNode.inputs[sourceInputIndex].name === dep.name) {
sourceInput = sourceInputIndex;
}
}
// TODO: This only works for nodes with one output
target.lightNode.connect(0, source.lightNode, sourceInput)
// source.lightNode.connect(i, target.lightNode, 0);
}
}
}
updateNodes(newSchema) {
const sortedNodes = this.sortNodesByName(newSchema.nodes);
let nodeAdded = false;
for (let node of sortedNodes) {
const nodeID = node[0];
const nodeData = node[1];
if (this.nodeIdToNode.has(nodeID)) {
const nodeToUpdate = this.nodeIdToNode.get(nodeID);
nodeToUpdate.update(nodeData);
} else {
const isProducer = newSchema.producers.includes(nodeData.name);
this.nodeIdToNode.set(nodeID, new PolyNode(this, nodeID, nodeData, this.app, this.guiFolderData, isProducer));
nodeAdded = true;
}
}
this.updateNodeConnections(sortedNodes);
if (nodeAdded) {
lgraphInstance.arrange();
}
}
subscribeToParameterChange(subscriber) {
this.subscribers.push(subscriber)
}
nodeParameterChanged(change) {
this.subscribers.forEach((e) => e(change))
}
}