nodes/frontend/src/lib/graph-manager.ts

164 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-03-06 18:31:06 +01:00
import { writable, type Writable } from "svelte/store";
2024-03-11 19:37:58 +01:00
import type { Graph, NodeRegistry as INodeRegistry, NodeType, Node, Edge } from "./types";
2024-03-06 18:31:06 +01:00
const nodeTypes: NodeType[] = [
{
id: "input/float",
inputs: {
"value": { type: "float" },
},
outputs: ["float"],
},
{
id: "math",
inputs: {
"a": { type: "float" },
"b": { type: "float" },
},
outputs: ["float"],
},
]
export class NodeRegistry implements INodeRegistry {
getNode(id: string): NodeType | undefined {
return nodeTypes.find((nodeType) => nodeType.id === id);
}
}
export class GraphManager {
status: Writable<"loading" | "idle" | "error"> = writable("loading");
nodes: Node[] = [];
2024-03-11 19:37:58 +01:00
edges: Edge[] = [];
2024-03-06 18:31:06 +01:00
private constructor(private graph: Graph, private nodeRegistry: NodeRegistry = new NodeRegistry()) {
}
async load() {
const nodes = this.graph.nodes;
for (const node of nodes) {
const nodeType = this.getNodeType(node.type);
if (!nodeType) {
console.error(`Node type not found: ${node.type}`);
this.status.set("error");
return;
}
}
this.nodes = this.graph.nodes;
this.edges = this.graph.edges;
this.status.set("idle");
}
2024-03-11 19:37:58 +01:00
getNode(id: number) {
2024-03-06 18:31:06 +01:00
return this.nodes.find((node) => node.id === id);
}
2024-03-11 19:37:58 +01:00
getPossibleSockets(node: Node, socketIndex: number, isInput: boolean): [Node, number][] {
const nodeType = this.getNodeType(node.type);
if (!nodeType) return [];
const nodes = this.nodes.filter(n => n.id !== node.id);
const sockets: [Node, number][] = []
if (isInput) {
const ownType = Object.values(nodeType?.inputs || {})[socketIndex].type;
for (const node of nodes) {
const nodeType = this.getNodeType(node.type);
const inputs = nodeType?.outputs;
if (!inputs) continue;
for (let index = 0; index < inputs.length; index++) {
if (inputs[index] === ownType) {
sockets.push([node, index]);
}
}
}
} else {
const ownType = nodeType.outputs?.[socketIndex];
for (const node of nodes) {
const nodeType = this.getNodeType(node.type);
const inputs = nodeType?.inputs;
const entries = Object.values(inputs || {});
entries.map((input, index) => {
if (input.type === ownType) {
sockets.push([node, index]);
}
});
}
}
return sockets;
}
getNodeType(id: string): NodeType {
2024-03-06 18:31:06 +01:00
return this.nodeRegistry.getNode(id)!;
}
2024-03-11 19:37:58 +01:00
getEdges() {
2024-03-06 18:31:06 +01:00
return this.edges
.map((edge) => {
const from = this.nodes.find((node) => node.id === edge.from);
const to = this.nodes.find((node) => node.id === edge.to);
if (!from || !to) return;
2024-03-11 19:37:58 +01:00
return [from, edge.fromSocket, to, edge.toSocket] as const;
2024-03-06 18:31:06 +01:00
})
2024-03-11 19:37:58 +01:00
.filter(Boolean) as unknown as [Node, number, Node, number][];
2024-03-06 18:31:06 +01:00
}
static createEmptyGraph({ width = 20, height = 20 } = {}): GraphManager {
2024-03-06 18:31:06 +01:00
const graph: Graph = {
edges: [],
nodes: [],
};
const amount = width * height;
for (let i = 0; i < amount; i++) {
const x = i % width;
const y = Math.floor(i / height);
2024-03-06 18:31:06 +01:00
graph.nodes.push({
2024-03-11 19:37:58 +01:00
id: i,
2024-03-06 18:31:06 +01:00
tmp: {
visible: false,
},
position: {
x: x * 7.5,
y: y * 10,
2024-03-06 18:31:06 +01:00
},
props: i == 0 ? { value: 0 } : {},
type: i == 0 ? "input/float" : "math",
});
graph.edges.push({
2024-03-11 19:37:58 +01:00
from: i,
fromSocket: 0,
to: (i + 1),
toSocket: 0,
2024-03-06 18:31:06 +01:00
});
}
return new GraphManager(graph);
}
}