import { getPathBounds, getPathSizeBounds, PathGameObjectData, drawPath, PathChild, } from '@gedit/canvas-draw'; import { GameObjectBaseType } from '@gedit/render-engine'; import { setGameObjectBaseData, getVisible } from '../../utils'; import { PixiGraphicsGameObject } from './common'; export class PathGameObject extends PixiGraphicsGameObject< PathGameObjectData, PIXI.Container > { static type = GameObjectBaseType.PATH; rendererObject: PIXI.Container; pathObject: PIXI.Sprite; protected textureCanvas: HTMLCanvasElement = document.createElement('canvas'); protected textures: PIXI.Texture; protected baseTexture: PIXI.CanvasSource; protected dataJSON: string; create(): void { this.rendererObject = new PIXI.Container(); this.pathObject = new PIXI.Sprite(); this.rendererObject.addChild(this.pathObject); this.scene.container.addChild?.(this.rendererObject); // this.textureCanvas = this.scene.rootTextureCanvas; this.baseTexture = new PIXI.CanvasSource({ resource: this.textureCanvas, }); this.textures = new PIXI.Texture({ source: this.baseTexture, }); this.pathObject.texture = this.textures; super.create.apply(this, arguments); } dispose(): void { this.rendererObject?.destroy(); super.dispose(); } setDepth(depth: number): void { if (this.rendererObject) { this.rendererObject!.zIndex = depth; } } getBounds( data: PathGameObjectData = this.data ): { bounds: ReturnType; paths: PathChild[] } { const { paths: p = [], closed } = data[GameObjectBaseType.PATH]; const paths = [...p]; if (closed) { paths.push(p[0]); } return { bounds: getPathBounds(paths), paths, }; } draw( data: PathGameObjectData, bound?: { bounds: ReturnType; paths: PathChild[] } ): void { // const { origin, size } = data; const { line = [] } = data.style || {}; // const p = data.path.paths.map(c => { // const ox = origin!.x * size!.width; // const oy = origin!.y * size!.height; // c.x = c.x - ox; // c.y = c.y - oy; // if (typeof c.x1 === 'number') { // c.x1 = c.x1 - ox; // } // if (typeof c.y1 === 'number') { // c.y1 = c.y1 - oy; // } // if (typeof c.x2 === 'number') { // c.x2 = c.x2 - ox; // } // if (typeof c.y2 === 'number') { // c.y2 = c.y2 - oy; // } // return c; // }); const { bounds, paths } = bound || this.getBounds(data); // origin 有值,,path 换位置; this.sizeBoxBounds = getPathSizeBounds(line, bounds, data.shadow); drawPath(this.textureCanvas, paths, data, bounds, this.sizeBoxBounds); this.textures.destroy(); this.baseTexture = new PIXI.CanvasSource({ resource: this.textureCanvas, }); this.textures = new PIXI.Texture({ source: this.baseTexture, }); this.pathObject.texture = this.textures; } update(data: PathGameObjectData): void { const { style, path, shadow, ...otherData } = data; const dataJSON = JSON.stringify({ style, path, shadow }); const bounds = this.getBounds(data); if (dataJSON !== this.dataJSON) { this.draw(data, bounds); } if (this.rendererObject) { setGameObjectBaseData(this, otherData, true); this.rendererObject.visible = getVisible(data, this); const { origin = { x: 0, y: 0 } } = data; const { originChangeBeforeSize } = path; const offset = { x: 0, y: 0, }; if (originChangeBeforeSize) { offset.x = -origin.x * bounds.bounds.width; offset.y = -origin.y * bounds.bounds.height; if (originChangeBeforeSize.width !== bounds.bounds.width) { offset.x += (bounds.bounds.width - originChangeBeforeSize.width) * origin.x; } if (originChangeBeforeSize.height !== bounds.bounds.height) { offset.y += (bounds.bounds.height - originChangeBeforeSize.height) * origin.y; } } this.pathObject.x = bounds.bounds.minX - (this.sizeBoxBounds.boxWidth - this.sizeBoxBounds.contentWidth) / 2 + offset.x; this.pathObject.y = bounds.bounds.minY - (this.sizeBoxBounds.boxHeight - this.sizeBoxBounds.contentHeight) / 2 + offset.y; } this.dataJSON = dataJSON; super.update(data); } getDisplaySize(): { width: number; height: number } { return { width: this.width ?? 0, height: this.height ?? 0, }; } }