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
@@ -79,11 +79,12 @@ function flowEdgesOnceSilent(graph: Graph, node: GraphNode): EdgeTest[] {
|
||||
return [{ graph, test: (edge) => onFlow(edge.to) || onFlow(edge.from) }];
|
||||
}
|
||||
|
||||
/** An output's edges include those on its field ports, while it is split. */
|
||||
function edgesOnPort(site: PortSite, key: string): (edge: GraphEdge) => boolean {
|
||||
return (edge) =>
|
||||
site.asInput
|
||||
? edge.to.node === site.nodeId && edge.to.port === key
|
||||
: edge.from.node === site.nodeId && edge.from.port === key;
|
||||
: edge.from.node === site.nodeId && (edge.from.port === key || edge.from.port.startsWith(`${key}.`));
|
||||
}
|
||||
|
||||
function nodeAt(site: PortSite): GraphNode | undefined {
|
||||
@@ -94,8 +95,10 @@ function nodeAt(site: PortSite): GraphNode | undefined {
|
||||
function carryPort(site: PortSite, from: string, to: string): void {
|
||||
for (const edge of site.graph.edges.filter(edgesOnPort(site, from))) {
|
||||
if (site.asInput) edge.to.port = to;
|
||||
else edge.from.port = to;
|
||||
else edge.from.port = `${to}${edge.from.port.slice(from.length)}`;
|
||||
}
|
||||
const split = site.asInput ? undefined : nodeAt(site)?.splits?.find((s) => s.port === from);
|
||||
if (split) split.port = to;
|
||||
const node = site.asInput ? nodeAt(site) : undefined;
|
||||
const value = node?.params[from];
|
||||
if (!node || value === undefined) return;
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
* those writes are recorded as they happen (`Recorder`) and undone in one go with the edit itself.
|
||||
*/
|
||||
import { newId } from '../ids';
|
||||
import type { GraphNode } from '../graph/Graph';
|
||||
import { STRUCT_TYPE_PARAM } from '../graph/nested';
|
||||
import type { GraphEdge, GraphNode, PortSplit } from '../graph/Graph';
|
||||
import { resolveDef, STRUCT_TYPE_PARAM } from '../graph/nested';
|
||||
import { canSplit, splitOf } from '../graph/portSplits';
|
||||
import { graphLookup } from '../model/graphLookup';
|
||||
import type { StructAsset } from '../model/Asset';
|
||||
import { nextAssetName } from './naming';
|
||||
import {
|
||||
@@ -41,7 +43,7 @@ import { valueAt } from '../properties/valuePath';
|
||||
import { removeById } from '../util/list';
|
||||
import { Recorder } from '../util/recorder';
|
||||
import { type Command, CommandError } from './Command';
|
||||
import { requireNode, requireVariableOwner } from './require';
|
||||
import { requireGraph, requireNode, requireVariableOwner } from './require';
|
||||
|
||||
/**
|
||||
* Runs an edit of a Struct asset's fields and then carries it to every place of that type. A
|
||||
@@ -129,6 +131,58 @@ export class SetStructTypeCommand implements Command {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes an output apart into one output per field of a Struct asset, or with `''` puts it together
|
||||
* again. The edges on what goes away go with it, in the same undo step.
|
||||
*/
|
||||
export class SplitOutputCommand implements Command {
|
||||
readonly label: string;
|
||||
private rec = new Recorder();
|
||||
|
||||
constructor(
|
||||
private readonly assetId: string,
|
||||
private readonly nodeId: string,
|
||||
private readonly port: string,
|
||||
private readonly typeId: string,
|
||||
) {
|
||||
this.label = typeId ? `Split ${port}` : `Recombine ${port}`;
|
||||
}
|
||||
|
||||
execute(project: Project): void {
|
||||
const graph = requireGraph(project, this.assetId);
|
||||
const node = requireNode(project, this.assetId, this.nodeId);
|
||||
const { port } = this;
|
||||
this.rec = new Recorder();
|
||||
const leaving = (key: string) => (this.typeId ? key === port : key.startsWith(`${port}.`));
|
||||
if (this.typeId) {
|
||||
const type = structAssetOf(project, this.typeId);
|
||||
if (!type) throw new CommandError('That struct is gone');
|
||||
const output = resolveDef(node, graph, graphLookup(project))?.outputs.find((p) => p.key === port);
|
||||
if (!output || !canSplit(node, output)) throw new CommandError(`"${port}" cannot be split`);
|
||||
const split: PortSplit = { port, structType: type.id, fields: [] };
|
||||
this.rec.set(node, 'splits', [...(node.splits ?? []), split]);
|
||||
} else {
|
||||
if (!splitOf(node, port)) throw new CommandError(`"${port}" is not split`);
|
||||
const rest = (node.splits ?? []).filter((s) => s.port !== port);
|
||||
if (rest.length > 0) this.rec.set(node, 'splits', rest);
|
||||
else this.rec.delete(node, 'splits');
|
||||
}
|
||||
const gone = (e: GraphEdge) => e.from.node === node.id && leaving(e.from.port);
|
||||
if (graph.edges.some(gone))
|
||||
this.rec.set(
|
||||
graph,
|
||||
'edges',
|
||||
graph.edges.filter((e) => !gone(e)),
|
||||
);
|
||||
// The fields come in the way every later edit of the type brings them: through the sync.
|
||||
syncStructTypes(project, this.rec);
|
||||
}
|
||||
|
||||
undo(): void {
|
||||
this.rec.revert();
|
||||
}
|
||||
}
|
||||
|
||||
/** A struct field as a declared variable of a Struct asset, its default taken from `value`. */
|
||||
function variableOfField(field: StructFieldDef, value: PropertyValue | undefined): PropertyDef {
|
||||
const def = variableDef(
|
||||
|
||||
Reference in new issue
Block a user