game-maker / suite (push) Failing after 8m59s
This reverses the design of the previous commit. `PropertyDef.scope` is gone without replacement: a behavior variable is an object property again, without exception, and sharing is something you point at rather than something you declare twice. A **Variables asset** is declarations and nothing else -- no graph, no position, never placed, no tab of its own (`TAB_OF_ASSET` is already `Partial<>`, so that costs no line). A `Variables` node names one through its new `variablesAsset` setting, empty meaning the behavior's own variables as before, and then reads and writes one value per key for the whole run. Its property list follows the chosen source, which was the actual request: a value another graph declared is now offerable, where before every participant had to declare the name itself. That deletes more than it adds (-72 lines net): `globalConflicts` and its run-stopping kind clash, `SetVariableScopeCommand`, the scope picker, both object-inspector filters and the "shared by the whole run" note on the placed object. A typo is no longer a silent second variable -- naming an asset is picking from a list. And two Variables assets may carry the same key without meeting, because the asset is the key, not the name. The one shape it copies is the one that already existed: `Set state` picks its machine the same way. Lifetime is unchanged. The bag is built at `BehaviorSystem.start()` from the declarations and never written back, so playing in the editor cannot change the project -- which is the whole reason a counter must not be an object property (`World.syncToModel` would `commit()` it, and the coin counter would stand at 7 for good). Full symmetry across that gap, without mirroring anything. Two named wires: `Inspector.liveVariables` shows the values as they stand while a run is on and falls back to the declarations when it ends, and `adoptDeclarations()` takes a key into the run *only where the document changed it since the last sync*. Without that condition any unrelated project edit would snap a running counter back to its starting value; a counter-test bends exactly that and falls over. Nothing was ever cached, incidentally: `GraphInterpreter` only pulse-caches nodes without a `read`, and the variable node has one. The wire was missing. The state machine `Scope` row is now an ordinary `enum` row through `createFieldRow`. It was hand-built from an `HStack` with a ghost dropdown and began ~34px left of every value above it; the explanations beside `Local` and `Global` are gone from the menu, the sentence below the row keeps saying it. Machine scope is otherwise untouched -- `StateMachineAsset.scope`, `SetMachineScopeCommand`, `stateBag()` and its persistence all stand, so #59 stays closed as built and `Scope` as a type now carries machines alone. Six counter-proofs, each biting at its own guard. One of them did not bite at first: the suggestion list was computed twice, in `nodeSection` and `portSection`, so only the browser test covered the request itself. It is one named function now, `variableChoices`, with a unit test of its own. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
151 lines
5.5 KiB
TypeScript
151 lines
5.5 KiB
TypeScript
/**
|
||
* One builder draws a variable on its asset (renamable) and on a placed object (names are labels).
|
||
* `NumberField extends TextField`, so the tests go by the name's widget rather than counting by type.
|
||
*/
|
||
import { describe, expect, it } from 'vitest';
|
||
import { createEmptyProject } from '../../src/core/model/Project';
|
||
import type { PropertyDef, StructDef } from '../../src/core/properties/PropertySchema';
|
||
import { HStack, Label, TextField, Theme, VStack, type Widget } from '../../src/ui';
|
||
import { type FieldHandlers, labelledRow } from '../../src/editor/inspector/fieldFactory';
|
||
import type { InspectorHost } from '../../src/editor/inspector/host';
|
||
import { buildVariable, type VariableEditing } from '../../src/editor/inspector/variableRows';
|
||
import { EditorState } from '../../src/editor/state';
|
||
|
||
const STRUCT: StructDef = {
|
||
key: 'piece',
|
||
label: 'piece',
|
||
kind: 'struct',
|
||
default: { x: 0, tag: '' },
|
||
fields: [
|
||
{ key: 'x', kind: 'number' },
|
||
{ key: 'tag', kind: 'text' },
|
||
],
|
||
};
|
||
|
||
const VALUE = { x: 30, tag: 'tee' };
|
||
|
||
const PLAIN: PropertyDef[] = [
|
||
{ key: 'speed', label: 'speed', kind: 'number', default: 0 },
|
||
{ key: 'title', label: 'title', kind: 'text', default: '' },
|
||
{ key: 'alive', label: 'alive', kind: 'boolean', default: true },
|
||
{ key: 'cells', label: 'cells', kind: 'list', default: [] },
|
||
{ key: 'a_rather_long_variable_name', label: 'a_rather_long_variable_name', kind: 'number', default: 0 },
|
||
];
|
||
|
||
function makeHost(): InspectorHost {
|
||
const state = new EditorState();
|
||
state.loadProject(createEmptyProject('p', 'P'), 1);
|
||
return {
|
||
state,
|
||
body: new VStack(),
|
||
fields: new Map(),
|
||
nameField: null,
|
||
project: () => state.project,
|
||
liveVariables: () => null,
|
||
};
|
||
}
|
||
|
||
const noHandlers = (): FieldHandlers => ({ onInput: () => {}, onChange: () => {} });
|
||
const withFields: FieldHandlers = { ...noHandlers(), structFieldHandlers: noHandlers };
|
||
|
||
const EDIT: VariableEditing = {
|
||
rename: () => {},
|
||
remove: () => {},
|
||
renameField: () => {},
|
||
removeField: () => {},
|
||
addField: () => {},
|
||
};
|
||
|
||
const rows = (host: InspectorHost): Widget[][] =>
|
||
host.body.children.map((row) => ('children' in row ? (row.children as Widget[]) : []));
|
||
|
||
/** Every widget of the body that carries `text` as its own content, name or value. */
|
||
const carrying = (host: InspectorHost, text: string): Widget[] =>
|
||
rows(host)
|
||
.flat()
|
||
.filter((w) => (w instanceof Label && w.text === text) || (w instanceof TextField && w.value === text));
|
||
|
||
const isName = (w: Widget, name: string) =>
|
||
(w instanceof Label && w.text === name) || (w instanceof TextField && w.value === name);
|
||
|
||
/** Where the widget after the name starts – summed from the measured cells before it. */
|
||
function valueX(host: InspectorHost, name: string): number {
|
||
for (const row of host.body.children) {
|
||
if (!(row instanceof HStack)) continue;
|
||
const items = row.items;
|
||
const at = items.findIndex((w) => isName(w, name));
|
||
if (at < 0) continue;
|
||
return items.slice(0, at + 1).reduce((x, w) => x + w.measure().w + row.gap, row.padding);
|
||
}
|
||
throw new Error(`no row named ${name}`);
|
||
}
|
||
|
||
function buildAll(edit?: VariableEditing): InspectorHost {
|
||
const host = makeHost();
|
||
for (const def of PLAIN) buildVariable(host, def, def.default, noHandlers(), { edit });
|
||
buildVariable(host, STRUCT, VALUE, withFields, { edit });
|
||
return host;
|
||
}
|
||
|
||
const NAMES = [...PLAIN.map((d) => d.key), 'x', 'tag'];
|
||
|
||
describe('the column grid', () => {
|
||
it('starts every value at the same x on a placed object, whatever the kind or depth', () => {
|
||
const host = buildAll();
|
||
const xs = NAMES.map((name) => valueX(host, name));
|
||
expect(new Set(xs), JSON.stringify(xs)).toEqual(new Set([xs[0]]));
|
||
});
|
||
|
||
it('and at that same x on the asset, where the names are editable fields', () => {
|
||
const placed = valueX(buildAll(), 'speed');
|
||
const host = buildAll(EDIT);
|
||
const xs = NAMES.map((name) => valueX(host, name));
|
||
expect(xs, JSON.stringify(xs)).toEqual(NAMES.map(() => placed));
|
||
});
|
||
|
||
it('which is where a plain labelled row starts its value too', () => {
|
||
const { label } = labelledRow('Enabled');
|
||
expect(label.minWidth + Theme.gap).toBe(valueX(buildAll(), 'speed'));
|
||
});
|
||
});
|
||
|
||
describe('a struct on a placed object', () => {
|
||
it('is a head row plus one row per field', () => {
|
||
const host = makeHost();
|
||
buildVariable(host, STRUCT, VALUE, withFields);
|
||
expect(host.body.children.length).toBe(1 + STRUCT.fields.length);
|
||
});
|
||
|
||
it('names the bundle and its fields with labels, so they cannot be edited', () => {
|
||
const host = makeHost();
|
||
buildVariable(host, STRUCT, VALUE, withFields);
|
||
for (const name of ['piece', 'x', 'tag']) {
|
||
const [widget] = carrying(host, name);
|
||
expect(widget, name).toBeInstanceOf(Label);
|
||
}
|
||
});
|
||
|
||
it('still shows the values in editable fields', () => {
|
||
const host = makeHost();
|
||
buildVariable(host, STRUCT, VALUE, withFields);
|
||
expect(carrying(host, 'tee')[0]).toBeInstanceOf(TextField);
|
||
});
|
||
});
|
||
|
||
describe('the same struct on the asset that declares it', () => {
|
||
it('names the bundle and its fields with text fields, so they can be renamed', () => {
|
||
const host = makeHost();
|
||
buildVariable(host, STRUCT, VALUE, withFields, { edit: EDIT });
|
||
for (const name of ['piece', 'x', 'tag']) {
|
||
const [widget] = carrying(host, name);
|
||
expect(widget, name).toBeInstanceOf(TextField);
|
||
}
|
||
});
|
||
|
||
it('keeps the same row count as the placed one', () => {
|
||
const host = makeHost();
|
||
buildVariable(host, STRUCT, VALUE, withFields, { edit: EDIT });
|
||
expect(host.body.children.length).toBe(1 + STRUCT.fields.length);
|
||
});
|
||
});
|