Replace the five variable nodes with one Variables node
A single node now lists any number of variables. Every entry is an output that reads the current value whenever it is pulled; an entry switched to Set is also an input and is written when the flow arrives. Exec pins only exist while at least one entry writes, and all inputs are read before any write, so one node can swap two variables. Get/Set variable, Get/Set variables and Variable are gone. Old project files are converted on load: each old node becomes a Variables node with the same variables, edges and typed values. A written port without a typed value now shows the zero of its kind on the node body, matching what the inspector shows and what the run writes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
854ae2e756
commit
7c63c884ef
40 files changed
+895
-590
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import { VARIABLES } from '../graph/nested';
|
||||
import { portKindOf } from '../graph/variablePorts';
|
||||
import type { PropertyDef } from '../properties/PropertySchema';
|
||||
|
||||
/** The nodes `Variables` replaced, and whether they wrote. */
|
||||
const SINGLE: Record<string, boolean> = { GetVariable: false, SetVariable: true, Variable: true };
|
||||
const PLURAL: Record<string, boolean> = { GetVariables: false, SetVariables: true };
|
||||
|
||||
type Raw = Record<string, unknown>;
|
||||
const record = (value: unknown): Raw =>
|
||||
value && typeof value === 'object' && !Array.isArray(value) ? (value as Raw) : {};
|
||||
|
||||
/**
|
||||
* Rewrites the variable nodes of an older file into `Variables`, on the raw graph before it is
|
||||
* normalized. A single node's `value` port is renamed to the variable, which is the key there; one
|
||||
* without a variable has no pins left and loses its edges, flow included.
|
||||
*/
|
||||
export function upgradeVariableNodes(raw: unknown, variables: readonly PropertyDef[]): unknown {
|
||||
const graph = record(raw);
|
||||
if (!Array.isArray(graph.nodes)) return raw;
|
||||
const renamed = new Map<string, string>();
|
||||
const nodes = (graph.nodes as unknown[]).map((item) => {
|
||||
const node = record(item);
|
||||
const type = String(node.type);
|
||||
if (type in PLURAL) {
|
||||
const write = PLURAL[type];
|
||||
const ports = Array.isArray(node.ports)
|
||||
? (node.ports as unknown[]).map((p) => (write ? { ...record(p), write } : p))
|
||||
: [];
|
||||
return { ...node, type: VARIABLES, ports };
|
||||
}
|
||||
if (!(type in SINGLE)) return item;
|
||||
const write = SINGLE[type];
|
||||
const { variable, value, ...params } = record(node.params);
|
||||
const key = typeof variable === 'string' ? variable : '';
|
||||
renamed.set(String(node.id), key);
|
||||
if (!key) return { ...node, type: VARIABLES, params, ports: [] };
|
||||
const declared = variables.find((v) => v.key === key);
|
||||
const kind = declared ? portKindOf(declared) : 'text';
|
||||
if (write && value !== undefined) params[key] = value;
|
||||
return { ...node, type: VARIABLES, params, ports: [write ? { key, kind, write } : { key, kind }] };
|
||||
});
|
||||
const edges = (Array.isArray(graph.edges) ? graph.edges : []).flatMap((item) => {
|
||||
const edge = record(item);
|
||||
const end = (side: unknown) => {
|
||||
const at = record(side);
|
||||
const key = renamed.get(String(at.node));
|
||||
if (key === '') return null;
|
||||
return key !== undefined && at.port === 'value' ? { ...at, port: key } : at;
|
||||
};
|
||||
const from = end(edge.from);
|
||||
const to = end(edge.to);
|
||||
return from && to ? [{ ...edge, from, to }] : [];
|
||||
});
|
||||
return { ...graph, nodes, edges };
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import { PROJECT_FORMAT_VERSION, type Project } from '../model/Project';
|
||||
import type { SceneObject } from '../model/SceneObject';
|
||||
import { isTextureFile, normalizeFrameRect } from '../model/texture';
|
||||
import { hasParentCycle } from '../model/walk';
|
||||
import { upgradeVariableNodes } from './legacyVariableNodes';
|
||||
import {
|
||||
coerceValue,
|
||||
COMMON_PROPS,
|
||||
@@ -79,7 +80,7 @@ export function deserializeProject(raw: unknown, fallbackId = ''): Project {
|
||||
name,
|
||||
props,
|
||||
variables: variables.get(id) ?? [],
|
||||
graph: normalizeGraph(a.graph),
|
||||
graph: normalizeGraph(upgradeVariableNodes(a.graph, variables.get(id) ?? [])),
|
||||
});
|
||||
} else if (type === 'texture') {
|
||||
assets.push({ id, type, name, props, ...normalizeTexture(a, takenIds) });
|
||||
@@ -211,7 +212,7 @@ function readField(key: string, f: Record<string, unknown>): StructFieldDef | nu
|
||||
return isFieldKind(f.kind) ? { key, kind: f.kind as FieldKind } : null;
|
||||
}
|
||||
|
||||
function normalizePorts(raw: unknown, fixed: readonly string[]): DeclaredPort[] {
|
||||
function normalizePorts(raw: unknown, fixed: readonly string[], writable: boolean): DeclaredPort[] {
|
||||
const ports: DeclaredPort[] = [];
|
||||
const seen = new Set(fixed);
|
||||
for (const item of Array.isArray(raw) ? raw : []) {
|
||||
@@ -222,7 +223,7 @@ function normalizePorts(raw: unknown, fixed: readonly string[]): DeclaredPort[]
|
||||
const kind = (DECLARED_PORT_KINDS as readonly string[]).includes(String(p.kind))
|
||||
? (p.kind as DeclaredPortKind)
|
||||
: 'number';
|
||||
ports.push({ key, kind });
|
||||
ports.push(writable && p.write === true ? { key, kind, write: true } : { key, kind });
|
||||
}
|
||||
return ports;
|
||||
}
|
||||
@@ -245,7 +246,7 @@ function normalizeGraph(raw: unknown): Graph {
|
||||
if (stored !== undefined) params[key] = stored;
|
||||
}
|
||||
const node: GraphNode = { id, type, x: toNumber(n.x), y: toNumber(n.y), params };
|
||||
const ports = normalizePorts(n.ports, fixedPortKeys(def));
|
||||
const ports = normalizePorts(n.ports, fixedPortKeys(def), def.declaresPorts === 'both');
|
||||
if (ports.length > 0) node.ports = ports;
|
||||
if (n.collapsed === true) node.collapsed = true;
|
||||
graph.nodes.push(node);
|
||||
|
||||
Reference in new issue
Block a user