@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import type { Edge, Node } from "@nodarium/types";
|
||||
import type { Edge, NodeInstance } from "@nodarium/types";
|
||||
import { onMount } from "svelte";
|
||||
import { createKeyMap } from "../../helpers/createKeyMap";
|
||||
import AddMenu from "../components/AddMenu.svelte";
|
||||
@@ -40,7 +40,7 @@
|
||||
return [pos1[0], pos1[1], pos2[0], pos2[1]];
|
||||
}
|
||||
|
||||
function handleNodeCreation(node: Node) {
|
||||
function handleNodeCreation(node: NodeInstance) {
|
||||
const newNode = graph.createNode({
|
||||
type: node.type,
|
||||
position: node.position,
|
||||
@@ -51,11 +51,11 @@
|
||||
if (graphState.activeSocket) {
|
||||
if (typeof graphState.activeSocket.index === "number") {
|
||||
const socketType =
|
||||
graphState.activeSocket.node.tmp?.type?.outputs?.[
|
||||
graphState.activeSocket.node.state?.type?.outputs?.[
|
||||
graphState.activeSocket.index
|
||||
];
|
||||
|
||||
const input = Object.entries(newNode?.tmp?.type?.inputs || {}).find(
|
||||
const input = Object.entries(newNode?.state?.type?.inputs || {}).find(
|
||||
(inp) => inp[1].type === socketType,
|
||||
);
|
||||
|
||||
@@ -69,11 +69,11 @@
|
||||
}
|
||||
} else {
|
||||
const socketType =
|
||||
graphState.activeSocket.node.tmp?.type?.inputs?.[
|
||||
graphState.activeSocket.node.state?.type?.inputs?.[
|
||||
graphState.activeSocket.index
|
||||
];
|
||||
|
||||
const output = newNode.tmp?.type?.outputs?.find((out) => {
|
||||
const output = newNode.state?.type?.outputs?.find((out) => {
|
||||
if (socketType?.type === out) return true;
|
||||
if (socketType?.accepts?.includes(out as any)) return true;
|
||||
return false;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import type { Graph, Node, NodeRegistry } from "@nodarium/types";
|
||||
import type { Graph, NodeInstance, NodeRegistry } from "@nodarium/types";
|
||||
import GraphEl from "./Graph.svelte";
|
||||
import { GraphManager } from "../graph-manager.svelte";
|
||||
import { createKeyMap } from "$lib/helpers/createKeyMap";
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
settings?: Record<string, any>;
|
||||
|
||||
activeNode?: Node;
|
||||
activeNode?: NodeInstance;
|
||||
showGrid?: boolean;
|
||||
snapToGrid?: boolean;
|
||||
showHelp?: boolean;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { GraphSchema, type NodeType, type Node } from "@nodarium/types";
|
||||
import { GraphSchema, type NodeId, type NodeInstance } from "@nodarium/types";
|
||||
import type { GraphManager } from "../graph-manager.svelte";
|
||||
import type { GraphState } from "./state.svelte";
|
||||
import { animate, lerp } from "$lib/helpers";
|
||||
@@ -17,7 +17,7 @@ export class FileDropEventManager {
|
||||
event.preventDefault();
|
||||
this.state.isDragging = false;
|
||||
if (!event.dataTransfer) return;
|
||||
const nodeId = event.dataTransfer.getData("data/node-id") as NodeType;
|
||||
const nodeId = event.dataTransfer.getData("data/node-id") as NodeId;
|
||||
let mx = event.clientX - this.state.rect.x;
|
||||
let my = event.clientY - this.state.rect.y;
|
||||
|
||||
@@ -131,45 +131,45 @@ export class MouseEventManager {
|
||||
|
||||
if (clickedNodeId !== -1) {
|
||||
if (activeNode) {
|
||||
if (!activeNode?.tmp?.isMoving && !event.ctrlKey && !event.shiftKey) {
|
||||
if (!activeNode?.state?.isMoving && !event.ctrlKey && !event.shiftKey) {
|
||||
this.state.activeNodeId = clickedNodeId;
|
||||
this.state.clearSelection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (activeNode?.tmp?.isMoving) {
|
||||
activeNode.tmp = activeNode.tmp || {};
|
||||
activeNode.tmp.isMoving = false;
|
||||
if (activeNode?.state?.isMoving) {
|
||||
activeNode.state = activeNode.state || {};
|
||||
activeNode.state.isMoving = false;
|
||||
if (this.state.snapToGrid) {
|
||||
const snapLevel = this.state.getSnapLevel();
|
||||
activeNode.position[0] = snapPointToGrid(
|
||||
activeNode?.tmp?.x ?? activeNode.position[0],
|
||||
activeNode?.state?.x ?? activeNode.position[0],
|
||||
5 / snapLevel,
|
||||
);
|
||||
activeNode.position[1] = snapPointToGrid(
|
||||
activeNode?.tmp?.y ?? activeNode.position[1],
|
||||
activeNode?.state?.y ?? activeNode.position[1],
|
||||
5 / snapLevel,
|
||||
);
|
||||
} else {
|
||||
activeNode.position[0] = activeNode?.tmp?.x ?? activeNode.position[0];
|
||||
activeNode.position[1] = activeNode?.tmp?.y ?? activeNode.position[1];
|
||||
activeNode.position[0] = activeNode?.state?.x ?? activeNode.position[0];
|
||||
activeNode.position[1] = activeNode?.state?.y ?? activeNode.position[1];
|
||||
}
|
||||
const nodes = [
|
||||
...[...(this.state.selectedNodes?.values() || [])].map((id) =>
|
||||
this.graph.getNode(id),
|
||||
),
|
||||
] as Node[];
|
||||
] as NodeInstance[];
|
||||
|
||||
const vec = [
|
||||
activeNode.position[0] - (activeNode?.tmp.x || 0),
|
||||
activeNode.position[1] - (activeNode?.tmp.y || 0),
|
||||
activeNode.position[0] - (activeNode?.state.x || 0),
|
||||
activeNode.position[1] - (activeNode?.state.y || 0),
|
||||
];
|
||||
|
||||
for (const node of nodes) {
|
||||
if (!node) continue;
|
||||
node.tmp = node.tmp || {};
|
||||
const { x, y } = node.tmp;
|
||||
node.state = node.state || {};
|
||||
const { x, y } = node.state;
|
||||
if (x !== undefined && y !== undefined) {
|
||||
node.position[0] = x + vec[0];
|
||||
node.position[1] = y + vec[1];
|
||||
@@ -179,14 +179,14 @@ export class MouseEventManager {
|
||||
animate(500, (a: number) => {
|
||||
for (const node of nodes) {
|
||||
if (
|
||||
node?.tmp &&
|
||||
node.tmp["x"] !== undefined &&
|
||||
node.tmp["y"] !== undefined
|
||||
node?.state &&
|
||||
node.state["x"] !== undefined &&
|
||||
node.state["y"] !== undefined
|
||||
) {
|
||||
node.tmp.x = lerp(node.tmp.x, node.position[0], a);
|
||||
node.tmp.y = lerp(node.tmp.y, node.position[1], a);
|
||||
node.state.x = lerp(node.state.x, node.position[0], a);
|
||||
node.state.y = lerp(node.state.y, node.position[1], a);
|
||||
this.state.updateNodePosition(node);
|
||||
if (node?.tmp?.isMoving) {
|
||||
if (node?.state?.isMoving) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -318,17 +318,17 @@ export class MouseEventManager {
|
||||
|
||||
const node = this.graph.getNode(this.state.activeNodeId);
|
||||
if (!node) return;
|
||||
node.tmp = node.tmp || {};
|
||||
node.tmp.downX = node.position[0];
|
||||
node.tmp.downY = node.position[1];
|
||||
node.state = node.state || {};
|
||||
node.state.downX = node.position[0];
|
||||
node.state.downY = node.position[1];
|
||||
|
||||
if (this.state.selectedNodes) {
|
||||
for (const nodeId of this.state.selectedNodes) {
|
||||
const n = this.graph.getNode(nodeId);
|
||||
if (!n) continue;
|
||||
n.tmp = n.tmp || {};
|
||||
n.tmp.downX = n.position[0];
|
||||
n.tmp.downY = n.position[1];
|
||||
n.state = n.state || {};
|
||||
n.state.downX = n.position[0];
|
||||
n.state.downY = n.position[1];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,7 +382,7 @@ export class MouseEventManager {
|
||||
const y1 = Math.min(mouseD[1], this.state.mousePosition[1]);
|
||||
const y2 = Math.max(mouseD[1], this.state.mousePosition[1]);
|
||||
for (const node of this.graph.nodes.values()) {
|
||||
if (!node?.tmp) continue;
|
||||
if (!node?.state) continue;
|
||||
const x = node.position[0];
|
||||
const y = node.position[1];
|
||||
const height = this.state.getNodeHeight(node.type);
|
||||
@@ -400,10 +400,10 @@ export class MouseEventManager {
|
||||
const node = this.graph.getNode(this.state.activeNodeId);
|
||||
if (!node || event.buttons !== 1) return;
|
||||
|
||||
node.tmp = node.tmp || {};
|
||||
node.state = node.state || {};
|
||||
|
||||
const oldX = node.tmp.downX || 0;
|
||||
const oldY = node.tmp.downY || 0;
|
||||
const oldX = node.state.downX || 0;
|
||||
const oldY = node.state.downY || 0;
|
||||
|
||||
let newX =
|
||||
oldX + (mx - this.state.mouseDown[0]) / this.state.cameraPosition[2];
|
||||
@@ -418,10 +418,10 @@ export class MouseEventManager {
|
||||
}
|
||||
}
|
||||
|
||||
if (!node.tmp.isMoving) {
|
||||
if (!node.state.isMoving) {
|
||||
const dist = Math.sqrt((oldX - newX) ** 2 + (oldY - newY) ** 2);
|
||||
if (dist > 0.2) {
|
||||
node.tmp.isMoving = true;
|
||||
node.state.isMoving = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -431,15 +431,15 @@ export class MouseEventManager {
|
||||
if (this.state.selectedNodes?.size) {
|
||||
for (const nodeId of this.state.selectedNodes) {
|
||||
const n = this.graph.getNode(nodeId);
|
||||
if (!n?.tmp) continue;
|
||||
n.tmp.x = (n?.tmp?.downX || 0) - vecX;
|
||||
n.tmp.y = (n?.tmp?.downY || 0) - vecY;
|
||||
if (!n?.state) continue;
|
||||
n.state.x = (n?.state?.downX || 0) - vecX;
|
||||
n.state.y = (n?.state?.downY || 0) - vecY;
|
||||
this.state.updateNodePosition(n);
|
||||
}
|
||||
}
|
||||
|
||||
node.tmp.x = newX;
|
||||
node.tmp.y = newY;
|
||||
node.state.x = newX;
|
||||
node.state.y = newY;
|
||||
|
||||
this.state.updateNodePosition(node);
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Node, Socket } from "@nodarium/types";
|
||||
import type { NodeInstance, Socket } from "@nodarium/types";
|
||||
import { getContext, setContext } from "svelte";
|
||||
import { SvelteSet } from "svelte/reactivity";
|
||||
import type { GraphManager } from "../graph-manager.svelte";
|
||||
@@ -38,7 +38,7 @@ export class GraphState {
|
||||
cameraPosition: [number, number, number] = $state([0, 0, 4]);
|
||||
|
||||
clipboard: null | {
|
||||
nodes: Node[];
|
||||
nodes: NodeInstance[];
|
||||
edges: [number, number, number, string][];
|
||||
} = null;
|
||||
|
||||
@@ -95,26 +95,26 @@ export class GraphState {
|
||||
}
|
||||
|
||||
|
||||
updateNodePosition(node: Node) {
|
||||
if (node?.tmp?.ref && node?.tmp?.mesh) {
|
||||
if (node.tmp["x"] !== undefined && node.tmp["y"] !== undefined) {
|
||||
node.tmp.ref.style.setProperty("--nx", `${node.tmp.x * 10}px`);
|
||||
node.tmp.ref.style.setProperty("--ny", `${node.tmp.y * 10}px`);
|
||||
node.tmp.mesh.position.x = node.tmp.x + 10;
|
||||
node.tmp.mesh.position.z = node.tmp.y + this.getNodeHeight(node.type) / 2;
|
||||
updateNodePosition(node: NodeInstance) {
|
||||
if (node.state.ref && node.state.mesh) {
|
||||
if (node.state["x"] !== undefined && node.state["y"] !== undefined) {
|
||||
node.state.ref.style.setProperty("--nx", `${node.state.x * 10}px`);
|
||||
node.state.ref.style.setProperty("--ny", `${node.state.y * 10}px`);
|
||||
node.state.mesh.position.x = node.state.x + 10;
|
||||
node.state.mesh.position.z = node.state.y + this.getNodeHeight(node.type) / 2;
|
||||
if (
|
||||
node.tmp.x === node.position[0] &&
|
||||
node.tmp.y === node.position[1]
|
||||
node.state.x === node.position[0] &&
|
||||
node.state.y === node.position[1]
|
||||
) {
|
||||
delete node.tmp.x;
|
||||
delete node.tmp.y;
|
||||
delete node.state.x;
|
||||
delete node.state.y;
|
||||
}
|
||||
this.graph.edges = [...this.graph.edges];
|
||||
} else {
|
||||
node.tmp.ref.style.setProperty("--nx", `${node.position[0] * 10}px`);
|
||||
node.tmp.ref.style.setProperty("--ny", `${node.position[1] * 10}px`);
|
||||
node.tmp.mesh.position.x = node.position[0] + 10;
|
||||
node.tmp.mesh.position.z =
|
||||
node.state.ref.style.setProperty("--nx", `${node.position[0] * 10}px`);
|
||||
node.state.ref.style.setProperty("--ny", `${node.position[1] * 10}px`);
|
||||
node.state.mesh.position.x = node.position[0] + 10;
|
||||
node.state.mesh.position.z =
|
||||
node.position[1] + this.getNodeHeight(node.type) / 2;
|
||||
}
|
||||
}
|
||||
@@ -134,19 +134,19 @@ export class GraphState {
|
||||
}
|
||||
|
||||
getSocketPosition(
|
||||
node: Node,
|
||||
node: NodeInstance,
|
||||
index: string | number,
|
||||
): [number, number] {
|
||||
if (typeof index === "number") {
|
||||
return [
|
||||
(node?.tmp?.x ?? node.position[0]) + 20,
|
||||
(node?.tmp?.y ?? node.position[1]) + 2.5 + 10 * index,
|
||||
(node?.state?.x ?? node.position[0]) + 20,
|
||||
(node?.state?.y ?? node.position[1]) + 2.5 + 10 * index,
|
||||
];
|
||||
} else {
|
||||
const _index = Object.keys(node.tmp?.type?.inputs || {}).indexOf(index);
|
||||
const _index = Object.keys(node.state?.type?.inputs || {}).indexOf(index);
|
||||
return [
|
||||
node?.tmp?.x ?? node.position[0],
|
||||
(node?.tmp?.y ?? node.position[1]) + 10 + 10 * _index,
|
||||
node?.state?.x ?? node.position[0],
|
||||
(node?.state?.y ?? node.position[1]) + 10 + 10 * _index,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -174,32 +174,6 @@ export class GraphState {
|
||||
return height;
|
||||
}
|
||||
|
||||
setNodePosition(node: Node) {
|
||||
if (node?.tmp?.ref && node?.tmp?.mesh) {
|
||||
if (node.tmp["x"] !== undefined && node.tmp["y"] !== undefined) {
|
||||
node.tmp.ref.style.setProperty("--nx", `${node.tmp.x * 10}px`);
|
||||
node.tmp.ref.style.setProperty("--ny", `${node.tmp.y * 10}px`);
|
||||
node.tmp.mesh.position.x = node.tmp.x + 10;
|
||||
node.tmp.mesh.position.z = node.tmp.y + this.getNodeHeight(node.type) / 2;
|
||||
if (
|
||||
node.tmp.x === node.position[0] &&
|
||||
node.tmp.y === node.position[1]
|
||||
) {
|
||||
delete node.tmp.x;
|
||||
delete node.tmp.y;
|
||||
}
|
||||
this.graph.edges = [...this.graph.edges];
|
||||
} else {
|
||||
node.tmp.ref.style.setProperty("--nx", `${node.position[0] * 10}px`);
|
||||
node.tmp.ref.style.setProperty("--ny", `${node.position[1] * 10}px`);
|
||||
node.tmp.mesh.position.x = node.position[0] + 10;
|
||||
node.tmp.mesh.position.z =
|
||||
node.position[1] + this.getNodeHeight(node.type) / 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
copyNodes() {
|
||||
if (this.activeNodeId === -1 && !this.selectedNodes?.size)
|
||||
return;
|
||||
@@ -231,12 +205,11 @@ export class GraphState {
|
||||
|
||||
const nodes = this.clipboard.nodes
|
||||
.map((node) => {
|
||||
node.tmp = node.tmp || {};
|
||||
node.position[0] = this.mousePosition[0] - node.position[0];
|
||||
node.position[1] = this.mousePosition[1] - node.position[1];
|
||||
return node;
|
||||
})
|
||||
.filter(Boolean) as Node[];
|
||||
.filter(Boolean) as NodeInstance[];
|
||||
|
||||
const newNodes = this.graph.createGraph(nodes, this.clipboard.edges);
|
||||
this.selectedNodes.clear();
|
||||
@@ -327,7 +300,7 @@ export class GraphState {
|
||||
return clickedNodeId;
|
||||
}
|
||||
|
||||
isNodeInView(node: Node) {
|
||||
isNodeInView(node: NodeInstance) {
|
||||
const height = this.getNodeHeight(node.type);
|
||||
const width = 20;
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user