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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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
  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 && this._anchors.length !== 0) {
  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
  91. })
  92. } else if (this._anchors.length === 0) {
  93. this._plotEvent.fire(PlotEventType.ANCHOR_MOVING, {
  94. position
  95. })
  96. }
  97. }
  98. /**
  99. *
  100. * @param e
  101. * @private
  102. */
  103. _onRightClick(e) {
  104. let position =
  105. this._options.clampToModel && e.position ? e.position : e.surfacePosition
  106. this._plotEvent.fire(PlotEventType.EDIT_STOP, {
  107. pickedAnchor: this._pickedAnchor,
  108. position
  109. })
  110. }
  111. /**
  112. *
  113. * @param position
  114. * @param index
  115. * @param isCenter
  116. * @param isMid
  117. * @private
  118. */
  119. _onCreateAnchor({ position, index, isCenter = false, isMid = false }) {
  120. let image = isMid
  121. ? this._options.icon_midAnchor
  122. : isCenter
  123. ? this._options.icon_center
  124. : this._options.icon_anchor
  125. let anchor = this._anchorLayer.entities.add({
  126. position: position,
  127. billboard: {
  128. image: image,
  129. width: this._options.icon_size[0],
  130. height: this._options.icon_size[1],
  131. eyeOffset: new Cesium.Cartesian3(0, 0, -100),
  132. heightReference:
  133. this._viewer.scene.mode === Cesium.SceneMode.SCENE3D &&
  134. !this._options.clampToModel
  135. ? Cesium.HeightReference.CLAMP_TO_GROUND
  136. : Cesium.HeightReference.NONE
  137. },
  138. properties: {
  139. isMid: isMid,
  140. index: index
  141. }
  142. })
  143. this._anchors.push(anchor)
  144. }
  145. /**
  146. *
  147. * @param index
  148. * @param position
  149. * @private
  150. */
  151. _onUpdateAnchor({ index, position }) {
  152. this._anchors[index] && this._anchors[index].position.setValue(position)
  153. }
  154. /**
  155. *
  156. * @private
  157. */
  158. _onClearAnchor() {
  159. this._anchorLayer.entities.removeAll()
  160. this._anchors = []
  161. }
  162. /**
  163. *
  164. * @private
  165. */
  166. _bindEvent() {
  167. this._viewer.on(MouseEventType.CLICK, this._onClick, this)
  168. this._viewer.on(MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
  169. this._viewer.on(MouseEventType.RIGHT_CLICK, this._onRightClick, this)
  170. this._plotEvent.on(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
  171. this._plotEvent.on(PlotEventType.UPDATE_ANCHOR, this._onUpdateAnchor, this)
  172. this._plotEvent.on(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  173. }
  174. /**
  175. *
  176. * @private
  177. */
  178. _unbindEvent() {
  179. this._viewer.off(MouseEventType.CLICK, this._onClick, this)
  180. this._viewer.off(MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
  181. this._viewer.off(MouseEventType.RIGHT_CLICK, this._onRightClick, this)
  182. this._plotEvent.off(PlotEventType.CREATE_ANCHOR, this._onCreateAnchor, this)
  183. this._plotEvent.off(PlotEventType.UPDATE_ANCHOR, this._onUpdateAnchor, this)
  184. this._plotEvent.off(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  185. }
  186. /**
  187. *
  188. * @param type
  189. * @param callback
  190. * @param context
  191. * @returns {EditTool}
  192. */
  193. on(type, callback, context) {
  194. this._plotEvent.on(type, callback, context || this)
  195. return this
  196. }
  197. /**
  198. *
  199. * @param type
  200. * @param callback
  201. * @param context
  202. * @returns {EditTool}
  203. */
  204. off(type, callback, context) {
  205. this._plotEvent.off(type, callback, context || this)
  206. return this
  207. }
  208. /**
  209. *
  210. * @param type
  211. * @param parmas
  212. * @returns {EditTool}
  213. */
  214. fire(type, parmas) {
  215. this._plotEvent.fire(type, parmas)
  216. return this
  217. }
  218. /**
  219. *
  220. * @param options
  221. * @returns {EditTool}
  222. */
  223. activate(options = {}) {
  224. this._viewer.tooltip.enable = true
  225. this._options = { ...DEF_OPTS, ...options }
  226. this._unbindEvent()
  227. this._bindEvent()
  228. this.fire(PlotEventType.EDIT_START, this._options)
  229. return this
  230. }
  231. /**
  232. *
  233. * @returns {EditTool}
  234. */
  235. deactivate() {
  236. this._unbindEvent()
  237. this._viewer.tooltip.enable = false
  238. this._anchorLayer.entities.removeAll()
  239. this._anchors = []
  240. return this
  241. }
  242. /**
  243. *
  244. * @param viewer
  245. */
  246. install(viewer) {
  247. this._viewer = viewer
  248. this._viewer.dataSources.add(this._anchorLayer)
  249. Object.defineProperty(this._viewer, 'editTool', {
  250. value: this,
  251. writable: false
  252. })
  253. }
  254. }
  255. export default EditTool