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 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @Author: Caven
  3. * @Date: 2021-06-08 20:41:51
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { SceneEventType, PathEventType } from '@dc-modules/event'
  7. class RoamingController {
  8. constructor(viewer) {
  9. this._viewer = viewer
  10. this._viewOption = {}
  11. this._cache = {}
  12. this._activedPath = undefined
  13. }
  14. /**
  15. *
  16. * @returns {boolean}
  17. * @private
  18. */
  19. _onPostRender() {
  20. if (!this._activedPath) {
  21. return false
  22. }
  23. this._activedPath.pathEvent &&
  24. this._activedPath.pathEvent.fire(PathEventType.POST_RENDER, {
  25. viewer: this._viewer,
  26. viewOption: this._viewOption
  27. })
  28. }
  29. /**
  30. *
  31. * @param path
  32. * @returns {RoamingController}
  33. */
  34. addPath(path) {
  35. if (path && !this._cache.hasOwnProperty(path.pathId)) {
  36. path.pathEvent.fire(PathEventType.ADD)
  37. this._cache[path.pathId] = path
  38. }
  39. return this
  40. }
  41. /**
  42. *
  43. * @param paths
  44. * @returns {RoamingController}
  45. */
  46. addPaths(paths) {
  47. if (Array.isArray(paths)) {
  48. paths.forEach(item => {
  49. this.addPath(item)
  50. })
  51. }
  52. return this
  53. }
  54. /**
  55. *
  56. * @param path
  57. * @returns {RoamingController}
  58. */
  59. removePath(path) {
  60. if (path && this._cache.hasOwnProperty(path.pathId)) {
  61. delete this._cache[path.pathId]
  62. path.pathEvent.fire(PathEventType.REMOVE)
  63. }
  64. return this
  65. }
  66. /**
  67. *
  68. * @param id
  69. * @returns {*|undefined}
  70. */
  71. getPath(id) {
  72. let filters = this.getPaths().filter(item => item.id === id)
  73. return filters && filters.length ? filters[0] : undefined
  74. }
  75. /**
  76. *
  77. * @returns {*[]}
  78. */
  79. getPaths() {
  80. let result = []
  81. Object.keys(this._cache).forEach(key => {
  82. result.push(this._cache[key])
  83. })
  84. return result
  85. }
  86. /**
  87. *
  88. * @param path
  89. * @param viewOption
  90. * @returns {RoamingController}
  91. */
  92. activate(path, viewOption = {}) {
  93. if (
  94. !path ||
  95. path?.pathId === this._activedPath?.pathId ||
  96. !this._cache.hasOwnProperty(path?.pathId)
  97. ) {
  98. return this
  99. }
  100. this._viewOption = viewOption
  101. this._activedPath && this.deactivate()
  102. this._activedPath = path
  103. this._activedPath.pathEvent &&
  104. this._activedPath.pathEvent.fire(PathEventType.RESET_TIME_LINE)
  105. this._viewer.on(SceneEventType.POST_RENDER, this._onPostRender, this)
  106. return this
  107. }
  108. /**
  109. *
  110. * @returns {RoamingController}
  111. */
  112. deactivate() {
  113. this._activedPath && (this._activedPath.actived = false)
  114. this._activedPath = undefined
  115. this._viewer.off(SceneEventType.POST_RENDER, this._onPostRender, this)
  116. this._viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY)
  117. return this
  118. }
  119. /**
  120. *
  121. * @returns {RoamingController}
  122. */
  123. clear() {
  124. this._cache = {}
  125. this._activedPath && (this._activedPath.actived = false)
  126. this._activedPath = undefined
  127. this._viewer.off(SceneEventType.POST_RENDER, this._onPostRender, this)
  128. this._viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY)
  129. return this
  130. }
  131. }
  132. export default RoamingController