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:
1 parent
e9fa592078
commit
854ae2e756
35 files changed
+1044
-101
No files matched your search
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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 : [];
|
||||
}
|
||||
Reference in new issue
Block a user