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.

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