import { GameObjectBaseType, GameResource } from '@gedit/render-engine'; import { setGameObjectBaseData, getVisible } from '../../utils'; import { drawTriangle, getSizeWidthAndHeight, TriangleGameObjectData, getPoint, } from '@gedit/canvas-draw'; import { PixiGraphicsGameObject } from './common'; export class TriangleGameObject extends PixiGraphicsGameObject { static type = GameObjectBaseType.TRIANGLE; protected textureCanvas: HTMLCanvasElement = document.createElement('canvas'); protected textures: PIXI.Texture; protected baseTexture: PIXI.CanvasSource; protected dataJSON: string; create(resource: GameResource): void { // graphics 不能画渐变;延用 canvas-draw 画 // this.rendererObject = new PIXI.Graphics(); this.rendererObject = new PIXI.Sprite(); this.scene.container.addChild?.(this.rendererObject); this.baseTexture = new PIXI.CanvasSource({ resource: this.textureCanvas, }); this.textures = new PIXI.Texture({ source: this.baseTexture, }); this.rendererObject.texture = this.textures; super.create.apply(this, arguments); } setDepth(depth: number): void { if (this.rendererObject) { this.rendererObject!.zIndex = depth; } } dispose(): void { this.rendererObject?.destroy(); super.dispose(); } draw(data: TriangleGameObjectData): void { const { style, size } = data; const { line = [] } = style || {}; this.sizeBoxBounds = getSizeWidthAndHeight( data, line, getPoint(size.width / 2, size.height / 2, size.width, size.height) ); drawTriangle(this.textureCanvas, data, this.sizeBoxBounds); this.textures.destroy(); this.baseTexture = new PIXI.CanvasSource({ resource: this.textureCanvas, }); this.textures = new PIXI.Texture({ source: this.baseTexture, }); this.rendererObject.texture = this.textures; } update(data: TriangleGameObjectData): void { const { style, radius, shadow, ...otherData } = data; const { size } = otherData; const dataJSON = JSON.stringify({ style, radius, size, shadow }); if (dataJSON !== this.dataJSON) { this.draw(data); } if (this.rendererObject) { setGameObjectBaseData(this, otherData, true); this.rendererObject.visible = getVisible(data, this); } this.dataJSON = dataJSON; super.update(data); } }