SelectPoint.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {CADElement, ElementProps} from '../core/element'
  2. import {SVGURI} from '../constant/Element'
  3. import {Point} from '../geometry'
  4. import Renderer from '../base/renderer'
  5. import { point } from '../core/fixedpoint'
  6. export interface StyleProps {
  7. border?: number,
  8. r?: number,
  9. show?: boolean,
  10. selectColor?: string,
  11. text?: string,
  12. color?: string
  13. }
  14. export interface SeftProps extends ElementProps {
  15. points: Array<Point & {text: string}>
  16. selectIndex: number,
  17. room: Array<point>,
  18. selectDom: boolean
  19. }
  20. class SelectPoint extends CADElement<SeftProps & StyleProps> {
  21. pointEles: Array<[SVGCircleElement, SVGTextElement]>
  22. polygon: SVGPathElement
  23. static Setting = new Map<Renderer, StyleProps>()
  24. constructor({border = 1, r = 4, show = true, color = 'rgb(0, 200, 175)', selectDom = false, selectColor = 'rgb(0, 200, 175)', ...args} : SeftProps & StyleProps) {
  25. super({border, r, show, color, selectColor, selectDom, ...args})
  26. this.update()
  27. }
  28. grentNode() {
  29. let node = document.createElementNS(SVGURI, 'g')
  30. this.polygon = document.createElementNS(SVGURI, 'path')
  31. this.polygon.setAttribute('stroke', this.selectColor)
  32. this.polygon.setAttribute('d', `M ${this.room.map(point => point.x + ' ' + point.y).join(' L ')} Z`)
  33. this.polygon.setAttribute('fill', 'rgba(0,0,0,0)')
  34. this.pointEles = this.points.map(() => {
  35. let inner = document.createElementNS(SVGURI, 'circle')
  36. inner.setAttribute('stroke', '#fff')
  37. let text = document.createElementNS(SVGURI, 'text')
  38. text.setAttribute('fill', '#fff')
  39. text.setAttribute('text-anchor', 'middle')
  40. text.setAttribute('dy', '.4em')
  41. node.appendChild(inner)
  42. node.appendChild(text)
  43. return [inner, text]
  44. })
  45. node.appendChild(this.polygon)
  46. return node
  47. }
  48. setHoverStyle() {
  49. this.selectDom = true
  50. }
  51. setUnHoverStyle() {
  52. this.selectDom = false
  53. }
  54. click(ev) {
  55. let selectIndex = this.pointEles.findIndex(([ele, text]) =>
  56. ele === ev.target ||
  57. text === ev.target ||
  58. ele.contains(ev.target) ||
  59. text.contains(ev.target)
  60. )
  61. if (this.selectIndex === selectIndex) {
  62. this.selectIndex = -1
  63. } else {
  64. this.selectIndex = selectIndex
  65. }
  66. }
  67. update() {
  68. if (this.selectDom || ~this.selectIndex) {
  69. this.polygon.setAttribute('stroke-width', (4 * this.multiple).toString())
  70. this.polygon.style.display = 'block'
  71. } else {
  72. this.polygon.style.display = 'none'
  73. }
  74. this.polygon.style.display = 'none'
  75. this.pointEles.forEach(([inner, text], i) => {
  76. inner.setAttribute('fill', this.selectIndex === i ? this.selectColor : this.color)
  77. inner.setAttribute('r', (this.r * this.multiple).toString())
  78. inner.setAttribute('stroke-width', (this.border * this.multiple).toString())
  79. inner.setAttribute('cx', this.points[i].x.toString())
  80. inner.setAttribute('cy', this.points[i].y.toString())
  81. text.setAttribute('font-size', (12 * this.multiple).toString())
  82. text.setAttribute('x', this.points[i].x.toString())
  83. text.setAttribute('y', this.points[i].y.toString())
  84. text.textContent = this.points[i].text
  85. text.setAttribute('fill', '#fff')
  86. })
  87. this.real.style.display = this.show ? 'inherit': 'none'
  88. }
  89. }
  90. export default SelectPoint