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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. eyeOffset: new Cesium.Cartesian3(0, 0, -100),
  90. heightReference:
  91. this._viewer.scene.mode === Cesium.SceneMode.SCENE3D &&
  92. !this._options.clampToModel
  93. ? Cesium.HeightReference.CLAMP_TO_GROUND
  94. : Cesium.HeightReference.NONE,
  95. },
  96. })
  97. }
  98. /**
  99. *
  100. * @private
  101. */
  102. _onClearAnchor() {
  103. this._anchorLayer.entities.removeAll()
  104. }
  105. /**
  106. *
  107. * @private
  108. */
  109. _bindEvent() {
  110. this._viewer.on(MouseEventType.CLICK, this._onClick, this)
  111. this._viewer.on(MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
  112. this._viewer.on(MouseEventType.RIGHT_CLICK, this._onRightClick, this)
  113. this._plotEvent.on(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
  114. this._plotEvent.on(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  115. }
  116. /**
  117. *
  118. * @private
  119. */
  120. _unbindEvent() {
  121. this._viewer.off(MouseEventType.CLICK, this._onClick, this)
  122. this._viewer.off(MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
  123. this._viewer.off(MouseEventType.RIGHT_CLICK, this._onRightClick, this)
  124. this._plotEvent.off(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
  125. this._plotEvent.off(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  126. }
  127. /**
  128. *
  129. * @param type
  130. * @param callback
  131. * @param context
  132. * @returns {DrawTool}
  133. */
  134. on(type, callback, context) {
  135. this._plotEvent.on(type, callback, context || this)
  136. return this
  137. }
  138. /**
  139. *
  140. * @param type
  141. * @param callback
  142. * @param context
  143. * @returns {DrawTool}
  144. */
  145. off(type, callback, context) {
  146. this._plotEvent.off(type, callback, context || this)
  147. return this
  148. }
  149. /**
  150. *
  151. * @param type
  152. * @param params
  153. * @returns {DrawTool}
  154. */
  155. fire(type, params = {}) {
  156. this._plotEvent.fire(type, params)
  157. return this
  158. }
  159. /**
  160. *
  161. * @param options
  162. * @returns {DrawTool}
  163. */
  164. activate(options = {}) {
  165. this._viewer.tooltip.enable = true
  166. this._options = { ...DEF_OPTS, ...options }
  167. this._unbindEvent()
  168. this._bindEvent()
  169. this.fire(PlotEventType.DRAW_START, this._options)
  170. return this
  171. }
  172. /**
  173. *
  174. * @returns {DrawTool}
  175. */
  176. deactivate() {
  177. this._unbindEvent()
  178. this._viewer.tooltip.enable = false
  179. this._anchorLayer.entities.removeAll()
  180. this._floatingAnchor = undefined
  181. return this
  182. }
  183. /**
  184. *
  185. * @param viewer
  186. */
  187. install(viewer) {
  188. this._viewer = viewer
  189. this._viewer.dataSources.add(this._anchorLayer)
  190. Object.defineProperty(this._viewer, 'drawTool', {
  191. value: this,
  192. writable: false,
  193. })
  194. }
  195. }
  196. export default DrawTool