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.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Shape } from "types"
  2. import * as vec from "utils/vec"
  3. import { getShapeUtils } from "lib/shapes"
  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: T
  11. constructor(props: T) {
  12. this._shape = props
  13. codeShapes.add(this)
  14. }
  15. destroy() {
  16. codeShapes.delete(this)
  17. }
  18. moveTo(point: number[]) {
  19. this.shape.point = point
  20. }
  21. translate(delta: number[]) {
  22. this.shape.point = vec.add(this._shape.point, delta)
  23. }
  24. rotate(rotation: number) {
  25. this.shape.rotation = rotation
  26. }
  27. scale(scale: number) {
  28. return getShapeUtils(this.shape).scale(this.shape, scale)
  29. }
  30. getBounds() {
  31. return getShapeUtils(this.shape).getBounds(this.shape)
  32. }
  33. hitTest(point: number[]) {
  34. return getShapeUtils(this.shape).hitTest(this.shape, point)
  35. }
  36. get shape() {
  37. return this._shape
  38. }
  39. get point() {
  40. return [...this.shape.point]
  41. }
  42. get rotation() {
  43. return this.shape.rotation
  44. }
  45. }