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

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