feat: initial node groups

This commit is contained in:
2026-04-26 18:41:25 +02:00
parent a56e8f445e
commit 72f07d0a50
17 changed files with 488 additions and 76 deletions
@@ -1,11 +1,11 @@
import { animate, lerp } from '$lib/helpers';
import type { NodeInstance, Socket } from '@nodarium/types';
import type { Box, Edge, GroupDefinition, NodeInput, NodeInstance, Socket } from '@nodarium/types';
import { getContext, setContext } from 'svelte';
import { SvelteMap, SvelteSet } from 'svelte/reactivity';
import type { OrthographicCamera, Vector3 } from 'three';
import type { GraphManager } from './graph-manager.svelte';
import { ColorGenerator } from './graph/colors';
import { getNodeHeight, getSocketPosition } from './helpers/nodeHelpers';
import { getNodeHeight, getParameterHeight } from './helpers/nodeHelpers';
const graphStateKey = Symbol('graph-state');
export function getGraphState() {
@@ -203,7 +203,7 @@ export class GraphState {
}
const debugNode = this.graph.createNode({
type: 'max/plantarium/debug',
type: '__internal/node/debug',
position: [node.position[0] + 30, node.position[1]],
props: {}
});
@@ -240,6 +240,119 @@ export class GraphState {
};
}
groupSelectedNodes(nodeIds = [...this.selectedNodes.keys(), this.activeNodeId]) {
const ids = new Set(nodeIds);
const nodes = [
...ids.values().map(id => this.graph.getNode(id)).filter(Boolean)
] as NodeInstance[];
const incomingEdges = this.graph.edges.filter((edge) =>
ids.has(edge[2].id) && !ids.has(edge[0].id)
);
const groupInputs = new Map<string, Edge>();
for (const edge of incomingEdges) {
groupInputs.set(`${edge[0].id}-${edge[1]}`, edge);
}
const outgoingEdges = this.graph.edges.filter((edge) =>
ids.has(edge[0].id) && !ids.has(edge[2].id)
);
const groupOutputs = new Map<string, Edge>();
for (const edge of outgoingEdges) {
groupOutputs.set(`${edge[2].id}-${edge[3]}`, edge);
}
const inputs: Record<string, NodeInput> = {};
[...groupInputs.values()].forEach((edge, i) => {
const input = {
label: `Input ${i}`,
type: edge[0].state.type?.outputs?.[edge[1]] || '*'
};
inputs[`input_${i}`] = input as NodeInput;
});
const outputs = [...groupOutputs.values()].map((edge, i) => ({
label: `Output ${i}`,
type: edge[2].state.type?.inputs?.[edge[3]].type
}));
const groupPosition = [0, 0] as [number, number];
const bounds: Box = { minX: Infinity, maxX: -Infinity, minY: Infinity, maxY: -Infinity };
for (const node of nodes) {
groupPosition[0] += node.position[0];
groupPosition[1] += node.position[1];
bounds.minX = Math.min(bounds.minX, node.position[0]);
bounds.maxX = Math.max(bounds.maxX, node.position[0]);
bounds.minY = Math.min(bounds.minY, node.position[1]);
bounds.maxY = Math.max(bounds.maxY, node.position[1]);
}
groupPosition[0] /= nodes.length;
groupPosition[1] /= nodes.length;
const groupInputNode: NodeInstance = {
id: this.graph.createNodeId(),
type: '__internal/group/input',
position: [bounds.minX - 50, (bounds.minY + bounds.maxY) / 2],
state: {}
};
const groupOutputNode: NodeInstance = {
id: this.graph.createNodeId(),
type: '__internal/group/output',
position: [bounds.maxX + 25, (bounds.minY + bounds.maxY) / 2],
state: {}
};
// Edges that are inside the group
const internalEdges = this.graph.edges.filter((edge) => {
return ids.has(edge[0].id) || ids.has(edge[2].id);
}).map((edge) => {
// Going in from the group
if (!ids.has(edge[0].id)) {
return [groupInputNode, 0, edge[2], edge[3]];
// Going out to the group
} else if (!ids.has(edge[2].id)) {
return [edge[0], edge[1], groupOutputNode, 'Out'];
}
return edge;
});
const groupId = this.graph.createGroupId();
const groupDefinition: GroupDefinition = {
id: groupId,
inputs: inputs,
outputs: outputs,
edges: internalEdges,
nodes: [groupInputNode, ...nodes, groupOutputNode]
};
const groupNode = this.graph.createGroupNode(groupPosition, groupDefinition);
// Update the edges that are now inside
// the group to be connected to that group node
const externalEdges = this.graph.edges.map((edge) => {
if (ids.has(edge[2].id)) {
// Edge going into the group
return [edge[0], edge[1], groupNode, 'input_0'] as Edge;
} else if (ids.has(edge[0].id)) {
// Edge going out of the group
return [groupNode, 0, edge[2], edge[3]] as Edge;
}
return edge;
});
for (const node of nodes) {
this.graph.nodes.delete(node.id);
}
this.graph.edges = externalEdges;
this.graph.saveUndoGroup();
console.log(
$state.snapshot({
groupNode,
groupDefinition
})
);
}
centerNode(node?: NodeInstance) {
const average = [0, 0, 4];
if (node) {
@@ -301,7 +414,7 @@ export class GraphState {
if (edge[3] === index) {
node = edge[0];
index = edge[1];
position = getSocketPosition(node, index);
position = this.getSocketPosition(node, index);
this.graph.removeEdge(edge);
break;
}
@@ -321,7 +434,7 @@ export class GraphState {
return {
node,
index,
position: getSocketPosition(node, index)
position: this.getSocketPosition(node, index)
};
});
}
@@ -358,7 +471,7 @@ export class GraphState {
for (const node of this.graph.nodes.values()) {
const x = node.position[0];
const y = node.position[1];
const height = getNodeHeight(node.state.type!);
const height = getNodeHeight(this.graph.getNodeType(node)!);
if (downX > x && downX < x + 20 && downY > y && downY < y + height) {
clickedNodeId = node.id;
break;
@@ -370,7 +483,7 @@ export class GraphState {
}
isNodeInView(node: NodeInstance) {
const height = getNodeHeight(node.state.type!);
const height = getNodeHeight(this.graph.getNodeType(node)!);
const width = 20;
return node.position[0] > this.cameraBounds[0] - width
&& node.position[0] < this.cameraBounds[1]
@@ -381,4 +494,38 @@ export class GraphState {
openNodePalette() {
this.addMenuPosition = [this.mousePosition[0], this.mousePosition[1]];
}
enterGroupNode() {
if (this.activeNodeId === -1) return;
const selectedNode = this.graph.getNode(this.activeNodeId);
if (!selectedNode || selectedNode.type.startsWith('__internal/group/instance')) return;
}
getSocketPosition(
node: NodeInstance,
index: string | number
): [number, number] {
if (typeof index === 'number') {
return [
(node?.state?.x ?? node.position[0]) + 20,
(node?.state?.y ?? node.position[1]) + 2.5 + 10 * index
];
} else {
let height = 5;
const nodeType = this.graph.getNodeType(node)!;
const inputs = nodeType.inputs || {};
for (const inputKey in inputs) {
const h = getParameterHeight(nodeType, inputKey) / 10;
if (inputKey === index) {
height += h / 2;
break;
}
height += h;
}
return [
node?.state?.x ?? node.position[0],
(node?.state?.y ?? node.position[1]) + height
];
}
}
}