Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RoamingController.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @Author: Caven
  3. * @Date: 2021-06-08 20:41:51
  4. */
  5. import { SceneEventType, PathEventType } from '@dc-modules/event'
  6. class RoamingController {
  7. constructor(viewer) {
  8. this._viewer = viewer
  9. this._viewOption = {}
  10. this._cache = {}
  11. this._activedPath = undefined
  12. }
  13. /**
  14. *
  15. * @returns {boolean}
  16. * @private
  17. */
  18. _onPostRender() {
  19. if (!this._activedPath) {
  20. return false
  21. }
  22. this._activedPath.pathEvent &&
  23. this._activedPath.pathEvent.fire(
  24. PathEventType.POST_RENDER,
  25. this._viewer,
  26. 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. return this
  117. }
  118. /**
  119. *
  120. * @returns {RoamingController}
  121. */
  122. clear() {
  123. this._cache = {}
  124. this._activedPath && (this._activedPath.actived = false)
  125. this._activedPath = undefined
  126. this._viewer.off(SceneEventType.POST_RENDER, this._onPostRender, this)
  127. return this
  128. }
  129. }
  130. export default RoamingController