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.

DrawTool.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * @Author: Caven
  3. * @Date: 2021-07-14 20:28:14
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { MouseEventType, PlotEventType, PlotEvent } from '@dc-modules/event'
  7. const IMG_CIRCLE_RED = require('@dc-modules/images/circle_red.png')
  8. const IMG_CIRCLE_YELLOW = require('@dc-modules/images/circle_yellow.png')
  9. const DEF_OPTS = {
  10. icon_center: IMG_CIRCLE_YELLOW,
  11. icon_anchor: IMG_CIRCLE_RED,
  12. icon_size: [12, 12],
  13. clampToModel: false
  14. }
  15. class DrawTool {
  16. constructor() {
  17. this._viewer = undefined
  18. this._anchorLayer = new Cesium.CustomDataSource('draw-anchor-layer')
  19. this._floatingAnchor = undefined
  20. this._options = {}
  21. this._plotEvent = new PlotEvent()
  22. this._tooltipMess = undefined
  23. }
  24. set tooltipMess(tooltipMess) {
  25. this._tooltipMess = tooltipMess
  26. return this
  27. }
  28. /**
  29. *
  30. * @param e
  31. * @returns {boolean}
  32. * @private
  33. */
  34. _onClick(e) {
  35. let position =
  36. this._options.clampToModel && e.position ? e.position : e.surfacePosition
  37. if (!position) {
  38. return false
  39. }
  40. if (!this._floatingAnchor) {
  41. this._floatingAnchor = this._onAddAnchor({ position })
  42. }
  43. this._plotEvent.fire(PlotEventType.ADD_ANCHOR, position)
  44. }
  45. /**
  46. *
  47. * @param e
  48. * @private
  49. */
  50. _onMouseMove(e) {
  51. this._viewer.tooltip.showAt(e.windowPosition, this._tooltipMess)
  52. let position =
  53. this._options.clampToModel && e.position ? e.position : e.surfacePosition
  54. if (!position) {
  55. return false
  56. }
  57. this._floatingAnchor && this._floatingAnchor.position.setValue(position)
  58. this._plotEvent.fire(PlotEventType.DRAW_MOVING, position)
  59. }
  60. /**
  61. *
  62. * @param e
  63. * @private
  64. */
  65. _onRightClick(e) {
  66. this._plotEvent.fire(
  67. PlotEventType.DRAW_STOP,
  68. this._options.clampToModel && e.position ? e.position : e.surfacePosition
  69. )
  70. }
  71. /**
  72. *
  73. * @param position
  74. * @param isCenter
  75. * @returns {*}
  76. * @private
  77. */
  78. _onAddAnchor({ position, isCenter = false }) {
  79. this._anchorLayer.entities.add({
  80. position: position,
  81. billboard: {
  82. image: isCenter ? this._options.icon_center : this._options.icon_anchor,
  83. width: this._options.icon_size[0],
  84. height: this._options.icon_size[1],
  85. eyeOffset: new Cesium.Cartesian3(0, 0, -500),
  86. heightReference:
  87. this._viewer.scene.mode === Cesium.SceneMode.SCENE3D &&
  88. !this._options.clampToModel
  89. ? Cesium.HeightReference.CLAMP_TO_GROUND
  90. : Cesium.HeightReference.NONE
  91. }
  92. })
  93. }
  94. /**
  95. *
  96. * @private
  97. */
  98. _onClearAnchor() {
  99. this._anchorLayer.entities.removeAll()
  100. }
  101. /**
  102. *
  103. * @private
  104. */
  105. _bindEvent() {
  106. this._viewer.on(MouseEventType.CLICK, this._onClick, this)
  107. this._viewer.on(MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
  108. this._viewer.on(MouseEventType.RIGHT_CLICK, this._onRightClick, this)
  109. this.on(PlotEventType.ADD_ANCHOR, this._onAddAnchor, this)
  110. this.on(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  111. }
  112. /**
  113. *
  114. * @private
  115. */
  116. _unbindEvent() {
  117. this._viewer.off(MouseEventType.CLICK, this._onClick, this)
  118. this._viewer.off(MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
  119. this._viewer.off(MouseEventType.RIGHT_CLICK, this._onRightClick, this)
  120. this.off(PlotEventType.ADD_ANCHOR, this._onAddAnchor, this)
  121. this.off(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  122. }
  123. /**
  124. *
  125. * @param type
  126. * @param callback
  127. * @param context
  128. * @returns {DrawTool}
  129. */
  130. on(type, callback, context) {
  131. this._plotEvent.on(type, callback, context || this)
  132. this._plotEvent.on(type, callback, context || this)
  133. return this
  134. }
  135. /**
  136. *
  137. * @param type
  138. * @param callback
  139. * @param context
  140. * @returns {DrawTool}
  141. */
  142. off(type, callback, context) {
  143. this._plotEvent.off(type, callback, context || this)
  144. this._plotEvent.off(type, callback, context || this)
  145. return this
  146. }
  147. /**
  148. *
  149. * @param type
  150. * @param parmas
  151. * @returns {DrawTool}
  152. */
  153. fire(type, parmas) {
  154. this._plotEvent.fire(type, parmas)
  155. return this
  156. }
  157. /**
  158. *
  159. * @param options
  160. * @returns {DrawTool}
  161. */
  162. activate(options = {}) {
  163. this._viewer.tooltip.enable = true
  164. this._options = { ...DEF_OPTS, ...options }
  165. this._unbindEvent()
  166. this._bindEvent()
  167. this.fire(PlotEventType.DRAW_START, this._options)
  168. return this
  169. }
  170. /**
  171. *
  172. * @returns {DrawTool}
  173. */
  174. deactivate() {
  175. this._unbindEvent()
  176. this._viewer.tooltip.enable = false
  177. this._anchorLayer.entities.removeAll()
  178. return this
  179. }
  180. /**
  181. *
  182. * @param viewer
  183. */
  184. install(viewer) {
  185. this._viewer = viewer
  186. this._viewer.dataSources.add(this._anchorLayer)
  187. Object.defineProperty(this._viewer, 'drawTool', {
  188. value: this,
  189. writable: false
  190. })
  191. }
  192. }
  193. export default DrawTool