您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

index.ts 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { Mutable, Shape, ShapeUtility } from 'types'
  2. import { createShape, getShapeUtils } from 'state/shape-utils'
  3. import vec from 'utils/vec'
  4. import Vector from './vector'
  5. import Utils from './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: Mutable<T>
  13. private utils: ShapeUtility<T>
  14. constructor(props: T) {
  15. this._shape = createShape(props.type, props) as Mutable<T>
  16. this.utils = getShapeUtils<T>(this._shape)
  17. codeShapes.add(this)
  18. }
  19. destroy(): void {
  20. codeShapes.delete(this)
  21. }
  22. moveTo(point: Vector): CodeShape<T> {
  23. this.utils.setProperty(this._shape, 'point', Utils.vectorToPoint(point))
  24. return this
  25. }
  26. translate(delta: Vector): CodeShape<T> {
  27. this.utils.setProperty(
  28. this._shape,
  29. 'point',
  30. vec.add(this._shape.point, Utils.vectorToPoint(delta))
  31. )
  32. return this
  33. }
  34. rotate(rotation: number): CodeShape<T> {
  35. this.utils.setProperty(this._shape, 'rotation', rotation)
  36. return this
  37. }
  38. getBounds(): CodeShape<T> {
  39. this.utils.getBounds(this.shape)
  40. return this
  41. }
  42. hitTest(point: Vector): CodeShape<T> {
  43. this.utils.hitTest(this.shape, Utils.vectorToPoint(point))
  44. return this
  45. }
  46. get shape(): T {
  47. return this._shape
  48. }
  49. get point(): number[] {
  50. return [...this.shape.point]
  51. }
  52. get rotation(): number {
  53. return this.shape.rotation
  54. }
  55. }