import { GroupGameObject, GroupGameObjectData } from './group'; import { GameConfig, GameObject, GameObjectBaseType, GameObjectData } from '@gedit/render-engine/lib/common'; import { setGameObjectBaseData, getVisible } from '../utils'; import { setByKey } from '@gedit/utils/lib/common/objects'; export interface ComponentSymbol extends Omit { value?: string | string[] | ((...args: any[]) => any); } export interface ComponentSchema { id: string // 组件的url地址 symbols: GameConfig.Symbol[] // 组件参数相关 } export interface ComponentGameObjectData extends GroupGameObjectData { [GameObjectBaseType.COMPONENT]: ComponentSchema } export class ComponentGameObject extends GroupGameObject { static type = GameObjectBaseType.COMPONENT; // 组件自定义变量, 这里事件被 factory 里改写了 symbols: ComponentSymbol[] = []; childrenTemplate: GameObjectData[] = []; /** * 获取组件的子节点 * @param idOrName */ select(idOrName: string): GameObject | undefined { if (this.children.length === 0) { return undefined; } return this.children[0].api!.select(idOrName); } async updateSymbols(newSymbols: ComponentSymbol[]): Promise { const component = (this.data as ComponentGameObjectData)[GameObjectBaseType.COMPONENT]; if (!component) { return; } if (!component.symbols) { component.symbols = []; } const textureUrls: string[] = []; for (const incoming of newSymbols) { const index = this.symbols.findIndex(s => s.id === incoming.id); const symbol = this.processRuntimeSymbol( index >= 0 ? { ...this.symbols[index], ...incoming } : incoming ); if (index >= 0) { this.symbols.splice(index, 1, symbol); } else { this.symbols.push(symbol); } const configIndex = component.symbols.findIndex(s => s.id === incoming.id); if (configIndex >= 0) { Object.assign(component.symbols[configIndex], incoming); } else { component.symbols.push(incoming as GameConfig.Symbol); } if ( incoming.type === GameConfig.SymbolType.TEXTURE && typeof incoming.value === 'string' && /^https?:\/\//.test(incoming.value) ) { textureUrls.push(incoming.value); } } if (textureUrls.length > 0) { await this.preloadTextures(textureUrls); } for (const incoming of newSymbols) { const value = incoming.value; if (value !== undefined && typeof value !== 'function') { this.replaceSymbolInChildren( incoming.id, value as string | string[], this.childrenTemplate, this.children ); } } } private async preloadTextures(urls: string[]): Promise { const toLoad: string[] = []; for (const url of urls) { if (!this.scene.getTexture?.(url)) { this.scene.addTexture?.(url, url, 'image' as any); toLoad.push(url); } } if (toLoad.length > 0 && typeof PIXI !== 'undefined') { await PIXI.Assets.load(toLoad); } } private processRuntimeSymbol(symbol: ComponentSymbol): ComponentSymbol { if ( symbol.type === GameConfig.SymbolType.EVENT && typeof symbol.value === 'string' && this.api ) { const routeKey = symbol.value; return { ...symbol, value: (...args: any[]) => this.api!.dispatch(routeKey, ...args), }; } return { ...symbol }; } private replaceSymbolInChildren( symbolId: string, value: string | string[], templates: GameObjectData[], children: GameObject[] ): void { for (const template of templates) { const child = children.find( c => c.id === template.id || c.name === template.name ); if (!child) { continue; } const updates: Record = {}; this.findSymbolRefs(template, symbolId, value, '', updates); if (Object.keys(updates).length > 0) { let newData = child.data; for (const [path, val] of Object.entries(updates)) { newData = setByKey(newData, path, val, true, true); } if (child.type === GameObjectBaseType.IMAGE && updates.textureId !== undefined) { this.updateChildTexture(child, newData); } child.update(newData as typeof child.data); } if (template.children?.length && child.children.length) { this.replaceSymbolInChildren(symbolId, value, template.children, child.children); } } } private findSymbolRefs( obj: any, symbolId: string, value: string | string[], path: string, updates: Record ): void { for (const key in obj) { if (key === 'cache' || key === 'children') { continue; } const v = obj[key]; const currentPath = path ? `${path}.${key}` : key; if (typeof v === 'string') { if ( v === `\${${symbolId}}` || (key === 'textureId' && v === `symbol/${symbolId}`) ) { updates[currentPath] = value; } } else if (Array.isArray(v)) { v.forEach((item, i) => { if (typeof item === 'string' && item === `\${${symbolId}}`) { updates[`${currentPath}.${i}`] = value; } else if (typeof item === 'object' && item) { this.findSymbolRefs(item, symbolId, value, `${currentPath}.${i}`, updates); } }); } else if (typeof v === 'object' && v) { this.findSymbolRefs(v, symbolId, value, currentPath, updates); } } } private updateChildTexture(child: GameObject, data: GameObjectData): void { const textureId = Array.isArray(data.textureId) ? data.textureId[0] : data.textureId; if (!textureId || typeof textureId !== 'string') { return; } const texture = this.scene.getTexture?.(textureId) as PIXI.Texture | undefined; if (texture && child.rendererObject instanceof PIXI.Sprite) { child.rendererObject.texture = texture; } } update(data: GroupGameObjectData): void { if (this.rendererObject) { setGameObjectBaseData(this, data, false, false, true); this.rendererObject.visible = getVisible(data, this); } super.update(data); } }