Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

refactor(ui): delete legacy StateMachine system

esenginead6fa1b18d8a
diff --git a/sdk/src/ui/StateMachine.ts b/sdk/src/ui/StateMachine.ts
deleted file mode 100644
index 7653d87a5..000000000
--- a/sdk/src/ui/StateMachine.ts
+++ /dev/null
@@ -1,83 +0,0 @@
-import { defineComponent, type AssetRef } from '../component';
-
-export interface Condition {
- inputName: string;
- comparator: 'eq' | 'neq' | 'gt' | 'lt' | 'gte' | 'lte';
- value: boolean | number;
-}
-
-export interface Transition {
- target: string;
- conditions: Condition[];
- duration: number;
- exitTime?: number;
- easing?: string;
-}
-
-export type StateType = 'standard' | 'blend1d' | 'blendDirect';
-
-export interface BlendEntry {
- timeline?: string;
- timelineWrapMode?: 'once' | 'loop';
- properties?: Record<string, unknown>;
- threshold?: number;
- mixValue?: number;
- mixInput?: string;
-}
-
-export interface StateNode {
- timeline?: string;
- timelineWrapMode?: 'once' | 'loop';
- properties?: Record<string, unknown>;
- transitions: Transition[];
- type?: StateType;
- blendInput?: string;
- blendStates?: BlendEntry[];
-}
-
-export interface InputDef {
- name: string;
- type: 'bool' | 'number' | 'trigger';
- defaultValue?: boolean | number;
-}
-
-export interface ListenerDef {
- event: 'pointerEnter' | 'pointerExit' | 'pointerDown' | 'pointerUp';
- inputName: string;
- action: 'set' | 'reset' | 'toggle';
- value?: boolean | number;
-}
-
-export interface LayerData {
- name: string;
- states: Record<string, StateNode>;
- initialState: string;
-}
-
-export interface StateMachineData {
- states: Record<string, StateNode>;
- inputs: InputDef[];
- listeners: ListenerDef[];
- initialState: string;
- layers?: LayerData[];
-}
-
-export const StateMachine = defineComponent<StateMachineData>('StateMachine', {
- states: {},
- inputs: [],
- listeners: [],
- initialState: '',
-}, {
- discoverAssets(data) {
- const refs: AssetRef[] = [];
- const states = data.states as Record<string, { timeline?: string }> | undefined;
- if (states) {
- for (const state of Object.values(states)) {
- if (typeof state.timeline === 'string' && state.timeline) {
- refs.push({ type: 'timeline', path: state.timeline });
- }
- }
- }
- return refs;
- },
-});
diff --git a/sdk/src/ui/StateMachineAssets.ts b/sdk/src/ui/StateMachineAssets.ts
deleted file mode 100644
index e9990420c..000000000
--- a/sdk/src/ui/StateMachineAssets.ts
+++ /dev/null
@@ -1,81 +0,0 @@
-import { getComponentAssetFieldDescriptors } from '../scene';
-import type { Assets } from '../asset/Assets';
-import type { TextureResult } from '../asset/AssetLoader';
-
-const ASSET_PREFIX = 'asset:';
-const assetPathCache_ = new Map<string, boolean>();
-
-export function isAssetPropertyPath(path: string): boolean {
- const cached = assetPathCache_.get(path);
- if (cached !== undefined) return cached;
- const dotIndex = path.indexOf('.');
- if (dotIndex === -1) { assetPathCache_.set(path, false); return false; }
- const componentName = path.substring(0, dotIndex);
- const fieldName = path.substring(dotIndex + 1).split('.')[0];
- const descriptors = getComponentAssetFieldDescriptors(componentName);
- const result = descriptors.some(d => d.field === fieldName && d.type === 'texture');
- assetPathCache_.set(path, result);
- return result;
-}
-
-export function normalizeAssetValue(value: unknown): string | null {
- if (typeof value !== 'string') return null;
- if (value.startsWith(ASSET_PREFIX)) return value.slice(ASSET_PREFIX.length);
- return value;
-}
-
-export function collectAssetPaths(properties?: Record<string, unknown>): string[] {
- if (!properties) return [];
- const paths: string[] = [];
- for (const [key, value] of Object.entries(properties)) {
- if (!isAssetPropertyPath(key)) continue;
- const normalized = normalizeAssetValue(value);
- if (normalized) paths.push(normalized);
- }
- return paths;
-}
-
-export class StateMachineAssetCache {
- private resolved_ = new Map<string, number>();
- private loading_ = false;
- readonly paths: string[];
-
- constructor(paths: string[]) {
- const unique = new Set(paths);
- this.paths = [...unique];
- }
-
- get isLoading(): boolean { return this.loading_; }
-
- get allLoaded(): boolean {
- return this.paths.length === 0 || this.resolved_.size >= this.paths.length;
- }
-
- startLoading(assets: Assets): void {
- if (this.loading_) return;
- this.loading_ = true;
- let pending = 0;
- for (const path of this.paths) {
- if (this.resolved_.has(path)) continue;
- pending++;
- assets.loadTexture(path).then((info: TextureResult) => {
- this.resolved_.set(path, info.handle);
- }).catch(() => {}).finally(() => {
- pending--;
- if (pending === 0) this.loading_ = false;
- });
- }
- if (pending === 0) this.loading_ = false;
- }
-
- getHandle(path: string): number | undefined {
- return this.resolved_.get(path);
- }
-
- release(assets: Assets): void {
- for (const path of this.paths) {
- assets.releaseTexture(path);
- }
- this.resolved_.clear();
- }
-}
diff --git a/sdk/src/ui/StateMachinePlugin.ts b/sdk/src/ui/StateMachinePlugin.ts
deleted file mode 100644
index a1ed6887f..000000000
--- a/sdk/src/ui/StateMachinePlugin.ts
+++ /dev/null
@@ -1,595 +0,0 @@
-import type { App, Plugin } from '../app';
-import { defineSystem, Schedule } from '../system';
-import { Res } from '../resource';
-import { Time, type TimeData } from '../resource';
-import { registerComponent } from '../component';
-import { isEditor, isPlayMode } from '../env';
-import type { Entity } from '../types';
-import type { World } from '../world';
-import { StateMachine } from './StateMachine';
-import type {
- StateMachineData, StateNode, Condition, ListenerDef,
- LayerData, BlendEntry,
-} from './StateMachine';
-import { UIInteraction } from './UIInteraction';
-import type { UIInteractionData } from './UIInteraction';
-import { Tween } from '../animation/Tween';
-import { EasingType } from '../animation/ValueTween';
-import type { ValueTweenHandle } from '../animation/ValueTween';
-import { getEntityProperty, setEntityProperty } from './propertyPath';
-import { WrapMode } from '../timeline/TimelineTypes';
-import { getTimelineAsset } from '../timeline/TimelinePlugin';
-import { setTimelineModule } from '../timeline/TimelineControl';
-import {
- createTimelineHandle,
- destroyTimelineHandle,
- resolveTrackTargets,
- advanceAndProcess,
-} from '../timeline/TimelineRuntime';
-import type { UploadResult } from '../timeline/TimelineUploader';
-import type { ESEngineModule } from '../wasm';
-import { Assets, type AssetsData } from '../asset/AssetPlugin';
-import { EntityStateMap } from './uiHelpers';
-import { SystemLabel, PluginName } from '../systemLabels';
-import {
- isAssetPropertyPath, normalizeAssetValue, collectAssetPaths,
- StateMachineAssetCache,
-} from './StateMachineAssets';
-
-const EASING_MAP: Record<string, EasingType> = {
- linear: EasingType.Linear,
- easeInQuad: EasingType.EaseInQuad,
- easeOutQuad: EasingType.EaseOutQuad,
- easeInOutQuad: EasingType.EaseInOutQuad,
- easeInCubic: EasingType.EaseInCubic,
- easeOutCubic: EasingType.EaseOutCubic,
- easeInOutCubic: EasingType.EaseInOutCubic,
- easeInBack: EasingType.EaseInBack,
- easeOutBack: EasingType.EaseOutBack,
- easeInOutBack: EasingType.EaseInOutBack,
- easeInElastic: EasingType.EaseInElastic,
- easeOutElastic: EasingType.EaseOutElastic,
- easeInOutElastic: EasingType.EaseInOutElastic,
- easeOutBounce: EasingType.EaseOutBounce,
-};
-
-const WRAP_MODE_MAP: Record<string, number> = {
- once: WrapMode.Once,
- loop: WrapMode.Loop,
-};
-
-const EXIT_STATE = '__exit__';
-
-interface PropertyContext {
- world: World;
- entity: Entity;
- assets: StateMachineAssetCache;
-}
-
-interface LayerRuntime {
- currentState: string;
- activeTweens: ValueTweenHandle[];
- timelineHandle: number;
- timelineUpload: UploadResult | null;
- timelinePath: string;
- timelineDuration: number;
- isExited: boolean;
- pendingAssetApply: boolean;
-}
-
-interface EntityRuntime {
- inputs: Map<string, boolean | number>;
- layers: LayerRuntime[];
- prevHovered: boolean;
- assets: StateMachineAssetCache;
-}
-
-function createInputs(data: StateMachineData): Map<string, boolean | number> {
- const inputs = new Map<string, boolean | number>();
- for (const def of data.inputs) {
- if (def.type === 'bool') {
- inputs.set(def.name, def.defaultValue as boolean ?? false);
- } else if (def.type === 'number') {
- inputs.set(def.name, def.defaultValue as number ?? 0);
- } else {
- inputs.set(def.name, false);
- }
- }
- return inputs;
-}
-
-function createLayerRuntime(initialState: string): LayerRuntime {
- return {
- currentState: initialState,
- activeTweens: [],
- timelineHandle: 0,
- timelineUpload: null,
- timelinePath: '',
- timelineDuration: 0,
- isExited: false,
- pendingAssetApply: true,
- };
-}
-
-function getLayers(data: StateMachineData): LayerData[] {
- if (data.layers && data.layers.length > 0) {
- return data.layers;
- }
- return [{ name: 'Base', states: data.states, initialState: data.initialState }];
-}
-
-function gatherAllAssetPaths(data: StateMachineData): string[] {
- const paths: string[] = [];
- const scanStates = (states: Record<string, StateNode>) => {
- for (const state of Object.values(states)) {
- paths.push(...collectAssetPaths(state.properties));
- if (state.blendStates) {
- for (const entry of state.blendStates) {
- paths.push(...collectAssetPaths(entry.properties));
- }
- }
- }
- };
- for (const layer of getLayers(data)) {
- scanStates(layer.states);
- }
- return paths;
-}
-
-function createEntityRuntime(data: StateMachineData): EntityRuntime {
- const layers = getLayers(data);
- return {
- inputs: createInputs(data),
- layers: layers.map(l => createLayerRuntime(l.initialState)),
- prevHovered: false,
- assets: new StateMachineAssetCache(gatherAllAssetPaths(data)),
- };
-}
-
-function applyListenerAction(
- inputs: Map<string, boolean | number>,
- listener: ListenerDef,
-): void {
- const current = inputs.get(listener.inputName);
- if (current === undefined) return;
-
- if (listener.action === 'set') {
- inputs.set(listener.inputName, listener.value ?? true);
- } else if (listener.action === 'reset') {
- inputs.set(listener.inputName, listener.value ?? false);
- } else if (listener.action === 'toggle') {
- inputs.set(listener.inputName, !current);
- }
-}
-
-export function evaluateCondition(
- condition: Condition,
- inputs: Map<string, boolean | number>,
-): boolean {
- const val = inputs.get(condition.inputName);
- if (val === undefined) return false;
-
- switch (condition.comparator) {
- case 'eq': return val === condition.value;
- case 'neq': return val !== condition.value;
- case 'gt': return (val as number) > (condition.value as number);
- case 'lt': return (val as number) < (condition.value as number);
- case 'gte': return (val as number) >= (condition.value as number);
- case 'lte': return (val as number) <= (condition.value as number);
- default: return false;
- }
-}
-
-function cancelActiveTweens(runtime: LayerRuntime): void {
- for (const handle of runtime.activeTweens) {
- handle.cancel();
- }
- runtime.activeTweens.length = 0;
-}
-
-function resolveEasing(name?: string): EasingType {
- if (!name) return EasingType.Linear;
- return EASING_MAP[name] ?? EasingType.Linear;
-}
-
-function resolveAssetProperty(
- ctx: PropertyContext,
- path: string,
- value: unknown,
-): boolean {
- const normalized = normalizeAssetValue(value);
- if (!normalized) return false;
- const handle = ctx.assets.getHandle(normalized);
- if (handle !== undefined) {
- setEntityProperty(ctx.world, ctx.entity, path, handle);
- return true;
- }
- return false;
-}
-
-function stateHasUnresolvedAssets(state: StateNode, assets: StateMachineAssetCache): boolean {
- if (!state.properties) return false;
- for (const [key, value] of Object.entries(state.properties)) {
- if (!isAssetPropertyPath(key)) continue;
- const normalized = normalizeAssetValue(value);
- if (normalized && assets.getHandle(normalized) === undefined) return true;
- }
- return false;
-}
-
-function applyStateProperties(
- ctx: PropertyContext,
- state: StateNode,
- duration: number,
- easing: EasingType,
- runtime: LayerRuntime,
-): void {
- if (!state.properties) return;
-
- for (const [path, targetValue] of Object.entries(state.properties)) {
- if (isAssetPropertyPath(path)) {
- resolveAssetProperty(ctx, path, targetValue);
- continue;
- }
-
- if (duration > 0 && typeof targetValue === 'number') {
- const currentValue = getEntityProperty(ctx.world, ctx.entity, path);
- if (typeof currentValue !== 'number') {
- setEntityProperty(ctx.world, ctx.entity, path, targetValue);
- continue;
- }
- const from = currentValue;
- const to = targetValue;
- const handle = Tween.value(from, to, duration, (v) => {
- setEntityProperty(ctx.world, ctx.entity, path, v);
- }, { easing });
- runtime.activeTweens.push(handle);
- } else {
- setEntityProperty(ctx.world, ctx.entity, path, targetValue);
- }
- }
-}
-
-function applyBlendEntryProperties(
- ctx: PropertyContext,
- entry: BlendEntry,
- weight: number,
-): void {
- if (!entry.properties || weight <= 0) return;
-
- for (const [path, targetValue] of Object.entries(entry.properties)) {
- if (isAssetPropertyPath(path)) {
- resolveAssetProperty(ctx, path, targetValue);
- continue;
- }
-
- if (typeof targetValue === 'number') {
- const current = getEntityProperty(ctx.world, ctx.entity, path);
- if (typeof current === 'number') {
- setEntityProperty(ctx.world, ctx.entity, path, current + (targetValue - current) * weight);
- }
- }
- }
-}
-
-function applyBlend1D(
- ctx: PropertyContext,
- state: StateNode,
- inputs: Map<string, boolean | number>,
-): void {
- if (!state.blendInput || !state.blendStates || state.blendStates.length === 0) return;
-
- const inputValue = (inputs.get(state.blendInput) as number) ?? 0;
- const entries = state.blendStates.slice().sort((a, b) => (a.threshold ?? 0) - (b.threshold ?? 0));
-
- if (entries.length === 1) {
- applyBlendEntryProperties(ctx, entries[0], 1);
- return;
- }
-
- const minThreshold = entries[0].threshold ?? 0;
- const maxThreshold = entries[entries.length - 1].threshold ?? 0;
-
- if (inputValue <= minThreshold) {
- applyBlendEntryProperties(ctx, entries[0], 1);
- return;
- }
- if (inputValue >= maxThreshold) {
- applyBlendEntryProperties(ctx, entries[entries.length - 1], 1);
- return;
- }
-
- for (let i = 0; i < entries.length - 1; i++) {
- const lo = entries[i].threshold ?? 0;
- const hi = entries[i + 1].threshold ?? 0;
- if (inputValue >= lo && inputValue <= hi) {
- const range = hi - lo;
- const t = range > 0 ? (inputValue - lo) / range : 0;
- const nearLo = t <= 0.5;
- if (entries[i].properties && entries[i + 1].properties) {
- for (const [path, loVal] of Object.entries(entries[i].properties!)) {
- const hiVal = entries[i + 1].properties![path];
- const isAsset = isAssetPropertyPath(path);
- if (isAsset) {
- const loNorm = normalizeAssetValue(loVal);
- const hiNorm = normalizeAssetValue(hiVal);
- const loHandle = loNorm ? ctx.assets.getHandle(loNorm) : undefined;
- const hiHandle = hiNorm ? ctx.assets.getHandle(hiNorm) : undefined;
- if (loHandle !== undefined || hiHandle !== undefined) {
- const snapped = nearLo ? loHandle : hiHandle;
- if (snapped !== undefined) {
- setEntityProperty(ctx.world, ctx.entity, path, snapped);
- }
- }
- } else if (typeof loVal === 'number' && typeof hiVal === 'number') {
- const blended = loVal + (hiVal - loVal) * t;
- setEntityProperty(ctx.world, ctx.entity, path, blended);
- }
- }
- }
- break;
- }
- }
-}
-
-function applyBlendDirect(
- ctx: PropertyContext,
- state: StateNode,
- inputs: Map<string, boolean | number>,
-): void {
- if (!state.blendStates) return;
-
- for (const entry of state.blendStates) {
- let mix: number;
- if (entry.mixInput) {
- mix = (inputs.get(entry.mixInput) as number) ?? 0;
- } else {
- mix = entry.mixValue ?? 1;
- }
- applyBlendEntryProperties(ctx, entry, mix);
- }
-}
-
-function enterTimelineState(
- world: any, module: ESEngineModule,
- entity: Entity, state: StateNode, runtime: LayerRuntime,
-): void {
- cleanupTimelineHandle(module, runtime);
-
- const path = state.timeline!;
- const asset = getTimelineAsset(path);
- if (!asset) return;
-
- const uploadResult = createTimelineHandle(module, asset);
- if (!uploadResult.handle) return;
-
- resolveTrackTargets(world, module, uploadResult, entity);
-
- const wrapMode = WRAP_MODE_MAP[state.timelineWrapMode ?? 'once'] ?? WrapMode.Once;
- module._tl_setWrapMode(uploadResult.handle, wrapMode);
- module._tl_play(uploadResult.handle);
-
- runtime.timelineHandle = uploadResult.handle;
- runtime.timelineUpload = uploadResult;
- runtime.timelinePath = path;
- runtime.timelineDuration = asset.duration;
-}
-
-function cleanupTimelineHandle(module: ESEngineModule | null, runtime: LayerRuntime): void {
- if (runtime.timelineHandle && module) {
- destroyTimelineHandle(module, runtime.timelineHandle);
- }
- runtime.timelineHandle = 0;
- runtime.timelineUpload = null;
- runtime.timelinePath = '';
- runtime.timelineDuration = 0;
-}
-
-function cleanupEntityRuntime(module: ESEngineModule | null, er: EntityRuntime, assets?: AssetsData | null): void {
- for (const layer of er.layers) {
- cancelActiveTweens(layer);
- cleanupTimelineHandle(module, layer);
- }
- if (assets) {
- er.assets.release(assets);
- }
-}
-
-function processLayer(
- ctx: PropertyContext,
- module: ESEngineModule,
- layerData: LayerData,
- layer: LayerRuntime,
- inputs: Map<string, boolean | number>,
- dt: number,
-): void {
- if (layer.isExited) return;
-
- if (layer.timelineHandle && layer.timelineUpload && module) {
- setTimelineModule(module);
- advanceAndProcess(
- ctx.world, module,
- layer.timelineHandle, ctx.entity,
- dt, 1.0,
- layer.timelineUpload,
- );
- }
-
- const currentState = layerData.states[layer.currentState];
- if (!currentState) return;
-
- const anyState = layerData.states['__any__'];
- const allTransitions = anyState
- ? [...currentState.transitions, ...anyState.transitions]
- : currentState.transitions;
-
- for (const transition of allTransitions) {
- if (transition.exitTime !== undefined && transition.exitTime > 0) {
- if (layer.timelineHandle && module && layer.timelineDuration > 0) {
- const currentTime = module._tl_getTime(layer.timelineHandle);
- if (currentTime / layer.timelineDuration < transition.exitTime) {
- continue;
- }
- }
- }
-
- const allMet = transition.conditions.every(
- c => evaluateCondition(c, inputs)
- );
- if (!allMet) continue;
-
- if (transition.target === EXIT_STATE) {
- cancelActiveTweens(layer);
- cleanupTimelineHandle(module, layer);
- layer.currentState = EXIT_STATE;
- layer.isExited = true;
- break;
- }
-
- const targetState = layerData.states[transition.target];
- if (!targetState) continue;
-
- cancelActiveTweens(layer);
-
- const stateType = targetState.type ?? 'standard';
-
- if (stateType === 'blend1d') {
- cleanupTimelineHandle(module, layer);
- applyBlend1D(ctx, targetState, inputs);
- } else if (stateType === 'blendDirect') {
- cleanupTimelineHandle(module, layer);
- applyBlendDirect(ctx, targetState, inputs);
- } else if (targetState.timeline) {
- if (module) {
- setTimelineModule(module);
- enterTimelineState(ctx.world, module, ctx.entity, targetState, layer);
- }
- } else {
- cleanupTimelineHandle(module, layer);
- applyStateProperties(
- ctx, targetState,
- transition.duration,
- resolveEasing(transition.easing),
- layer,
- );
- }
-
- layer.currentState = transition.target;
- layer.pendingAssetApply = stateHasUnresolvedAssets(targetState, ctx.assets);
- break;
- }
-
- const activeState = layerData.states[layer.currentState];
- if (activeState) {
- const activeType = activeState.type ?? 'standard';
- if (activeType === 'blend1d') {
- applyBlend1D(ctx, activeState, inputs);
- } else if (activeType === 'blendDirect') {
- applyBlendDirect(ctx, activeState, inputs);
- } else if (layer.pendingAssetApply && !stateHasUnresolvedAssets(activeState, ctx.assets)) {
- applyStateProperties(ctx, activeState, 0, EasingType.Linear, layer);
- layer.pendingAssetApply = false;
- }
- }
-}
-
-export class StateMachinePlugin implements Plugin {
- name = PluginName.StateMachine;
- dependencies = [PluginName.UIInteraction];
-
- build(app: App): void {
- registerComponent('StateMachine', StateMachine);
-
- const world = app.world;
- const runtimes = new EntityStateMap<EntityRuntime>();
- let wasPlayMode = false;
-
- app.addSystemToSchedule(Schedule.Update, defineSystem(
- [Res(Time)],
- (time: TimeData) => {
- const module = world.getWasmModule() as ESEngineModule;
- const assetServer = app.getResource(Assets);
-
- const inPlayMode = !isEditor() || isPlayMode();
- if (!inPlayMode) {
- if (wasPlayMode) {
- for (const [, er] of runtimes) {
- cleanupEntityRuntime(module, er, assetServer);
- }
- runtimes.clear();
- }
- wasPlayMode = false;
- return;
- }
- wasPlayMode = true;
-
- runtimes.cleanup(world);
-
- const entities = world.getEntitiesWithComponents([StateMachine]);
-
- for (const entity of entities) {
- const data = world.get(entity, StateMachine) as StateMachineData;
- const layerDefs = getLayers(data);
-
- if (layerDefs.length === 0) continue;
- if (!layerDefs[0].initialState || !layerDefs[0].states[layerDefs[0].initialState]) continue;
-
- let er = runtimes.get(entity);
- if (!er) {
- er = createEntityRuntime(data);
- runtimes.set(entity, er);
-
- for (let i = 0; i < layerDefs.length; i++) {
- const initialState = layerDefs[i].states[er.layers[i].currentState];
- if (initialState?.timeline && module) {
- setTimelineModule(module);
- enterTimelineState(world, module, entity, initialState, er.layers[i]);
- }
- }
- }
-
- if (assetServer && er.assets.paths.length > 0 && !er.assets.allLoaded) {
- er.assets.startLoading(assetServer);
- }
-
- const hasInteraction = world.has(entity, UIInteraction);
- if (hasInteraction) {
- const interaction = world.get(entity, UIInteraction) as UIInteractionData;
- const justEntered = interaction.hovered && !er.prevHovered;
- const justExited = !interaction.hovered && er.prevHovered;
- er.prevHovered = interaction.hovered;
-
- for (const listener of data.listeners) {
- let matched = false;
- switch (listener.event) {
- case 'pointerDown': matched = interaction.justPressed; break;
- case 'pointerUp': matched = interaction.justReleased; break;
- case 'pointerEnter': matched = justEntered; break;
- case 'pointerExit': matched = justExited; break;
- }
- if (matched) {
- applyListenerAction(er.inputs, listener);
- }
- }
- }
-
- const ctx: PropertyContext = { world, entity, assets: er.assets };
- for (let i = 0; i < layerDefs.length; i++) {
- if (i < er.layers.length) {
- processLayer(ctx, module, layerDefs[i], er.layers[i], er.inputs, time.delta);
- }
- }
-
- for (const inputDef of data.inputs) {
- if (inputDef.type === 'trigger') {
- er.inputs.set(inputDef.name, false);
- }
- }
- }
- },
- { name: 'StateMachineSystem' }
- ), { runAfter: [SystemLabel.Button] });
- }
-}
-
-export const stateMachinePlugin = new StateMachinePlugin();
diff --git a/sdk/src/ui/index.ts b/sdk/src/ui/index.ts
index fc8905593..b6c94f1c5 100644
--- a/sdk/src/ui/index.ts
+++ b/sdk/src/ui/index.ts
@@ -337,24 +337,11 @@ export {
type UITheme,
} from './UITheme';
-// StateMachine Component
+// StateMachine (C++-backed) — canonical in ui2
export {
StateMachine,
type StateMachineData,
- type StateNode,
- type Transition,
- type Condition,
- type InputDef,
- type ListenerDef,
-} from './StateMachine';
-
-// StateMachine Plugin
-export {
- StateMachinePlugin,
- stateMachinePlugin,
- evaluateCondition,
-} from './StateMachinePlugin';
-
+} from '../ui2/behavior/state-machine';
// Property Path Utilities
export {
diff --git a/sdk/src/uiPlugins.ts b/sdk/src/uiPlugins.ts
index f9f5f1854..b33a29502 100644
--- a/sdk/src/uiPlugins.ts
+++ b/sdk/src/uiPlugins.ts
@@ -13,14 +13,13 @@ import { focusPlugin } from './ui/FocusPlugin';
import { safeAreaPlugin } from './ui/SafeAreaPlugin';
import { dropdownPlugin } from './ui/DropdownPlugin';
import { uiRenderOrderPlugin } from './ui/UIRenderOrderPlugin';
-import { stateMachinePlugin } from './ui/StateMachinePlugin';
import type { Plugin } from './app';
export const uiPlugins: Plugin[] = [
textPlugin, uiMaskPlugin, uiLayoutPlugin,
imagePlugin, progressBarPlugin,
uiInteractionPlugin, dragPlugin, scrollViewPlugin, sliderPlugin,
- togglePlugin, textInputPlugin, stateMachinePlugin,
+ togglePlugin, textInputPlugin,
focusPlugin, safeAreaPlugin, dropdownPlugin,
uiRenderOrderPlugin,
];
diff --git a/sdk/src/webAppFactory.ts b/sdk/src/webAppFactory.ts
index 9c113aea3..20d6164f0 100644
--- a/sdk/src/webAppFactory.ts
+++ b/sdk/src/webAppFactory.ts
@@ -30,7 +30,6 @@ export { scrollViewPlugin, ScrollViewPlugin } from './ui/ScrollViewPlugin';
export { focusPlugin, FocusPlugin } from './ui/FocusPlugin';
export { safeAreaPlugin, SafeAreaPlugin } from './ui/SafeAreaPlugin';
export { dropdownPlugin, DropdownPlugin } from './ui/DropdownPlugin';
-export { stateMachinePlugin, StateMachinePlugin } from './ui/StateMachinePlugin';
export { PhysicsPlugin, PhysicsEvents, Physics, loadPhysicsModule } from './physics';
export { AnimationPlugin, animationPlugin } from './animation';
diff --git a/sdk/tests/state-machine.test.ts b/sdk/tests/propertyPath.test.ts
similarity index 57%
rename from sdk/tests/state-machine.test.ts
rename to sdk/tests/propertyPath.test.ts
index 406e20cb5..7fc2969b6 100644
--- a/sdk/tests/state-machine.test.ts
+++ b/sdk/tests/propertyPath.test.ts
@@ -1,7 +1,5 @@
import { describe, it, expect } from 'vitest';
import { getNestedProperty, setNestedProperty, parsePropertyPath } from '../src/ui/propertyPath';
-import { evaluateCondition } from '../src/ui/StateMachinePlugin';
-import type { Condition } from '../src/ui/StateMachine';
describe('propertyPath', () => {
describe('getNestedProperty', () => {
@@ -63,46 +61,3 @@ describe('propertyPath', () => {
});
});
});
-
-describe('evaluateCondition', () => {
- const inputs = new Map<string, boolean | number>([
- ['hovered', true],
- ['pressed', false],
- ['speed', 5],
- ]);
-
- it('eq with bool', () => {
- const c: Condition = { inputName: 'hovered', comparator: 'eq', value: true };
- expect(evaluateCondition(c, inputs)).toBe(true);
- });
-
- it('neq with bool', () => {
- const c: Condition = { inputName: 'pressed', comparator: 'neq', value: true };
- expect(evaluateCondition(c, inputs)).toBe(true);
- });
-
- it('gt with number', () => {
- const c: Condition = { inputName: 'speed', comparator: 'gt', value: 3 };
- expect(evaluateCondition(c, inputs)).toBe(true);
- });
-
- it('lt with number', () => {
- const c: Condition = { inputName: 'speed', comparator: 'lt', value: 3 };
- expect(evaluateCondition(c, inputs)).toBe(false);
- });
-
- it('gte boundary', () => {
- const c: Condition = { inputName: 'speed', comparator: 'gte', value: 5 };
- expect(evaluateCondition(c, inputs)).toBe(true);
- });
-
- it('lte boundary', () => {
- const c: Condition = { inputName: 'speed', comparator: 'lte', value: 5 };
- expect(evaluateCondition(c, inputs)).toBe(true);
- });
-
- it('returns false for unknown input', () => {
- const c: Condition = { inputName: 'missing', comparator: 'eq', value: true };
- expect(evaluateCondition(c, inputs)).toBe(false);
- });
-});