Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. let properties = this._pickedAnchor.properties.getValue(
  53. Cesium.JulianDate.now()
  54. )
  55. this._pickedAnchor.position.setValue(position)
  56. this._plotEvent.fire(PlotEventType.EDIT_ANCHOR_STOP, {
  57. index: properties.index,
  58. position: position
  59. })
  60. }
  61. this._isMoving = false
  62. } else {
  63. if (!e.target || !e.target.id) {
  64. return false
  65. }
  66. this._pickedAnchor = e.target.id
  67. this._isMoving = true
  68. }
  69. }
  70. /**
  71. *
  72. * @param e
  73. * @private
  74. */
  75. _onMouseMove(e) {
  76. this._viewer.tooltip.showAt(e.windowPosition, this._tooltipMess)
  77. if (!this._isMoving) {
  78. return false
  79. }
  80. let position =
  81. this._options.clampToModel && e.position ? e.position : e.surfacePosition
  82. if (!position) {
  83. return false
  84. }
  85. if (
  86. this._pickedAnchor &&
  87. this._pickedAnchor.position &&
  88. this._pickedAnchor.properties
  89. ) {
  90. let properties = this._pickedAnchor.properties.getValue(
  91. Cesium.JulianDate.now()
  92. )
  93. this._pickedAnchor.position.setValue(position)
  94. this._plotEvent.fire(PlotEventType.ANCHOR_MOVING, {
  95. index: properties.index,
  96. position: position
  97. })
  98. }
  99. }
  100. /**
  101. *
  102. * @param e
  103. * @private
  104. */
  105. _onRightClick(e) {
  106. this._plotEvent.fire(
  107. PlotEventType.DRAW_STOP,
  108. this._options.clampToModel && e.position ? e.position : e.surfacePosition
  109. )
  110. }
  111. /**
  112. *
  113. * @param position
  114. * @param index
  115. * @param isCenter
  116. * @param isMid
  117. * @private
  118. */
  119. _onAddAnchor({ 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.add({
  126. position: position,
  127. billboard: {
  128. image: image,
  129. width: 12,
  130. height: 12,
  131. eyeOffset: new Cesium.ConstantProperty(
  132. new Cesium.Cartesian3(0, 0, -500)
  133. ),
  134. heightReference:
  135. this._viewer.scene.mode === Cesium.SceneMode.SCENE3D &&
  136. !this._options.clampToModel
  137. ? Cesium.HeightReference.CLAMP_TO_GROUND
  138. : Cesium.HeightReference.NONE
  139. },
  140. properties: {
  141. isMid: isMid,
  142. index: index
  143. }
  144. })
  145. this._anchors.push(anchor)
  146. }
  147. /**
  148. *
  149. * @private
  150. */
  151. _onClearAnchor() {
  152. this._anchorLayer.entities.removeAll()
  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.ADD_ANCHOR, this._onAddAnchor, this)
  163. this.on(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  164. }
  165. /**
  166. *
  167. * @private
  168. */
  169. _unbindEvent() {
  170. this._viewer.off(MouseEventType.CLICK, this._onClick, this)
  171. this._viewer.off(MouseEventType.MOUSE_MOVE, this._onMouseMove, this)
  172. this._viewer.off(MouseEventType.RIGHT_CLICK, this._onRightClick, this)
  173. this.off(PlotEventType.ADD_ANCHOR, this._onAddAnchor, this)
  174. this.off(PlotEventType.CLEAR_ANCHOR, this._onClearAnchor, this)
  175. }
  176. /**
  177. *
  178. * @param p1
  179. * @param p2
  180. * @returns {Cartesian3}
  181. */
  182. computeMidPosition(p1, p2) {
  183. let c1 = Cesium.Ellipsoid.WGS84.cartesianToCartographic(p1)
  184. let c2 = Cesium.Ellipsoid.WGS84.cartesianToCartographic(p2)
  185. let cm = new Cesium.EllipsoidGeodesic(c1, c2).interpolateUsingFraction(0.5)
  186. return Cesium.Ellipsoid.WGS84.cartographicToCartesian(cm)
  187. }
  188. /**
  189. *
  190. * @param type
  191. * @param callback
  192. * @param context
  193. * @returns {EditTool}
  194. */
  195. on(type, callback, context) {
  196. this._plotEvent.on(type, callback, context || this)
  197. this._plotEvent.on(type, callback, context || this)
  198. return this
  199. }
  200. /**
  201. *
  202. * @param type
  203. * @param callback
  204. * @param context
  205. * @returns {EditTool}
  206. */
  207. off(type, callback, context) {
  208. this._plotEvent.off(type, callback, context || this)
  209. this._plotEvent.off(type, callback, context || this)
  210. return this
  211. }
  212. /**
  213. *
  214. * @param type
  215. * @param parmas
  216. * @returns {EditTool}
  217. */
  218. fire(type, parmas) {
  219. this._plotEvent.fire(type, parmas)
  220. return this
  221. }
  222. /**
  223. *
  224. * @param options
  225. * @returns {EditTool}
  226. */
  227. activate(options = {}) {
  228. this._viewer.tooltip.enable = true
  229. this._options = { ...DEF_OPTS, ...options }
  230. this._unbindEvent()
  231. this._bindEvent()
  232. this.fire(PlotEventType.DRAW_START, this._options)
  233. return this
  234. }
  235. /**
  236. *
  237. * @returns {EditTool}
  238. */
  239. deactivate() {
  240. this._unbindEvent()
  241. this._viewer.tooltip.enable = false
  242. this._anchorLayer.entities.removeAll()
  243. return this
  244. }
  245. /**
  246. *
  247. * @param viewer
  248. */
  249. install(viewer) {
  250. this._viewer = viewer
  251. this._viewer.dataSources.add(this._anchorLayer)
  252. Object.defineProperty(this._viewer, 'editTool', {
  253. value: this,
  254. writable: false
  255. })
  256. }
  257. }
  258. export default EditTool