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

EditTool.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * @Author: Caven
  3. * @Date: 2021-07-14 20:28:10
  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_BLUE = require('@dc-modules/images/circle_blue.png')
  9. const IMG_CIRCLE_YELLOW = require('@dc-modules/images/circle_yellow.png')
  10. const DEF_OPTS = {
  11. icon_center: IMG_CIRCLE_YELLOW,
  12. icon_anchor: IMG_CIRCLE_RED,
  13. icon_midAnchor: IMG_CIRCLE_BLUE,
  14. icon_size: [12, 12],
  15. clampToModel: true
  16. }
  17. class EditTool {
  18. constructor() {
  19. this._viewer = undefined
  20. this._anchorLayer = new Cesium.CustomDataSource('edit-anchor-layer')
  21. this._options = {}
  22. this._plotEvent = new PlotEvent()
  23. this._tooltipMess = undefined
  24. this._pickedAnchor = undefined
  25. this._isMoving = false
  26. this._anchors = []
  27. }
  28. set tooltipMess(tooltipMess) {
  29. this._tooltipMess = tooltipMess
  30. return this
  31. }
  32. /**
  33. *
  34. * @param e
  35. * @returns {boolean}
  36. * @private
  37. */
  38. _onClick(e) {
  39. if (this._isMoving) {
  40. let position =
  41. this._options.clampToModel && e.position
  42. ? e.position
  43. : e.surfacePosition
  44. if (!position) {
  45. return false
  46. }
  47. if (
  48. this._pickedAnchor &&
  49. this._pickedAnchor.position &&
  50. this._pickedAnchor.properties
  51. ) {
  52. this._pickedAnchor.position.setValue(position)
  53. this._plotEvent.fire(PlotEventType.EDIT_ANCHOR_STOP, {
  54. pickedAnchor: this._pickedAnchor,
  55. position: position
  56. })
  57. }
  58. this._isMoving = false
  59. } else {
  60. if (!e.target || !e.target.id) {
  61. return false
  62. }
  63. this._pickedAnchor = e.target.id
  64. this._isMoving = true
  65. }
  66. }
  67. /**
  68. *
  69. * @param e
  70. * @private
  71. */
  72. _onMouseMove(e) {
  73. this._viewer.tooltip.showAt(e.windowPosition, this._tooltipMess)
  74. if (!this._isMoving) {
  75. return false
  76. }
  77. let position =
  78. this._options.clampToModel && e.position ? e.position : e.surfacePosition
  79. if (!position) {
  80. return false
  81. }
  82. if (
  83. this._pickedAnchor &&
  84. this._pickedAnchor.position &&
  85. this._pickedAnchor.properties
  86. ) {
  87. this._pickedAnchor.position.setValue(position)
  88. this._plotEvent.fire(PlotEventType.ANCHOR_MOVING, {
  89. pickedAnchor: this._pickedAnchor,
  90. position: position
  91. })
  92. }
  93. }
  94. /**
  95. *
  96. * @param e
  97. * @private
  98. */
  99. _onRightClick(e) {
  100. let position =
  101. this._options.clampToModel && e.position ? e.position : e.surfacePosition
  102. this._plotEvent.fire(PlotEventType.DRAW_STOP, {
  103. pickedAnchor: this._pickedAnchor,
  104. position: position
  105. })
  106. }
  107. /**
  108. *
  109. * @param position
  110. * @param index
  111. * @param isCenter
  112. * @param isMid
  113. * @private
  114. */
  115. _onCreateAnchor({ position, index, isCenter = false, isMid = false }) {
  116. let image = isMid
  117. ? this._options.icon_midAnchor
  118. : isCenter
  119. ? this._options.icon_center
  120. : this._options.icon_anchor
  121. let anchor = this._anchorLayer.add({
  122. position: position,
  123. billboard: {
  124. image: image,
  125. width: 12,
  126. height: 12,
  127. eyeOffset: new Cesium.ConstantProperty(
  128. new Cesium.Cartesian3(0, 0, -500)
  129. ),
  130. heightReference:
  131. this._viewer.scene.mode === Cesium.SceneMode.SCENE3D &&
  132. !this._options.clampToModel
  133. ? Cesium.HeightReference.CLAMP_TO_GROUND
  134. : Cesium.HeightReference.NONE
  135. },
  136. properties: {
  137. isMid: isMid,
  138. index: index
  139. }
  140. })
  141. this._anchors.push(anchor)
  142. }
  143. _onUpdateAnchor({ index, position }) {
  144. this._anchors[index] && this._anchors[index].position.setValue(position)
  145. }
  146. /**
  147. *
  148. * @private
  149. */
  150. _onClearAnchor() {
  151. this._anchorLayer.entities.removeAll()
  152. this._anchors = []
  153. }
  154. /**
  155. *
  156. * @private
  157. */
  158. _bindEvent() {
  159. this._viewer.on(MouseEventType.CLICK, this._onClick, this)
  160. this._viewer.on(MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
  161. this._viewer.on(MouseEventType.RIGHT_CLICK, this._onRightClick, this)
  162. this.on(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
  163. this.on(PlotEventType.UPDATE_ANCHOR, this._onUpdateAnchor, this)
  164. this.on(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  165. }
  166. /**
  167. *
  168. * @private
  169. */
  170. _unbindEvent() {
  171. this._viewer.off(MouseEventType.CLICK, this._onClick, this)
  172. this._viewer.off(MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
  173. this._viewer.off(MouseEventType.RIGHT_CLICK, this._onRightClick, this)
  174. this.off(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
  175. this.off(PlotEventType.UPDATE_ANCHOR, this._onUpdateAnchor, this)
  176. this.off(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  177. }
  178. /**
  179. *
  180. * @param type
  181. * @param callback
  182. * @param context
  183. * @returns {EditTool}
  184. */
  185. on(type, callback, context) {
  186. this._plotEvent.on(type, callback, context || this)
  187. this._plotEvent.on(type, callback, context || this)
  188. return this
  189. }
  190. /**
  191. *
  192. * @param type
  193. * @param callback
  194. * @param context
  195. * @returns {EditTool}
  196. */
  197. off(type, callback, context) {
  198. this._plotEvent.off(type, callback, context || this)
  199. this._plotEvent.off(type, callback, context || this)
  200. return this
  201. }
  202. /**
  203. *
  204. * @param type
  205. * @param parmas
  206. * @returns {EditTool}
  207. */
  208. fire(type, parmas) {
  209. this._plotEvent.fire(type, parmas)
  210. return this
  211. }
  212. /**
  213. *
  214. * @param options
  215. * @returns {EditTool}
  216. */
  217. activate(options = {}) {
  218. this._viewer.tooltip.enable = true
  219. this._options = { ...DEF_OPTS, ...options }
  220. this._unbindEvent()
  221. this._bindEvent()
  222. this.fire(PlotEventType.DRAW_START, this._options)
  223. return this
  224. }
  225. /**
  226. *
  227. * @returns {EditTool}
  228. */
  229. deactivate() {
  230. this._unbindEvent()
  231. this._viewer.tooltip.enable = false
  232. this._anchorLayer.entities.removeAll()
  233. return this
  234. }
  235. /**
  236. *
  237. * @param viewer
  238. */
  239. install(viewer) {
  240. this._viewer = viewer
  241. this._viewer.dataSources.add(this._anchorLayer)
  242. Object.defineProperty(this._viewer, 'editTool', {
  243. value: this,
  244. writable: false
  245. })
  246. }
  247. }
  248. export default EditTool