Split a struct output into one pin per field (#256)
Reading one field of a struct needed its own Expand node, even where the graph already knows the struct type. A split output shows the fields directly on the node, so a Find … Match test becomes a single Equals and the wires keep running left to right. - Right-clicking an output pin offers "Split into <Type> fields" when the type is known and "Split as …" for every other struct asset. The output is replaced by one pin per field with the field's kind. - The type is known for typed struct variables, typed Pack/Expand, fields of another split and item of For each, Find, Match and Item at over a typed list (portSplits.ts). - Right-clicking a field pin offers "Recombine". Splitting drops the edges of the whole output, recombining drops the field edges. One undo step brings them back. - The split is stored as GraphNode.splits and saved with the graph. Copy and paste carry it along. - Field pins follow the struct asset like a typed Expand. A new field gets a pin, a removed field loses pin and edge, a renamed field keeps its edge. Deleting the asset keeps the last fields. The delete dialog counts splits, so it now says "node(s)" instead of "Pack/Expand node(s)". - Renaming a variable carries its split and the field edges along. - At run time the node still produces the whole value. A field pin reads its field like Expand, a missing field reads as empty. - Limits: outputs only, one level, behavior graphs only. Tests: - ./check.sh: 185 files, 2706 tests, CHECK EXIT 0 - e2e GM_STEPS=1-6,54,55: ALL PASSED, 303 checks - New port-splits.test.ts with 12 tests. e2e 6z-vi with 3 checks using a real mouse: split Find.item, wire item.Name into the test and Match.item.Amount into the log (logs 7), recombine. - Counter-checks: without field reads in the interpreter, without rename in the split, without moving edges on rename and without cleanup on field removal one unit test fails each. In the browser, without field reads nothing is logged and without type inference the split entry is missing. - Check total in README: 1520. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2aaca01929
commit
38e0f656be
17 files changed
+705
-22
No files matched your search
@@ -10,6 +10,7 @@ import {
|
||||
type Graph,
|
||||
type GraphEdge,
|
||||
type GraphNode,
|
||||
type PortSplit,
|
||||
isValidPortName,
|
||||
fixedPortKeys,
|
||||
type NodeDef,
|
||||
@@ -456,6 +457,19 @@ function normalizePorts(raw: unknown, fixed: readonly string[], writable: boolea
|
||||
return ports;
|
||||
}
|
||||
|
||||
/** One split per output; its fields are checked like declared ports, the type is kept even when gone. */
|
||||
function normalizeSplits(raw: unknown): PortSplit[] {
|
||||
const splits: PortSplit[] = [];
|
||||
for (const item of Array.isArray(raw) ? raw : []) {
|
||||
const s = asRecord(item);
|
||||
const port = typeof s.port === 'string' ? s.port : '';
|
||||
if (!port || splits.some((other) => other.port === port)) continue;
|
||||
const structType = typeof s.structType === 'string' ? s.structType : '';
|
||||
splits.push({ port, structType, fields: normalizePorts(s.fields, [], false) });
|
||||
}
|
||||
return splits;
|
||||
}
|
||||
|
||||
/** `keep` restricts the node types kept: a state machine's graph holds states, a shader's shader nodes, and nothing else. */
|
||||
function normalizeGraph(raw: unknown, keep?: (def: NodeDef) => boolean): Graph {
|
||||
const g = asRecord(raw);
|
||||
@@ -477,6 +491,8 @@ function normalizeGraph(raw: unknown, keep?: (def: NodeDef) => boolean): Graph {
|
||||
const node: GraphNode = { id, type, x: toNumber(n.x), y: toNumber(n.y), params };
|
||||
const ports = normalizePorts(n.ports, fixedPortKeys(def), def.declaresPorts === 'both');
|
||||
if (ports.length > 0) node.ports = ports;
|
||||
const splits = normalizeSplits(n.splits);
|
||||
if (splits.length > 0) node.splits = splits;
|
||||
if (n.collapsed === true) node.collapsed = true;
|
||||
graph.nodes.push(node);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user