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

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. maxAnchorSize: 999
  15. }
  16. class DrawTool {
  17. constructor() {
  18. this._viewer = undefined
  19. this._anchorLayer = new Cesium.CustomDataSource('draw-anchor-layer')
  20. this._floatingAnchor = undefined
  21. this._options = {}
  22. this._plotEvent = new PlotEvent()
  23. this._tooltipMess = undefined
  24. }
  25. set tooltipMess(tooltipMess) {
  26. this._tooltipMess = tooltipMess
  27. return this
  28. }
  29. /**
  30. *
  31. * @param e
  32. * @returns {boolean}
  33. * @private
  34. */
  35. _onClick(e) {
  36. let position =
  37. this._options.clampToModel && e.position ? e.position : e.surfacePosition
  38. if (!position) {
  39. return false
  40. }
  41. if (!this._floatingAnchor) {
  42. this._floatingAnchor = this._onCreateAnchor({ position })
  43. }
  44. this._plotEvent.fire(PlotEventType.DRAW_ANCHOR, position)
  45. }
  46. /**
  47. *
  48. * @param e
  49. * @private
  50. */
  51. _onMouseMove(e) {
  52. this._viewer.tooltip.showAt(e.windowPosition, this._tooltipMess)
  53. let position =
  54. this._options.clampToModel && e.position ? e.position : e.surfacePosition
  55. if (!position) {
  56. return false
  57. }
  58. this._floatingAnchor && this._floatingAnchor.position.setValue(position)
  59. this._plotEvent.fire(PlotEventType.ANCHOR_MOVING, position)
  60. }
  61. /**
  62. *
  63. * @param e
  64. * @private
  65. */
  66. _onRightClick(e) {
  67. this._plotEvent.fire(
  68. PlotEventType.DRAW_STOP,
  69. this._options.clampToModel && e.position ? e.position : e.surfacePosition
  70. )
  71. }
  72. /**
  73. *
  74. * @param position
  75. * @param isCenter
  76. * @returns {*}
  77. * @private
  78. */
  79. _onCreateAnchor({ position, isCenter = false }) {
  80. return this._anchorLayer.entities.add({
  81. position: position,
  82. billboard: {
  83. image: isCenter ? this._options.icon_center : this._options.icon_anchor,
  84. width: this._options.icon_size[0],
  85. height: this._options.icon_size[1],
  86. eyeOffset: new Cesium.Cartesian3(0, 0, -100),
  87. heightReference:
  88. this._viewer.scene.mode === Cesium.SceneMode.SCENE3D &&
  89. !this._options.clampToModel
  90. ? Cesium.HeightReference.CLAMP_TO_GROUND
  91. : Cesium.HeightReference.NONE
  92. }
  93. })
  94. }
  95. /**
  96. *
  97. * @private
  98. */
  99. _onClearAnchor() {
  100. this._anchorLayer.entities.removeAll()
  101. }
  102. /**
  103. *
  104. * @private
  105. */
  106. _bindEvent() {
  107. this._viewer.on(MouseEventType.CLICK, this._onClick, this)
  108. this._viewer.on(MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
  109. this._viewer.on(MouseEventType.RIGHT_CLICK, this._onRightClick, this)
  110. this._plotEvent.on(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
  111. this._plotEvent.on(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  112. }
  113. /**
  114. *
  115. * @private
  116. */
  117. _unbindEvent() {
  118. this._viewer.off(MouseEventType.CLICK, this._onClick, this)
  119. this._viewer.off(MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
  120. this._viewer.off(MouseEventType.RIGHT_CLICK, this._onRightClick, this)
  121. this._plotEvent.off(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
  122. this._plotEvent.off(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  123. }
  124. /**
  125. *
  126. * @param type
  127. * @param callback
  128. * @param context
  129. * @returns {DrawTool}
  130. */
  131. on(type, callback, context) {
  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. return this
  145. }
  146. /**
  147. *
  148. * @param type
  149. * @param parmas
  150. * @returns {DrawTool}
  151. */
  152. fire(type, parmas) {
  153. this._plotEvent.fire(type, parmas)
  154. return this
  155. }
  156. /**
  157. *
  158. * @param options
  159. * @returns {DrawTool}
  160. */
  161. activate(options = {}) {
  162. this._viewer.tooltip.enable = true
  163. this._options = { ...DEF_OPTS, ...options }
  164. this._unbindEvent()
  165. this._bindEvent()
  166. this.fire(PlotEventType.DRAW_START, this._options)
  167. return this
  168. }
  169. /**
  170. *
  171. * @returns {DrawTool}
  172. */
  173. deactivate() {
  174. this._unbindEvent()
  175. this._viewer.tooltip.enable = false
  176. this._anchorLayer.entities.removeAll()
  177. this._floatingAnchor = undefined
  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