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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
refactor(ui): delete DataBinding system
esenginec7d81d924678
diff --git a/sdk/src/systemLabels.ts b/sdk/src/systemLabels.tsindex 04d249c41..378d6baf7 100644--- a/sdk/src/systemLabels.ts+++ b/sdk/src/systemLabels.ts@@ -12,7 +12,6 @@ export const PluginName = {Text: 'text',Image: 'image',Focus: 'focus',- DataBinding: 'dataBinding',ScrollView: 'scrollView',Dropdown: 'dropdown',ProgressBar: 'progressBar',diff --git a/sdk/src/ui/DataBinding.ts b/sdk/src/ui/DataBinding.tsdeleted file mode 100644index 597d34a44..000000000--- a/sdk/src/ui/DataBinding.ts+++ /dev/null@@ -1,16 +0,0 @@-import { defineComponent } from '../component';--export interface BindingEntry {- target: string;- expression: string;-}--export interface DataBindingData {- source: string;- bindings: BindingEntry[];-}--export const DataBinding = defineComponent<DataBindingData>('DataBinding', {- source: '',- bindings: [],-});diff --git a/sdk/src/ui/DataBindingExpression.ts b/sdk/src/ui/DataBindingExpression.tsdeleted file mode 100644index 449661262..000000000--- a/sdk/src/ui/DataBindingExpression.ts+++ /dev/null@@ -1,202 +0,0 @@-export interface CompiledExpression {- evaluate(source: Record<string, unknown>): unknown;-}--const CACHE_MAX_SIZE = 512;-const cache = new Map<string, CompiledExpression>();--import { getNestedProperty } from './propertyPath';--export function compileExpression(expr: string): CompiledExpression {- let compiled = cache.get(expr);- if (compiled) return compiled;- compiled = compile(expr);- if (cache.size >= CACHE_MAX_SIZE) {- const firstKey = cache.keys().next().value!;- cache.delete(firstKey);- }- cache.set(expr, compiled);- return compiled;-}--function compile(expr: string): CompiledExpression {- const segments = parseSegments(expr);-- if (segments.length === 1 && segments[0].type === 'expr') {- const inner = segments[0].value.trim();- if (isSimpleKey(inner)) {- return { evaluate: (source) => getNestedProperty(source, inner) };- }- return compileMath(inner);- }-- const evaluators: ((source: Record<string, unknown>) => string)[] = [];- for (const seg of segments) {- if (seg.type === 'text') {- const text = seg.value;- evaluators.push(() => text);- } else {- const inner = seg.value.trim();- if (isSimpleKey(inner)) {- evaluators.push((source) => String(getNestedProperty(source, inner) ?? ''));- } else {- const mathExpr = compileMath(inner);- evaluators.push((source) => String(mathExpr.evaluate(source) ?? ''));- }- }- }-- return {- evaluate(source) {- let result = '';- for (const ev of evaluators) result += ev(source);- return result;- },- };-}--interface Segment {- type: 'text' | 'expr';- value: string;-}--function parseSegments(expr: string): Segment[] {- const segments: Segment[] = [];- let i = 0;- let textStart = 0;-- while (i < expr.length) {- if (expr[i] === '{') {- if (i > textStart) {- segments.push({ type: 'text', value: expr.slice(textStart, i) });- }- const closeIdx = expr.indexOf('}', i + 1);- if (closeIdx === -1) {- segments.push({ type: 'text', value: expr.slice(i) });- return segments;- }- segments.push({ type: 'expr', value: expr.slice(i + 1, closeIdx) });- i = closeIdx + 1;- textStart = i;- } else {- i++;- }- }-- if (textStart < expr.length) {- segments.push({ type: 'text', value: expr.slice(textStart) });- }-- return segments;-}--function isSimpleKey(s: string): boolean {- return /^[a-zA-Z_]\w*(\.\w+)*$/.test(s);-}--// ── Math Expression Parser (recursive descent, no eval) ──--interface MathNode {- eval(source: Record<string, unknown>): number;-}--class NumberNode implements MathNode {- constructor(private value_: number) {}- eval(): number { return this.value_; }-}--class VarNode implements MathNode {- constructor(private key_: string) {}- eval(source: Record<string, unknown>): number {- const v = getNestedProperty(source, this.key_);- return typeof v === 'number' ? v : Number(v) || 0;- }-}--class BinaryNode implements MathNode {- constructor(private left_: MathNode, private op_: string, private right_: MathNode) {}- eval(source: Record<string, unknown>): number {- const l = this.left_.eval(source);- const r = this.right_.eval(source);- switch (this.op_) {- case '+': return l + r;- case '-': return l - r;- case '*': return l * r;- case '/': return r !== 0 ? l / r : 0;- default: return 0;- }- }-}--class MathParser {- private pos_ = 0;- private tokens_: string[] = [];-- constructor(input: string) {- this.tokens_ = tokenize(input);- }-- parse(): MathNode {- const node = this.parseExpression_();- return node;- }-- private parseExpression_(): MathNode {- let left = this.parseTerm_();- while (this.pos_ < this.tokens_.length && (this.peek_() === '+' || this.peek_() === '-')) {- const op = this.advance_();- const right = this.parseTerm_();- left = new BinaryNode(left, op, right);- }- return left;- }-- private parseTerm_(): MathNode {- let left = this.parseFactor_();- while (this.pos_ < this.tokens_.length && (this.peek_() === '*' || this.peek_() === '/')) {- const op = this.advance_();- const right = this.parseFactor_();- left = new BinaryNode(left, op, right);- }- return left;- }-- private parseFactor_(): MathNode {- const token = this.advance_();- if (token === '(') {- const node = this.parseExpression_();- if (this.peek_() === ')') this.advance_();- return node;- }- const num = Number(token);- if (!isNaN(num)) return new NumberNode(num);- return new VarNode(token);- }-- private peek_(): string { return this.tokens_[this.pos_] ?? ''; }- private advance_(): string { return this.tokens_[this.pos_++] ?? ''; }-}--function tokenize(input: string): string[] {- const tokens: string[] = [];- let i = 0;- while (i < input.length) {- const ch = input[i];- if (ch === ' ' || ch === '\t') { i++; continue; }- if ('+-*/()'.includes(ch)) {- tokens.push(ch);- i++;- } else {- let start = i;- while (i < input.length && !'+-*/() \t'.includes(input[i])) i++;- tokens.push(input.slice(start, i));- }- }- return tokens;-}--function compileMath(inner: string): CompiledExpression {- const parser = new MathParser(inner);- const ast = parser.parse();- return { evaluate: (source) => ast.eval(source) };-}diff --git a/sdk/src/ui/DataBindingPlugin.ts b/sdk/src/ui/DataBindingPlugin.tsdeleted file mode 100644index bc561d626..000000000--- a/sdk/src/ui/DataBindingPlugin.ts+++ /dev/null@@ -1,141 +0,0 @@-import type { App, Plugin } from '../app';-import type { Entity } from '../types';-import type { AnyComponentDef } from '../component';-import { defineSystem, Schedule } from '../system';-import { registerComponent, getComponent } from '../component';-import { playModeOnly } from '../env';-import { DataBinding, type DataBindingData, type BindingEntry } from './DataBinding';-import { compileExpression } from './DataBindingExpression';-import { SystemLabel, PluginName } from '../systemLabels';--const componentDefCache = new Map<string, AnyComponentDef | null>();--function getCachedComponentDef(name: string): AnyComponentDef | null {- if (componentDefCache.has(name)) return componentDefCache.get(name)!;- const def = getComponent(name) ?? null;- componentDefCache.set(name, def);- return def;-}--export class DataBindingPlugin implements Plugin {- name = PluginName.DataBinding;- dependencies = [PluginName.Text, PluginName.Image];-- build(app: App): void {- registerComponent('DataBinding', DataBinding);-- const world = app.world;- const lastSourceTick = new Map<Entity, number>();-- app.addSystemToSchedule(Schedule.PreUpdate, defineSystem(- [],- () => {- for (const entity of lastSourceTick.keys()) {- if (!world.valid(entity) || !world.has(entity, DataBinding)) {- lastSourceTick.delete(entity);- }- }-- const entities = world.getEntitiesWithComponents([DataBinding]);- for (const entity of entities) {- const binding = world.get(entity, DataBinding) as DataBindingData;- if (!binding.source || binding.bindings.length === 0) continue;-- const currentTick = app.getResourceChangeTick(binding.source);- const prevTick = lastSourceTick.get(entity) ?? -1;- if (currentTick === prevTick) continue;- lastSourceTick.set(entity, currentTick);-- const sourceData = app.getResourceByName(binding.source);- if (sourceData == null || typeof sourceData !== 'object') continue;-- for (const raw of binding.bindings) {- const entry = normalizeBinding(raw);- if (entry) applyBinding(world, entity, entry, sourceData as Record<string, unknown>);- }- }- },- { name: 'DataBindingSystem' }- ), { runBefore: [SystemLabel.Text, SystemLabel.Image], runIf: playModeOnly });- }-}--function normalizeBinding(raw: unknown): BindingEntry | null {- if (typeof raw === 'string') {- const eqIdx = raw.indexOf('=');- if (eqIdx === -1) return null;- return { target: raw.slice(0, eqIdx), expression: raw.slice(eqIdx + 1) };- }- if (raw && typeof raw === 'object' && 'target' in raw && 'expression' in raw) {- return raw as BindingEntry;- }- return null;-}--function coerceValue(raw: unknown, existing: unknown): unknown {- if (raw == null) return raw;- if (existing == null) return raw;-- const existingType = typeof existing;- const rawType = typeof raw;-- if (existingType === rawType) return raw;-- if (existingType === 'number' && rawType === 'string') {- const n = Number(raw);- return isNaN(n) ? existing : n;- }- if (existingType === 'string' && rawType !== 'string') {- return String(raw);- }- if (existingType === 'boolean') {- return Boolean(raw);- }-- if (existingType === 'object' && rawType === 'object') {- return raw;- }-- return raw;-}--function shallowEqual(a: unknown, b: unknown): boolean {- if (a === b) return true;- if (a == null || b == null) return false;- if (typeof a !== 'object' || typeof b !== 'object') return false;- const ak = Object.keys(a as Record<string, unknown>);- const bk = Object.keys(b as Record<string, unknown>);- if (ak.length !== bk.length) return false;- for (const key of ak) {- if ((a as Record<string, unknown>)[key] !== (b as Record<string, unknown>)[key]) return false;- }- return true;-}--function applyBinding(- world: import('../world').World,- entity: Entity,- entry: BindingEntry,- source: Record<string, unknown>,-): void {- const dotIdx = entry.target.indexOf('.');- if (dotIdx === -1) return;-- const componentName = entry.target.slice(0, dotIdx);- const fieldName = entry.target.slice(dotIdx + 1);-- const componentDef = getCachedComponentDef(componentName);- if (!componentDef || !world.has(entity, componentDef)) return;-- const compiled = compileExpression(entry.expression);- const rawValue = compiled.evaluate(source);-- const data = world.get(entity, componentDef) as Record<string, unknown>;- const coerced = coerceValue(rawValue, data[fieldName]);- if (shallowEqual(data[fieldName], coerced)) return;-- data[fieldName] = coerced;- world.insert(entity, componentDef, data);-}--export const dataBindingPlugin = new DataBindingPlugin();diff --git a/sdk/src/ui/index.ts b/sdk/src/ui/index.tsindex e3432645c..e2e2958f7 100644--- a/sdk/src/ui/index.ts+++ b/sdk/src/ui/index.ts@@ -366,23 +366,6 @@ export {type ParsedPropertyPath,} from './propertyPath';-// DataBinding-export {- DataBinding,- type DataBindingData,- type BindingEntry,-} from './DataBinding';--export {- DataBindingPlugin,- dataBindingPlugin,-} from './DataBindingPlugin';--export {- compileExpression,- type CompiledExpression,-} from './DataBindingExpression';-// Rich Textexport {parseRichText,diff --git a/sdk/src/uiPlugins.ts b/sdk/src/uiPlugins.tsindex dec51bdf7..9593fb4dc 100644--- a/sdk/src/uiPlugins.ts+++ b/sdk/src/uiPlugins.ts@@ -14,14 +14,12 @@ import { safeAreaPlugin } from './ui/SafeAreaPlugin';import { dropdownPlugin } from './ui/DropdownPlugin';import { uiRenderOrderPlugin } from './ui/UIRenderOrderPlugin';import { stateMachinePlugin } from './ui/StateMachinePlugin';-import { dataBindingPlugin } from './ui/DataBindingPlugin';import { collectionViewPlugin } from './ui/CollectionViewPlugin';import { linearLayoutPlugin } from './ui/LinearLayoutPlugin';import { gridLayoutPlugin } from './ui/GridLayoutPlugin';import type { Plugin } from './app';export const uiPlugins: Plugin[] = [- dataBindingPlugin,textPlugin, uiMaskPlugin, uiLayoutPlugin,imagePlugin, progressBarPlugin,uiInteractionPlugin, dragPlugin, scrollViewPlugin, sliderPlugin,