2021-06-21 21:35:28 +00:00
|
|
|
import { Mutable, Shape, ShapeUtility } from 'types'
|
|
|
|
import { createShape, getShapeUtils } from 'state/shape-utils'
|
2021-06-16 12:09:45 +00:00
|
|
|
import vec from 'utils/vec'
|
2021-05-14 22:56:41 +00:00
|
|
|
|
2021-05-15 13:02:13 +00:00
|
|
|
export const codeShapes = new Set<CodeShape<Shape>>([])
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-05-14 22:56:41 +00:00
|
|
|
export default class CodeShape<T extends Shape> {
|
2021-06-06 11:05:51 +00:00
|
|
|
private _shape: Mutable<T>
|
2021-05-25 09:09:51 +00:00
|
|
|
private utils: ShapeUtility<T>
|
2021-05-14 22:56:41 +00:00
|
|
|
|
|
|
|
constructor(props: T) {
|
2021-06-06 11:05:51 +00:00
|
|
|
this._shape = createShape(props.type, props) as Mutable<T>
|
2021-05-25 09:09:51 +00:00
|
|
|
this.utils = getShapeUtils<T>(this._shape)
|
2021-05-15 13:02:13 +00:00
|
|
|
codeShapes.add(this)
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 22:32:21 +00:00
|
|
|
export(): Mutable<T> {
|
|
|
|
return { ...this._shape }
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
destroy(): void {
|
2021-05-15 13:02:13 +00:00
|
|
|
codeShapes.delete(this)
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 22:32:21 +00:00
|
|
|
moveTo(point: number[]): CodeShape<T> {
|
|
|
|
this.utils.setProperty(this._shape, 'point', point)
|
2021-05-25 09:00:59 +00:00
|
|
|
return this
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 22:32:21 +00:00
|
|
|
translate(delta: number[]): CodeShape<T> {
|
2021-06-02 15:05:44 +00:00
|
|
|
this.utils.setProperty(
|
2021-05-25 09:00:59 +00:00
|
|
|
this._shape,
|
2021-06-02 15:05:44 +00:00
|
|
|
'point',
|
2021-06-23 22:32:21 +00:00
|
|
|
vec.add(this._shape.point, delta)
|
2021-05-25 09:00:59 +00:00
|
|
|
)
|
|
|
|
return this
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
rotate(rotation: number): CodeShape<T> {
|
2021-06-02 15:05:44 +00:00
|
|
|
this.utils.setProperty(this._shape, 'rotation', rotation)
|
2021-05-25 09:00:59 +00:00
|
|
|
return this
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
getBounds(): CodeShape<T> {
|
2021-05-25 09:00:59 +00:00
|
|
|
this.utils.getBounds(this.shape)
|
|
|
|
return this
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 22:32:21 +00:00
|
|
|
hitTest(point: number[]): CodeShape<T> {
|
|
|
|
this.utils.hitTest(this.shape, point)
|
2021-05-25 09:00:59 +00:00
|
|
|
return this
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
get shape(): T {
|
2021-05-14 22:56:41 +00:00
|
|
|
return this._shape
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
get point(): number[] {
|
2021-05-14 22:56:41 +00:00
|
|
|
return [...this.shape.point]
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
get rotation(): number {
|
2021-05-14 22:56:41 +00:00
|
|
|
return this.shape.rotation
|
|
|
|
}
|
|
|
|
}
|