feat: make more node group features work
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { describe, expect, it } from 'vitest';
|
import { assert, describe, expect, it } from 'vitest';
|
||||||
import { GraphManager } from './graph-manager.svelte';
|
import { GraphManager } from './graph-manager.svelte';
|
||||||
import {
|
import {
|
||||||
createMockNodeRegistry,
|
createMockNodeRegistry,
|
||||||
@@ -9,257 +9,399 @@ import {
|
|||||||
mockVec3OutputNode
|
mockVec3OutputNode
|
||||||
} from './test-utils';
|
} from './test-utils';
|
||||||
|
|
||||||
describe('GraphManager', () => {
|
describe('groupNodes', () => {
|
||||||
describe('getPossibleSockets', () => {
|
it('should not do anything if no nodes are selected', () => {
|
||||||
describe('when dragging an output socket', () => {
|
const registry = createMockNodeRegistry([
|
||||||
it('should return compatible input sockets based on type', () => {
|
mockFloatOutputNode,
|
||||||
const registry = createMockNodeRegistry([
|
mockFloatInputNode,
|
||||||
mockFloatOutputNode,
|
mockGeometryOutputNode,
|
||||||
mockFloatInputNode,
|
mockPathInputNode
|
||||||
mockGeometryOutputNode,
|
]);
|
||||||
mockPathInputNode
|
|
||||||
]);
|
|
||||||
|
|
||||||
const manager = new GraphManager(registry);
|
const manager = new GraphManager(registry);
|
||||||
|
|
||||||
const floatInputNode = manager.createNode({
|
const floatInputNode = manager.createNode({
|
||||||
type: 'test/node/input',
|
type: 'test/node/input',
|
||||||
position: [100, 100],
|
position: [100, 100],
|
||||||
props: {}
|
props: {}
|
||||||
});
|
});
|
||||||
|
|
||||||
const floatOutputNode = manager.createNode({
|
assert.isDefined(floatInputNode);
|
||||||
type: 'test/node/output',
|
|
||||||
position: [0, 0],
|
|
||||||
props: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(floatInputNode).toBeDefined();
|
const floatOutputNode = manager.createNode({
|
||||||
expect(floatOutputNode).toBeDefined();
|
type: 'test/node/output',
|
||||||
|
position: [0, 0],
|
||||||
|
props: {}
|
||||||
|
});
|
||||||
|
assert.isDefined(floatOutputNode);
|
||||||
|
|
||||||
const possibleSockets = manager.getPossibleSockets({
|
const edge = manager.createEdge(floatInputNode, 0, floatOutputNode, 'input');
|
||||||
node: floatOutputNode!,
|
assert.isDefined(edge);
|
||||||
index: 0,
|
manager.save();
|
||||||
position: [0, 0]
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(possibleSockets.length).toBe(1);
|
manager.groupNodes([]);
|
||||||
const socketNodeIds = possibleSockets.map(([node]) => node.id);
|
|
||||||
expect(socketNodeIds).toContain(floatInputNode!.id);
|
const graph = manager.serialize();
|
||||||
|
expect(graph.nodes.length).toBe(2);
|
||||||
|
expect(graph.edges.length).toBe(1);
|
||||||
|
expect(graph.groups.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should group selected nodes and create a group node', () => {
|
||||||
|
const registry = createMockNodeRegistry([
|
||||||
|
mockFloatOutputNode,
|
||||||
|
mockFloatInputNode,
|
||||||
|
mockGeometryOutputNode,
|
||||||
|
mockPathInputNode
|
||||||
|
]);
|
||||||
|
|
||||||
|
const manager = new GraphManager(registry);
|
||||||
|
|
||||||
|
const floatInputNode = manager.createNode({
|
||||||
|
type: 'test/node/input',
|
||||||
|
position: [100, 100],
|
||||||
|
props: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.isDefined(floatInputNode);
|
||||||
|
|
||||||
|
const floatOutputNode = manager.createNode({
|
||||||
|
type: 'test/node/output',
|
||||||
|
position: [0, 0],
|
||||||
|
props: {}
|
||||||
|
});
|
||||||
|
assert.isDefined(floatOutputNode);
|
||||||
|
|
||||||
|
const edge = manager.createEdge(floatInputNode, 0, floatOutputNode, 'input');
|
||||||
|
assert.isDefined(edge);
|
||||||
|
manager.save();
|
||||||
|
|
||||||
|
const groupNode = manager.groupNodes([floatInputNode.id]);
|
||||||
|
assert.isDefined(groupNode);
|
||||||
|
|
||||||
|
const graph = manager.serialize();
|
||||||
|
|
||||||
|
expect(graph.nodes.map(n => n.id), 'graph to contain group node').to.contain(groupNode.id);
|
||||||
|
expect(graph.groups[0].nodes.map(n => n.id), 'group graph to contain float node').to.contain(
|
||||||
|
floatInputNode.id
|
||||||
|
);
|
||||||
|
expect(graph.nodes.map(n => n.id)).not.to.contain(floatInputNode.id);
|
||||||
|
|
||||||
|
expect(graph.nodes.length).toBe(2);
|
||||||
|
expect(graph.edges.length).toBe(1);
|
||||||
|
expect(graph.groups.length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should rewire external edges when grouping a middle node in a chain', () => {
|
||||||
|
const registry = createMockNodeRegistry([mockFloatOutputNode, mockFloatInputNode]);
|
||||||
|
const manager = new GraphManager(registry);
|
||||||
|
|
||||||
|
// A → B → C (float chain: output → middle → input)
|
||||||
|
const nodeA = manager.createNode({ type: 'test/node/output', position: [0, 0], props: {} });
|
||||||
|
const nodeB = manager.createNode({ type: 'test/node/output', position: [100, 0], props: {} });
|
||||||
|
const nodeC = manager.createNode({ type: 'test/node/input', position: [200, 0], props: {} });
|
||||||
|
|
||||||
|
assert.isDefined(nodeA);
|
||||||
|
assert.isDefined(nodeB);
|
||||||
|
assert.isDefined(nodeC);
|
||||||
|
|
||||||
|
manager.createEdge(nodeA, 0, nodeB, 'input');
|
||||||
|
manager.createEdge(nodeB, 0, nodeC, 'value');
|
||||||
|
|
||||||
|
const groupNode = manager.groupNodes([nodeB.id]);
|
||||||
|
assert.isDefined(groupNode);
|
||||||
|
|
||||||
|
const graph = manager.serialize();
|
||||||
|
|
||||||
|
// Top-level: A, C, groupNode — B is gone
|
||||||
|
expect(graph.nodes.length, 'top-level node count').toBe(3);
|
||||||
|
const topLevelIds = graph.nodes.map(n => n.id);
|
||||||
|
expect(topLevelIds).toContain(nodeA.id);
|
||||||
|
expect(topLevelIds).toContain(nodeC.id);
|
||||||
|
expect(topLevelIds).toContain(groupNode.id);
|
||||||
|
expect(topLevelIds).not.toContain(nodeB.id);
|
||||||
|
|
||||||
|
// Both original edges survive, now routing through the group node
|
||||||
|
expect(graph.edges.length, 'edge count unchanged').toBe(2);
|
||||||
|
const edgeSources = graph.edges.map(e => e[0]);
|
||||||
|
const edgeTargets = graph.edges.map(e => e[2]);
|
||||||
|
expect(edgeTargets).toContain(groupNode.id); // A → groupNode
|
||||||
|
expect(edgeSources).toContain(groupNode.id); // groupNode → C
|
||||||
|
|
||||||
|
// One group definition was created
|
||||||
|
expect(graph.groups.length).toBe(1);
|
||||||
|
const group = graph.groups[0];
|
||||||
|
|
||||||
|
// Group contains B plus the two boundary nodes
|
||||||
|
const groupNodeIds = group.nodes.map(n => n.id);
|
||||||
|
expect(groupNodeIds).toContain(nodeB.id);
|
||||||
|
const inputBoundary = group.nodes.find(n => n.type === '__internal/group/input');
|
||||||
|
const outputBoundary = group.nodes.find(n => n.type === '__internal/group/output');
|
||||||
|
expect(inputBoundary, 'group input boundary node').toBeDefined();
|
||||||
|
expect(outputBoundary, 'group output boundary node').toBeDefined();
|
||||||
|
|
||||||
|
// Group declares one input slot and one output slot
|
||||||
|
expect(Object.keys(group.inputs ?? {}).length, 'group input count').toBe(1);
|
||||||
|
expect(group.outputs?.length, 'group output count').toBe(1);
|
||||||
|
|
||||||
|
// Internal edges wire: inputBoundary → B → outputBoundary
|
||||||
|
expect(group.edges.length, 'internal edge count').toBe(2);
|
||||||
|
const internalSources = group.edges.map(e => e[0]);
|
||||||
|
const internalTargets = group.edges.map(e => e[2]);
|
||||||
|
expect(internalTargets).toContain(nodeB.id);
|
||||||
|
expect(internalSources).toContain(nodeB.id);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getPossibleSockets', () => {
|
||||||
|
describe('when dragging an output socket', () => {
|
||||||
|
it('should return compatible input sockets based on type', () => {
|
||||||
|
const registry = createMockNodeRegistry([
|
||||||
|
mockFloatOutputNode,
|
||||||
|
mockFloatInputNode,
|
||||||
|
mockGeometryOutputNode,
|
||||||
|
mockPathInputNode
|
||||||
|
]);
|
||||||
|
|
||||||
|
const manager = new GraphManager(registry);
|
||||||
|
|
||||||
|
const floatInputNode = manager.createNode({
|
||||||
|
type: 'test/node/input',
|
||||||
|
position: [100, 100],
|
||||||
|
props: {}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should exclude self node from possible sockets', () => {
|
const floatOutputNode = manager.createNode({
|
||||||
const registry = createMockNodeRegistry([
|
type: 'test/node/output',
|
||||||
mockFloatOutputNode,
|
position: [0, 0],
|
||||||
mockFloatInputNode
|
props: {}
|
||||||
]);
|
|
||||||
|
|
||||||
const manager = new GraphManager(registry);
|
|
||||||
|
|
||||||
const floatInputNode = manager.createNode({
|
|
||||||
type: 'test/node/input',
|
|
||||||
position: [100, 100],
|
|
||||||
props: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(floatInputNode).toBeDefined();
|
|
||||||
|
|
||||||
const possibleSockets = manager.getPossibleSockets({
|
|
||||||
node: floatInputNode!,
|
|
||||||
index: 'value',
|
|
||||||
position: [0, 0]
|
|
||||||
});
|
|
||||||
|
|
||||||
const socketNodeIds = possibleSockets.map(([node]) => node.id);
|
|
||||||
expect(socketNodeIds).not.toContain(floatInputNode!.id);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should exclude parent nodes from possible sockets when dragging output', () => {
|
expect(floatInputNode).toBeDefined();
|
||||||
const registry = createMockNodeRegistry([
|
expect(floatOutputNode).toBeDefined();
|
||||||
mockFloatOutputNode,
|
|
||||||
mockFloatInputNode
|
|
||||||
]);
|
|
||||||
|
|
||||||
const manager = new GraphManager(registry);
|
const possibleSockets = manager.getPossibleSockets({
|
||||||
|
node: floatOutputNode!,
|
||||||
const parentNode = manager.createNode({
|
index: 0,
|
||||||
type: 'test/node/output',
|
position: [0, 0]
|
||||||
position: [0, 0],
|
|
||||||
props: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
const childNode = manager.createNode({
|
|
||||||
type: 'test/node/input',
|
|
||||||
position: [100, 100],
|
|
||||||
props: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(parentNode).toBeDefined();
|
|
||||||
expect(childNode).toBeDefined();
|
|
||||||
|
|
||||||
if (parentNode && childNode) {
|
|
||||||
manager.createEdge(parentNode, 0, childNode, 'value');
|
|
||||||
}
|
|
||||||
|
|
||||||
const possibleSockets = manager.getPossibleSockets({
|
|
||||||
node: parentNode!,
|
|
||||||
index: 0,
|
|
||||||
position: [0, 0]
|
|
||||||
});
|
|
||||||
|
|
||||||
const socketNodeIds = possibleSockets.map(([node]) => node.id);
|
|
||||||
expect(socketNodeIds).not.toContain(childNode!.id);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return sockets compatible with accepts property', () => {
|
expect(possibleSockets.length).toBe(1);
|
||||||
const registry = createMockNodeRegistry([
|
const socketNodeIds = possibleSockets.map(([node]) => node.id);
|
||||||
mockGeometryOutputNode,
|
expect(socketNodeIds).toContain(floatInputNode!.id);
|
||||||
mockPathInputNode
|
});
|
||||||
]);
|
|
||||||
|
|
||||||
const manager = new GraphManager(registry);
|
it('should exclude self node from possible sockets', () => {
|
||||||
|
const registry = createMockNodeRegistry([
|
||||||
|
mockFloatOutputNode,
|
||||||
|
mockFloatInputNode
|
||||||
|
]);
|
||||||
|
|
||||||
const geometryOutputNode = manager.createNode({
|
const manager = new GraphManager(registry);
|
||||||
type: 'test/node/geometry',
|
|
||||||
position: [0, 0],
|
|
||||||
props: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
const pathInputNode = manager.createNode({
|
const floatInputNode = manager.createNode({
|
||||||
type: 'test/node/path',
|
type: 'test/node/input',
|
||||||
position: [100, 100],
|
position: [100, 100],
|
||||||
props: {}
|
props: {}
|
||||||
});
|
|
||||||
|
|
||||||
expect(geometryOutputNode).toBeDefined();
|
|
||||||
expect(pathInputNode).toBeDefined();
|
|
||||||
|
|
||||||
const possibleSockets = manager.getPossibleSockets({
|
|
||||||
node: geometryOutputNode!,
|
|
||||||
index: 0,
|
|
||||||
position: [0, 0]
|
|
||||||
});
|
|
||||||
|
|
||||||
const socketNodeIds = possibleSockets.map(([node]) => node.id);
|
|
||||||
expect(socketNodeIds).toContain(pathInputNode!.id);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return empty array when no compatible sockets exist', () => {
|
expect(floatInputNode).toBeDefined();
|
||||||
const registry = createMockNodeRegistry([
|
|
||||||
mockVec3OutputNode,
|
|
||||||
mockFloatInputNode
|
|
||||||
]);
|
|
||||||
|
|
||||||
const manager = new GraphManager(registry);
|
const possibleSockets = manager.getPossibleSockets({
|
||||||
|
node: floatInputNode!,
|
||||||
const vec3OutputNode = manager.createNode({
|
index: 'value',
|
||||||
type: 'test/node/vec3',
|
position: [0, 0]
|
||||||
position: [0, 0],
|
|
||||||
props: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
const floatInputNode = manager.createNode({
|
|
||||||
type: 'test/node/input',
|
|
||||||
position: [100, 100],
|
|
||||||
props: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(vec3OutputNode).toBeDefined();
|
|
||||||
expect(floatInputNode).toBeDefined();
|
|
||||||
|
|
||||||
const possibleSockets = manager.getPossibleSockets({
|
|
||||||
node: vec3OutputNode!,
|
|
||||||
index: 0,
|
|
||||||
position: [0, 0]
|
|
||||||
});
|
|
||||||
|
|
||||||
const socketNodeIds = possibleSockets.map(([node]) => node.id);
|
|
||||||
expect(socketNodeIds).not.toContain(floatInputNode!.id);
|
|
||||||
expect(possibleSockets.length).toBe(0);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return socket info with correct socket key for inputs', () => {
|
const socketNodeIds = possibleSockets.map(([node]) => node.id);
|
||||||
const registry = createMockNodeRegistry([
|
expect(socketNodeIds).not.toContain(floatInputNode!.id);
|
||||||
mockFloatOutputNode,
|
});
|
||||||
mockFloatInputNode
|
|
||||||
]);
|
|
||||||
|
|
||||||
const manager = new GraphManager(registry);
|
it('should exclude parent nodes from possible sockets when dragging output', () => {
|
||||||
|
const registry = createMockNodeRegistry([
|
||||||
|
mockFloatOutputNode,
|
||||||
|
mockFloatInputNode
|
||||||
|
]);
|
||||||
|
|
||||||
const floatOutputNode = manager.createNode({
|
const manager = new GraphManager(registry);
|
||||||
type: 'test/node/output',
|
|
||||||
position: [0, 0],
|
|
||||||
props: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
const floatInputNode = manager.createNode({
|
const parentNode = manager.createNode({
|
||||||
type: 'test/node/input',
|
type: 'test/node/output',
|
||||||
position: [100, 100],
|
position: [0, 0],
|
||||||
props: {}
|
props: {}
|
||||||
});
|
|
||||||
|
|
||||||
expect(floatOutputNode).toBeDefined();
|
|
||||||
expect(floatInputNode).toBeDefined();
|
|
||||||
|
|
||||||
const possibleSockets = manager.getPossibleSockets({
|
|
||||||
node: floatOutputNode!,
|
|
||||||
index: 0,
|
|
||||||
position: [0, 0]
|
|
||||||
});
|
|
||||||
|
|
||||||
const matchingSocket = possibleSockets.find(([node]) => node.id === floatInputNode!.id);
|
|
||||||
expect(matchingSocket).toBeDefined();
|
|
||||||
expect(matchingSocket![1]).toBe('value');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return multiple compatible sockets', () => {
|
const childNode = manager.createNode({
|
||||||
const registry = createMockNodeRegistry([
|
type: 'test/node/input',
|
||||||
mockFloatOutputNode,
|
position: [100, 100],
|
||||||
mockFloatInputNode,
|
props: {}
|
||||||
mockGeometryOutputNode,
|
|
||||||
mockPathInputNode
|
|
||||||
]);
|
|
||||||
|
|
||||||
const manager = new GraphManager(registry);
|
|
||||||
|
|
||||||
const floatOutputNode = manager.createNode({
|
|
||||||
type: 'test/node/output',
|
|
||||||
position: [0, 0],
|
|
||||||
props: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
const geometryOutputNode = manager.createNode({
|
|
||||||
type: 'test/node/geometry',
|
|
||||||
position: [200, 0],
|
|
||||||
props: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
const floatInputNode = manager.createNode({
|
|
||||||
type: 'test/node/input',
|
|
||||||
position: [100, 100],
|
|
||||||
props: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
const pathInputNode = manager.createNode({
|
|
||||||
type: 'test/node/path',
|
|
||||||
position: [300, 100],
|
|
||||||
props: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(floatOutputNode).toBeDefined();
|
|
||||||
expect(geometryOutputNode).toBeDefined();
|
|
||||||
expect(floatInputNode).toBeDefined();
|
|
||||||
expect(pathInputNode).toBeDefined();
|
|
||||||
|
|
||||||
const possibleSocketsForFloat = manager.getPossibleSockets({
|
|
||||||
node: floatOutputNode!,
|
|
||||||
index: 0,
|
|
||||||
position: [0, 0]
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(possibleSocketsForFloat.length).toBe(1);
|
|
||||||
expect(possibleSocketsForFloat.map(([n]) => n.id)).toContain(floatInputNode!.id);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
expect(parentNode).toBeDefined();
|
||||||
|
expect(childNode).toBeDefined();
|
||||||
|
|
||||||
|
if (parentNode && childNode) {
|
||||||
|
manager.createEdge(parentNode, 0, childNode, 'value');
|
||||||
|
}
|
||||||
|
|
||||||
|
const possibleSockets = manager.getPossibleSockets({
|
||||||
|
node: parentNode!,
|
||||||
|
index: 0,
|
||||||
|
position: [0, 0]
|
||||||
|
});
|
||||||
|
|
||||||
|
const socketNodeIds = possibleSockets.map(([node]) => node.id);
|
||||||
|
expect(socketNodeIds).not.toContain(childNode!.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return sockets compatible with accepts property', () => {
|
||||||
|
const registry = createMockNodeRegistry([
|
||||||
|
mockGeometryOutputNode,
|
||||||
|
mockPathInputNode
|
||||||
|
]);
|
||||||
|
|
||||||
|
const manager = new GraphManager(registry);
|
||||||
|
|
||||||
|
const geometryOutputNode = manager.createNode({
|
||||||
|
type: 'test/node/geometry',
|
||||||
|
position: [0, 0],
|
||||||
|
props: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
const pathInputNode = manager.createNode({
|
||||||
|
type: 'test/node/path',
|
||||||
|
position: [100, 100],
|
||||||
|
props: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(geometryOutputNode).toBeDefined();
|
||||||
|
expect(pathInputNode).toBeDefined();
|
||||||
|
|
||||||
|
const possibleSockets = manager.getPossibleSockets({
|
||||||
|
node: geometryOutputNode!,
|
||||||
|
index: 0,
|
||||||
|
position: [0, 0]
|
||||||
|
});
|
||||||
|
|
||||||
|
const socketNodeIds = possibleSockets.map(([node]) => node.id);
|
||||||
|
expect(socketNodeIds).toContain(pathInputNode!.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return empty array when no compatible sockets exist', () => {
|
||||||
|
const registry = createMockNodeRegistry([
|
||||||
|
mockVec3OutputNode,
|
||||||
|
mockFloatInputNode
|
||||||
|
]);
|
||||||
|
|
||||||
|
const manager = new GraphManager(registry);
|
||||||
|
|
||||||
|
const vec3OutputNode = manager.createNode({
|
||||||
|
type: 'test/node/vec3',
|
||||||
|
position: [0, 0],
|
||||||
|
props: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
const floatInputNode = manager.createNode({
|
||||||
|
type: 'test/node/input',
|
||||||
|
position: [100, 100],
|
||||||
|
props: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(vec3OutputNode).toBeDefined();
|
||||||
|
expect(floatInputNode).toBeDefined();
|
||||||
|
|
||||||
|
const possibleSockets = manager.getPossibleSockets({
|
||||||
|
node: vec3OutputNode!,
|
||||||
|
index: 0,
|
||||||
|
position: [0, 0]
|
||||||
|
});
|
||||||
|
|
||||||
|
const socketNodeIds = possibleSockets.map(([node]) => node.id);
|
||||||
|
expect(socketNodeIds).not.toContain(floatInputNode!.id);
|
||||||
|
expect(possibleSockets.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return socket info with correct socket key for inputs', () => {
|
||||||
|
const registry = createMockNodeRegistry([
|
||||||
|
mockFloatOutputNode,
|
||||||
|
mockFloatInputNode
|
||||||
|
]);
|
||||||
|
|
||||||
|
const manager = new GraphManager(registry);
|
||||||
|
|
||||||
|
const floatOutputNode = manager.createNode({
|
||||||
|
type: 'test/node/output',
|
||||||
|
position: [0, 0],
|
||||||
|
props: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
const floatInputNode = manager.createNode({
|
||||||
|
type: 'test/node/input',
|
||||||
|
position: [100, 100],
|
||||||
|
props: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(floatOutputNode).toBeDefined();
|
||||||
|
expect(floatInputNode).toBeDefined();
|
||||||
|
|
||||||
|
const possibleSockets = manager.getPossibleSockets({
|
||||||
|
node: floatOutputNode!,
|
||||||
|
index: 0,
|
||||||
|
position: [0, 0]
|
||||||
|
});
|
||||||
|
|
||||||
|
const matchingSocket = possibleSockets.find(([node]) => node.id === floatInputNode!.id);
|
||||||
|
expect(matchingSocket).toBeDefined();
|
||||||
|
expect(matchingSocket![1]).toBe('value');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return multiple compatible sockets', () => {
|
||||||
|
const registry = createMockNodeRegistry([
|
||||||
|
mockFloatOutputNode,
|
||||||
|
mockFloatInputNode,
|
||||||
|
mockGeometryOutputNode,
|
||||||
|
mockPathInputNode
|
||||||
|
]);
|
||||||
|
|
||||||
|
const manager = new GraphManager(registry);
|
||||||
|
|
||||||
|
const floatOutputNode = manager.createNode({
|
||||||
|
type: 'test/node/output',
|
||||||
|
position: [0, 0],
|
||||||
|
props: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
const geometryOutputNode = manager.createNode({
|
||||||
|
type: 'test/node/geometry',
|
||||||
|
position: [200, 0],
|
||||||
|
props: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
const floatInputNode = manager.createNode({
|
||||||
|
type: 'test/node/input',
|
||||||
|
position: [100, 100],
|
||||||
|
props: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
const pathInputNode = manager.createNode({
|
||||||
|
type: 'test/node/path',
|
||||||
|
position: [300, 100],
|
||||||
|
props: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(floatOutputNode).toBeDefined();
|
||||||
|
expect(geometryOutputNode).toBeDefined();
|
||||||
|
expect(floatInputNode).toBeDefined();
|
||||||
|
expect(pathInputNode).toBeDefined();
|
||||||
|
|
||||||
|
const possibleSocketsForFloat = manager.getPossibleSockets({
|
||||||
|
node: floatOutputNode!,
|
||||||
|
index: 0,
|
||||||
|
position: [0, 0]
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(possibleSocketsForFloat.length).toBe(1);
|
||||||
|
expect(possibleSocketsForFloat.map(([n]) => n.id)).toContain(floatInputNode!.id);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import throttle from '$lib/helpers/throttle';
|
import throttle from '$lib/helpers/throttle';
|
||||||
import { RemoteNodeRegistry } from '$lib/node-registry/index';
|
import { RemoteNodeRegistry } from '$lib/node-registry/index';
|
||||||
import type {
|
import type {
|
||||||
|
Box,
|
||||||
Edge,
|
Edge,
|
||||||
Graph,
|
Graph,
|
||||||
|
GroupDefinition,
|
||||||
NodeDefinition,
|
NodeDefinition,
|
||||||
NodeId,
|
NodeId,
|
||||||
NodeInput,
|
NodeInput,
|
||||||
@@ -10,7 +12,6 @@ import type {
|
|||||||
NodeRegistry,
|
NodeRegistry,
|
||||||
Socket
|
Socket
|
||||||
} from '@nodarium/types';
|
} from '@nodarium/types';
|
||||||
import { type GroupDefinition } from '@nodarium/types';
|
|
||||||
import { fastHashString } from '@nodarium/utils';
|
import { fastHashString } from '@nodarium/utils';
|
||||||
import { createLogger } from '@nodarium/utils';
|
import { createLogger } from '@nodarium/utils';
|
||||||
import { SvelteMap, SvelteSet } from 'svelte/reactivity';
|
import { SvelteMap, SvelteSet } from 'svelte/reactivity';
|
||||||
@@ -120,19 +121,12 @@ export class GraphManager extends EventEmitter<{
|
|||||||
props: node.props
|
props: node.props
|
||||||
})) as NodeInstance[];
|
})) as NodeInstance[];
|
||||||
|
|
||||||
const groupEdges = this.edges.map((edge) => [
|
|
||||||
edge[0].id,
|
|
||||||
edge[1],
|
|
||||||
edge[2].id,
|
|
||||||
edge[3]
|
|
||||||
]) as Graph['edges'];
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: group.id,
|
id: group.id,
|
||||||
inputs: group.inputs,
|
inputs: group.inputs,
|
||||||
outputs: group.outputs,
|
outputs: group.outputs,
|
||||||
nodes: groupNodes,
|
nodes: groupNodes,
|
||||||
edges: groupEdges
|
edges: group.edges
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -332,15 +326,29 @@ export class GraphManager extends EventEmitter<{
|
|||||||
const a = performance.now();
|
const a = performance.now();
|
||||||
|
|
||||||
this.loaded = false;
|
this.loaded = false;
|
||||||
|
graph.groups ??= [];
|
||||||
this.graph = graph;
|
this.graph = graph;
|
||||||
this.status = 'loading';
|
this.status = 'loading';
|
||||||
this.id = graph.id;
|
this.id = graph.id;
|
||||||
|
|
||||||
logger.info('loading graph', { nodes: graph.nodes, edges: graph.edges, id: graph.id });
|
logger.info(
|
||||||
|
'loading graph',
|
||||||
|
{ nodes: graph.nodes, edges: graph.edges, id: graph.id }
|
||||||
|
);
|
||||||
|
|
||||||
const nodeIds = Array
|
const nodeIds = Array
|
||||||
.from(new SvelteSet([...graph.nodes.map((n) => n.type)]))
|
.from(
|
||||||
|
new SvelteSet(
|
||||||
|
[
|
||||||
|
...graph.nodes,
|
||||||
|
graph?.groups?.map(g => g.nodes).flat()
|
||||||
|
]
|
||||||
|
.filter(n => n && 'type' in n)
|
||||||
|
.map((n) => n.type)
|
||||||
|
)
|
||||||
|
)
|
||||||
.filter(n => !n.startsWith('__internal/'));
|
.filter(n => !n.startsWith('__internal/'));
|
||||||
|
|
||||||
await this.registry.load(nodeIds);
|
await this.registry.load(nodeIds);
|
||||||
|
|
||||||
// Fetch all nodes from all collections of the loaded nodes
|
// Fetch all nodes from all collections of the loaded nodes
|
||||||
@@ -419,15 +427,6 @@ export class GraphManager extends EventEmitter<{
|
|||||||
}
|
}
|
||||||
|
|
||||||
getAllNodes() {
|
getAllNodes() {
|
||||||
this.graph.groups ??= [];
|
|
||||||
if (!this.graph.groups.length) {
|
|
||||||
this.graph.groups.push({
|
|
||||||
id: 0,
|
|
||||||
nodes: [],
|
|
||||||
edges: []
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return Array
|
return Array
|
||||||
.from(this.nodes.values());
|
.from(this.nodes.values());
|
||||||
}
|
}
|
||||||
@@ -437,26 +436,34 @@ export class GraphManager extends EventEmitter<{
|
|||||||
}
|
}
|
||||||
|
|
||||||
getNodeType(node: NodeInstance) {
|
getNodeType(node: NodeInstance) {
|
||||||
// Construct the inputs on the fly
|
// Construct the group inputs on the fly
|
||||||
if (node.type === '__internal/group/instance') {
|
if (node.type === '__internal/group/instance') {
|
||||||
const groupDefinition = this.getGroup(node.props?.groupId as number);
|
const groupDefinition = this.getGroup(node.props?.groupId as number);
|
||||||
|
|
||||||
|
if (!groupDefinition) {
|
||||||
|
logger.error(`Group not found: ${node.props?.groupId}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const inputs = {
|
const inputs = {
|
||||||
'groupId': {
|
'groupId': {
|
||||||
type: 'select',
|
type: 'select',
|
||||||
label: '',
|
label: '',
|
||||||
value: node.props?.groupId.toString(),
|
value: node.props?.groupId,
|
||||||
internal: true,
|
internal: true,
|
||||||
options: this.graph.groups.map(g => g.id.toString())
|
options: this.graph.groups.map(g => g.id)
|
||||||
},
|
},
|
||||||
...(node.state.type?.inputs || {}),
|
...(node.state.type?.inputs || {}),
|
||||||
...groupDefinition?.inputs
|
...groupDefinition?.inputs
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
const groupType = {
|
||||||
...node.state.type,
|
...node.state.type,
|
||||||
inputs
|
inputs,
|
||||||
|
outputs: groupDefinition?.outputs?.map(o => o.type)
|
||||||
} as NodeDefinition;
|
} as NodeDefinition;
|
||||||
|
|
||||||
|
return groupType;
|
||||||
}
|
}
|
||||||
|
|
||||||
return node.state.type;
|
return node.state.type;
|
||||||
@@ -521,6 +528,7 @@ export class GraphManager extends EventEmitter<{
|
|||||||
}
|
}
|
||||||
|
|
||||||
removeNode(node: NodeInstance, { restoreEdges = false } = {}) {
|
removeNode(node: NodeInstance, { restoreEdges = false } = {}) {
|
||||||
|
console.log('REMOVING NODE', $state.snapshot({ node }));
|
||||||
const edgesToNode = this.edges.filter((edge) => edge[2].id === node.id);
|
const edgesToNode = this.edges.filter((edge) => edge[2].id === node.id);
|
||||||
const edgesFromNode = this.edges.filter((edge) => edge[0].id === node.id);
|
const edgesFromNode = this.edges.filter((edge) => edge[0].id === node.id);
|
||||||
for (const edge of [...edgesToNode, ...edgesFromNode]) {
|
for (const edge of [...edgesToNode, ...edgesFromNode]) {
|
||||||
@@ -564,16 +572,22 @@ export class GraphManager extends EventEmitter<{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
createGroupId() {
|
|
||||||
return Math.max(0, ...this.graph.groups.keys()) + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
getGroup(id: number) {
|
getGroup(id: number) {
|
||||||
return this.graph.groups.find(g => g.id === id);
|
return this.graph.groups.find(g => g.id === id);
|
||||||
}
|
}
|
||||||
|
|
||||||
createNodeId() {
|
createNodeId() {
|
||||||
return Math.max(0, ...this.nodes.keys()) + 1;
|
const ids = [
|
||||||
|
...this.nodes.keys(),
|
||||||
|
...this.graph.groups.map(g => g.id),
|
||||||
|
...this.graph.groups.flatMap(g => g.nodes.map(n => n.id))
|
||||||
|
];
|
||||||
|
|
||||||
|
let id = 0;
|
||||||
|
while (ids.includes(id)) {
|
||||||
|
id++;
|
||||||
|
}
|
||||||
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
createGraph(nodes: NodeInstance[], edges: [number, number, number, string][]) {
|
createGraph(nodes: NodeInstance[], edges: [number, number, number, string][]) {
|
||||||
@@ -586,7 +600,7 @@ export class GraphManager extends EventEmitter<{
|
|||||||
const id = startId++;
|
const id = startId++;
|
||||||
idMap.set(node.id, id);
|
idMap.set(node.id, id);
|
||||||
const type = this.registry.getNode(node.type);
|
const type = this.registry.getNode(node.type);
|
||||||
if (!type) {
|
if (!type && !node.type.startsWith('__internal/')) {
|
||||||
throw new Error(`Node type not found: ${node.type}`);
|
throw new Error(`Node type not found: ${node.type}`);
|
||||||
}
|
}
|
||||||
return { ...node, id, tmp: { type } };
|
return { ...node, id, tmp: { type } };
|
||||||
@@ -619,6 +633,148 @@ export class GraphManager extends EventEmitter<{
|
|||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
removeUnusedGroups() {
|
||||||
|
const usedGroups = new Set(this.getAllNodes().map(n => n.props?.groupId));
|
||||||
|
const unusedGroupAmount = this.graph.groups.length - usedGroups.size;
|
||||||
|
this.graph.groups = this.graph.groups.filter(g => usedGroups.has(g.id));
|
||||||
|
this.save();
|
||||||
|
return unusedGroupAmount;
|
||||||
|
}
|
||||||
|
|
||||||
|
groupNodes(nodeIds: number[]) {
|
||||||
|
this.startUndoGroup();
|
||||||
|
this.removeUnusedGroups();
|
||||||
|
|
||||||
|
const nodes = [
|
||||||
|
...new Set(nodeIds).values().map(id => this.getNode(id)).filter(Boolean)
|
||||||
|
] as NodeInstance[];
|
||||||
|
|
||||||
|
if (!nodes.length) return;
|
||||||
|
|
||||||
|
logger.log(`Grouping ${nodes.length} nodes`, { nodes });
|
||||||
|
|
||||||
|
const ids = new Set(nodes.map(n => n.id));
|
||||||
|
|
||||||
|
// We use the map to dedupe when one external node is connected to multiple internal nodes
|
||||||
|
// ┌──internal_a
|
||||||
|
// external──┤
|
||||||
|
// └──internal_b
|
||||||
|
// This should only result in one group input not two
|
||||||
|
const incomingEdges = this.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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// And the same for the outputs
|
||||||
|
const outgoingEdges = this.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.createNodeId(),
|
||||||
|
type: '__internal/group/input',
|
||||||
|
position: [bounds.minX - 50, (bounds.minY + bounds.maxY) / 2],
|
||||||
|
state: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
const groupOutputNode: NodeInstance = {
|
||||||
|
id: this.createNodeId(),
|
||||||
|
type: '__internal/group/output',
|
||||||
|
position: [bounds.maxX + 25, (bounds.minY + bounds.maxY) / 2],
|
||||||
|
state: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Edges that are inside the group
|
||||||
|
const internalEdges = this.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.id, 0, edge[2].id, edge[3]];
|
||||||
|
// Going out to the group
|
||||||
|
} else if (!ids.has(edge[2].id)) {
|
||||||
|
return [edge[0].id, edge[1], groupOutputNode.id, 'Out'];
|
||||||
|
}
|
||||||
|
return [edge[0].id, edge[1], edge[2].id, edge[3]];
|
||||||
|
}) as [number, number, number, string][];
|
||||||
|
|
||||||
|
const groupId = this.createNodeId();
|
||||||
|
const groupDefinition: GroupDefinition = {
|
||||||
|
id: groupId,
|
||||||
|
inputs: inputs,
|
||||||
|
outputs: outputs,
|
||||||
|
edges: internalEdges,
|
||||||
|
nodes: [groupInputNode, ...nodes, groupOutputNode]
|
||||||
|
};
|
||||||
|
|
||||||
|
const groupNode = this.createNode({
|
||||||
|
type: '__internal/group/instance',
|
||||||
|
position: [groupPosition[0], groupPosition[1]],
|
||||||
|
props: {
|
||||||
|
groupId: groupId
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!groupNode) throw new Error('Failed to create group node');
|
||||||
|
|
||||||
|
// Update the edges that are now inside
|
||||||
|
// the group to be connected to that group node
|
||||||
|
const externalEdges = this.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;
|
||||||
|
});
|
||||||
|
|
||||||
|
this.graph.groups.push(groupDefinition);
|
||||||
|
this.nodes.set(groupNode.id, groupNode);
|
||||||
|
this.edges = externalEdges;
|
||||||
|
|
||||||
|
// Remove nodes from graph which are not part of the group
|
||||||
|
for (const node of nodes) {
|
||||||
|
this.removeNode(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('FINISHED', this.serialize());
|
||||||
|
this.saveUndoGroup();
|
||||||
|
|
||||||
|
return groupNode;
|
||||||
|
}
|
||||||
|
|
||||||
createNode({
|
createNode({
|
||||||
type,
|
type,
|
||||||
position,
|
position,
|
||||||
@@ -629,7 +785,7 @@ export class GraphManager extends EventEmitter<{
|
|||||||
props: NodeInstance['props'];
|
props: NodeInstance['props'];
|
||||||
}) {
|
}) {
|
||||||
const nodeType = this.registry.getNode(type);
|
const nodeType = this.registry.getNode(type);
|
||||||
if (!nodeType) {
|
if (!nodeType && !type.startsWith('__internal/')) {
|
||||||
logger.error(`Node type not found: ${type}`);
|
logger.error(`Node type not found: ${type}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -649,26 +805,6 @@ export class GraphManager extends EventEmitter<{
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
createGroupNode(position: [number, number], groupDefinition: GroupDefinition): NodeInstance {
|
|
||||||
this.graph.groups ??= [];
|
|
||||||
this.graph.groups.push(groupDefinition);
|
|
||||||
const node = {
|
|
||||||
id: this.createNodeId(),
|
|
||||||
type: '__internal/group/instance',
|
|
||||||
meta: {
|
|
||||||
title: 'Group'
|
|
||||||
},
|
|
||||||
props: {
|
|
||||||
groupId: groupDefinition.id
|
|
||||||
},
|
|
||||||
position,
|
|
||||||
state: {}
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
this.nodes.set(node.id, node);
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
createEdge(
|
createEdge(
|
||||||
from: NodeInstance,
|
from: NodeInstance,
|
||||||
fromSocket: number,
|
fromSocket: number,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { animate, lerp } from '$lib/helpers';
|
import { animate, lerp } from '$lib/helpers';
|
||||||
import type { Box, Edge, GroupDefinition, NodeInput, NodeInstance, Socket } from '@nodarium/types';
|
import type { NodeInstance, Socket } from '@nodarium/types';
|
||||||
import { getContext, setContext } from 'svelte';
|
import { getContext, setContext } from 'svelte';
|
||||||
import { SvelteMap, SvelteSet } from 'svelte/reactivity';
|
import { SvelteMap, SvelteSet } from 'svelte/reactivity';
|
||||||
import type { OrthographicCamera, Vector3 } from 'three';
|
import type { OrthographicCamera, Vector3 } from 'three';
|
||||||
@@ -240,117 +240,8 @@ export class GraphState {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
groupSelectedNodes(nodeIds = [...this.selectedNodes.keys(), this.activeNodeId]) {
|
groupSelectedNodes() {
|
||||||
const ids = new Set(nodeIds);
|
return this.graph.groupNodes([...this.selectedNodes.keys(), this.activeNodeId]);
|
||||||
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) {
|
centerNode(node?: NodeInstance) {
|
||||||
|
|||||||
@@ -97,7 +97,6 @@
|
|||||||
|
|
||||||
function getSocketType(node: NodeInstance, index: number | string, e: unknown): string {
|
function getSocketType(node: NodeInstance, index: number | string, e: unknown): string {
|
||||||
const nodeType = graph.getNodeType(node);
|
const nodeType = graph.getNodeType(node);
|
||||||
console.log($state.snapshot({ nodeType, index, e }));
|
|
||||||
if (typeof index === 'string') {
|
if (typeof index === 'string') {
|
||||||
return nodeType?.inputs?.[index].type || 'unknown';
|
return nodeType?.inputs?.[index].type || 'unknown';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { createKeyMap } from '$lib/helpers/createKeyMap';
|
import { createKeyMap } from '$lib/helpers/createKeyMap';
|
||||||
import type { Graph, NodeInstance, NodeRegistry } from '@nodarium/types';
|
import type { Graph, NodeInstance, NodeRegistry } from '@nodarium/types';
|
||||||
|
import { onMount } from 'svelte';
|
||||||
import { GraphManager } from '../graph-manager.svelte';
|
import { GraphManager } from '../graph-manager.svelte';
|
||||||
import { GraphState, setGraphManager, setGraphState } from '../graph-state.svelte';
|
import { GraphState, setGraphManager, setGraphState } from '../graph-state.svelte';
|
||||||
import { setupKeymaps } from '../keymaps';
|
import { setupKeymaps } from '../keymaps';
|
||||||
@@ -83,7 +84,7 @@
|
|||||||
manager.on('save', (save) => onsave?.(save));
|
manager.on('save', (save) => onsave?.(save));
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (graph) {
|
if (graph && (manager.status !== 'idle' || manager.graph.id !== graph.id)) {
|
||||||
manager.load(graph);
|
manager.load(graph);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ export function setupKeymaps(keymap: Keymap, graph: GraphManager, graphState: Gr
|
|||||||
keymap.addShortcut({
|
keymap.addShortcut({
|
||||||
key: 'g',
|
key: 'g',
|
||||||
ctrl: true,
|
ctrl: true,
|
||||||
|
preventDefault: true,
|
||||||
description: 'Group selected nodes',
|
description: 'Group selected nodes',
|
||||||
callback: () => graphState.groupSelectedNodes()
|
callback: () => graphState.groupSelectedNodes()
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -43,10 +43,6 @@
|
|||||||
|
|
||||||
const height = $derived(getNodeHeight(nodeType));
|
const height = $derived(getNodeHeight(nodeType));
|
||||||
|
|
||||||
if (node.type.startsWith('__internal/')) {
|
|
||||||
$inspect({ node, nodeType, height, sectionHeights });
|
|
||||||
}
|
|
||||||
|
|
||||||
const zoom = $derived(graphState.cameraPosition[2]);
|
const zoom = $derived(graphState.cameraPosition[2]);
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { appSettings } from '$lib/settings/app-settings.svelte';
|
import { appSettings } from '$lib/settings/app-settings.svelte';
|
||||||
import type { NodeInstance, Socket } from '@nodarium/types';
|
import type { NodeInstance, Socket } from '@nodarium/types';
|
||||||
import { getGraphState } from '../graph-state.svelte';
|
import { getGraphManager, getGraphState } from '../graph-state.svelte';
|
||||||
import { createNodePath } from '../helpers/index.js';
|
import { createNodePath } from '../helpers/index.js';
|
||||||
|
|
||||||
const graphState = getGraphState();
|
const graphState = getGraphState();
|
||||||
|
const graph = getGraphManager();
|
||||||
|
|
||||||
const { node }: { node: NodeInstance } = $props();
|
const { node }: { node: NodeInstance } = $props();
|
||||||
|
|
||||||
@@ -21,7 +22,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const cornerTop = 10;
|
const cornerTop = 10;
|
||||||
const rightBump = $derived(!!node?.state?.type?.outputs?.length);
|
const nodeType = $derived(graph.getNodeType(node));
|
||||||
|
const rightBump = $derived(!!nodeType?.outputs?.length);
|
||||||
const aspectRatio = 0.25;
|
const aspectRatio = 0.25;
|
||||||
|
|
||||||
const path = $derived(
|
const path = $derived(
|
||||||
|
|||||||
@@ -23,7 +23,11 @@ export function createMockNodeRegistry(nodes: NodeDefinition[]): NodeRegistry {
|
|||||||
|
|
||||||
export const mockFloatOutputNode: NodeDefinition = {
|
export const mockFloatOutputNode: NodeDefinition = {
|
||||||
id: 'test/node/output',
|
id: 'test/node/output',
|
||||||
inputs: {},
|
inputs: {
|
||||||
|
'input': {
|
||||||
|
type: 'float'
|
||||||
|
}
|
||||||
|
},
|
||||||
outputs: ['float'],
|
outputs: ['float'],
|
||||||
meta: { title: 'Float Output' },
|
meta: { title: 'Float Output' },
|
||||||
execute: () => new Int32Array()
|
execute: () => new Int32Array()
|
||||||
@@ -32,7 +36,7 @@ export const mockFloatOutputNode: NodeDefinition = {
|
|||||||
export const mockFloatInputNode: NodeDefinition = {
|
export const mockFloatInputNode: NodeDefinition = {
|
||||||
id: 'test/node/input',
|
id: 'test/node/input',
|
||||||
inputs: { value: { type: 'float' } },
|
inputs: { value: { type: 'float' } },
|
||||||
outputs: [],
|
outputs: ['float'],
|
||||||
meta: { title: 'Float Input' },
|
meta: { title: 'Float Input' },
|
||||||
execute: () => new Int32Array()
|
execute: () => new Int32Array()
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -365,7 +365,7 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor {
|
|||||||
if (inputNode) {
|
if (inputNode) {
|
||||||
if (results[inputNode.id] === undefined) {
|
if (results[inputNode.id] === undefined) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Node ${node.type} is missing input from node ${inputNode.type}`
|
`Node ${node.type} is missing input from node ${inputNode.type}#${inputNode.id}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return results[inputNode.id];
|
return results[inputNode.id];
|
||||||
|
|||||||
@@ -24,3 +24,9 @@
|
|||||||
{:else}
|
{:else}
|
||||||
<p class="mx-4 mt-4">No node selected</p>
|
<p class="mx-4 mt-4">No node selected</p>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
{#if manager?.graph.groups.length}
|
||||||
|
<button onclick={() => manager.removeUnusedGroups()}>
|
||||||
|
remove unused groups
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
|||||||
@@ -81,7 +81,10 @@ export const GroupSchema = z.object({
|
|||||||
nodes: z.array(NodeSchema),
|
nodes: z.array(NodeSchema),
|
||||||
edges: z.array(z.tuple([z.number(), z.number(), z.number(), z.string()])),
|
edges: z.array(z.tuple([z.number(), z.number(), z.number(), z.string()])),
|
||||||
inputs: z.record(z.string(), NodeInputSchema).optional(),
|
inputs: z.record(z.string(), NodeInputSchema).optional(),
|
||||||
outputs: z.array(z.string()).optional()
|
outputs: z.array(z.object({
|
||||||
|
type: z.string(),
|
||||||
|
label: z.string().optional()
|
||||||
|
})).optional()
|
||||||
});
|
});
|
||||||
|
|
||||||
export type GroupDefinition = z.infer<typeof GroupSchema>;
|
export type GroupDefinition = z.infer<typeof GroupSchema>;
|
||||||
|
|||||||
@@ -6,6 +6,13 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
let { options = [], value = $bindable(0), id = '' }: Props = $props();
|
let { options = [], value = $bindable(0), id = '' }: Props = $props();
|
||||||
|
|
||||||
|
$effect(() => {
|
||||||
|
console.log({ options, value });
|
||||||
|
if (typeof value !== typeof options[0]) {
|
||||||
|
console.trace('WARNING: value type does not match options type');
|
||||||
|
}
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<select {id} bind:value class="bg-layer-2 text-text">
|
<select {id} bind:value class="bg-layer-2 text-text">
|
||||||
|
|||||||
Reference in New Issue
Block a user