From 7d5d0fd577fd6c4ad5de8d07a71aa7599c457b70 Mon Sep 17 00:00:00 2001 From: Jairo Correa Date: Mon, 16 Oct 2023 15:12:40 -0300 Subject: [PATCH 1/3] Group options - Add Group For Selected Nodes - Add Selected Nodes To Group - Fit Group To Nodes --- web/extensions/core/groupOptions.js | 71 +++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/web/extensions/core/groupOptions.js b/web/extensions/core/groupOptions.js index d523737dc..3ec0e3fd9 100644 --- a/web/extensions/core/groupOptions.js +++ b/web/extensions/core/groupOptions.js @@ -5,6 +5,49 @@ function setNodeMode(node, mode) { node.graph.change(); } +function addNodesToGroup(group, nodes=[]) { + var x1, y1, x2, y2; + var nx1, ny1, nx2, ny2; + var node; + + x1 = y1 = x2 = y2 = -1; + nx1 = ny1 = nx2 = ny2 = -1; + + for (var n of [group._nodes, nodes]) { + for (var i in n) { + node = n[i] + + nx1 = node.pos[0] + ny1 = node.pos[1] + nx2 = node.pos[0] + node.size[0] + ny2 = node.pos[1] + node.size[1] + + if (x1 == -1 || nx1 < x1) { + x1 = nx1; + } + + if (y1 == -1 || ny1 < y1) { + y1 = ny1; + } + + if (x2 == -1 || nx2 > x2) { + x2 = nx2; + } + + if (y2 == -1 || ny2 > y2) { + y2 = ny2; + } + } + } + + var padding = 10; + + y1 = y1 - Math.round(group.font_size * 2.7); + + group.pos = [x1 - padding, y1 - padding]; + group.size = [x2 - x1 + padding * 2, y2 - y1 + padding * 2]; +} + app.registerExtension({ name: "Comfy.GroupOptions", setup() { @@ -14,6 +57,17 @@ app.registerExtension({ const options = orig.apply(this, arguments); const group = this.graph.getGroupOnPos(this.graph_mouse[0], this.graph_mouse[1]); if (!group) { + options.push({ + content: "Add Group For Selected Nodes", + disabled: !Object.keys(app.canvas.selected_nodes || {}).length, + callback: () => { + var group = new LiteGraph.LGraphGroup(); + addNodesToGroup(group, this.selected_nodes) + app.canvas.graph.add(group); + this.graph.change(); + } + }); + return options; } @@ -38,6 +92,23 @@ app.registerExtension({ } } + options.push({ + content: "Add Selected Nodes To Group", + disabled: !Object.keys(app.canvas.selected_nodes || {}).length, + callback: () => { + addNodesToGroup(group, this.selected_nodes) + this.graph.change(); + } + }); + + options.push({ + content: "Fit Group To Nodes", + callback: () => { + addNodesToGroup(group) + this.graph.change(); + } + }); + options.push({ content: "Select Nodes", callback: () => { From e8c02219ee41424d076682841dd84a1ee9093190 Mon Sep 17 00:00:00 2001 From: Jairo Correa Date: Mon, 16 Oct 2023 15:26:36 -0300 Subject: [PATCH 2/3] Fix add selected nodes to empty group --- web/extensions/core/groupOptions.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/web/extensions/core/groupOptions.js b/web/extensions/core/groupOptions.js index 3ec0e3fd9..d3d52bf29 100644 --- a/web/extensions/core/groupOptions.js +++ b/web/extensions/core/groupOptions.js @@ -75,6 +75,15 @@ app.registerExtension({ group.recomputeInsideNodes(); const nodesInGroup = group._nodes; + options.push({ + content: "Add Selected Nodes To Group", + disabled: !Object.keys(app.canvas.selected_nodes || {}).length, + callback: () => { + addNodesToGroup(group, this.selected_nodes) + this.graph.change(); + } + }); + // No nodes in group, return default options if (nodesInGroup.length === 0) { return options; @@ -92,15 +101,6 @@ app.registerExtension({ } } - options.push({ - content: "Add Selected Nodes To Group", - disabled: !Object.keys(app.canvas.selected_nodes || {}).length, - callback: () => { - addNodesToGroup(group, this.selected_nodes) - this.graph.change(); - } - }); - options.push({ content: "Fit Group To Nodes", callback: () => { From 682c84ccf3fb601d0bd3ae8a506cc7e3d42d2199 Mon Sep 17 00:00:00 2001 From: Jairo Correa Date: Mon, 16 Oct 2023 16:00:01 -0300 Subject: [PATCH 3/3] Fix fit group to nodes with reroute and collapsed nodes --- web/extensions/core/groupOptions.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/web/extensions/core/groupOptions.js b/web/extensions/core/groupOptions.js index d3d52bf29..5dd21e730 100644 --- a/web/extensions/core/groupOptions.js +++ b/web/extensions/core/groupOptions.js @@ -22,6 +22,18 @@ function addNodesToGroup(group, nodes=[]) { nx2 = node.pos[0] + node.size[0] ny2 = node.pos[1] + node.size[1] + if (node.type != "Reroute") { + ny1 -= LiteGraph.NODE_TITLE_HEIGHT; + } + + if (node.flags?.collapsed) { + ny2 = ny1 + LiteGraph.NODE_TITLE_HEIGHT; + + if (node?._collapsed_width) { + nx2 = nx1 + Math.round(node._collapsed_width); + } + } + if (x1 == -1 || nx1 < x1) { x1 = nx1; } @@ -42,7 +54,7 @@ function addNodesToGroup(group, nodes=[]) { var padding = 10; - y1 = y1 - Math.round(group.font_size * 2.7); + y1 = y1 - Math.round(group.font_size * 1.4); group.pos = [x1 - padding, y1 - padding]; group.size = [x2 - x1 + padding * 2, y2 - y1 + padding * 2];