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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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._onCreateAnchor({ position })
  42. }
  43. this._plotEvent.fire(PlotEventType.DRAW_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.ANCHOR_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. _onCreateAnchor({ 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, -100),
  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._plotEvent.on(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
  110. this._plotEvent.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._plotEvent.off(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
  121. this._plotEvent.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. return this
  133. }
  134. /**
  135. *
  136. * @param type
  137. * @param callback
  138. * @param context
  139. * @returns {DrawTool}
  140. */
  141. off(type, callback, context) {
  142. this._plotEvent.off(type, callback, context || this)
  143. return this
  144. }
  145. /**
  146. *
  147. * @param type
  148. * @param parmas
  149. * @returns {DrawTool}
  150. */
  151. fire(type, parmas) {
  152. this._plotEvent.fire(type, parmas)
  153. return this
  154. }
  155. /**
  156. *
  157. * @param options
  158. * @returns {DrawTool}
  159. */
  160. activate(options = {}) {
  161. this._viewer.tooltip.enable = true
  162. this._options = { ...DEF_OPTS, ...options }
  163. this._unbindEvent()
  164. this._bindEvent()
  165. this.fire(PlotEventType.DRAW_START, this._options)
  166. return this
  167. }
  168. /**
  169. *
  170. * @returns {DrawTool}
  171. */
  172. deactivate() {
  173. this._unbindEvent()
  174. this._viewer.tooltip.enable = false
  175. this._anchorLayer.entities.removeAll()
  176. return this
  177. }
  178. /**
  179. *
  180. * @param viewer
  181. */
  182. install(viewer) {
  183. this._viewer = viewer
  184. this._viewer.dataSources.add(this._anchorLayer)
  185. Object.defineProperty(this._viewer, 'drawTool', {
  186. value: this,
  187. writable: false
  188. })
  189. }
  190. }
  191. export default DrawTool