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.

RoamingController.js 5.3KB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-04-01 10:36:36
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { RoamingEventType } from '@dc-modules/event'
  7. class RoamingController {
  8. constructor(viewer) {
  9. this._viewer = viewer
  10. this._roamingLayer = new Cesium.CustomDataSource('roaming-layer')
  11. viewer.dataSources.add(this._roamingLayer)
  12. this._postUpdateRemoveCallback = undefined
  13. this._startTime = undefined
  14. this._cache = {}
  15. this._activePath = undefined
  16. this._viewMode = undefined
  17. this._viewOption = {}
  18. }
  19. get startTime() {
  20. return this._startTime
  21. }
  22. get roamingLayer() {
  23. return this._roamingLayer.entities
  24. }
  25. /**
  26. * @private
  27. */
  28. _onPostUpdate(scene, time) {
  29. Object.keys(this._cache).forEach(key => {
  30. let path = this._cache[key]
  31. path.roamingEvent &&
  32. path.roamingEvent.fire(RoamingEventType.POST_UPDATE, {
  33. currentTime: time,
  34. viewMode: this._viewMode,
  35. viewOption: this._viewOption
  36. })
  37. })
  38. }
  39. /**
  40. *
  41. * @private
  42. */
  43. _addPostUpdateListener() {
  44. this._postUpdateRemoveCallback && this._postUpdateRemoveCallback()
  45. this._postUpdateRemoveCallback = this._viewer.scene.postUpdate.addEventListener(
  46. this._onPostUpdate,
  47. this
  48. )
  49. }
  50. /**
  51. * Sets time range
  52. * @param startTime
  53. * @returns {RoamingController}
  54. */
  55. setStartTime(startTime) {
  56. if (!startTime || !(startTime instanceof Date)) {
  57. throw new Error('RoamingController: the start time invalid ')
  58. }
  59. this._startTime = Cesium.JulianDate.fromDate(startTime)
  60. return this
  61. }
  62. /**
  63. * Starts play all path
  64. * @returns {RoamingController}
  65. */
  66. play() {
  67. this._viewer.clock.shouldAnimate = true
  68. this._viewer.clock.currentTime = this._startTime || Cesium.JulianDate.now()
  69. this._addPostUpdateListener()
  70. return this
  71. }
  72. /**
  73. *
  74. */
  75. pause() {
  76. this._viewer.clock.shouldAnimate = false
  77. this._viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY)
  78. this._viewer.delegate.trackedEntity = undefined
  79. this._postUpdateRemoveCallback && this._postUpdateRemoveCallback()
  80. this._postUpdateRemoveCallback = undefined
  81. return this
  82. }
  83. /**
  84. *
  85. */
  86. restore() {
  87. this._viewer.clock.shouldAnimate = true
  88. this._postUpdateRemoveCallback && this._postUpdateRemoveCallback()
  89. this._postUpdateRemoveCallback = this._viewer.scene.postUpdate.addEventListener(
  90. this._onPostUpdate,
  91. this
  92. )
  93. this._addPostUpdateListener()
  94. return this
  95. }
  96. /**
  97. *
  98. * @param speed
  99. * @returns {RoamingController}
  100. */
  101. changeSpeed(speed) {
  102. this._viewer.clock.multiplier = speed
  103. return this
  104. }
  105. /**
  106. * Adds a path
  107. * @param path
  108. * @returns {RoamingController}
  109. */
  110. addPath(path) {
  111. if (
  112. path &&
  113. path.roamingEvent &&
  114. !Object(this._cache).hasOwnProperty(path.id)
  115. ) {
  116. path.roamingEvent.fire(RoamingEventType.ADD, this)
  117. this._cache[path.id] = path
  118. }
  119. return this
  120. }
  121. /**
  122. * Returns a path
  123. * @param id
  124. * @returns {*|undefined}
  125. */
  126. getPath(id) {
  127. return this._cache[id] || undefined
  128. }
  129. /**
  130. * removes a path
  131. * @param path
  132. * @returns {RoamingController}
  133. */
  134. removePath(path) {
  135. if (
  136. path &&
  137. Object(this._cache).hasOwnProperty(path.id) &&
  138. path.roamingEvent
  139. ) {
  140. path.roamingEvent.fire(RoamingEventType.REMOVE, this)
  141. delete this._cache[path.id]
  142. }
  143. return this
  144. }
  145. /**
  146. *
  147. * @returns {RoamingController}
  148. */
  149. clearPath() {
  150. Object.keys(this._cache).forEach(key => {
  151. let path = this._cache[key]
  152. path && this.removePath(path)
  153. })
  154. return this
  155. }
  156. /**
  157. *
  158. * @param path
  159. * @param viewMode
  160. * @param viewOption
  161. * @returns {RoamingController}
  162. */
  163. trackedPath(path, viewMode, viewOption = {}) {
  164. if (!this._cache[path.id]) {
  165. throw new Error('RoamingController: path does not added ')
  166. }
  167. this._viewMode = viewMode
  168. this._viewOption = viewOption
  169. if (this._activePath && this._activePath.id === path.id) {
  170. return this
  171. }
  172. if (this._activePath && this._activePath.roamingEvent) {
  173. this._activePath.roamingEvent.fire(RoamingEventType.RELEASE, path.id)
  174. }
  175. this._activePath = path
  176. if (this._activePath && this._activePath.roamingEvent) {
  177. this._activePath.roamingEvent.fire(
  178. RoamingEventType.ACTIVE,
  179. this._activePath.id
  180. )
  181. }
  182. return this
  183. }
  184. /**
  185. *
  186. * @param path
  187. * @returns {RoamingController}
  188. */
  189. releasePath(path) {
  190. if (!this._cache[path.id]) {
  191. throw new Error('RoamingController: path does not added ')
  192. }
  193. if (path && path.isActive && path.roamingEvent) {
  194. path.roamingEvent.fire(RoamingEventType.RELEASE, path.id)
  195. }
  196. this._activePath = undefined
  197. this._viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY)
  198. this._viewer.delegate.trackedEntity = undefined
  199. return this
  200. }
  201. /**
  202. *
  203. * @returns {RoamingController}
  204. */
  205. releaseCamera() {
  206. this._viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY)
  207. this._viewer.delegate.trackedEntity = undefined
  208. if (this._activePath && this._activePath.roamingEvent) {
  209. this._activePath.roamingEvent.fire(
  210. RoamingEventType.RELEASE,
  211. this._activePath.id
  212. )
  213. }
  214. this._activePath = undefined
  215. return this
  216. }
  217. }
  218. export default RoamingController