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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { Mutable, 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: Mutable<T>
  17. private utils: ShapeUtility<T>
  18. constructor(props: T) {
  19. this._shape = createShape(props.type, props) as Mutable<T>
  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.setProperty(this._shape, 'point', vectorToPoint(point))
  28. return this
  29. }
  30. translate(delta: Vector) {
  31. this.utils.setProperty(
  32. this._shape,
  33. 'point',
  34. vec.add(this._shape.point, vectorToPoint(delta))
  35. )
  36. return this
  37. }
  38. rotate(rotation: number) {
  39. this.utils.setProperty(this._shape, 'rotation', rotation)
  40. return this
  41. }
  42. getBounds() {
  43. this.utils.getBounds(this.shape)
  44. return this
  45. }
  46. hitTest(point: Vector) {
  47. this.utils.hitTest(this.shape, vectorToPoint(point))
  48. return this
  49. }
  50. get shape() {
  51. return this._shape
  52. }
  53. get point() {
  54. return [...this.shape.point]
  55. }
  56. get rotation() {
  57. return this.shape.rotation
  58. }
  59. }