Replace the property nodes with one Properties node
Get/Set property, Get/Set properties, Set enabled and Set text are gone. A single Properties node takes a fixed "object" input; every chosen property is an output that reads the current value, and one switched to Set is also an input written when the flow arrives. Exec pins only exist while at least one entry writes. The node carries a type (Sprite, Text, Collider, Camera, Container, Behavior, Any) that decides what "+ Property" offers: the fields of that type, the variables of the chosen behavior, or for Any a kind to pick and a name to type. Each port takes the default and field of its type, so a Camera width starts at 640. Switching the type keeps the ports and marks those the new type lacks. At runtime the type is only an editor aid; the node writes to whatever object it gets. Old project files are converted on load. The type is inferred from the keys: the first object type that has them all, else the one behavior that declares them all, else Any. enabled belongs to every object, so a converted Set enabled lands on Any. The jump-run and tetris seeders use Properties throughout; draw-piece switches its cell on in the same node that sets the colour. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7c63c884ef
commit
58f19a3451
36 files changed
+1040
-719
No files matched your search
@@ -0,0 +1,167 @@
|
||||
import { PROPERTIES, VARIABLES } from '../graph/nested';
|
||||
import { OBJECT_BEHAVIOR_PARAM, OBJECT_TYPE_PARAM, targetProperties } from '../graph/propertyPorts';
|
||||
import { portKindOf } from '../graph/variablePorts';
|
||||
import type { PropertyBag } from '../model/Properties';
|
||||
import { COMMON_PROPS, type PropertyDef, type PropertySchema } 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 };
|
||||
}
|
||||
|
||||
const SINGLE_PROPERTY: Record<string, boolean> = { GetProperty: false, SetProperty: true };
|
||||
const PLURAL_PROPERTY: Record<string, boolean> = { GetProperties: false, SetProperties: true };
|
||||
const FIXED_TARGETS = ['sprite', 'text', 'collider', 'camera', 'container'] as const;
|
||||
|
||||
interface Pinned {
|
||||
params: PropertyBag;
|
||||
defs: PropertySchema;
|
||||
}
|
||||
|
||||
/**
|
||||
* The type an old node's ports fit: the first object type holding every key, else the one behavior
|
||||
* declaring them all as variables, else Any.
|
||||
*/
|
||||
export function pinPropertyTarget(
|
||||
keys: readonly string[],
|
||||
behaviors: ReadonlyMap<string, readonly PropertyDef[]>,
|
||||
): Pinned {
|
||||
const holds = (defs: PropertySchema) => keys.every((key) => defs.some((d) => d.key === key));
|
||||
if (keys.length === 0) return { params: {}, defs: [] };
|
||||
// Every object has these, so they say nothing about which type the node was meant for.
|
||||
if (holds(COMMON_PROPS)) return { params: {}, defs: COMMON_PROPS };
|
||||
for (const target of FIXED_TARGETS) {
|
||||
const defs = targetProperties(target);
|
||||
if (holds(defs)) return { params: { [OBJECT_TYPE_PARAM]: target }, defs };
|
||||
}
|
||||
const owners = [...behaviors].filter(([, defs]) => holds(defs));
|
||||
const [owner] = owners;
|
||||
if (owners.length !== 1 || !owner) return { params: {}, defs: [] };
|
||||
return { params: { [OBJECT_TYPE_PARAM]: 'behavior', [OBJECT_BEHAVIOR_PARAM]: owner[0] }, defs: owner[1] };
|
||||
}
|
||||
|
||||
/**
|
||||
* Nodes that were a `Set property` of one key, with what their unconnected input wrote. It has to
|
||||
* be spelled out: the new port would write the type's default instead (a text object's is "Text").
|
||||
*/
|
||||
const SETTERS: Record<string, { key: string; unset: boolean | string }> = {
|
||||
SetEnabled: { key: 'enabled', unset: true },
|
||||
SetText: { key: 'text', unset: '' },
|
||||
};
|
||||
|
||||
function fromSetter(node: Raw): Raw {
|
||||
const setter = SETTERS[String(node.type)];
|
||||
if (!setter) return node;
|
||||
const { [setter.key]: value = setter.unset } = record(node.params);
|
||||
return { ...node, type: 'SetProperty', params: { property: setter.key, value } };
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrites Get/Set property(ies), Set enabled and Set text of an older file into `Properties`, on
|
||||
* the raw graph before it is normalized. A single node's `value` port is renamed to the property; one
|
||||
* without a property keeps only its object input and loses its other edges.
|
||||
*/
|
||||
export function upgradePropertyNodes(
|
||||
raw: unknown,
|
||||
behaviors: ReadonlyMap<string, 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 = fromSetter(record(item));
|
||||
const type = String(node.type);
|
||||
if (type in PLURAL_PROPERTY) {
|
||||
const write = PLURAL_PROPERTY[type];
|
||||
const ports = (Array.isArray(node.ports) ? (node.ports as unknown[]) : []).map(record);
|
||||
const pinned = pinPropertyTarget(
|
||||
ports.map((p) => String(p.key)),
|
||||
behaviors,
|
||||
);
|
||||
return {
|
||||
...node,
|
||||
type: PROPERTIES,
|
||||
params: { ...record(node.params), ...pinned.params },
|
||||
ports: ports.map((p) => (write ? { ...p, write } : p)),
|
||||
};
|
||||
}
|
||||
if (!(type in SINGLE_PROPERTY)) return node;
|
||||
const write = SINGLE_PROPERTY[type];
|
||||
const { property, value, ...params } = record(node.params);
|
||||
const key = typeof property === 'string' ? property : '';
|
||||
renamed.set(String(node.id), key);
|
||||
if (!key) return { ...node, type: PROPERTIES, params, ports: [] };
|
||||
const pinned = pinPropertyTarget([key], behaviors);
|
||||
const def = pinned.defs.find((d) => d.key === key);
|
||||
const kind = def ? portKindOf(def) : 'text';
|
||||
if (write && value !== undefined) params[key] = value;
|
||||
return {
|
||||
...node,
|
||||
type: PROPERTIES,
|
||||
params: { ...params, ...pinned.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 === undefined || at.port === 'object') return at;
|
||||
if (key === '') return null;
|
||||
return 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 };
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
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,7 +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 { upgradePropertyNodes, upgradeVariableNodes } from './legacyNodes';
|
||||
import {
|
||||
coerceValue,
|
||||
COMMON_PROPS,
|
||||
@@ -80,7 +80,9 @@ export function deserializeProject(raw: unknown, fallbackId = ''): Project {
|
||||
name,
|
||||
props,
|
||||
variables: variables.get(id) ?? [],
|
||||
graph: normalizeGraph(upgradeVariableNodes(a.graph, variables.get(id) ?? [])),
|
||||
graph: normalizeGraph(
|
||||
upgradePropertyNodes(upgradeVariableNodes(a.graph, variables.get(id) ?? []), variables),
|
||||
),
|
||||
});
|
||||
} else if (type === 'texture') {
|
||||
assets.push({ id, type, name, props, ...normalizeTexture(a, takenIds) });
|
||||
|
||||
Reference in new issue
Block a user