From fafaa60f372adbd65a2cea7c93a626d5e0c2d7f8 Mon Sep 17 00:00:00 2001 From: Maoxie Date: Fri, 19 Apr 2024 18:41:07 +0800 Subject: [PATCH] when selected, display links between GetNodes & SetNode --- web/js/setgetnodes.js | 83 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/web/js/setgetnodes.js b/web/js/setgetnodes.js index d3035f7..cb7f6d3 100644 --- a/web/js/setgetnodes.js +++ b/web/js/setgetnodes.js @@ -41,6 +41,7 @@ app.registerExtension({ class SetNode { defaultVisibility = true; serialize_widgets = true; + drawConnection = false; constructor() { if (!this.properties) { this.properties = { @@ -213,6 +214,49 @@ app.registerExtension({ } }) } + onSelected() { + this.drawConnection = true; + } + onDeselected() { + this.drawConnection = false; + } + onDrawForeground(ctx, lGraphCanvas) { + if (this.drawConnection) { + this._drawVirtualLinks(lGraphCanvas, ctx); + } + } + onDrawCollapsed(ctx, lGraphCanvas) { + if (this.drawConnection) { + this._drawVirtualLinks(lGraphCanvas, ctx); + } + } + _drawVirtualLinks(lGraphCanvas, ctx) { + const getters = this.findGetters(this.graph); + if (!getters?.length) return; + // draw the virtual connection from SetNode to GetNode + let start_node_slotpos = [ + this.size[0], + LiteGraph.NODE_TITLE_HEIGHT * 0.5, + ]; + for (const getter of getters) { + let end_node_slotpos = this.getConnectionPos(false, 0); + end_node_slotpos = [ + getter.pos[0] - end_node_slotpos[0] + this.size[0], + getter.pos[1] - end_node_slotpos[1], + ]; + lGraphCanvas.renderLink( + ctx, + start_node_slotpos, + end_node_slotpos, + null, + false, + null, + "#FFF", + LiteGraph.RIGHT, + LiteGraph.LEFT + ); + } + } } LiteGraph.registerNodeType( @@ -233,6 +277,7 @@ app.registerExtension({ defaultVisibility = true; serialize_widgets = true; + drawConnection = false; constructor() { if (!this.properties) { @@ -266,7 +311,7 @@ app.registerExtension({ ) { this.validateLinks(); } - + this.setName = function(name) { node.widgets[0].value = name; node.onRename(); @@ -337,6 +382,42 @@ app.registerExtension({ } onAdded(graph) { } + onSelected() { + this.drawConnection = true; + } + onDeselected() { + this.drawConnection = false; + } + onDrawForeground(ctx, lGraphCanvas) { + if (this.drawConnection) { + this._drawVirtualLink(lGraphCanvas, ctx); + } + } + onDrawCollapsed(ctx, lGraphCanvas) { + if (this.drawConnection) { + this._drawVirtualLink(lGraphCanvas, ctx); + } + } + _drawVirtualLink(lGraphCanvas, ctx) { + const setter = this.findSetter(this.graph); + if (!setter) return; + // draw the virtual connection from SetNode to GetNode + let start_node_slotpos = setter.getConnectionPos(false, 0); + start_node_slotpos = [ + start_node_slotpos[0] - this.pos[0], + start_node_slotpos[1] - this.pos[1], + ]; + let end_node_slotpos = [0, -LiteGraph.NODE_TITLE_HEIGHT * 0.5]; + lGraphCanvas.renderLink( + ctx, + start_node_slotpos, + end_node_slotpos, + null, + false, + null, + "#FFF" + ); + } } LiteGraph.registerNodeType(