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

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