Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.ts 1.3KB

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