import { GameObjectBaseType, GameResource, } from '@gedit/render-engine'; import { setGameObjectBaseData, getVisible, } from '../../utils'; import { drawCircle, getSizeWidthAndHeight, CircleGameObjectData, } from '@gedit/canvas-draw'; import { PixiGraphicsGameObject } from './common'; export class CircleGameObject extends PixiGraphicsGameObject { static type = GameObjectBaseType.CIRCLE; rendererObject: PIXI.Sprite; 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.textureCanvas = this.scene.rootTextureCanvas; 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: CircleGameObjectData): void { this.sizeBoxBounds = getSizeWidthAndHeight( data, data.style.line!, [ { x: 0, y: 0 }, { x: data.size.width, y: 0 }, { x: data.size.width, y: data.size.height }, { x: 0, y: data.size.height }, ], true ); drawCircle(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: CircleGameObjectData): void { const { style, shadow, ...otherData } = data; const dataJSON = JSON.stringify({ style, size: data.size, shadow }); if (dataJSON !== this.dataJSON) { // console.time('ddd'); this.draw(data); } if (this.rendererObject) { setGameObjectBaseData(this, otherData, true); this.rendererObject.visible = getVisible(data, this); } this.dataJSON = dataJSON; super.update(data); } }