File size: 17,316 Bytes
5b97e57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// ComfyUI.mxToolkit.Reroute v.0.9.6 - Max Smirnov 2024
import { app } from "../../scripts/app.js";
import { mergeIfValid, getWidgetConfig, setWidgetConfig } from "../core/widgetInputs.js";

app.registerExtension({
    name: "Comfy.MxRerouteNode",
    registerCustomNodes(app) {
        class MxRerouteNode extends LGraphNode {
            constructor(title = MxRerouteNode.title) {
                super(title);
                if (!this.properties) {
                    this.properties = {};
                }
                this.addInput("", "*");
                this.addOutput("", "*");

                this.properties.inputDir = "LEFT";
                this.properties.outputDir= "RIGHT";
                this.linkType = "*";
                this.bgcolor="#0000";
                this.keyCode = 0;

                this.size = [2*LiteGraph.CANVAS_GRID_SIZE + 1.4*LiteGraph.NODE_SLOT_HEIGHT, 2*LiteGraph.CANVAS_GRID_SIZE + 1.4*LiteGraph.NODE_SLOT_HEIGHT];

                this.onAdded = function ()
                {
                    this.resizable = false;
                    this.ioOrientation();
                }

                this.onConfigure = function ()
                {
                    this.bgcolor="#0000";
                }

                this.onGraphConfigured = function ()
                {
                    this.configured = true;
                    this.onConnectionsChange();
                }

                this.onDrawBackground = function (ctx)
                {
                    const canvas = app.graph.list_of_graphcanvas[0];
                    let linkColor = LGraphCanvas.link_type_colors[this.linkType];
                    if (linkColor === "") linkColor = LiteGraph.LINK_COLOR;

                    if (this.inputs && this.outputs) if (this.inputs[0].pos && this.outputs[0].pos)
                    {
                        const drawLink = (ctx) =>
                        {
                            ctx.beginPath();
                            ctx.moveTo(this.inputs[0].pos[0], this.inputs[0].pos[1]);
                            if (canvas.links_render_mode < 2)
                            {
                                ctx.lineTo(this.size[0]/2, this.size[1]/2);
                                ctx.lineTo(this.outputs[0].pos[0], this.outputs[0].pos[1]);
                            } else ctx.quadraticCurveTo(this.size[0]/2, this.size[1]/2, this.outputs[0].pos[0], this.outputs[0].pos[1]);
                            ctx.stroke();
                        }
                        this.inputs[0].color_on = "#0000";
                        this.outputs[0].color_on = "#0000";
                        this.outputs[0].shape = LiteGraph.ROUND_SHAPE;
                        if (this.outputs[0].links) if (this.outputs[0].links.length) if (this.outputs[0].links.length > 0) this.outputs[0].shape = LiteGraph.GRID_SHAPE;

                        if (canvas) if (canvas.render_connections_border && canvas.ds.scale > 0.6)
                        {
                            ctx.lineWidth = canvas.connections_width + 4;
                            ctx.strokeStyle = "#0007";
                            drawLink(ctx);
                        }
                        ctx.lineWidth = canvas.connections_width;
                        ctx.strokeStyle = linkColor;
                        drawLink(ctx);
                    }

                    if (this.mouseOver) if (this.inputs && this.outputs) if (this.inputs[0].pos && this.outputs[0].pos)
                    {
                        ctx.lineWidth = 1;
                        ctx.strokeStyle = linkColor;
                        ctx.beginPath();
                        ctx.moveTo(this.pos[0], this.pos[1]);
                        ctx.roundRect(0, 0, this.size[0], this.size[1], 10);
                        ctx.stroke();
                        ctx.fillStyle = linkColor;
                        ctx.beginPath();
                        ctx.arc(this.inputs[0].pos[0], this.inputs[0].pos[1], 5, 0, 2 * Math.PI, false);
                        ctx.arc(this.outputs[0].pos[0], this.outputs[0].pos[1], 5, 0, 2 * Math.PI, false);
                        ctx.fill();
                    }
                }

                this.onKeyUp = function(e)
                {
                    if (e.keyCode < 37 || e.keyCode > 40) return;
                    if (this.keyCode > 0)
                    {
                        const arrowKeys = { 37: "LEFT", 38: "UP", 39: "RIGHT", 40: "DOWN"};
                        this.properties.inputDir  = arrowKeys[this.keyCode];
                        this.properties.outputDir = arrowKeys[e.keyCode];
                        this.onPropertyChanged();
                        this.keyCode = 0;
                    } else this.keyCode = e.keyCode;
                }

                this.onDeselected = () => {this.keyCode = 0};

                this.getExtraMenuOptions = function()
                {
                    const that = this;
                    const iDir = ["LEFT",  "RIGHT", "DOWN", "UP",   "LEFT", "LEFT", "RIGHT", "RIGHT", "UP",    "DOWN",  "DOWN", "UP"];
                    const oDir = ["RIGHT", "LEFT",  "UP",   "DOWN", "UP",   "DOWN", "UP",    "DOWN",  "RIGHT", "RIGHT", "LEFT", "LEFT"];
                    const key  = ["🠖",     "🠔",    "🠕",    "🠗",    "⮥",    "⮧",    "⮤",    "⮦",     "⮡",     "⮣",     "⮢",   "⮠"], options = [];
                    for (let i = 0; i < 12; i++) options.push({ content: key[i], callback: () => { that.properties.inputDir = iDir[i]; that.properties.outputDir = oDir[i]; that.onPropertyChanged(); }});
                    return options;
                };

                this.onPropertyChanged = function ()
                {
                    const aValues = ["LEFT","RIGHT","UP","DOWN","TOP"];
                    const sValues = ["L","R","U","D","T"]
                    this.properties.inputDir = this.properties.inputDir.toUpperCase();
                    this.properties.outputDir = this.properties.outputDir.toUpperCase();
                    if (sValues.indexOf(this.properties.inputDir)>=0) this.properties.inputDir = aValues[sValues.indexOf(this.properties.inputDir)];
                    if (sValues.indexOf(this.properties.outputDir)>=0) this.properties.outputDir = aValues[sValues.indexOf(this.properties.outputDir)];
                    if (this.properties.inputDir === "TOP") this.properties.inputDir = "UP";
                    if (this.properties.outputDir === "TOP") this.properties.inputDir = "UP";
                    this.properties.inputDir = aValues.includes(this.properties.inputDir)?this.properties.inputDir:"LEFT";
                    this.properties.outputDir= aValues.includes(this.properties.outputDir)?this.properties.outputDir:"RIGHT";
                    if (this.properties.inputDir === this.properties.outputDir) this.properties.outputDir=(this.properties.outputDir==="RIGHT")?"LEFT":"RIGHT";
                    this.ioOrientation();
                    app.graph.setDirtyCanvas(true, true);
                }

                this.ioOrientation = function ()
                {
                    let i = this.properties.inputDir;
                    let o = this.properties.outputDir;
                    switch (i)
                    {
                        case "LEFT":
                            this.inputs[0].pos = [0,0.7*LiteGraph.NODE_SLOT_HEIGHT+LiteGraph.CANVAS_GRID_SIZE];
                            this.inputs[0].dir = LiteGraph.LEFT;
                            break;
                        case "RIGHT":
                            this.inputs[0].pos = [this.size[0],0.7*LiteGraph.NODE_SLOT_HEIGHT+LiteGraph.CANVAS_GRID_SIZE];
                            this.inputs[0].dir = LiteGraph.RIGHT;
                            break;
                        case "UP":
                            this.inputs[0].pos = [this.size[0]/2,0];
                            this.inputs[0].dir = LiteGraph.UP;
                            break;
                        case "DOWN":
                            this.inputs[0].pos = [this.size[0]/2,this.size[1]];
                            this.inputs[0].dir = LiteGraph.DOWN;
                            break;
                    }
                    switch (o)
                    {
                        case "LEFT":
                            this.outputs[0].pos = [0,0.7*LiteGraph.NODE_SLOT_HEIGHT+LiteGraph.CANVAS_GRID_SIZE];
                            this.outputs[0].dir = LiteGraph.LEFT;
                            break;
                        case "RIGHT":
                            this.outputs[0].pos = [this.size[0],0.7*LiteGraph.NODE_SLOT_HEIGHT+LiteGraph.CANVAS_GRID_SIZE];
                            this.outputs[0].dir = LiteGraph.RIGHT;
                            break;
                        case "UP":
                            this.outputs[0].pos = [this.size[0]/2,0];
                            this.outputs[0].dir = LiteGraph.UP;
                            break;
                        case "DOWN":
                            this.outputs[0].pos = [this.size[0]/2,this.size[1]];
                            this.outputs[0].dir = LiteGraph.DOWN;
                            break;
                    }
                }

                this.onConnectionsChange = function (type, index, connected, link_info) {

                    if (this.configured && connected && type === LiteGraph.OUTPUT) {
                        const types = new Set(this.outputs[0].links.map((l) => app.graph.links[l].type).filter((t) => t !== "*"));
                        if (types.size > 1) {
                            const linksToDisconnect = [];
                            for (let i = 0; i < this.outputs[0].links.length - 1; i++) {
                                const linkId = this.outputs[0].links[i];
                                const link = app.graph.links[linkId];
                                linksToDisconnect.push(link);
                            }
                            for (const link of linksToDisconnect) {
                                const node = app.graph.getNodeById(link.target_id);
                                node.disconnectInput(link.target_slot);
                            }
                        }
                    }

                    let currentNode = this;
                    let updateNodes = [];
                    let inputType = null;
                    let inputNode = null;
                    while (currentNode) {
                        updateNodes.unshift(currentNode);
                        const linkId = currentNode.inputs[0].link;
                        if (linkId !== null) {
                            const link = app.graph.links[linkId];
                            if (!link) return;
                            const node = app.graph.getNodeById(link.origin_id);
                            const type = node.constructor.type;
                            if (type && type.includes ("Reroute")) {
                                if (node === this) {
                                    currentNode.disconnectInput(link.target_slot);
                                    currentNode = null;
                                } else {
                                    currentNode = node;
                                }
                            } else {
                                inputNode = currentNode;
                                inputType = node.outputs[link.origin_slot]?.type ?? null;
                                break;
                            }
                        } else {
                            currentNode = null;
                            break;
                        }
                    }

                    const nodes = [this];
                    let outputType = null;
                    while (nodes.length) {
                        currentNode = nodes.pop();
                        const outputs = (currentNode.outputs ? currentNode.outputs[0].links : []) || [];
                        if (outputs.length) {
                            for (const linkId of outputs) {
                                const link = app.graph.links[linkId];

                                if (!link) continue;

                                const node = app.graph.getNodeById(link.target_id);
                                const type = node.constructor.type;

                                if (type && type.includes("Reroute")) {
                                    nodes.push(node);
                                    updateNodes.push(node);
                                } else {
                                    const nodeOutType =
                                        node.inputs && node.inputs[link?.target_slot] && node.inputs[link.target_slot].type
                                            ? node.inputs[link.target_slot].type
                                            : null;
                                    if (this.configured && inputType && inputType !== "*" && nodeOutType !== inputType) {
                                        node.disconnectInput(link.target_slot);
                                    } else {
                                        outputType = nodeOutType;
                                    }
                                }
                            }
                        }
                    }

                    this.linkType = inputType || outputType || "*";
                    const linkColor = LGraphCanvas.link_type_colors[this.linkType];

                    let widgetConfig;
                    let targetWidget;
                    let widgetType;
                    for (const node of updateNodes) {
                        node.outputs[0].type = inputType || "*";
                        node.__outputType = this.linkType;
                        node.outputs[0].name = node.properties.showOutputText ? this.linkType : "";
                        if (node.linkType) node.linkType = this.linkType;
                        node.size = node.computeSize();
                        if (node.applyOrientation) node.applyOrientation();

                        for (const l of node.outputs[0].links || []) {
                            const link = app.graph.links[l];
                            if (link) {
                                link.color = linkColor;

                                if (app.configuringGraph) continue;
                                const targetNode = app.graph.getNodeById(link.target_id);
                                const targetInput = targetNode.inputs?.[link.target_slot];
                                if (targetInput?.widget) {
                                    const config = getWidgetConfig(targetInput);
                                    if (!widgetConfig) {
                                        widgetConfig = config[1] ?? {};
                                        widgetType = config[0];
                                    }
                                    if (!targetWidget) {
                                        targetWidget = targetNode.widgets?.find((w) => w.name === targetInput.widget.name);
                                    }

                                    const merged = mergeIfValid(targetInput, [config[0], widgetConfig]);
                                    if (merged.customConfig) {
                                        widgetConfig = merged.customConfig;
                                    }
                                }
                            }
                        }
                    }

                    for (const node of updateNodes) {
                        if (widgetConfig && outputType) {
                            node.inputs[0].widget = { name: "value" };
                            setWidgetConfig(node.inputs[0], [widgetType ?? this.linkType, widgetConfig], targetWidget);
                        } else {
                            setWidgetConfig(node.inputs[0], null);
                        }
                    }

                    if (inputNode) {
                        const link = app.graph.links[inputNode.inputs[0].link];
                        if (link) {
                            link.color = linkColor;
                        }
                    }
                };

                this.clone = function () {
                    const cloned = MxRerouteNode.prototype.clone.apply(this);
                    cloned.removeOutput(0);
                    cloned.addOutput("", "*");
                    cloned.size = cloned.computeSize();
                    cloned.ioOrientation();
                    return cloned;
                };

                this.isVirtualNode = true;
            }

            computeSize() { return [2*LiteGraph.CANVAS_GRID_SIZE+1.4*LiteGraph.NODE_SLOT_HEIGHT,2*LiteGraph.CANVAS_GRID_SIZE+1.4*LiteGraph.NODE_SLOT_HEIGHT] }
        }

        LiteGraph.registerNodeType(
            "mxReroute",
            Object.assign(MxRerouteNode, {
                title_mode: LiteGraph.NO_TITLE,
                title: "mxReroute",
                collapsable: false,
            })
        );

        MxRerouteNode.category = "utils";
    },
});