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
@@ -111,7 +111,7 @@ describe('the enabled switch', () => {
|
||||
world.tick(0.1);
|
||||
expect(entity.props.ticks).toBe(0); // off: no On start, no ticks
|
||||
|
||||
entity.props.enabled = true; // what "Set enabled" does
|
||||
entity.props.enabled = true; // what Properties with `enabled` set does
|
||||
world.tick(0.1);
|
||||
expect(entity.props.ticks).toBe(2); // On start plus this frame's On update
|
||||
world.tick(0.1);
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { GraphInterpreter, OBJECT_BEHAVIOR_PARAM, OBJECT_TYPE_PARAM } from '../../src/core/graph';
|
||||
import type { BehaviorAsset } from '../../src/core/model/Asset';
|
||||
import { behaviorLookup } from '../../src/core/model/behaviorPrograms';
|
||||
import { findAsset } from '../../src/core/model/Project';
|
||||
import type { PropertyDef } from '../../src/core/properties/PropertySchema';
|
||||
import { pinPropertyTarget } from '../../src/core/store/legacyNodes';
|
||||
import { deserializeProject, serializeProject } from '../../src/core/store/serialize';
|
||||
import { fakeContext, fakeEntity, makeBehavior, makeProject } from '../helpers';
|
||||
|
||||
type RawNode = { id: string; type: string; params?: Record<string, unknown>; ports?: unknown[] };
|
||||
type RawEdge = [from: string, fromPort: string, to: string, toPort: string];
|
||||
|
||||
/** A saved project whose behavior "B" holds `nodes` and `edges` exactly as an older editor wrote them. */
|
||||
function loadOld(nodes: RawNode[], edges: RawEdge[]) {
|
||||
const fixture = makeProject();
|
||||
const behavior = makeBehavior(fixture, 'B', ['seen']);
|
||||
const game = makeBehavior(fixture, 'Game', ['coins']);
|
||||
const raw = serializeProject(fixture.project) as { assets: Record<string, unknown>[] };
|
||||
raw.assets.find((a) => a.id === behavior.id)!.graph = {
|
||||
nodes: nodes.map((n) => ({ x: 0, y: 0, params: {}, ...n })),
|
||||
edges: edges.map(([from, fromPort, to, toPort], i) => ({
|
||||
id: `e${i}`,
|
||||
from: { node: from, port: fromPort },
|
||||
to: { node: to, port: toPort },
|
||||
})),
|
||||
};
|
||||
const project = deserializeProject(raw);
|
||||
return { project, game, behavior: findAsset(project, behavior.id) as BehaviorAsset };
|
||||
}
|
||||
|
||||
const node = (behavior: BehaviorAsset, id: string) => behavior.graph.nodes.find((n) => n.id === id)!;
|
||||
const edges = (behavior: BehaviorAsset) =>
|
||||
behavior.graph.edges.map((e) => `${e.from.node}.${e.from.port}>${e.to.node}.${e.to.port}`);
|
||||
|
||||
describe('property nodes of an older file', () => {
|
||||
it('load as Properties nodes that compute what the old graph computed', () => {
|
||||
const { project, behavior } = loadOld(
|
||||
[
|
||||
{ id: 'start', type: 'OnStart' },
|
||||
{ id: 'self', type: 'Self' },
|
||||
{ id: 'paint', type: 'SetProperty', params: { property: 'color', value: 0xff0000 } },
|
||||
{
|
||||
id: 'size',
|
||||
type: 'SetProperties',
|
||||
params: { width: 40 },
|
||||
ports: [{ key: 'width', kind: 'number' }],
|
||||
},
|
||||
{ id: 'get', type: 'GetProperty', params: { property: 'width' } },
|
||||
{ id: 'store', type: 'Variables', ports: [{ key: 'seen', kind: 'number', write: true }] },
|
||||
],
|
||||
[
|
||||
['start', 'exec', 'paint', 'exec'],
|
||||
['self', 'object', 'paint', 'object'],
|
||||
['paint', 'exec', 'size', 'exec'],
|
||||
['self', 'object', 'size', 'object'],
|
||||
['size', 'exec', 'store', 'exec'],
|
||||
['self', 'object', 'get', 'object'],
|
||||
['get', 'value', 'store', 'seen'],
|
||||
],
|
||||
);
|
||||
expect(['paint', 'size', 'get'].map((id) => node(behavior, id).type)).toEqual([
|
||||
'Properties',
|
||||
'Properties',
|
||||
'Properties',
|
||||
]);
|
||||
expect(edges(behavior)).toContain('get.width>store.seen');
|
||||
const self = { ...fakeEntity('self'), props: { width: 10, color: 0 } };
|
||||
const ctx = fakeContext({ self, variables: { seen: 0 } });
|
||||
new GraphInterpreter(behavior.graph, behaviorLookup(project)).fire('OnStart', ctx);
|
||||
expect({ ...self.props, seen: ctx.variables.seen }).toEqual({ width: 40, color: 0xff0000, seen: 40 });
|
||||
});
|
||||
|
||||
it('keep whether they wrote and their typed-in value, under the kind of the property', () => {
|
||||
const { behavior, game } = loadOld(
|
||||
[
|
||||
{ id: 'paint', type: 'SetProperty', params: { property: 'color', value: 7 } },
|
||||
{ id: 'coins', type: 'SetProperty', params: { property: 'coins', value: 0 } },
|
||||
{ id: 'speed', type: 'GetProperty', params: { property: 'speed' } },
|
||||
],
|
||||
[],
|
||||
);
|
||||
expect(node(behavior, 'paint')).toMatchObject({
|
||||
params: { [OBJECT_TYPE_PARAM]: 'sprite', color: 7 },
|
||||
ports: [{ key: 'color', kind: 'number', write: true }],
|
||||
});
|
||||
expect(node(behavior, 'coins')).toMatchObject({
|
||||
params: { [OBJECT_TYPE_PARAM]: 'behavior', [OBJECT_BEHAVIOR_PARAM]: game.id, coins: 0 },
|
||||
ports: [{ key: 'coins', kind: 'number', write: true }],
|
||||
});
|
||||
// Nothing declares `speed`, so nothing says its kind.
|
||||
expect(node(behavior, 'speed').ports).toEqual([{ key: 'speed', kind: 'text' }]);
|
||||
expect(node(behavior, 'speed').params).toEqual({});
|
||||
});
|
||||
|
||||
it('turn Set enabled into Properties writing enabled, on unless told otherwise', () => {
|
||||
const { project, behavior } = loadOld(
|
||||
[
|
||||
{ id: 'start', type: 'OnStart' },
|
||||
{ id: 'self', type: 'Self' },
|
||||
{ id: 'screen', type: 'ObjectByName', params: { name: 'screen' } },
|
||||
{ id: 'no', type: 'Boolean', params: { value: false } },
|
||||
{ id: 'hide', type: 'SetEnabled' },
|
||||
{ id: 'show', type: 'SetEnabled', params: { enabled: false } },
|
||||
],
|
||||
[
|
||||
['start', 'exec', 'hide', 'exec'],
|
||||
['self', 'object', 'hide', 'object'],
|
||||
['no', 'value', 'hide', 'enabled'],
|
||||
['hide', 'exec', 'show', 'exec'],
|
||||
['screen', 'object', 'show', 'object'],
|
||||
],
|
||||
);
|
||||
expect(node(behavior, 'show')).toMatchObject({
|
||||
type: 'Properties',
|
||||
params: { enabled: false },
|
||||
ports: [{ key: 'enabled', kind: 'boolean', write: true }],
|
||||
});
|
||||
expect(node(behavior, 'hide').params).toEqual({ enabled: true });
|
||||
expect(edges(behavior)).toEqual([
|
||||
'start.exec>hide.exec',
|
||||
'self.object>hide.object',
|
||||
'no.value>hide.enabled',
|
||||
'hide.exec>show.exec',
|
||||
'screen.object>show.object',
|
||||
]);
|
||||
const self = { ...fakeEntity('self'), props: { enabled: true } };
|
||||
const screen = { ...fakeEntity('screen'), props: { enabled: true } };
|
||||
const ctx = fakeContext({ self, findByName: (name) => (name === 'screen' ? screen : null) });
|
||||
new GraphInterpreter(behavior.graph, behaviorLookup(project)).fire('OnStart', ctx);
|
||||
expect([self.props.enabled, screen.props.enabled]).toEqual([false, false]);
|
||||
});
|
||||
|
||||
it('turn Set text into Text properties writing text, empty unless told otherwise', () => {
|
||||
const { project, behavior } = loadOld(
|
||||
[
|
||||
{ id: 'start', type: 'OnStart' },
|
||||
{ id: 'a', type: 'ObjectByName', params: { name: 'a' } },
|
||||
{ id: 'b', type: 'ObjectByName', params: { name: 'b' } },
|
||||
{ id: 'c', type: 'ObjectByName', params: { name: 'c' } },
|
||||
{ id: 'num', type: 'Number', params: { value: 12 } },
|
||||
{ id: 'blank', type: 'SetText' },
|
||||
{ id: 'typed', type: 'SetText', params: { text: 'Go' } },
|
||||
{ id: 'fed', type: 'SetText' },
|
||||
],
|
||||
[
|
||||
['start', 'exec', 'blank', 'exec'],
|
||||
['a', 'object', 'blank', 'object'],
|
||||
['blank', 'exec', 'typed', 'exec'],
|
||||
['b', 'object', 'typed', 'object'],
|
||||
['typed', 'exec', 'fed', 'exec'],
|
||||
['c', 'object', 'fed', 'object'],
|
||||
['num', 'value', 'fed', 'text'],
|
||||
],
|
||||
);
|
||||
expect(node(behavior, 'typed')).toMatchObject({
|
||||
type: 'Properties',
|
||||
params: { [OBJECT_TYPE_PARAM]: 'text', text: 'Go' },
|
||||
ports: [{ key: 'text', kind: 'text', write: true }],
|
||||
});
|
||||
expect(node(behavior, 'blank').params).toEqual({ [OBJECT_TYPE_PARAM]: 'text', text: '' });
|
||||
expect(edges(behavior)).toContain('num.value>fed.text');
|
||||
const texts = Object.fromEntries(
|
||||
['a', 'b', 'c'].map((name) => [name, { ...fakeEntity(name), props: { text: 'old' } }]),
|
||||
);
|
||||
const ctx = fakeContext({ findByName: (name) => texts[name] ?? null });
|
||||
new GraphInterpreter(behavior.graph, behaviorLookup(project)).fire('OnStart', ctx);
|
||||
expect(['a', 'b', 'c'].map((name) => texts[name]!.props.text)).toEqual(['', 'Go', '12']);
|
||||
});
|
||||
|
||||
it('keep only the object input of one that named no property', () => {
|
||||
const { behavior } = loadOld(
|
||||
[
|
||||
{ id: 'start', type: 'OnStart' },
|
||||
{ id: 'self', type: 'Self' },
|
||||
{ id: 'num', type: 'Number' },
|
||||
{ id: 'blank', type: 'SetProperty', params: { value: 5 } },
|
||||
],
|
||||
[
|
||||
['start', 'exec', 'blank', 'exec'],
|
||||
['self', 'object', 'blank', 'object'],
|
||||
['num', 'value', 'blank', 'value'],
|
||||
],
|
||||
);
|
||||
expect(node(behavior, 'blank')).toMatchObject({ type: 'Properties', params: {} });
|
||||
expect(node(behavior, 'blank').ports).toBeUndefined();
|
||||
expect(edges(behavior)).toEqual(['self.object>blank.object']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('the type an old node is pinned to', () => {
|
||||
const variable = (key: string): PropertyDef => ({ key, label: key, kind: 'number', default: 0 });
|
||||
const behaviors = new Map([
|
||||
['game', [variable('coins'), variable('lives')]],
|
||||
['hud', [variable('lives')]],
|
||||
]);
|
||||
const pinned = (keys: string[]) => pinPropertyTarget(keys, behaviors).params;
|
||||
|
||||
it('is the first object type holding every key', () => {
|
||||
expect(pinned(['color'])).toEqual({ [OBJECT_TYPE_PARAM]: 'sprite' });
|
||||
expect(pinned(['color', 'align'])).toEqual({ [OBJECT_TYPE_PARAM]: 'text' });
|
||||
expect(pinned(['width', 'zoom'])).toEqual({ [OBJECT_TYPE_PARAM]: 'camera' });
|
||||
});
|
||||
|
||||
it('else the one behavior declaring them all', () => {
|
||||
expect(pinned(['coins', 'lives'])).toEqual({
|
||||
[OBJECT_TYPE_PARAM]: 'behavior',
|
||||
[OBJECT_BEHAVIOR_PARAM]: 'game',
|
||||
});
|
||||
});
|
||||
|
||||
it('else Any: every object has it, nothing declares it, two behaviors do, or nothing to go by', () => {
|
||||
expect(pinPropertyTarget(['enabled'], behaviors)).toMatchObject({
|
||||
params: {},
|
||||
defs: [{ kind: 'boolean' }],
|
||||
});
|
||||
expect(pinned(['enabled', 'color'])).toEqual({ [OBJECT_TYPE_PARAM]: 'sprite' });
|
||||
expect(pinned(['speed'])).toEqual({});
|
||||
expect(pinned(['lives'])).toEqual({});
|
||||
expect(pinned([])).toEqual({});
|
||||
});
|
||||
});
|
||||
+19
-107
@@ -125,6 +125,25 @@ const CASES: Record<string, Case[]> = {
|
||||
after: (ctx) => expect(ctx.variables).toEqual({ speed: 100 }),
|
||||
},
|
||||
],
|
||||
Properties: [
|
||||
{
|
||||
name: 'writes the properties switched to Set, each keeping the type it already had',
|
||||
ports: [
|
||||
{ key: 'width', kind: 'number', write: true },
|
||||
{ key: 'text', kind: 'text', write: true },
|
||||
{ key: 'height', kind: 'number' },
|
||||
],
|
||||
inputs: { object: painted, width: '250', text: 7 },
|
||||
next: 'exec',
|
||||
after: () => expect(painted.props).toEqual({ width: 250, text: '7' }),
|
||||
},
|
||||
{
|
||||
name: 'no object is a no-op',
|
||||
ports: [{ key: 'width', kind: 'number', write: true }],
|
||||
inputs: { object: null, width: 5 },
|
||||
next: 'exec',
|
||||
},
|
||||
],
|
||||
|
||||
// references and properties
|
||||
|
||||
@@ -180,43 +199,6 @@ const CASES: Record<string, Case[]> = {
|
||||
},
|
||||
{ name: 'no object is off', inputs: { object: null }, outputs: { result: false, own: false } },
|
||||
],
|
||||
GetProperties: [
|
||||
{
|
||||
name: 'reads one output per property it names',
|
||||
ports: [
|
||||
{ key: 'width', kind: 'number' },
|
||||
{ key: 'speed', kind: 'number' },
|
||||
],
|
||||
inputs: { object: fakeHandle({ width: 42, speed: 3 }) },
|
||||
outputs: { width: 42, speed: 3 },
|
||||
},
|
||||
{
|
||||
name: 'an absent property reads as null',
|
||||
ports: [{ key: 'radius', kind: 'number' }],
|
||||
inputs: { object: fakeHandle({}) },
|
||||
outputs: { radius: null },
|
||||
},
|
||||
{
|
||||
name: 'no object reads null for every port',
|
||||
ports: [{ key: 'width', kind: 'number' }],
|
||||
inputs: { object: null },
|
||||
outputs: { width: null },
|
||||
},
|
||||
],
|
||||
GetProperty: [
|
||||
{
|
||||
name: 'reads a property',
|
||||
inputs: { object: fakeHandle({ width: 42 }) },
|
||||
params: { property: 'width' },
|
||||
outputs: { value: 42 },
|
||||
},
|
||||
{
|
||||
name: 'an absent property is null',
|
||||
inputs: { object: fakeHandle({}) },
|
||||
params: { property: 'width' },
|
||||
outputs: { value: null },
|
||||
},
|
||||
],
|
||||
GetPosition: [
|
||||
{
|
||||
name: 'local and world position of a top-level object are the same',
|
||||
@@ -599,24 +581,6 @@ const CASES: Record<string, Case[]> = {
|
||||
outputs: { blockedX: false, blockedY: false },
|
||||
},
|
||||
],
|
||||
SetProperties: [
|
||||
{
|
||||
name: 'writes every property it names, each keeping the type it already had',
|
||||
ports: [
|
||||
{ key: 'width', kind: 'number' },
|
||||
{ key: 'text', kind: 'text' },
|
||||
],
|
||||
inputs: { object: painted, width: '250', text: 7 },
|
||||
next: 'exec',
|
||||
after: () => expect(painted.props).toEqual({ width: 250, text: '7' }),
|
||||
},
|
||||
{
|
||||
name: 'no object is a no-op',
|
||||
ports: [{ key: 'width', kind: 'number' }],
|
||||
inputs: { object: null, width: 5 },
|
||||
next: 'exec',
|
||||
},
|
||||
],
|
||||
SetPosition: [
|
||||
{
|
||||
name: 'places the object and continues',
|
||||
@@ -625,34 +589,6 @@ const CASES: Record<string, Case[]> = {
|
||||
},
|
||||
{ name: 'no object is a no-op', inputs: { object: null, x: 1, y: 1 }, next: 'exec' },
|
||||
],
|
||||
SetProperty: [
|
||||
{
|
||||
name: 'writes a property, keeping the existing type',
|
||||
inputs: { object: fakeHandle({ width: 100 }), value: '250' },
|
||||
params: { property: 'width' },
|
||||
next: 'exec',
|
||||
},
|
||||
{
|
||||
name: 'no property name chosen writes nothing',
|
||||
inputs: { object: fakeHandle({ width: 100 }), value: 5 },
|
||||
next: 'exec',
|
||||
},
|
||||
],
|
||||
SetEnabled: [
|
||||
{
|
||||
name: 'switches an object off',
|
||||
inputs: { object: fakeHandle({ enabled: true }), enabled: false },
|
||||
next: 'exec',
|
||||
},
|
||||
{ name: 'no object is a no-op', inputs: { object: null, enabled: false }, next: 'exec' },
|
||||
],
|
||||
SetText: [
|
||||
{
|
||||
name: 'writes the content of a text object',
|
||||
inputs: { object: fakeHandle({ text: 'a' }), text: 12 },
|
||||
next: 'exec',
|
||||
},
|
||||
],
|
||||
Spawn: [
|
||||
{
|
||||
name: 'no asset chosen logs a hint and continues with no object',
|
||||
@@ -773,30 +709,6 @@ describe('action nodes write through to the object they are given', () => {
|
||||
expect({ x: target.x, y: target.y }).toEqual({ x: 30, y: -40 });
|
||||
});
|
||||
|
||||
it('SetProperty keeps the type the property already had', () => {
|
||||
const target = fakeHandle({ width: 100 });
|
||||
runDef('SetProperty', fakeContext(), { object: target, value: '250' }, { property: 'width' });
|
||||
expect(target.props.width).toBe(250);
|
||||
});
|
||||
|
||||
it('SetProperty without a property name writes nothing', () => {
|
||||
const target = fakeHandle({ width: 100 });
|
||||
runDef('SetProperty', fakeContext(), { object: target, value: 5 }, {});
|
||||
expect(target.props).toEqual({ width: 100 });
|
||||
});
|
||||
|
||||
it('SetEnabled switches the object off', () => {
|
||||
const target = fakeHandle({ enabled: true });
|
||||
runDef('SetEnabled', fakeContext(), { object: target, enabled: false }, {});
|
||||
expect(target.props.enabled).toBe(false);
|
||||
});
|
||||
|
||||
it('SetText writes the text property, stringifying numbers', () => {
|
||||
const target = fakeHandle({ text: 'a' });
|
||||
runDef('SetText', fakeContext(), { object: target, text: 12 }, {});
|
||||
expect(target.props.text).toBe('12');
|
||||
});
|
||||
|
||||
it('Destroy hands the object to the context', () => {
|
||||
const removed: string[] = [];
|
||||
const ctx = fakeContext({ destroy: (target) => removed.push(target.name) });
|
||||
|
||||
@@ -1,146 +1,219 @@
|
||||
/**
|
||||
* Get/Set properties: the plural of Get/Set property, built like Get/Set variables. A port naming a
|
||||
* built-in property carries that property – its default and its field – while a custom name is a
|
||||
* plain port for a variable of another behavior's object.
|
||||
* Properties: the Variables node for the object it is handed. Its type decides what a port carries:
|
||||
* a property of that type keeps its own default and field, Any names plain ports of a picked kind.
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { CommandError } from '../../src/core/commands/Command';
|
||||
import { AddPortCommand, RenamePortCommand } from '../../src/core/commands/portCommands';
|
||||
import { RenameVariableCommand } from '../../src/core/commands/variableCommands';
|
||||
import {
|
||||
GET_PROPERTIES,
|
||||
type DeclaredPort,
|
||||
GraphInterpreter,
|
||||
inlineInputDef,
|
||||
NO_BEHAVIORS,
|
||||
OBJECT_BEHAVIOR_PARAM,
|
||||
OBJECT_TYPE_PARAM,
|
||||
propertyPort,
|
||||
requireNodeDef,
|
||||
resolveDef,
|
||||
SET_PROPERTIES,
|
||||
} from '../../src/core/graph';
|
||||
import type { BehaviorAsset } from '../../src/core/model/Asset';
|
||||
import { behaviorLookup } from '../../src/core/model/behaviorPrograms';
|
||||
import { OBJECT_PROPERTIES, OBJECT_PROPERTY_CHOICES } from '../../src/core/properties/PropertySchema';
|
||||
import type { PropertyBag } from '../../src/core/model/Properties';
|
||||
import { fakeContext, fakeEntity, graphEditor, makeBehavior, makeProject, roundTrip } from '../helpers';
|
||||
|
||||
/** Self's `width` and `color` are set with nothing wired into them, then read back into `seen`. */
|
||||
function typedIn(params: Record<string, number> = {}) {
|
||||
const SPRITE = { [OBJECT_TYPE_PARAM]: 'sprite' };
|
||||
const set = (key: string): DeclaredPort => ({ key, kind: 'number', write: true });
|
||||
|
||||
/** `On start → Properties{…} on self`, its `width` output then stored in `seen`. */
|
||||
function onSelf(ports: DeclaredPort[], params: PropertyBag = SPRITE) {
|
||||
const fixture = makeProject();
|
||||
const behavior = makeBehavior(fixture, 'B', ['seen']);
|
||||
const edit = graphEditor(fixture, behavior);
|
||||
const start = edit.addNode('OnStart');
|
||||
const self = edit.addNode('Self');
|
||||
const set = edit.addNode(SET_PROPERTIES, params);
|
||||
const get = edit.addNode(GET_PROPERTIES);
|
||||
const props = edit.properties(ports, params);
|
||||
const store = edit.variables(['seen'], { write: true });
|
||||
for (const key of ['width', 'color'])
|
||||
fixture.stack.execute(new AddPortCommand(behavior.id, set.id, { key, kind: 'number' }));
|
||||
fixture.stack.execute(new AddPortCommand(behavior.id, get.id, { key: 'width', kind: 'number' }));
|
||||
edit.wire([
|
||||
[start, 'exec', set, 'exec'],
|
||||
[self, 'object', set, 'object'],
|
||||
[set, 'exec', store, 'exec'],
|
||||
[self, 'object', get, 'object'],
|
||||
[get, 'width', store, 'seen'],
|
||||
[start, 'exec', props, 'exec'],
|
||||
[self, 'object', props, 'object'],
|
||||
[props, 'exec', store, 'exec'],
|
||||
[props, 'width', store, 'seen'],
|
||||
]);
|
||||
return { fixture, behavior, set, get };
|
||||
return { fixture, behavior, props };
|
||||
}
|
||||
|
||||
function run(behavior: BehaviorAsset, fixture: ReturnType<typeof makeProject>) {
|
||||
const self = { ...fakeEntity('self'), props: { width: 10, color: 0 } };
|
||||
function run(behavior: BehaviorAsset, fixture: ReturnType<typeof makeProject>, props: PropertyBag) {
|
||||
const self = { ...fakeEntity('self'), props: { ...props } };
|
||||
const ctx = fakeContext({ self, variables: { seen: 0 } });
|
||||
new GraphInterpreter(behavior.graph, behaviorLookup(fixture.project)).fire('OnStart', ctx);
|
||||
return { props: self.props, seen: ctx.variables.seen };
|
||||
}
|
||||
|
||||
describe('a node that reads or writes several properties at once', () => {
|
||||
it("writes an unconnected input as the property's own default, not as a bare 0", () => {
|
||||
const { fixture, behavior } = typedIn();
|
||||
expect(run(behavior, fixture)).toEqual({ props: { width: 100, color: 0xffffff }, seen: 100 });
|
||||
describe('a node that reads and writes properties of an object', () => {
|
||||
it("writes an unconnected input as its property's default, and reads it back after", () => {
|
||||
const { fixture, behavior } = onSelf([set('width'), set('color')]);
|
||||
expect(run(behavior, fixture, { width: 10, color: 0 })).toEqual({
|
||||
props: { width: 100, color: 0xffffff },
|
||||
seen: 100,
|
||||
});
|
||||
});
|
||||
|
||||
it('writes the value typed on the node once there is one', () => {
|
||||
const { fixture, behavior } = typedIn({ width: 64 });
|
||||
expect(run(behavior, fixture).props.width).toBe(64);
|
||||
const { fixture, behavior, props } = onSelf([set('width')]);
|
||||
props.params.width = 64;
|
||||
expect(run(behavior, fixture, { width: 10 }).props.width).toBe(64);
|
||||
});
|
||||
|
||||
it('edits a typed-in value with the field of the property it names', () => {
|
||||
expect(inlineInputDef(propertyPort({ key: 'color', kind: 'number' })).kind).toBe('color');
|
||||
expect(inlineInputDef(propertyPort({ key: 'align', kind: 'text' })).kind).toBe('enum');
|
||||
expect(inlineInputDef(propertyPort({ key: 'speed', kind: 'number' })).kind).toBe('number');
|
||||
// A kind the property does not travel as (a hand-edited file) is left a plain port.
|
||||
expect(propertyPort({ key: 'width', kind: 'text' }).field).toBeUndefined();
|
||||
});
|
||||
|
||||
it('gives a custom name the zero it writes untyped, so the node shows a value', () => {
|
||||
expect(propertyPort({ key: 'speed', kind: 'number' }).default).toBe(0);
|
||||
expect(propertyPort({ key: 'ready', kind: 'boolean' }).default).toBe(false);
|
||||
});
|
||||
|
||||
it('keeps the fixed object input its own, so no declared port can take its key', () => {
|
||||
const { fixture, behavior, set, get } = typedIn();
|
||||
const add = (nodeId: string) =>
|
||||
new AddPortCommand(behavior.id, nodeId, { key: 'object', kind: 'number' });
|
||||
expect(() => fixture.stack.execute(add(set.id))).toThrow(CommandError);
|
||||
expect(() =>
|
||||
fixture.stack.execute(new RenamePortCommand(behavior.id, set.id, 'width', 'object')),
|
||||
).toThrow(CommandError);
|
||||
// Get properties has no *output* called object, so there the name is free.
|
||||
fixture.stack.execute(add(get.id));
|
||||
const keys = resolveDef(set, behavior.graph, () => null)!.inputs.map((p) => p.key);
|
||||
expect(keys).toEqual(['exec', 'object', 'width', 'color']);
|
||||
});
|
||||
|
||||
it('drops a port from a file that would shadow the object input', () => {
|
||||
const { fixture, behavior, set } = typedIn();
|
||||
set.ports = [...(set.ports ?? []), { key: 'object', kind: 'number' }];
|
||||
const loaded = roundTrip(fixture.project);
|
||||
const asset = loaded.assets.find((a) => a.id === behavior.id) as BehaviorAsset;
|
||||
const node = asset.graph.nodes.find((n) => n.id === set.id)!;
|
||||
expect((node.ports ?? []).map((p) => p.key)).toEqual(['width', 'color']);
|
||||
});
|
||||
|
||||
it('does not follow a variable that happens to share a property name', () => {
|
||||
it('reads a property at the moment it is asked, so one node counts it up', () => {
|
||||
const fixture = makeProject();
|
||||
const behavior = makeBehavior(fixture, 'B', ['width']);
|
||||
const set = graphEditor(fixture, behavior).addNode(SET_PROPERTIES);
|
||||
fixture.stack.execute(new AddPortCommand(behavior.id, set.id, { key: 'width', kind: 'number' }));
|
||||
fixture.stack.execute(new RenameVariableCommand(behavior.id, 'width', 'w'));
|
||||
expect((set.ports ?? []).map((p) => p.key)).toEqual(['width']);
|
||||
const behavior = makeBehavior(fixture, 'B');
|
||||
const edit = graphEditor(fixture, behavior);
|
||||
const start = edit.addNode('OnStart');
|
||||
const self = edit.addNode('Self');
|
||||
const coins = edit.properties([set('coins')]);
|
||||
const add = edit.addNode('Add', { b: 1 });
|
||||
edit.wire([
|
||||
[start, 'exec', coins, 'exec'],
|
||||
[self, 'object', coins, 'object'],
|
||||
[coins, 'coins', add, 'a'],
|
||||
[add, 'result', coins, 'coins'],
|
||||
]);
|
||||
const self_ = { ...fakeEntity('self'), props: { coins: 5 } };
|
||||
const interpreter = new GraphInterpreter(behavior.graph, behaviorLookup(fixture.project));
|
||||
for (let i = 0; i < 2; i++) interpreter.fire('OnStart', fakeContext({ self: self_ }));
|
||||
expect(self_.props.coins).toBe(7);
|
||||
});
|
||||
|
||||
it('switches an object off and, left unconnected, on – what Set enabled did', () => {
|
||||
const switched = (typed: PropertyBag, before: boolean) => {
|
||||
const fixture = makeProject();
|
||||
const behavior = makeBehavior(fixture, 'B');
|
||||
const edit = graphEditor(fixture, behavior);
|
||||
const start = edit.addNode('OnStart');
|
||||
const self = edit.addNode('Self');
|
||||
const node = edit.properties([{ key: 'enabled', kind: 'boolean', write: true }], {
|
||||
[OBJECT_TYPE_PARAM]: 'container',
|
||||
});
|
||||
Object.assign(node.params, typed);
|
||||
edit.wire([
|
||||
[start, 'exec', node, 'exec'],
|
||||
[self, 'object', node, 'object'],
|
||||
]);
|
||||
return run(behavior, fixture, { enabled: before }).props.enabled;
|
||||
};
|
||||
expect([switched({ enabled: false }, true), switched({}, false)]).toEqual([false, true]);
|
||||
});
|
||||
|
||||
it('has flow pins only while a property is set, and they sit above the object input', () => {
|
||||
const { behavior, props } = onSelf([set('width'), { key: 'height', kind: 'number' }]);
|
||||
const keys = () => {
|
||||
const def = resolveDef(props, behavior.graph, NO_BEHAVIORS)!;
|
||||
return { inputs: def.inputs.map((p) => p.key), outputs: def.outputs.map((p) => p.key) };
|
||||
};
|
||||
expect(keys()).toEqual({ inputs: ['exec', 'object', 'width'], outputs: ['exec', 'width', 'height'] });
|
||||
props.ports = props.ports!.map(({ write: _, ...p }) => p);
|
||||
expect(keys()).toEqual({ inputs: ['object'], outputs: ['width', 'height'] });
|
||||
});
|
||||
});
|
||||
|
||||
describe('the properties on offer', () => {
|
||||
it('lists each key once, with the def of a sprite where types share it', () => {
|
||||
const keys = OBJECT_PROPERTIES.map((d) => d.key);
|
||||
expect(new Set(keys).size).toBe(keys.length);
|
||||
expect(OBJECT_PROPERTIES.find((d) => d.key === 'width')).toMatchObject({ label: 'Width', min: 1 });
|
||||
expect(keys).toEqual(expect.arrayContaining(['enabled', 'color', 'texture', 'text', 'zoom', 'radius']));
|
||||
// The asset's own switches and a scene's background are not properties of a placed object.
|
||||
expect(keys).not.toContain('runInEditor');
|
||||
expect(keys).not.toContain('backgroundColor');
|
||||
describe('what a port carries, by the type of the node', () => {
|
||||
const width: DeclaredPort = { key: 'width', kind: 'number' };
|
||||
|
||||
it("takes the def of its own type, not another type's that shares the key", () => {
|
||||
expect(propertyPort(width, SPRITE)).toMatchObject({ default: 100, field: { min: 1 } });
|
||||
expect(propertyPort(width, { [OBJECT_TYPE_PARAM]: 'camera' })).toMatchObject({
|
||||
default: 640,
|
||||
field: { decimals: 0 },
|
||||
});
|
||||
});
|
||||
|
||||
it('names each entry with the type it belongs to, a shared key once per type', () => {
|
||||
const labels = OBJECT_PROPERTY_CHOICES.map((c) => c.label);
|
||||
expect(labels).toEqual(
|
||||
expect.arrayContaining([
|
||||
'Object enabled',
|
||||
'Sprite width',
|
||||
'Collider width',
|
||||
'Camera width',
|
||||
'Text align',
|
||||
]),
|
||||
it('edits a typed-in value with the field of that property', () => {
|
||||
const text = { [OBJECT_TYPE_PARAM]: 'text' };
|
||||
expect(inlineInputDef(propertyPort({ key: 'color', kind: 'number' }, text)).kind).toBe('color');
|
||||
expect(inlineInputDef(propertyPort({ key: 'align', kind: 'text' }, text)).kind).toBe('enum');
|
||||
// A kind the property does not travel as (a hand-edited file) is left a plain port.
|
||||
expect(propertyPort({ key: 'width', kind: 'text' }, SPRITE).field).toBeUndefined();
|
||||
});
|
||||
|
||||
it('keeps a port under Any plain, at the zero of its kind, whatever its name', () => {
|
||||
expect(propertyPort(width, {})).toMatchObject({ default: 0 });
|
||||
expect(propertyPort(width, {}).field).toBeUndefined();
|
||||
expect(propertyPort({ key: 'ready', kind: 'boolean' }, {}).default).toBe(false);
|
||||
});
|
||||
|
||||
it('names the node after its type, or after the behavior it points at', () => {
|
||||
const fixture = makeProject();
|
||||
const game = makeBehavior(fixture, 'Game');
|
||||
const edit = graphEditor(fixture, makeBehavior(fixture, 'Player'));
|
||||
const title = (params: PropertyBag) =>
|
||||
resolveDef(edit.properties([], params), game.graph, behaviorLookup(fixture.project))!.label;
|
||||
expect(title({})).toBe('Properties');
|
||||
expect(title(SPRITE)).toBe('Sprite properties');
|
||||
expect(title({ [OBJECT_TYPE_PARAM]: 'behavior', [OBJECT_BEHAVIOR_PARAM]: game.id })).toBe(
|
||||
'Game properties',
|
||||
);
|
||||
expect(new Set(labels).size).toBe(labels.length);
|
||||
expect(OBJECT_PROPERTY_CHOICES.find((c) => c.label === 'Camera width')?.def.key).toBe('width');
|
||||
expect(title({ [OBJECT_TYPE_PARAM]: 'behavior' })).toBe('Behavior properties');
|
||||
});
|
||||
});
|
||||
|
||||
it('offers the same entries as suggestions for the property a single Get/Set property names', () => {
|
||||
for (const type of ['GetProperty', 'SetProperty']) {
|
||||
const param = requireNodeDef(type).params.find((p) => p.key === 'property');
|
||||
expect(param?.kind === 'text' && param.suggestions).toContainEqual({
|
||||
value: 'width',
|
||||
label: 'Camera width',
|
||||
});
|
||||
describe('the keys a port cannot take', () => {
|
||||
it('keeps the object input and the settings their own', () => {
|
||||
const { fixture, behavior, props } = onSelf([set('width')]);
|
||||
for (const key of ['object', OBJECT_TYPE_PARAM, OBJECT_BEHAVIOR_PARAM]) {
|
||||
expect(() =>
|
||||
fixture.stack.execute(new AddPortCommand(behavior.id, props.id, { key, kind: 'number' })),
|
||||
).toThrow(CommandError);
|
||||
expect(() => fixture.stack.execute(new RenamePortCommand(behavior.id, props.id, 'width', key))).toThrow(
|
||||
CommandError,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it('drops a port from a file that would take one of them', () => {
|
||||
const { fixture, behavior, props } = onSelf([set('width')]);
|
||||
props.ports = [
|
||||
...props.ports!,
|
||||
{ key: 'object', kind: 'number' },
|
||||
{ key: OBJECT_TYPE_PARAM, kind: 'text' },
|
||||
];
|
||||
const loaded = roundTrip(fixture.project);
|
||||
const asset = loaded.assets.find((a) => a.id === behavior.id) as BehaviorAsset;
|
||||
const node = asset.graph.nodes.find((n) => n.id === props.id)!;
|
||||
expect((node.ports ?? []).map((p) => p.key)).toEqual(['width']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('a renamed variable', () => {
|
||||
it('pulls along the ports of Properties nodes pointing at its behavior, in every graph', () => {
|
||||
const fixture = makeProject();
|
||||
const game = makeBehavior(fixture, 'Game', ['coins']);
|
||||
const player = makeBehavior(fixture, 'Player', ['coins']);
|
||||
const edit = graphEditor(fixture, player);
|
||||
const pointing = { [OBJECT_TYPE_PARAM]: 'behavior', [OBJECT_BEHAVIOR_PARAM]: game.id };
|
||||
const ofGame = edit.properties([set('coins')], { ...pointing, coins: 3 });
|
||||
const any = edit.properties([set('coins')]);
|
||||
const ofPlayer = edit.properties([set('coins')], { ...pointing, [OBJECT_BEHAVIOR_PARAM]: player.id });
|
||||
const log = edit.addNode('Log');
|
||||
edit.connect(ofGame, 'coins', log, 'message');
|
||||
const keys = () => [ofGame, any, ofPlayer].map((n) => n.ports!.map((p) => p.key).join());
|
||||
|
||||
fixture.stack.execute(new RenameVariableCommand(game.id, 'coins', 'gold'));
|
||||
expect(keys()).toEqual(['gold', 'coins', 'coins']);
|
||||
expect(ofGame.params).toMatchObject({ gold: 3 });
|
||||
expect(player.graph.edges.map((e) => `${e.from.port}>${e.to.port}`)).toEqual(['gold>message']);
|
||||
|
||||
fixture.stack.undo();
|
||||
expect(keys()).toEqual(['coins', 'coins', 'coins']);
|
||||
expect(ofGame.params).toMatchObject({ coins: 3 });
|
||||
});
|
||||
|
||||
it('leaves a port alone whose new key the node already has', () => {
|
||||
const fixture = makeProject();
|
||||
const game = makeBehavior(fixture, 'Game', ['coins']);
|
||||
const pointing = { [OBJECT_TYPE_PARAM]: 'behavior', [OBJECT_BEHAVIOR_PARAM]: game.id };
|
||||
// `gold` is the port of a variable deleted since.
|
||||
const node = graphEditor(fixture, game).properties([set('coins'), set('gold')], pointing);
|
||||
fixture.stack.execute(new RenameVariableCommand(game.id, 'coins', 'gold'));
|
||||
expect(node.ports!.map((p) => p.key)).toEqual(['coins', 'gold']);
|
||||
});
|
||||
});
|
||||
@@ -3,14 +3,13 @@ import { CreateAssetCommand } from '../../src/core/commands/assetCommands';
|
||||
import { AddColliderCommand } from '../../src/core/commands/colliderCommands';
|
||||
import { SetOverrideCommand } from '../../src/core/commands/sceneObjectCommands';
|
||||
import { footprintOf, textSize } from '../../src/core/properties/bounds';
|
||||
import { fakeHandle, makeSpriteScene, place, runNode, startWorld } from '../helpers';
|
||||
import { makeSpriteScene, place, startWorld } from '../helpers';
|
||||
|
||||
describe('text objects', () => {
|
||||
it('measures a text footprint from its content and font size', () => {
|
||||
expect(textSize({ text: 'ab', fontSize: 100 }).w).toBeCloseTo(110, 5);
|
||||
expect(textSize({ text: 'ab', fontSize: 100 }).h).toBeCloseTo(125, 5);
|
||||
expect(footprintOf('text', { text: 'abcd', fontSize: 100 }).w).toBeCloseTo(220, 5);
|
||||
expect(runNode('SetText', { object: fakeHandle({ text: 'old' }), text: 42 })).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
requireNodeDef,
|
||||
type RunResult,
|
||||
portKindOf,
|
||||
PROPERTIES,
|
||||
VARIABLES,
|
||||
} from '../src/core/graph';
|
||||
import type { BehaviorAsset, SceneAsset } from '../src/core/model/Asset';
|
||||
@@ -107,6 +108,8 @@ export interface GraphEditor {
|
||||
keys: readonly string[],
|
||||
options?: { write?: boolean | readonly string[]; params?: PropertyBag },
|
||||
): GraphNode;
|
||||
/** A Properties node with `params` (its type) declaring `ports`. */
|
||||
properties(ports: readonly DeclaredPort[], params?: PropertyBag): GraphNode;
|
||||
connect(from: GraphNode, fromPort: string, to: GraphNode, toPort: string): void;
|
||||
wire(edges: readonly Edge[]): void;
|
||||
}
|
||||
@@ -137,6 +140,12 @@ export function graphEditor(fixture: ProjectFixture, behavior: BehaviorAsset): G
|
||||
if (ports.length > 0) stack.execute(new AddPortsCommand(behavior.id, command.node.id, ports));
|
||||
return command.node;
|
||||
},
|
||||
properties(ports, params = {}) {
|
||||
const command = new AddNodeCommand(behavior.id, PROPERTIES, 0, 0, params);
|
||||
stack.execute(command);
|
||||
if (ports.length > 0) stack.execute(new AddPortsCommand(behavior.id, command.node.id, ports));
|
||||
return command.node;
|
||||
},
|
||||
connect,
|
||||
wire(edges) {
|
||||
for (const [from, fromPort, to, toPort] of edges) connect(from, fromPort, to, toPort);
|
||||
|
||||
Reference in new issue
Block a user