threepipe
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.

AAssetManagerProcessStatePlugin.ts 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {createDiv, onChange, serialize} from 'ts-browser-helpers'
  2. import {AViewerPluginEventMap, AViewerPluginSync, ThreeViewer} from '../../viewer'
  3. import {uiToggle} from 'uiconfig.js'
  4. export abstract class AAssetManagerProcessStatePlugin<TE extends AViewerPluginEventMap = AViewerPluginEventMap> extends AViewerPluginSync<TE> {
  5. @uiToggle('Enabled')
  6. @onChange(AAssetManagerProcessStatePlugin.prototype._onEnabledChange)
  7. @serialize() enabled = true
  8. protected _mainDiv: HTMLDivElement
  9. protected _contentDiv: HTMLDivElement | undefined
  10. private _onEnabledChange() {
  11. if (!this.enabled) this._mainDiv.style.display = 'none'
  12. }
  13. protected constructor(suffix: string, public readonly container?: HTMLElement) {
  14. super()
  15. this._mainDiv = createDiv({
  16. id: 'assetManager' + suffix,
  17. addToBody: false,
  18. innerHTML: '',
  19. })
  20. this._contentDiv = createDiv({
  21. id: 'assetManager' + suffix + 'Content',
  22. addToBody: false,
  23. innerHTML: '',
  24. })
  25. if (!this.enabled) {
  26. this._mainDiv.style.display = 'none'
  27. }
  28. this._mainDiv.appendChild(this._contentDiv)
  29. this._onProcessStateUpdate = this._onProcessStateUpdate.bind(this)
  30. }
  31. protected abstract _updateMainDiv(processState: Map<string, {state: string, progress?: number|undefined}>): void
  32. onAdded(viewer: ThreeViewer) {
  33. super.onAdded(viewer)
  34. ;(this.container ?? viewer.container).appendChild(this._mainDiv)
  35. this._updateMainDiv(viewer.assetManager.processState)
  36. viewer.assetManager.addEventListener('processStateUpdate', this._onProcessStateUpdate)
  37. }
  38. protected _onProcessStateUpdate() {
  39. if (!this._viewer) return
  40. this._updateMainDiv(this._viewer.assetManager.processState)
  41. }
  42. onRemove(viewer: ThreeViewer) {
  43. this._mainDiv.remove()
  44. // this._contentDiv?.remove()
  45. viewer.assetManager.removeEventListener('processStateUpdate', this._onProcessStateUpdate)
  46. return super.onRemove(viewer)
  47. }
  48. }