import { Mutable, Shape, ShapeUtility } from 'types' import { createShape, getShapeUtils } from 'state/shape-utils' import vec from 'utils/vec' export const codeShapes = new Set>([]) /** * A base class for code shapes. Note that creating a shape adds it to the * shape map, while deleting it removes it from the collected shapes set */ export default class CodeShape { private _shape: Mutable private utils: ShapeUtility constructor(props: T) { this._shape = createShape(props.type, props) as Mutable this.utils = getShapeUtils(this._shape) codeShapes.add(this) } export(): Mutable { return { ...this._shape } } destroy(): void { codeShapes.delete(this) } moveTo(point: number[]): CodeShape { this.utils.setProperty(this._shape, 'point', point) return this } translate(delta: number[]): CodeShape { this.utils.setProperty( this._shape, 'point', vec.add(this._shape.point, delta) ) return this } rotate(rotation: number): CodeShape { this.utils.setProperty(this._shape, 'rotation', rotation) return this } getBounds(): CodeShape { this.utils.getBounds(this.shape) return this } hitTest(point: number[]): CodeShape { this.utils.hitTest(this.shape, point) return this } get shape(): T { return this._shape } get point(): number[] { return [...this.shape.point] } get rotation(): number { return this.shape.rotation } }