Add Get/Set properties nodes and a breadcrumb for opened sub-behaviors

Get properties / Set properties work like their variable counterparts:
one port per chosen property, an unconnected input writes its inline
value. A port chosen from the built-in list keeps that property's field
and default (height starts at 100, color is a colour field). "object" is
reserved for the fixed input, checked by the command and on load.

The property picker names the owning type ("Sprite width"), and text
fields can now carry a suggestion list; Get/Set property use it.

Double-clicking a call node shows the path of opened behaviors under the
graph header. Clicking an entry jumps back and trims the path; choosing
a behavior any other way hides it. Reopening a behavior already on the
path steps back to it instead of repeating it. The bar shows before the
graph switches, otherwise it clips the graph that was just fitted.

The "Add node..." header button is gone; nodes come from the context menu.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
rootandClaude Opus 5 committed 2026-09-11 15:07:07 +02:00
1 parent e9fa592078
commit 854ae2e756
35 files changed
+1044 -101

No files matched your search

+13 -5
View File
@@ -4,6 +4,7 @@ import {
declaresVariablePorts,
type DeclaredPort,
findNode,
fixedPortKeys,
type Graph,
type GraphEdge,
type GraphNode,
@@ -73,6 +74,12 @@ function carryPort(site: PortSite, from: string, to: string): void {
const INVALID_NAME =
'Port names must start with a letter, contain only letters, digits or _, and "exec" is taken by the flow port';
function checkPortName(node: GraphNode, key: string): void {
if (!isValidPortName(key)) throw new CommandError(INVALID_NAME);
if (fixedPortKeys(requireNodeDef(node.type)).includes(key))
throw new CommandError(`"${key}" is taken by a port of the node itself`);
}
/** Several ports as one step, so seeding a node from a struct is one undo rather than one per field. */
export class AddPortsCommand implements Command {
readonly label: string;
@@ -91,7 +98,7 @@ export class AddPortsCommand implements Command {
const node = requireNode(project, this.assetId, this.nodeId);
const keys = new Set((node.ports ?? []).map((p) => p.key));
for (const port of this.ports) {
if (!isValidPortName(port.key)) throw new CommandError(INVALID_NAME);
checkPortName(node, port.key);
if (keys.has(port.key)) throw new CommandError(`"${port.key}" already exists`);
keys.add(port.key);
}
@@ -187,7 +194,7 @@ export class RenamePortCommand implements Command {
if (from === to) return;
const node = requireNode(project, this.assetId, this.nodeId);
const sites = portSites(project, this.assetId, node);
if (!isValidPortName(to)) throw new CommandError(INVALID_NAME);
checkPortName(node, to);
const ports = node.ports ?? [];
if (!ports.some((p) => p.key === from)) throw new CommandError(`Port "${from}" not found`);
if (ports.some((p) => p.key === to)) throw new CommandError(`"${to}" already exists`);
@@ -209,11 +216,11 @@ function renamePortOn(graph: Graph, node: GraphNode, from: string, to: string):
}
/**
* Points one declared port at another variable. Key and kind change together, because on a
* Get/Set variables node the kind is the variable's rather than a choice of its own; the edges
* Points one declared port at another variable or property. Key and kind change together, because
* there the kind is the variable's (the property's) rather than a choice of its own; the edges
* hanging on the port come along, so correcting a pick is not a rewiring.
*/
export class SetVariablePortCommand implements Command {
export class RepointPortCommand implements Command {
readonly label: string;
private previous?: DeclaredPort;
@@ -231,6 +238,7 @@ export class SetVariablePortCommand implements Command {
const ports = node.ports ?? [];
const current = ports.find((p) => p.key === this.from);
if (!current) throw new CommandError(`Port "${this.from}" not found`);
checkPortName(node, this.port.key);
if (this.from !== this.port.key && ports.some((p) => p.key === this.port.key))
throw new CommandError(`"${this.port.key}" already exists`);
this.previous = { ...current };
+6 -1
View File
@@ -1,6 +1,6 @@
import type { PropertyBag, PropertyValue } from '../model/Properties';
import type { PropertyDef } from '../properties/PropertySchema';
import type { Graph, GraphNode, PortType } from './Graph';
import type { DeclaredPort, Graph, GraphNode, PortType } from './Graph';
import type { BehaviorLookup } from './lookup';
import type { NodeContext } from './NodeContext';
import { asBoolean, asList, asNumber, asString, type Value } from './values';
@@ -11,6 +11,8 @@ export interface PortDef {
type: PortType;
/** Inline value used while the input is unconnected and the node has no own value (default 0/''/false). */
default?: PropertyValue;
/** The property this port carries; its typed-in value is edited as that property (color, enum, …). */
field?: PropertyDef;
}
export type NodeCategory = 'event' | 'flow' | 'data' | 'variable' | 'action';
@@ -76,6 +78,8 @@ export interface NodeDef {
* is what gives a node the port editor in the inspector.
*/
declaresPorts?: 'inputs' | 'outputs';
/** How a declared port becomes a port; without it, named after its key and typed by its kind. */
declaredPort?(port: DeclaredPort): PortDef;
/**
* Ports this node computes beyond the fixed and the declared ones – how many outputs a Sequence
* offers, which values the behavior a call node names takes. `resolveDef` applies it.
@@ -132,6 +136,7 @@ export function inlineInputs(def: NodeDef): PortDef[] {
}
export function inlineInputDef(port: PortDef): PropertyDef {
if (port.field) return { ...port.field, key: port.key, label: port.label };
switch (port.type) {
case 'boolean':
return { key: port.key, label: port.label, kind: 'boolean', default: asBoolean(port.default ?? false) };
+1
View File
@@ -7,6 +7,7 @@ export * from './lookup';
export * from './nested';
export * from './NodeContext';
export * from './NodeRegistry';
export * from './propertyPorts';
export * from './values';
export * from './variablePorts';
export { GraphInterpreter, type TraceListener } from './GraphInterpreter';
+13 -1
View File
@@ -9,6 +9,8 @@ export const BEHAVIOR_END = 'BehaviorEnd';
export const RUN_BEHAVIOR = 'RunBehavior';
export const GET_VARIABLES = 'GetVariables';
export const SET_VARIABLES = 'SetVariables';
export const GET_PROPERTIES = 'GetProperties';
export const SET_PROPERTIES = 'SetProperties';
export const BEHAVIOR_PARAM = 'behavior';
@@ -21,11 +23,21 @@ export function declaresVariablePorts(type: string): boolean {
return type === GET_VARIABLES || type === SET_VARIABLES;
}
/** Nodes whose declared ports are properties of the object they are handed. */
export function declaresPropertyPorts(type: string): boolean {
return type === GET_PROPERTIES || type === SET_PROPERTIES;
}
/** Identifier-like, and not `exec`: that key belongs to the fixed flow port of every terminal node. */
export function isValidPortName(name: string): boolean {
return isValidFieldName(name) && name !== 'exec';
}
/** Keys a declared port cannot take: the node's fixed ports on the side it declares (`object`). */
export function fixedPortKeys(def: NodeDef): string[] {
return (def.declaresPorts === 'inputs' ? def.inputs : def.outputs).map((p) => p.key);
}
/** The single start (or end) node of a graph; a second one is ignored. */
export function terminalNode(graph: Graph, type: string): GraphNode | undefined {
return graph.nodes.find((n) => n.type === type);
@@ -56,7 +68,7 @@ export function declaredPorts(graph: Graph | undefined, type: string): PortDef[]
export function resolveDef(node: GraphNode, graph: Graph, lookup: BehaviorLookup): NodeDef | undefined {
const def = nodeDef(node.type);
if (!def || (!def.declaresPorts && !def.derivePorts)) return def;
const own = def.declaresPorts ? (node.ports ?? []).map(portDefOf) : [];
const own = def.declaresPorts ? (node.ports ?? []).map((p) => (def.declaredPort ?? portDefOf)(p)) : [];
const side = (which: 'inputs' | 'outputs') => (def.declaresPorts === which ? own : []);
const grown = def.derivePorts?.({ node, graph, behaviors: lookup }) ?? {};
return {
@@ -0,0 +1,28 @@
import { SET_PROPERTIES } from '../../nested';
import type { NodeDef } from '../../NodeRegistry';
import { propertyPort } from '../../propertyPorts';
import { asEntity, writeTyped } from '../../values';
import { exec, obj } from '../shared';
/**
* The plural of Set property: one input per property picked. Like Set variables it writes every
* property it lists each time it runs – an unconnected input writes the value typed on the node.
*/
export const SetProperties: NodeDef = {
type: SET_PROPERTIES,
label: 'Set properties',
category: 'action',
description: 'Writes several properties (width, color, …) of an object at once, one input per property.',
inputs: [exec(), obj('object')],
outputs: [exec()],
params: [],
declaresPorts: 'inputs',
declaredPort: propertyPort,
run: (_ctx, inputs, _params, def) => {
const target = asEntity(inputs.object);
if (target)
for (const port of def.inputs.slice(SetProperties.inputs.length))
writeTyped(target.props, port.key, inputs[port.key]);
return { next: 'exec' };
},
};
+3 -3
View File
@@ -1,8 +1,8 @@
import { textOf } from '../../../model/Properties';
import type { NodeDef } from '../../NodeRegistry';
import { asEntity, writeTyped } from '../../values';
import { any, exec, obj } from '../shared';
import { any, exec, obj, PROPERTY_PARAM } from '../shared';
import { textOf } from '../../../model/Properties';
export const SetProperty: NodeDef = {
type: 'SetProperty',
label: 'Set property',
@@ -10,7 +10,7 @@ export const SetProperty: NodeDef = {
description: 'Writes a property (width, color, …) of an object.',
inputs: [exec(), obj('object'), any('value')],
outputs: [exec()],
params: [{ key: 'property', label: 'Property', kind: 'text', default: 'width' }],
params: [PROPERTY_PARAM],
run: (_ctx, inputs, params) => {
const target = asEntity(inputs.object);
const key = textOf(params.property ?? '');
@@ -0,0 +1,24 @@
import { GET_PROPERTIES } from '../../nested';
import type { NodeDef } from '../../NodeRegistry';
import { propertyPort } from '../../propertyPorts';
import { asEntity, type Value } from '../../values';
import { obj } from '../shared';
/** The plural of Get property: one output per property picked, the way Get variables reads. */
export const GetProperties: NodeDef = {
type: GET_PROPERTIES,
label: 'Get properties',
category: 'data',
description: 'Reads several properties (width, color, …) of an object at once, one output per property.',
inputs: [obj('object')],
outputs: [],
params: [],
declaresPorts: 'outputs',
declaredPort: propertyPort,
run: (_ctx, inputs, _params, def) => {
const target = asEntity(inputs.object);
const outputs: Record<string, Value> = {};
for (const port of def.outputs) outputs[port.key] = target?.props[port.key] ?? null;
return { outputs };
},
};
+3 -3
View File
@@ -1,8 +1,8 @@
import { textOf } from '../../../model/Properties';
import type { NodeDef } from '../../NodeRegistry';
import { asEntity } from '../../values';
import { any, obj } from '../shared';
import { any, obj, PROPERTY_PARAM } from '../shared';
import { textOf } from '../../../model/Properties';
export const GetProperty: NodeDef = {
type: 'GetProperty',
label: 'Get property',
@@ -10,7 +10,7 @@ export const GetProperty: NodeDef = {
description: 'Reads a property (width, color, …) of an object.',
inputs: [obj('object')],
outputs: [any('value')],
params: [{ key: 'property', label: 'Property', kind: 'text', default: 'width' }],
params: [PROPERTY_PARAM],
run: (_ctx, inputs, params) => ({
outputs: { value: asEntity(inputs.object)?.props[textOf(params.property ?? '')] ?? null },
}),
+4
View File
@@ -5,6 +5,7 @@ import { Move } from './action/Move';
import { RestartGame } from './action/RestartGame';
import { SetEnabled } from './action/SetEnabled';
import { SetPosition } from './action/SetPosition';
import { SetProperties } from './action/SetProperties';
import { SetProperty } from './action/SetProperty';
import { SetText } from './action/SetText';
import { Spawn } from './action/Spawn';
@@ -23,6 +24,7 @@ import { Equals } from './data/Equals';
import { FindText } from './data/FindText';
import { Floor } from './data/Floor';
import { GetPosition } from './data/GetPosition';
import { GetProperties } from './data/GetProperties';
import { GetProperty } from './data/GetProperty';
import { IsAsset } from './data/IsAsset';
import { IsEnabled } from './data/IsEnabled';
@@ -125,6 +127,7 @@ export const BUILT_IN_NODES: readonly NodeDef[] = [
IsAsset,
IsEnabled,
GetProperty,
GetProperties,
GetPosition,
// data: literals and math
NumberLiteral,
@@ -182,6 +185,7 @@ export const BUILT_IN_NODES: readonly NodeDef[] = [
Move,
SetPosition,
SetProperty,
SetProperties,
SetEnabled,
SetText,
Spawn,
+10 -1
View File
@@ -1,9 +1,18 @@
import { type PropertyBag, type PropertyValue, textOf } from '../../model/Properties';
import type { PropertyDef } from '../../properties/PropertySchema';
import { OBJECT_PROPERTY_SUGGESTIONS, type PropertyDef } from '../../properties/PropertySchema';
import type { NodeContext } from '../NodeContext';
import type { DerivedShape, NodeDef, PortDef, PortSource, RunResult } from '../NodeRegistry';
import { asBoolean, asNumber, type Value, writeTyped } from '../values';
/** The property a Get/Set property node names: typed freely, or picked from the built-in ones. */
export const PROPERTY_PARAM: PropertyDef = {
key: 'property',
label: 'Property',
kind: 'text',
default: 'width',
suggestions: OBJECT_PROPERTY_SUGGESTIONS,
};
export const exec = (key = 'exec', label = ''): PortDef => ({ key, label, type: 'exec' });
export const num = (key: string, label = key): PortDef => ({ key, label, type: 'number' });
export const bool = (key: string, label = key): PortDef => ({ key, label, type: 'boolean' });
+24
View File
@@ -0,0 +1,24 @@
import { OBJECT_PROPERTIES, type PropertyDef } from '../properties/PropertySchema';
import type { DeclaredPort } from './Graph';
import { portDefOf } from './nested';
import type { PortDef } from './NodeRegistry';
import { portKindOf } from './variablePorts';
export function objectPropertyDef(key: string): PropertyDef | undefined {
return OBJECT_PROPERTIES.find((def) => def.key === key);
}
export function propertyPortOf(def: PropertyDef): DeclaredPort {
return { key: def.key, kind: portKindOf(def) };
}
/**
* A port naming a built-in property carries its def: a typed-in value starts at its default and is
* edited with its field. A custom name, or a kind the property does not travel as, stays plain.
*/
export function propertyPort(port: DeclaredPort): PortDef {
const plain = portDefOf(port);
const field = objectPropertyDef(port.key);
if (!field || portKindOf(field) !== port.kind) return plain;
return { ...plain, field, default: field.default };
}
+3 -2
View File
@@ -1,8 +1,9 @@
import type { PropertyDef, StructFieldDef } from '../properties/PropertySchema';
import { DECLARED_PORT_KINDS, type DeclaredPort, type DeclaredPortKind, type GraphNode } from './Graph';
/** Every variable kind is a port kind too; the fallback is for a project file that says otherwise. */
export function variablePortKind(def: PropertyDef): DeclaredPortKind {
/** The port a variable or property travels on. A color is a number; an enum or a texture is text. */
export function portKindOf(def: PropertyDef): DeclaredPortKind {
if (def.kind === 'color') return 'number';
return (DECLARED_PORT_KINDS as readonly string[]).includes(def.kind)
? (def.kind as DeclaredPortKind)
: 'text';
+34 -2
View File
@@ -1,5 +1,5 @@
import type { Asset } from '../model/Asset';
import { type AssetType, isPlaceableType } from '../model/AssetTypes';
import { ASSET_TYPE_LABELS, type AssetType, isPlaceableType } from '../model/AssetTypes';
import {
type FieldValue,
isList,
@@ -13,6 +13,12 @@ import {
colorOf,
} from '../model/Properties';
/** A value a text field offers to fill in; the text stays free to type. */
export interface TextSuggestion {
value: string;
label: string;
}
/** The inspector is generated from these entries, so a new property is one line in a schema below. */
export type PropertyDef =
/**
@@ -30,7 +36,7 @@ export type PropertyDef =
step?: number;
decimals?: number;
}
| { key: string; label: string; kind: 'text'; default: string }
| { key: string; label: string; kind: 'text'; default: string; suggestions?: readonly TextSuggestion[] }
| { key: string; label: string; kind: 'color'; default: number }
| { key: string; label: string; kind: 'boolean'; default: boolean }
| { key: string; label: string; kind: 'enum'; default: string; options: { value: string; label: string }[] }
@@ -197,6 +203,32 @@ const FULL_SCHEMAS: Record<AssetType, PropertySchema> = Object.fromEntries(
]),
) as Record<AssetType, PropertySchema>;
const OBJECT_TYPES = ['sprite', 'text', 'collider', 'camera'] as const;
/** A built-in property with the type it belongs to ("Sprite width"); a shared key appears once per type. */
export const OBJECT_PROPERTY_CHOICES: readonly { label: string; def: PropertyDef }[] = [
...COMMON_PROPS.map((def) => ({ label: `Object ${def.key}`, def })),
...OBJECT_TYPES.flatMap((type) =>
ASSET_SCHEMAS[type].map((def) => ({ label: `${ASSET_TYPE_LABELS[type]} ${def.key}`, def })),
),
];
/**
* Every built-in property once per key – what a Get/Set properties port resolves to. Sprite comes
* first so a shared key (`width`) keeps its def, not the camera's whole-pixel view width.
*/
export const OBJECT_PROPERTIES: PropertySchema = firstPerKey(OBJECT_PROPERTY_CHOICES.map((c) => c.def));
export const OBJECT_PROPERTY_SUGGESTIONS: readonly TextSuggestion[] = OBJECT_PROPERTY_CHOICES.map((c) => ({
value: c.def.key,
label: c.label,
}));
function firstPerKey(defs: readonly PropertyDef[]): PropertyDef[] {
const seen = new Set<string>();
return defs.filter((def) => !seen.has(def.key) && seen.add(def.key));
}
export function schemaFor(type: AssetType): PropertySchema {
return FULL_SCHEMAS[type];
}
+6 -4
View File
@@ -8,6 +8,7 @@ import {
type GraphEdge,
type GraphNode,
isValidPortName,
fixedPortKeys,
nodeDef,
} from '../graph';
import type { Asset, TextureFrame } from '../model/Asset';
@@ -210,9 +211,9 @@ 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): DeclaredPort[] {
function normalizePorts(raw: unknown, fixed: readonly string[]): DeclaredPort[] {
const ports: DeclaredPort[] = [];
const seen = new Set<string>();
const seen = new Set(fixed);
for (const item of Array.isArray(raw) ? raw : []) {
const p = asRecord(item);
const key = typeof p.key === 'string' ? p.key : '';
@@ -234,7 +235,8 @@ function normalizeGraph(raw: unknown): Graph {
const n = asRecord(item);
const id = typeof n.id === 'string' ? n.id : '';
const type = typeof n.type === 'string' ? n.type : '';
if (!id || nodeIds.has(id) || !nodeDef(type)) continue;
const def = nodeDef(type);
if (!id || nodeIds.has(id) || !def) continue;
nodeIds.add(id);
// Params carry both settings and inline input values, so there is no schema to coerce against.
const params: PropertyBag = {};
@@ -243,7 +245,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);
const ports = normalizePorts(n.ports, fixedPortKeys(def));
if (ports.length > 0) node.ports = ports;
if (n.collapsed === true) node.collapsed = true;
graph.nodes.push(node);
+5 -8
View File
@@ -37,7 +37,8 @@ export abstract class AssetTabEditor<
A extends Asset,
C extends TabCanvas & Widget = TabCanvas & Widget,
> extends VStack {
protected readonly picker: Dropdown;
/** Public for the check scripts. */
readonly picker: Dropdown;
protected readonly info: Label;
/** Assigned by `mount`. Public because the check hooks and the play loop reach for it. */
canvas!: C;
@@ -53,8 +54,8 @@ export abstract class AssetTabEditor<
this.info.minWidth = options.infoWidth;
}
/** Builds the header around `canvas`, wires the state events and shows the first asset. */
protected mount(canvas: C, extras: readonly Widget[] = []): void {
/** Builds the header (with `strip` below it) around `canvas`, wires the state events and shows the first asset. */
protected mount(canvas: C, extras: readonly Widget[] = [], strip?: Widget): void {
this.canvas = canvas;
const header = new HStack({ gap: Theme.gap, padding: Theme.padding, align: 'center' });
header.minHeight = Theme.controlHeight + Theme.padding * 2;
@@ -67,6 +68,7 @@ export abstract class AssetTabEditor<
header.add(this.info);
this.add(header);
if (strip) this.add(strip);
this.add(canvas, 1);
this.state.on('selection', () => this.followSelection());
@@ -83,11 +85,6 @@ export abstract class AssetTabEditor<
return this.canvas.currentAssetId;
}
/** For the check scripts: the picker's width must not follow the name it happens to show. */
get pickerWidth(): number {
return this.picker.w;
}
show(assetId: string): void {
this.canvas.show(assetId);
this.refresh();
+17 -14
View File
@@ -18,24 +18,24 @@ export function installDebugHook(app: Application, state: EditorState, editor: E
const p = widget.getGlobalPosition();
return { x: p.x + widget.w / 2, y: p.y + widget.h / 2 };
};
/** Centre of the first (or, with `last`, the topmost/last-drawn) visible text equal to `text`. */
const locate = (text: string, last = false): { x: number; y: number } | null => {
let found: Text | null = null;
/** Centres of every visible text equal to `text`, in drawing order (the topmost last). */
const locateAll = (text: string): { x: number; y: number }[] => {
const hits: { x: number; y: number }[] = [];
const walk = (node: Container): void => {
if ((found && !last) || !node.visible) return;
if (!node.visible) return;
if (node instanceof Text && node.text === text) {
found = node;
const p = node.getGlobalPosition();
hits.push({ x: p.x + node.width / 2, y: p.y + node.height / 2 });
return;
}
for (const child of node.children) walk(child);
};
walk(app.stage);
// TypeScript does not track the assignment inside `walk`, hence the disable and the cast.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!found) return null;
const node = found as Text;
const p = node.getGlobalPosition();
return { x: p.x + node.width / 2, y: p.y + node.height / 2 };
return hits;
};
const locate = (text: string, last = false): { x: number; y: number } | null => {
const hits = locateAll(text);
return (last ? hits.at(-1) : hits[0]) ?? null;
};
/** Fill colour of a visible text, as `#rrggbb` – what tells an override label from a plain one. */
const textColor = (text: string): string | null => {
@@ -160,6 +160,7 @@ export function installDebugHook(app: Application, state: EditorState, editor: E
objectsOf,
sceneObjects: () => (state.activeScene ? objectsOf(state.activeScene.id) : []),
locate,
locateAll,
inspectorField: fieldOf,
inspectorInfo: () => inspector.debugInfo(),
textColor,
@@ -207,21 +208,23 @@ export function installDebugHook(app: Application, state: EditorState, editor: E
// The canvas, not the whole tab: a centre measured through the header sits above the
// drawing area.
center: () => centreOf(graphEditor.canvas),
pickerWidth: () => graphEditor.pickerWidth,
pickerWidth: () => graphEditor.picker.w,
pickerAt: () => centreOf(graphEditor.picker),
snapping: () => graphEditor.canvas.snapEnabled,
crumbs: () => graphEditor.breadcrumb.crumbsDrawn(),
},
/** The open image picker: what it offers, what is picked, and where a tile sits. */
imagePicker: () => currentImagePicker()?.debugInfo() ?? null,
imagePickerTile: (file: string) => currentImagePicker()?.tileGlobal(file) ?? null,
texture: {
current: () => textureEditor.currentAssetId,
pickerWidth: () => textureEditor.pickerWidth,
pickerWidth: () => textureEditor.picker.w,
/** Where an image pixel of the shown sheet sits on screen (frames are dragged in image coordinates). */
pointAt: (x: number, y: number) => textureEditor.imageToScreen(x, y),
},
container: {
current: () => containerEditor.currentAssetId,
pickerWidth: () => containerEditor.pickerWidth,
pickerWidth: () => containerEditor.picker.w,
center: () => centreOf(containerEditor),
rect: () => containerEditor.canvasRect(),
/** Where a template object is drawn, so a check can click or sample it. */
+74
View File
@@ -0,0 +1,74 @@
import { Graphics } from 'pixi.js';
import { Button, HStack, Label, Theme, type Widget } from '../../ui';
/** Past this a crumb cuts its name short, so a deep trail of long names still fits the tab. */
const CRUMB_MAX_WIDTH = 160;
/** "Game › Move › Other": every crumb but the last one leads back; empty hides the bar. */
export class Breadcrumb extends HStack {
private readonly bg = new Graphics();
private crumbs: Widget[] = [];
onPick: (index: number) => void = () => undefined;
constructor() {
super({ gap: 2, padding: 3, align: 'center' });
this.addChild(this.bg);
this.visible = false;
}
/** Returns whether the bar changed, so the caller relayouts only then. */
show(names: readonly string[]): boolean {
if (names.length === this.crumbs.length && names.every((n, i) => crumbText(this.crumbs[i]!) === n))
return false;
for (const widget of this.items) widget.destroy({ children: true });
this.crumbs = names.map((name, i) => {
if (i > 0) this.add(new Label('›', { style: 'muted' }));
const last = i === names.length - 1;
return this.add(
capped((width) =>
last
? new Label(name, { width, padding: Theme.padding })
: new Button(name, { variant: 'ghost', width, onClick: () => this.onPick(i) }),
),
);
});
this.visible = names.length > 0;
// The bar keeps its size across a rebuild, and `resize` lays out only on a new size.
this.relayout();
return true;
}
/** For the check scripts: what each crumb says, where it sits, and whether it leads anywhere. */
crumbsDrawn(): { name: string; x: number; y: number; left: number; right: number; clickable: boolean }[] {
if (!this.visible) return [];
return this.crumbs.map((widget) => {
const at = widget.getGlobalPosition();
return {
name: crumbText(widget),
x: at.x + widget.w / 2,
y: at.y + widget.h / 2,
left: at.x,
right: at.x + widget.w,
clickable: widget instanceof Button,
};
});
}
override redraw(): void {
super.redraw();
this.bg.clear();
this.bg.rect(0, 0, this.w, this.h).fill(Theme.colors.panelHeader);
this.bg.rect(0, this.h - 1, this.w, 1).fill(Theme.colors.border);
}
}
function crumbText(widget: Widget): string {
return widget instanceof Button || widget instanceof Label ? widget.text : '';
}
function capped(build: (width?: number) => Widget): Widget {
const natural = build();
if (natural.measure().w <= CRUMB_MAX_WIDTH) return natural;
natural.destroy({ children: true });
return build(CRUMB_MAX_WIDTH);
}
+3 -11
View File
@@ -112,6 +112,8 @@ export class GraphCanvas extends Widget {
/** Supplies live execution data while playing (null otherwise). */
traceProvider: (assetId: string) => Trace | null = () => null;
timeProvider: () => number = () => 0;
/** A double-clicked call node; the tab opens the behavior and records the step. */
onOpenBehavior: (assetId: string) => void = (assetId) => this.state.select({ kind: 'asset', assetId });
constructor(private readonly state: EditorState) {
super();
@@ -173,15 +175,6 @@ export class GraphCanvas extends Widget {
return this.snapping;
}
/** Drops the node in the middle of the view, since a button press names no position. */
addNodeFrom(anchor: Widget): void {
if (!this.assetId || this.state.mode !== 'edit') return;
const centre = this.screenToGraph(this.w / 2, this.h / 2);
new NodePalette(this.entries(), (entry) =>
this.addNode(entry, { x: centre.x - NODE_WIDTH / 2, y: centre.y - NODE_HEIGHT_ESTIMATE / 2 }),
).openBelow(anchor);
}
override measure(): Size {
return { w: Math.max(this.minWidth, 200), h: Math.max(this.minHeight, 200) };
}
@@ -367,13 +360,12 @@ export class GraphCanvas extends Widget {
this.nodeLayer.addChild(view); // raise
}
/** Selecting the asset is what opens it: the graph tab follows the selection. */
private openCalledBehavior(node: GraphNode): boolean {
if (node.type !== RUN_BEHAVIOR) return false;
const id = textOf(node.params[BEHAVIOR_PARAM] ?? '');
const asset = id ? findAsset(this.state.project, id) : undefined;
if (asset?.type !== 'behavior') return false;
this.state.select({ kind: 'asset', assetId: id });
this.onOpenBehavior(id);
return true;
}
+39 -6
View File
@@ -1,25 +1,59 @@
import type { Asset, BehaviorAsset } from '../../core/model/Asset';
import { Button, Checkbox, Label } from '../../ui';
import { findAsset, findAssetOf } from '../../core/model/Project';
import { Checkbox, Label } from '../../ui';
import { AssetTabEditor } from '../AssetTabEditor';
import type { EditorState } from '../state';
import { Breadcrumb } from './Breadcrumb';
import { GraphCanvas } from './GraphCanvas';
import { backTo, descend, follow, type Trail } from './trail';
/** Room kept for the "N nodes · M links" readout, wide enough for a three-digit graph. */
const INFO_WIDTH = 150;
export class GraphEditor extends AssetTabEditor<BehaviorAsset, GraphCanvas> {
private readonly addButton: Button;
private readonly snapBox = new Checkbox();
readonly breadcrumb = new Breadcrumb();
private trail: Trail = [];
constructor(state: EditorState) {
super(state, { noun: 'Behavior', infoWidth: INFO_WIDTH });
this.addButton = new Button('Add node…', { onClick: () => this.canvas.addNodeFrom(this.addButton) });
this.snapBox.onChange = (on) => this.canvas.setSnapping(on);
this.mount(new GraphCanvas(state), [this.addButton, new Label('Snap', { style: 'muted' }), this.snapBox]);
// Adding nodes is an edit, and edits made while playing would be thrown away with the run.
this.breadcrumb.onPick = (index) => this.open(this.trail[index]!, backTo(this.trail, index));
this.mount(
new GraphCanvas(state),
[new Label('Snap', { style: 'muted' }), this.snapBox],
this.breadcrumb,
);
this.canvas.onOpenBehavior = (id) => this.open(id, descend(this.trail, this.currentAssetId, id));
// Edits made while playing would be thrown away with the run.
state.on('mode', () => this.refresh());
}
/**
* The bar goes up before the graph switches: switching fits the view to the canvas, and a bar
* appearing afterwards would cut the bottom of the fitted graph off.
*/
private open(assetId: string, trail: Trail): void {
this.trail = trail;
this.showTrail();
this.state.select({ kind: 'asset', assetId });
this.refresh();
}
protected override refresh(): void {
super.refresh();
const project = this.state.project;
this.trail = follow(this.trail, this.currentAssetId, (id) =>
Boolean(findAssetOf(project, id, 'behavior')),
);
this.showTrail();
}
private showTrail(): void {
const names = this.trail.map((id) => findAsset(this.state.project, id)?.name ?? '');
if (this.breadcrumb.show(names)) this.relayout();
}
protected override selectedId(): string {
return this.state.selectedBehaviorId ?? '';
}
@@ -36,7 +70,6 @@ export class GraphEditor extends AssetTabEditor<BehaviorAsset, GraphCanvas> {
protected override syncExtras(asset: BehaviorAsset | null): void {
const editable = Boolean(asset) && this.state.mode === 'edit';
this.addButton.setEnabled(editable);
this.snapBox.setEnabled(editable);
}
}
+23 -9
View File
@@ -1,9 +1,11 @@
import { CanvasTextMetrics, Graphics, Rectangle, Text } from 'pixi.js';
import {
asNumber,
asString,
edgesInto,
type Graph,
type GraphNode,
inlineInputDef,
inlineInputs,
inlineValue,
type NodeCategory,
@@ -15,10 +17,11 @@ import {
} from '../../core/graph';
import type { BehaviorAsset } from '../../core/model/Asset';
import type { Project } from '../../core/model/Project';
import { isList, type PropertyValue, textOf } from '../../core/model/Properties';
import { isList, type PropertyValue } from '../../core/model/Properties';
import { assetRefLabel } from '../../core/properties/assetRef';
import type { PropertyDef } from '../../core/properties/PropertySchema';
import { formatNumber, Theme, textStyle, type TextStyleToken, truncate, Widget } from '../../ui';
import { textureRefLabel } from '../../core/properties/textureRef';
import { formatNumber, Theme, textStyle, type TextStyleToken, toHex, truncate, Widget } from '../../ui';
import { keyLabel } from '../inspector/fieldFactory';
import { collapsedNodeWidth } from './folding';
@@ -93,6 +96,7 @@ export class NodeView extends Widget {
targetPort: { port: string; isOutput: boolean } | null = null;
/** Data inputs that currently have a connection (their inline value is hidden). */
private connected = new Set<string>();
private inlineTexts = new Map<string, string>();
/** Which sides carry a link – a collapsed node marks those with one dot each. */
private linked = { inputs: false, outputs: false };
@@ -148,6 +152,12 @@ export class NodeView extends Widget {
.filter((p) => edgesInto(graph, node.id, p.key).length > 0)
.map((p) => p.key),
);
this.inlineTexts = new Map();
for (const port of inlineInputs(this.def)) {
const value = inlineValue(node, port);
if (!this.connected.has(port.key) && value !== null && value !== '')
this.inlineTexts.set(port.key, formatField(inlineInputDef(port), value, project));
}
this.linked = {
inputs: graph.edges.some((e) => e.to.node === node.id),
outputs: graph.edges.some((e) => e.from.node === node.id),
@@ -266,11 +276,8 @@ export class NodeView extends Widget {
const output = this.def.outputs[i];
const rowTop = y + i * ROW_HEIGHT + 2;
if (input) {
let label = input.label;
if (input.type !== 'exec' && input.type !== 'object' && !this.connected.has(input.key)) {
const value = inlineValue(this.node, input);
if (value !== null && value !== '') label += ` ${formatValue(value)}`;
}
const inline = this.inlineTexts.get(input.key);
const label = inline === undefined ? input.label : `${input.label} ${inline}`;
add(
label,
input.type === 'exec' ? 'body' : 'muted',
@@ -369,8 +376,6 @@ function formatParam(
project: Project | null,
): string {
if (value === undefined || value === '') return '—';
if (param.kind === 'key') return keyLabel(textOf(value));
if (param.kind === 'asset') return assetRefLabel(project, textOf(value));
if (
param.kind === 'enum' &&
param.key === 'variable' &&
@@ -378,6 +383,15 @@ function formatParam(
!asset.variables.some((v) => v.key === value)
)
return `${asString(value)}?`;
return formatField(param, value, project);
}
/** A value in the words of the field that edits it: a key's name, a color's hex, a texture's name. */
function formatField(def: PropertyDef, value: Value, project: Project | null): string {
if (def.kind === 'key') return keyLabel(asString(value));
if (def.kind === 'asset') return assetRefLabel(project, asString(value));
if (def.kind === 'texture') return textureRefLabel(project, asString(value));
if (def.kind === 'color') return toHex(asNumber(value));
return formatValue(value);
}
+23
View File
@@ -0,0 +1,23 @@
/**
* The behaviors entered by double-clicking call nodes, root first. Either empty or at least two
* long, and always ending at the behavior the graph shows – anything else opened it otherwise.
*/
export type Trail = readonly string[];
/** Opening `to` from a call node in `from`. A behavior already on the trail is a step back to it. */
export function descend(trail: Trail, from: string, to: string): Trail {
const base = trail.at(-1) === from ? trail : [from];
const known = base.indexOf(to);
return known >= 0 ? backTo(base, known) : [...base, to];
}
/** Clicking crumb `index`; back at the root there is nothing left to show. */
export function backTo(trail: Trail, index: number): Trail {
const next = trail.slice(0, index + 1);
return next.length > 1 ? next : [];
}
/** What survives the graph showing `shown`: only a trail that still leads there, every step still alive. */
export function follow(trail: Trail, shown: string, exists: (id: string) => boolean): Trail {
return trail.at(-1) === shown && trail.every(exists) ? trail : [];
}
+19 -1
View File
@@ -251,7 +251,25 @@ export function createField(
}
case 'text': {
const field = new TextField(textOf(value), { onInput: handlers.onInput, onChange: handlers.onChange });
return { widget: field, grow: true, setValue: (v) => field.setValue(textOf(v)) };
const setValue = (v: PropertyValue) => field.setValue(textOf(v));
const suggestions = def.suggestions;
if (!suggestions) return { widget: field, grow: true, setValue };
const pick = new Dropdown('', { variant: 'ghost', width: MARK_WIDTH });
pick.onClick = () => {
const current = field.value.trim();
const items = suggestions.map((s) => ({
label: s.value === current ? `● ${s.label}` : s.label,
onSelect: () => {
setValue(s.value);
handlers.onChange(s.value);
},
}));
new Menu(items, { searchable: true }).openBelow(pick);
};
const combo = new HStack({ gap: Theme.gap, align: 'center' });
combo.add(field, 1);
combo.add(pick);
return { widget: combo, grow: true, setValue };
}
case 'color': {
const field = new ColorField(Number(value), { onInput: handlers.onInput, onChange: handlers.onChange });
+3 -1
View File
@@ -1,5 +1,6 @@
import {
callProblem,
declaresPropertyPorts,
declaresVariablePorts,
edgesInto,
type GraphNode,
@@ -15,7 +16,7 @@ import type { PropertyDef } from '../../core/properties/PropertySchema';
import { Label } from '../../ui';
import { createFieldRow, type FieldHandlers } from './fieldFactory';
import { addNote, addTitle, type InspectorHost } from './host';
import { buildPortSection, buildVariablePortSection } from './portSection';
import { buildPortSection, buildPropertyPortSection, buildVariablePortSection } from './portSection';
import { listOpener, previewingHandlers } from './previewHandlers';
import { currentNode, nodeParamValue } from './valueSpecs';
@@ -52,6 +53,7 @@ export function buildNodeSection(host: InspectorHost, assetId: string, nodeId: s
const problem = callProblem(node, behaviorLookup(host.state.project), assetId);
if (problem) addNote(host, problem, 'warning');
if (declaresVariablePorts(node.type)) buildVariablePortSection(host, assetId, node, asset.variables);
else if (declaresPropertyPorts(node.type)) buildPropertyPortSection(host, assetId, node);
else if (def.declaresPorts) buildPortSection(host, assetId, node, asset.variables);
if (def.params.length > 0) addTitle(host, 'SETTINGS');
+80 -17
View File
@@ -4,7 +4,7 @@ import {
nextPortName,
RemovePortCommand,
RenamePortCommand,
SetVariablePortCommand,
RepointPortCommand,
} from '../../core/commands/portCommands';
import {
BEHAVIOR_START,
@@ -13,10 +13,16 @@ import {
type GraphNode,
isTerminalNode,
missingFieldPorts,
objectPropertyDef,
portKindOf,
propertyPortOf,
structShapeOf,
variablePortKind,
} from '../../core/graph';
import type { PropertyDef } from '../../core/properties/PropertySchema';
import {
OBJECT_PROPERTY_CHOICES,
type PropertyDef,
VARIABLE_KINDS,
} from '../../core/properties/PropertySchema';
import {
Dropdown,
HStack,
@@ -42,44 +48,101 @@ export function buildVariablePortSection(
node: GraphNode,
variables: readonly PropertyDef[],
): void {
const choices = variables.map((def) => ({ label: def.key, def }));
buildPickedPortSection(host, assetId, node, { noun: 'Variable', choices });
}
/**
* The ports of a Get/Set properties node: a built-in property is picked like a variable, and a name
* the list does not know (a variable of another behavior's object) is typed, with a kind of its own.
*/
export function buildPropertyPortSection(host: InspectorHost, assetId: string, node: GraphNode): void {
buildPickedPortSection(host, assetId, node, {
noun: 'Property',
choices: OBJECT_PROPERTY_CHOICES,
custom: true,
});
}
interface PickedPorts {
noun: 'Variable' | 'Property';
choices: readonly { label: string; def: PropertyDef }[];
custom?: boolean;
}
function buildPickedPortSection(
host: InspectorHost,
assetId: string,
node: GraphNode,
{ noun, choices, custom = false }: PickedPorts,
): void {
const plural = noun === 'Property' ? 'properties' : 'variables';
const ports = node.ports ?? [];
const taken = new Set(ports.map((p) => p.key));
const portOf = (v: PropertyDef): DeclaredPort => ({ key: v.key, kind: variablePortKind(v) });
const portOf = (def: PropertyDef): DeclaredPort => ({ key: def.key, kind: portKindOf(def) });
const pick = (own: string, onPick: (port: DeclaredPort) => void): MenuItem[] =>
variables.map((v) => ({
label: v.key,
disabled: taken.has(v.key) && v.key !== own,
onSelect: () => onPick(portOf(v)),
choices.map(({ label, def }) => ({
label,
disabled: taken.has(def.key) && def.key !== own,
onSelect: () => onPick(portOf(def)),
}));
const typed = (): MenuItem[] =>
VARIABLE_KINDS.map((kind) => ({
label: `Custom ${KIND_LABELS[kind].toLowerCase()}`,
onSelect: () =>
host.state.execute(
new AddPortCommand(assetId, node.id, { key: nextPortName(node, noun.toLowerCase()), kind }),
),
}));
const header = new HStack({ gap: Theme.gap, align: 'center' });
header.add(new Label('VARIABLES', { style: 'title' }), 1);
const add = new Dropdown('+ Variable', { variant: 'ghost' });
header.add(new Label(plural.toUpperCase(), { style: 'title' }), 1);
const add = new Dropdown(`+ ${noun}`, { variant: 'ghost' });
add.onClick = () => {
const items = pick('', (port) => host.state.execute(new AddPortCommand(assetId, node.id, port)));
new Menu(items).openBelow(add);
new Menu(custom ? [...items, ...typed()] : items, { searchable: custom }).openBelow(add);
};
add.setEnabled(variables.length > taken.size);
add.setEnabled(custom || choices.some(({ def }) => !taken.has(def.key)));
header.add(add);
host.body.add(header);
if (variables.length === 0) addNote(host, 'This behavior has no variables yet.');
else if (ports.length === 0) addNote(host, 'No variables picked yet. Each one adds a port.');
if (choices.length === 0) addNote(host, `This behavior has no ${plural} yet.`);
else if (ports.length === 0) addNote(host, `No ${plural} picked yet. Each one adds a port.`);
for (const port of ports) {
const known = variables.some((v) => v.key === port.key);
const known = choices.some(({ def }) => def.key === port.key);
if (!known && custom) {
portRow(host, assetId, node, port, customName(host, assetId, node, port));
continue;
}
const choose = new Dropdown(port.key, { variant: known ? 'default' : 'danger' });
choose.onClick = () => {
const items = pick(port.key, (next) =>
host.state.execute(new SetVariablePortCommand(assetId, node.id, port.key, next)),
host.state.execute(new RepointPortCommand(assetId, node.id, port.key, next)),
);
new Menu(items).openBelow(choose);
new Menu(items, { searchable: custom }).openBelow(choose);
};
portRow(host, assetId, node, port, choose);
if (!known) addNote(host, `"${port.key}" is not a variable of this behavior.`, 'warning');
}
}
/** Typing a built-in property's name turns the port into that property, kind included. */
function customName(host: InspectorHost, assetId: string, node: GraphNode, port: DeclaredPort): TextField {
return new TextField(port.key, {
width: NAME_WIDTH,
onChange: (text) => {
const next = text.trim();
const builtIn = objectPropertyDef(next);
host.state.execute(
builtIn
? new RepointPortCommand(assetId, node.id, port.key, propertyPortOf(builtIn))
: new RenamePortCommand(assetId, node.id, port.key, next),
);
},
});
}
/**
* The ports a node declares for itself: the signature of a behavior seen as a node, or the fields
* of a struct. A start node's are named from the caller's side – what it hands into the graph is