import { MeshGameObject, Origin } from '@gedit/runtime-render'; import { BlockPair, PluginPureHook } from '@gedit/runtime-compiler'; import { local } from '../common'; function checkOriginValid(origin: Origin): boolean { if ( origin && typeof origin.x !== 'undefined' && typeof origin.y !== 'undefined' && (origin.x !== 0 || origin.y !== 0) ) { return true; } return false; } export enum TransformFilter { origin, depth, opacity, scale, rotation, size, position, flip, } export const filterOrigin = (t: TransformFilter) => t === TransformFilter.origin; export function transform( ctx: PluginPureHook, mesh: T, withLocalValue: boolean = false, filter: (type: TransformFilter) => boolean = () => false ): void { if (withLocalValue) { ctx.print(`${local.COMPONENT}`); // 保留一下 id,方便查找 ctx.println(`.setName('${mesh.id}')`); ctx.push(BlockPair.Space, false); } else { ctx.push(BlockPair.Space, false); // 保留一下 id,方便查找 ctx.println(`.setName('${mesh.id}')`); } if ( !filter(TransformFilter.position) && mesh.position && (mesh.position.x !== 0 || mesh.position.y !== 0) ) { ctx.println( `.setPosition(${mesh.position.x + mesh.size.width / 2}, ${ mesh.position.y + mesh.size.height / 2 })` ); } if ( !filter(TransformFilter.size) && mesh.size && (mesh.size.width !== 0 || mesh.size.height !== 0) ) { ctx.println(`.setSize(${mesh.size.width}, ${mesh.size.height})`); } if (!filter(TransformFilter.rotation) && mesh.rotation) { ctx.println(`.setRotation(${mesh.rotation})`); } if ( !filter(TransformFilter.scale) && mesh.scale && (mesh.scale.x !== 1 || mesh.scale.y !== 1) ) { ctx.println(`.setScale(${mesh.scale.x}, ${mesh.scale.y || 1})`); } if (!filter(TransformFilter.opacity) && mesh.opacity && mesh.opacity !== 1) { ctx.println(`.setAlpha(${mesh.opacity})`); } if (!filter(TransformFilter.origin) && checkOriginValid(mesh.origin)) { ctx.println(`.setOrigin(${mesh.origin.x}, ${mesh.origin.y})`); } if (!filter(TransformFilter.depth) && mesh.depth) { ctx.println(`.setDepth(${mesh.depth})`); } if (!filter(TransformFilter.flip) && mesh.flip && (mesh.flip.x || mesh.flip.y)) { ctx.println(`.setFlip(${!!mesh.flip.x}, ${!!mesh.flip.y})`); } ctx.pop(false); }