state.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { State } from 'mol-state';
  7. import { PluginStateObject as SO } from './state/objects';
  8. import { Camera } from 'mol-canvas3d/camera';
  9. import { PluginBehavior } from './behavior';
  10. import { CameraSnapshotManager } from './state/camera';
  11. import { PluginStateSnapshotManager } from './state/snapshots';
  12. import { RxEventHelper } from 'mol-util/rx-event-helper';
  13. import { Canvas3DProps } from 'mol-canvas3d/canvas3d';
  14. import { PluginCommands } from './command';
  15. import { PluginAnimationManager } from './state/animation/manager';
  16. export { PluginState }
  17. class PluginState {
  18. private ev = RxEventHelper.create();
  19. readonly dataState: State;
  20. readonly behaviorState: State;
  21. readonly animation: PluginAnimationManager;
  22. readonly cameraSnapshots = new CameraSnapshotManager();
  23. readonly snapshots = new PluginStateSnapshotManager();
  24. readonly behavior = {
  25. kind: this.ev.behavior<PluginState.Kind>('data'),
  26. currentObject: this.ev.behavior<State.ObjectEvent>({} as any)
  27. }
  28. setKind(kind: PluginState.Kind) {
  29. const current = this.behavior.kind.value;
  30. if (kind !== current) {
  31. this.behavior.kind.next(kind);
  32. this.behavior.currentObject.next(kind === 'data'
  33. ? this.dataState.behaviors.currentObject.value
  34. : this.behaviorState.behaviors.currentObject.value)
  35. }
  36. }
  37. getSnapshot(): PluginState.Snapshot {
  38. return {
  39. data: this.dataState.getSnapshot(),
  40. behaviour: this.behaviorState.getSnapshot(),
  41. animation: this.animation.getSnapshot(),
  42. cameraSnapshots: this.cameraSnapshots.getStateSnapshot(),
  43. canvas3d: {
  44. camera: this.plugin.canvas3d.camera.getSnapshot(),
  45. viewport: this.plugin.canvas3d.props
  46. }
  47. };
  48. }
  49. async setSnapshot(snapshot: PluginState.Snapshot) {
  50. if (snapshot.behaviour) await this.plugin.runTask(this.behaviorState.setSnapshot(snapshot.behaviour));
  51. if (snapshot.data) await this.plugin.runTask(this.dataState.setSnapshot(snapshot.data));
  52. if (snapshot.cameraSnapshots) this.cameraSnapshots.setStateSnapshot(snapshot.cameraSnapshots);
  53. if (snapshot.canvas3d) {
  54. if (snapshot.canvas3d.viewport) PluginCommands.Canvas3D.SetSettings.dispatch(this.plugin, { settings: snapshot.canvas3d.viewport || { } });
  55. if (snapshot.canvas3d.camera) this.plugin.canvas3d.camera.setState(snapshot.canvas3d.camera);
  56. }
  57. this.plugin.canvas3d.requestDraw(true);
  58. if (snapshot.animation) {
  59. this.animation.setSnapshot(snapshot.animation);
  60. }
  61. }
  62. dispose() {
  63. this.ev.dispose();
  64. this.dataState.dispose();
  65. this.behaviorState.dispose();
  66. this.cameraSnapshots.dispose();
  67. this.animation.dispose();
  68. }
  69. constructor(private plugin: import('./context').PluginContext) {
  70. this.dataState = State.create(new SO.Root({ }), { globalContext: plugin });
  71. this.behaviorState = State.create(new PluginBehavior.Root({ }), { globalContext: plugin });
  72. this.dataState.behaviors.currentObject.subscribe(o => {
  73. if (this.behavior.kind.value === 'data') this.behavior.currentObject.next(o);
  74. });
  75. this.behaviorState.behaviors.currentObject.subscribe(o => {
  76. if (this.behavior.kind.value === 'behavior') this.behavior.currentObject.next(o);
  77. });
  78. this.behavior.currentObject.next(this.dataState.behaviors.currentObject.value);
  79. this.animation = new PluginAnimationManager(plugin);
  80. }
  81. }
  82. namespace PluginState {
  83. export type Kind = 'data' | 'behavior'
  84. export interface Snapshot {
  85. data?: State.Snapshot,
  86. behaviour?: State.Snapshot,
  87. animation?: PluginAnimationManager.Snapshot,
  88. cameraSnapshots?: CameraSnapshotManager.StateSnapshot,
  89. canvas3d?: {
  90. camera?: Camera.Snapshot,
  91. viewport?: Canvas3DProps
  92. }
  93. }
  94. }