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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.EDIT_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.entities.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. /**
  144. *
  145. * @param index
  146. * @param position
  147. * @private
  148. */
  149. _onUpdateAnchor({ index, position }) {
  150. this._anchors[index] && this._anchors[index].position.setValue(position)
  151. }
  152. /**
  153. *
  154. * @private
  155. */
  156. _onClearAnchor() {
  157. this._anchorLayer.entities.removeAll()
  158. this._anchors = []
  159. }
  160. /**
  161. *
  162. * @private
  163. */
  164. _bindEvent() {
  165. this._viewer.on(MouseEventType.CLICK, this._onClick, this)
  166. this._viewer.on(MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
  167. this._viewer.on(MouseEventType.RIGHT_CLICK, this._onRightClick, this)
  168. this._plotEvent.on(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
  169. this._plotEvent.on(PlotEventType.UPDATE_ANCHOR, this._onUpdateAnchor, this)
  170. this._plotEvent.on(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  171. }
  172. /**
  173. *
  174. * @private
  175. */
  176. _unbindEvent() {
  177. this._viewer.off(MouseEventType.CLICK, this._onClick, this)
  178. this._viewer.off(MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
  179. this._viewer.off(MouseEventType.RIGHT_CLICK, this._onRightClick, this)
  180. this._plotEvent.off(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
  181. this._plotEvent.off(PlotEventType.UPDATE_ANCHOR, this._onUpdateAnchor, this)
  182. this._plotEvent.off(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  183. }
  184. /**
  185. *
  186. * @param type
  187. * @param callback
  188. * @param context
  189. * @returns {EditTool}
  190. */
  191. on(type, callback, context) {
  192. this._plotEvent.on(type, callback, context || this)
  193. return this
  194. }
  195. /**
  196. *
  197. * @param type
  198. * @param callback
  199. * @param context
  200. * @returns {EditTool}
  201. */
  202. off(type, callback, context) {
  203. this._plotEvent.off(type, callback, context || this)
  204. return this
  205. }
  206. /**
  207. *
  208. * @param type
  209. * @param parmas
  210. * @returns {EditTool}
  211. */
  212. fire(type, parmas) {
  213. this._plotEvent.fire(type, parmas)
  214. return this
  215. }
  216. /**
  217. *
  218. * @param options
  219. * @returns {EditTool}
  220. */
  221. activate(options = {}) {
  222. this._viewer.tooltip.enable = true
  223. this._options = { ...DEF_OPTS, ...options }
  224. this._unbindEvent()
  225. this._bindEvent()
  226. this.fire(PlotEventType.EDIT_START, this._options)
  227. return this
  228. }
  229. /**
  230. *
  231. * @returns {EditTool}
  232. */
  233. deactivate() {
  234. this._unbindEvent()
  235. this._viewer.tooltip.enable = false
  236. this._anchorLayer.entities.removeAll()
  237. this._anchors = []
  238. return this
  239. }
  240. /**
  241. *
  242. * @param viewer
  243. */
  244. install(viewer) {
  245. this._viewer = viewer
  246. this._viewer.dataSources.add(this._anchorLayer)
  247. Object.defineProperty(this._viewer, 'editTool', {
  248. value: this,
  249. writable: false
  250. })
  251. }
  252. }
  253. export default EditTool