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

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