state.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. export { PluginState }
  14. class PluginState {
  15. private ev = RxEventHelper.create();
  16. readonly dataState: State;
  17. readonly behaviorState: State;
  18. readonly cameraSnapshots = new CameraSnapshotManager();
  19. readonly snapshots = new PluginStateSnapshotManager();
  20. readonly behavior = {
  21. kind: this.ev.behavior<PluginState.Kind>('data'),
  22. currentObject: this.ev.behavior<State.ObjectEvent>({} as any)
  23. }
  24. setKind(kind: PluginState.Kind) {
  25. const current = this.behavior.kind.value;
  26. if (kind !== current) {
  27. this.behavior.kind.next(kind);
  28. this.behavior.currentObject.next(kind === 'data'
  29. ? this.dataState.behaviors.currentObject.value
  30. : this.behaviorState.behaviors.currentObject.value)
  31. }
  32. }
  33. getSnapshot(): PluginState.Snapshot {
  34. return {
  35. data: this.dataState.getSnapshot(),
  36. behaviour: this.behaviorState.getSnapshot(),
  37. cameraSnapshots: this.cameraSnapshots.getStateSnapshot(),
  38. canvas3d: {
  39. camera: this.plugin.canvas3d.camera.getSnapshot()
  40. }
  41. };
  42. }
  43. async setSnapshot(snapshot: PluginState.Snapshot) {
  44. // await this.plugin.runTask(this.behaviorState.setSnapshot(snapshot.behaviour));
  45. await this.plugin.runTask(this.dataState.setSnapshot(snapshot.data));
  46. this.cameraSnapshots.setStateSnapshot(snapshot.cameraSnapshots);
  47. this.plugin.canvas3d.camera.setState(snapshot.canvas3d.camera);
  48. this.plugin.canvas3d.requestDraw(true);
  49. }
  50. dispose() {
  51. this.ev.dispose();
  52. this.dataState.dispose();
  53. this.behaviorState.dispose();
  54. this.cameraSnapshots.dispose();
  55. }
  56. constructor(private plugin: import('./context').PluginContext) {
  57. this.dataState = State.create(new SO.Root({ }), { globalContext: plugin });
  58. this.behaviorState = State.create(new PluginBehavior.Root({ }), { globalContext: plugin });
  59. this.dataState.behaviors.currentObject.subscribe(o => {
  60. if (this.behavior.kind.value === 'data') this.behavior.currentObject.next(o);
  61. });
  62. this.behaviorState.behaviors.currentObject.subscribe(o => {
  63. if (this.behavior.kind.value === 'behavior') this.behavior.currentObject.next(o);
  64. });
  65. this.behavior.currentObject.next(this.dataState.behaviors.currentObject.value);
  66. }
  67. }
  68. namespace PluginState {
  69. export type Kind = 'data' | 'behavior'
  70. export interface Snapshot {
  71. data: State.Snapshot,
  72. behaviour: State.Snapshot,
  73. cameraSnapshots: CameraSnapshotManager.StateSnapshot,
  74. canvas3d: {
  75. camera: Camera.Snapshot
  76. }
  77. }
  78. }