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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-01-31 19:45:32
  4. */
  5. import { PlotEventType } from '@dc-modules/event'
  6. class Draw {
  7. constructor(style) {
  8. this._style = style
  9. this._viewer = undefined
  10. this._layer = undefined
  11. this._delegate = undefined
  12. this._options = {}
  13. this._positions = []
  14. }
  15. get drawTool() {
  16. return this._viewer.drawTool
  17. }
  18. /**
  19. * The hook for mount viewer
  20. * Subclasses need to be overridden
  21. * @private
  22. */
  23. _mountedHook() {}
  24. /**
  25. * The hook for mount stop
  26. * Subclasses need to be overridden
  27. * @private
  28. */
  29. _stopdHook() {}
  30. /**
  31. *
  32. * @param position
  33. * @private
  34. */
  35. _onDrawAnchor(position) {}
  36. /**
  37. *
  38. * @param position
  39. * @private
  40. */
  41. _onAnchorMoving(position) {
  42. this._positions.pop()
  43. this._positions.push(position)
  44. }
  45. /**
  46. *
  47. * @private
  48. */
  49. _onDrawStop() {
  50. this._unbindEvent()
  51. this._viewer.drawTool.deactivate()
  52. this._layer.entities.remove(this._delegate)
  53. this._stopdHook()
  54. }
  55. /**
  56. *
  57. * @private
  58. */
  59. _bindEvent() {
  60. this.drawTool.on(PlotEventType.DRAW_ANCHOR, this._onDrawAnchor, this)
  61. this.drawTool.on(PlotEventType.ANCHOR_MOVING, this._onAnchorMoving, this)
  62. this.drawTool.on(PlotEventType.DRAW_STOP, this._onDrawStop, this)
  63. }
  64. /**
  65. *
  66. * @private
  67. */
  68. _unbindEvent() {
  69. this.drawTool.off(PlotEventType.DRAW_ANCHOR, this._onDrawAnchor, this)
  70. this.drawTool.off(PlotEventType.ANCHOR_MOVING, this._onAnchorMoving, this)
  71. this.drawTool.off(PlotEventType.DRAW_STOP, this._onDrawStop, this)
  72. }
  73. /**
  74. *
  75. * @param plot
  76. * @param options
  77. * @returns {Draw}
  78. */
  79. start(plot, options) {
  80. this._viewer = plot.viewer
  81. this._layer = plot.layer
  82. this._options = options
  83. this._viewer.editTool.deactivate()
  84. this._viewer.drawTool.activate(options)
  85. this._mountedHook()
  86. this._unbindEvent()
  87. this._bindEvent()
  88. return this
  89. }
  90. /**
  91. *
  92. * @returns {Draw}
  93. */
  94. stop() {
  95. this.drawTool.fire(PlotEventType.DRAW_STOP)
  96. return this
  97. }
  98. }
  99. export default Draw