You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.ts 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { Mutable, Shape, ShapeUtility } from 'types'
  2. import { createShape, getShapeUtils } from 'state/shape-utils'
  3. import vec from 'utils/vec'
  4. export const codeShapes = new Set<CodeShape<Shape>>([])
  5. /**
  6. * A base class for code shapes. Note that creating a shape adds it to the
  7. * shape map, while deleting it removes it from the collected shapes set
  8. */
  9. export default class CodeShape<T extends Shape> {
  10. private _shape: Mutable<T>
  11. private utils: ShapeUtility<T>
  12. constructor(props: T) {
  13. this._shape = createShape(props.type, props) as Mutable<T>
  14. this.utils = getShapeUtils<T>(this._shape)
  15. codeShapes.add(this)
  16. }
  17. export(): Mutable<T> {
  18. return { ...this._shape }
  19. }
  20. destroy(): void {
  21. codeShapes.delete(this)
  22. }
  23. moveTo(point: number[]): CodeShape<T> {
  24. this.utils.setProperty(this._shape, 'point', point)
  25. return this
  26. }
  27. translate(delta: number[]): CodeShape<T> {
  28. this.utils.setProperty(
  29. this._shape,
  30. 'point',
  31. vec.add(this._shape.point, delta)
  32. )
  33. return this
  34. }
  35. rotate(rotation: number): CodeShape<T> {
  36. this.utils.setProperty(this._shape, 'rotation', rotation)
  37. return this
  38. }
  39. getBounds(): CodeShape<T> {
  40. this.utils.getBounds(this.shape)
  41. return this
  42. }
  43. hitTest(point: number[]): CodeShape<T> {
  44. this.utils.hitTest(this.shape, point)
  45. return this
  46. }
  47. get shape(): T {
  48. return this._shape
  49. }
  50. get point(): number[] {
  51. return [...this.shape.point]
  52. }
  53. get rotation(): number {
  54. return this.shape.rotation
  55. }
  56. }