Skip to content

Commit

Permalink
node.state moment
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendonovich committed Aug 10, 2023
1 parent db9a843 commit c7f669f
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 104 deletions.
6 changes: 3 additions & 3 deletions app/src/components/Graph/CommentBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ export default (props: Props) => {
);

nodes.forEach((node) => {
node.position = {
x: node.position.x + e.movementX / scale,
y: node.position.y + e.movementY / scale,
node.state.position = {
x: node.state.position.x + e.movementX / scale,
y: node.state.position.y + e.movementY / scale,
};
});
};
Expand Down
2 changes: 1 addition & 1 deletion app/src/components/Graph/Connection/ConnectionRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const ConnectionRender = () => {
<g>
<For each={[...graph().nodes.values()]}>
{(n) => (
<For each={n.inputs}>
<For each={n.state.inputs}>
{(i) => {
const connectionData = () => {
const connections =
Expand Down
4 changes: 2 additions & 2 deletions app/src/components/Graph/IO/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ export const usePin = (pin: Pin) => {
UI.state.translate.y;
UI.state.scale;

pin.node.position.x;
pin.node.position.y;
pin.node.state.position.x;
pin.node.state.position.y;

let rect = getRef().getBoundingClientRect();

Expand Down
20 changes: 11 additions & 9 deletions app/src/components/Graph/Node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export const Node = (props: Props) => {
const scale = UI.state.scale;

node().setPosition({
x: node().position.x + e.movementX / scale,
y: node().position.y + e.movementY / scale,
x: node().state.position.x + e.movementX / scale,
y: node().state.position.y + e.movementY / scale,
});
};

Expand Down Expand Up @@ -103,8 +103,8 @@ export const Node = (props: Props) => {
UI.state.selectedItem === node() && "ring-2 ring-yellow-500"
)}
style={{
transform: `translate(${node().position.x}px, ${
node().position.y
transform: `translate(${node().state.position.x}px, ${
node().state.position.y
}px)`,
}}
>
Expand Down Expand Up @@ -160,13 +160,15 @@ export const Node = (props: Props) => {
break;
}
}}
onMouseUp={() => node().setPosition(node().position, true)}
onMouseUp={() =>
node().setPosition(node().state.position, true)
}
onContextMenu={(e) => {
e.preventDefault();
e.stopPropagation();
}}
>
<div>{node().name}</div>
<div>{node().state.name}</div>
</div>
}
>
Expand All @@ -186,7 +188,7 @@ export const Node = (props: Props) => {
value={value()}
onInput={(e) => setValue(e.target.value)}
onBlur={() => {
if (value() !== "") node().name = value();
if (value() !== "") node().state.name = value();
node().graph.project.save();

setEditingName(false);
Expand All @@ -201,7 +203,7 @@ export const Node = (props: Props) => {
</div>
<div class="flex flex-row gap-2">
<div class="p-2 flex flex-col space-y-2.5">
<For each={node().inputs}>
<For each={node().state.inputs}>
{(i) => (
<Switch>
<Match when={i instanceof DataInputModel ? i : null}>
Expand All @@ -218,7 +220,7 @@ export const Node = (props: Props) => {
</For>
</div>
<div class="p-2 ml-auto flex flex-col space-y-2.5 items-end">
<For each={node().outputs}>
<For each={node().state.outputs}>
{(o) => (
<Switch>
<Match when={o instanceof DataOutputModel ? o : null}>
Expand Down
27 changes: 20 additions & 7 deletions app/src/components/Graph/SchemaMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import clsx from "clsx";
import { createMemo, createSignal, For, onMount, Show } from "solid-js";

import { useCore } from "~/contexts";
import { XY, Package, NodeSchema, NodeSchemaVariant } from "@macrograph/core";
import {
XY,
Package,
NodeSchema,
NodeSchemaVariant,
EventsMap,
} from "@macrograph/core";
import { useGraph } from "./Graph";
import { useUIStore } from "~/UIStore";

Expand Down Expand Up @@ -91,13 +97,20 @@ export const SchemaMenu = (props: Props) => {
(s) => !lowercasePackageName.startsWith(s)
);

return p.schemas.filter((s) => {
let lowercaseSchemaName = s.name.toLowerCase();
const ret: NodeSchema<EventsMap>[] = [];

return leftoverSearchTokens.every((t) =>
lowercaseSchemaName.includes(t)
);
});
for (const schema of p.schemas) {
let lowercaseSchemaName = schema.name.toLowerCase();

if (
leftoverSearchTokens.every((t) =>
lowercaseSchemaName.includes(t)
)
)
ret.push(schema);
}

return ret;
});

return (
Expand Down
9 changes: 5 additions & 4 deletions core/src/models/CommentBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ export class CommentBox {
const ret: Node[] = [];

for (const node of nodes) {
if (node.position.x < position.x || node.position.y < position.y)
continue;
const nodePosition = node.state.position;

if (nodePosition.x < position.x || nodePosition.y < position.y) continue;

const nodeSize = getNodeSize(node);
if (!nodeSize) continue;

if (
node.position.x + nodeSize.width > position.x + size.x ||
node.position.y + nodeSize.height > position.y + size.y
nodePosition.x + nodeSize.width > position.x + size.x ||
nodePosition.y + nodeSize.height > position.y + size.y
)
continue;

Expand Down
52 changes: 33 additions & 19 deletions core/src/models/Core.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createMutable } from "solid-js/store";
import { Package, PackageArgs } from "./Package";
import { Node } from "./Node";
import { DataInput, DataOutput, ExecOutput, ScopeOutput } from "./IO";
import { DataInput, DataOutput, ScopeOutput } from "./IO";
import { EventsMap, RunCtx } from "./NodeSchema";
import { z } from "zod";
import { Project, SerializedProject } from "./Project";
Expand All @@ -10,19 +10,19 @@ import { Option } from "../types";
class NodeEmit {
listeners = new Map<Node, Set<(d: Node) => any>>();

emit(data: Node) {
this.listeners.get(data)?.forEach((l) => l(data));
emit(node: Node) {
this.listeners.get(node)?.forEach((l) => l(node));
}

subscribe(SubType: Node, cb: (d: Node) => any) {
let listeners = this.listeners.get(SubType);
if (!listeners) this.listeners.set(SubType, new Set());
listeners = this.listeners.get(SubType);
subscribe(node: Node, cb: (d: Node) => any) {
if (!this.listeners.has(node)) this.listeners.set(node, new Set());
const listeners = this.listeners.get(node)!;

listeners?.add(cb);

return () => {
listeners?.delete(cb);
if (listeners?.size === 0) this.listeners.delete(SubType);
if (listeners?.size === 0) this.listeners.delete(node);
};
}
}
Expand Down Expand Up @@ -77,12 +77,26 @@ export class Core {
const mappings = this.eventNodeMappings;

if (!mappings.has(pkg)) mappings.set(pkg, new Map());

const pkgMappings = mappings.get(pkg)!;

if (!pkgMappings.has(event)) pkgMappings.set(event, new Set());
pkgMappings.get(event)!.add(node);
}
}

pkgMappings.get(event)?.add(node);
removeEventNodeMapping(node: Node) {
if ("event" in node.schema) {
const event = node.schema.event;
const pkg = node.schema.package;
const mappings = this.eventNodeMappings;

const pkgMappings = mappings.get(pkg);
if (!pkgMappings) return;

const eventMappings = pkgMappings.get(event);
if (!eventMappings) return;

eventMappings.delete(node);
}
}
}
Expand All @@ -93,25 +107,23 @@ class ExecutionContext {
constructor(public root: Node) {}

run(data: any) {
NODE_EMIT.emit(this.root);

this.root.schema.run({
ctx: this.createCtx(this.root),
ctx: this.createCtx(),
io: this.root.ioReturn,
data,
});
}

createCtx(node: Node): RunCtx {
createCtx(): RunCtx {
return {
exec: async (execOutput) => {
NODE_EMIT.emit(node);

await execOutput.connection.peekAsync((conn) =>
this.execNode(conn.node)
);
},
execScope: async (scopeOutput, data) => {
NODE_EMIT.emit(node);

await scopeOutput.connection.peekAsync(async (conn) => {
this.data.set(scopeOutput, data);

Expand Down Expand Up @@ -144,8 +156,10 @@ class ExecutionContext {
async execNode(node: Node) {
if ("event" in node.schema) throw new Error("Cannot exec an Event node!");

NODE_EMIT.emit(node);

// calculate previous outputs
node.inputs.forEach((i) => {
node.state.inputs.forEach((i) => {
if (!(i instanceof DataInput)) return;

i.connection.peek((conn) => {
Expand All @@ -163,14 +177,14 @@ class ExecutionContext {

if (value === undefined)
throw new Error(
`Data for Pin ${conn.name}, Node ${conn.node.name} not found!`
`Data for Pin ${conn.name}, Node ${conn.node.state.name} not found!`
);
}
});
});

await node.schema.run({
ctx: this.createCtx(node),
ctx: this.createCtx(),
io: node.ioReturn,
});
}
Expand Down
7 changes: 2 additions & 5 deletions core/src/models/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
disconnectWildcardsInIO,
None,
Some,
Option,
} from "../types";

export interface GraphArgs {
Expand Down Expand Up @@ -182,8 +181,8 @@ export class Graph {

deleteItem(item: Node | CommentBox) {
if (item instanceof Node) {
item.inputs.forEach((i) => this.disconnectPin(i));
item.outputs.forEach((o) => this.disconnectPin(o));
item.state.inputs.forEach((i) => this.disconnectPin(i));
item.state.outputs.forEach((o) => this.disconnectPin(o));

this.nodes.delete(item.id);
item.dispose();
Expand Down Expand Up @@ -236,8 +235,6 @@ export class Graph {

if (node === null) return null;

project.core.addEventNodeMapping(node);

return [id, node] as [number, Node];
})
.filter(Boolean) as [number, Node][]
Expand Down
Loading

0 comments on commit c7f669f

Please sign in to comment.