import { GameObjectBaseType, GameObjectData, GameObjectFollowType, GameObjectMixin, GameObjectType, GameObject, FollowOffset, GameScene, GameObjectBase, // GameScene, } from '@gedit/render-engine'; import { PositionSchema } from '@gedit/utils'; import { setGameObjectBaseData } from '../utils'; export const followCheckArray: GameObjectType[] = [ GameObjectBaseType.IMAGE, GameObjectBaseType.RECTANGLE, GameObjectBaseType.PATH, GameObjectBaseType.CIRCLE, GameObjectBaseType.POLYGON, GameObjectBaseType.TEXT, GameObjectBaseType.TRIANGLE, GameObjectBaseType.GROUP, GameObjectBaseType.COMPONENT, GameObjectBaseType.PARTICLES, GameObjectBaseType.ANIM_FRAMES, ]; export const followSizeTypeClass = { x: 'width', y: 'height', }; export const followKeyType = { x: 'x', y: 'y', x_before: 'x', x_after: 'x', y_before: 'y', y_after: 'y', width: 'width', height: 'height', }; export const getFollowValue = ( follow: { offset?: FollowOffset | number; type?: GameObjectFollowType[]; }, target: GameObject | GameObjectData ): { value: number; type: 'x' | 'y' | 'width' | 'height' }[] => { let obj: | GameObject | { data: GameObjectData; x: number; y: number; width: number; height: number; scale: { x: number; y: number }; origin: { x: number; y: number }; }; if (target instanceof GameObjectBase) { obj = target; } else { const t = target as GameObjectData; obj = { data: t, x: t.position?.x || 0, y: t.position?.y || 0, width: t.size?.width || 0, height: t.size?.height || 0, scale: t.scale || { x: 1, y: 1 }, origin: t.origin || { x: 0, y: 0 }, }; } const { offset = 0, type: t = ['x'] } = follow; const offsetObj = typeof offset === 'number' ? { x: offset, y: offset, width: offset, height: offset } : offset; return t.map(c => { if (c === 'width' || c === 'height') { const w = obj[c] ?? 0; const boundOffset = offsetObj?.[c] ?? 0; return { value: w + boundOffset, type: c as 'width' | 'height', }; } const type = followKeyType[c] as 'x' | 'y'; const keys = c.split('_'); // TODO: 后续有其它类型的跟随再扩展 const v = obj![type]; const s = obj[followSizeTypeClass[type] as 'width' | 'height'] * obj.scale![type]; const o = obj.origin![type]; const offsetNum = offsetObj[type] ?? 0; return { value: keys.length === 1 ? v + offsetNum : v + (keys[1] === 'before' ? -(s * o + offsetNum) : s * (1 - o) + offsetNum), type, }; }); }; const setGameObjectPosition = ( obj: GameObject, node?: GameObject, cb?: typeof setGameObjectBaseData ) => { const renderObj = node?.rendererObject; if (!node || !renderObj) { return; } const { follow } = node.data; if ( !follow || !follow.open || !follow?.target || !obj.id.match(follow.target) ) { return; } const data = { ...node.data, follow, position: { ...node.data.position, }, size: { ...node.data.size, }, } as GameObjectData; getFollowValue(follow, obj).forEach(c => { if (renderObj.destroyed) { return; } const { type, value } = c; const position = node.data.position; if (type !== 'width' && type !== 'height') { data.position![type] = value; node.followPos = { ...position, ...node.followPos, [type]: value, } as PositionSchema; } else { data.size![type] = value; } // @ts-ignore node[type] = value; // console.log(renderObj, type, value); // renderObj[type] = value; }); cb?.(node, data, true); if (node.data.followChild && node.data.followChild) { updateFollow(cb!, node); } // node.update(data as GameObjectData); }; const getParent = (scene: GameScene, ids: string[]): GameObject | undefined => { const parentIds = ids.slice(0, ids.length - 1); const parent = scene.getGameObjectById(parentIds.join('/')); if ( parent?.type === GameObjectBaseType.COMPONENT || parent?.type === GameObjectBaseType.SCENE ) { return parent; } return parentIds.length ? getParent(scene, parentIds) : undefined; }; const getAllChild = (obj: GameObject, id: string): GameObject[] => { const t = obj.scene.gameObjectStoreTree?.get(id) || []; return t.reduce((acc, c) => { acc.push(c); if (c.id === id) { return acc; } if (c.children) { return acc.concat(getAllChild(obj, c.id)); } return acc; }, [] as GameObject[]); }; export const updateFollow = ( cb: typeof setGameObjectBaseData, obj: GameObject ) => { if (obj.data.followChild && obj.parent) { if (obj.api) { obj.data.followChild.forEach(str => { const node = obj.api!.select(str); setGameObjectPosition(obj, node, cb); }); return; } // 跟随只有元素 id,不带目录id, 不能用 scene.getGameObjectById, 取出父节点遍历; const parent = getParent(obj.scene, obj.id.split('/')); // console.log(obj, parent, parent?.children); if (!parent) { return; } const allChild: GameObject[] = getAllChild(obj, parent.id); // console.log(obj, obj.created, allChild); obj.data.followChild!.forEach(str => { // 组件复制会把 id 带走,只取同根目录下的节点,避免取到跨场景的节点 const node = allChild.find(c => { const reg = new RegExp(`${str}$`, 'g'); return c.id.match(reg); }); setGameObjectPosition(obj, node, cb); }); } }; export const FollowMixin: GameObjectMixin = { key: 'followMixin', check: (obj: GameObject) => followCheckArray.includes(obj.type), hook: { ready: updateFollow.bind(this, setGameObjectBaseData), update: updateFollow.bind(this, setGameObjectBaseData), }, };