import { PROPERTIES } from '../../nested'; import type { NodeDef } from '../../NodeRegistry'; import { OBJECT_BEHAVIOR_SETTING, OBJECT_TYPE_SETTING, propertiesTitle, propertyPort, } from '../../propertyPorts'; import { asEntity, writeTyped } from '../../values'; import { exec, obj } from '../shared'; export const Properties: NodeDef = { type: PROPERTIES, label: 'Properties', category: 'data', description: 'Reads properties (width, color, a variable, …) of the object it is handed, at any time. Those ' + 'switched to Set are written together when the flow arrives.', inputs: [obj('object')], outputs: [], params: [OBJECT_TYPE_SETTING, OBJECT_BEHAVIOR_SETTING], // The title names both ("Sprite properties", "Game properties"). hiddenParams: () => [OBJECT_TYPE_SETTING.key, OBJECT_BEHAVIOR_SETTING.key], declaresPorts: 'both', declaredPort: (port, node) => propertyPort(port, node.params), derivePorts: ({ node, behaviors }) => { const label = propertiesTitle(node, behaviors); return (node.ports ?? []).some((p) => p.write) ? { label, inputs: [exec()], outputs: [exec()] } : { label }; }, read: (_ctx, _params, input) => asEntity(input('object'))?.props ?? {}, run: (_ctx, inputs, _params, def) => { const target = asEntity(inputs.object); if (target) for (const port of def.inputs) if (port.type !== 'exec' && port.key !== 'object') writeTyped(target.props, port.key, inputs[port.key]); return { next: 'exec' }; }, };