state.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. import { ParamDefinition as PD } from '../mol-util/param-definition';
  17. import { UUID } from '../mol-util';
  18. export { PluginState }
  19. class PluginState {
  20. private ev = RxEventHelper.create();
  21. readonly dataState: State;
  22. readonly behaviorState: State;
  23. readonly animation: PluginAnimationManager;
  24. readonly cameraSnapshots = new CameraSnapshotManager();
  25. readonly snapshots: PluginStateSnapshotManager;
  26. readonly behavior = {
  27. kind: this.ev.behavior<PluginState.Kind>('data'),
  28. currentObject: this.ev.behavior<State.ObjectEvent>({} as any)
  29. }
  30. setKind(kind: PluginState.Kind) {
  31. const current = this.behavior.kind.value;
  32. if (kind !== current) {
  33. this.behavior.kind.next(kind);
  34. this.behavior.currentObject.next(kind === 'data'
  35. ? this.dataState.behaviors.currentObject.value
  36. : this.behaviorState.behaviors.currentObject.value)
  37. }
  38. }
  39. getSnapshot(params?: PluginState.GetSnapshotParams): PluginState.Snapshot {
  40. const p = { ...PluginState.DefaultGetSnapshotParams, ...params };
  41. return {
  42. id: UUID.create22(),
  43. data: p.data ? this.dataState.getSnapshot() : void 0,
  44. behaviour: p.behavior ? this.behaviorState.getSnapshot() : void 0,
  45. animation: p.animation ? this.animation.getSnapshot() : void 0,
  46. startAnimation: p.startAnimation ? !!p.startAnimation : void 0,
  47. camera: p.camera ? {
  48. current: this.plugin.canvas3d.camera.getSnapshot(),
  49. transitionStyle: p.cameraTranstion.name,
  50. transitionDurationInMs: (params && params.cameraTranstion && params.cameraTranstion.name === 'animate') ? params.cameraTranstion.params.durationInMs : undefined
  51. } : void 0,
  52. cameraSnapshots: p.cameraSnapshots ? this.cameraSnapshots.getStateSnapshot() : void 0,
  53. canvas3d: p.canvas3d ? { props: this.plugin.canvas3d.props } : void 0,
  54. durationInMs: params && params.durationInMs
  55. };
  56. }
  57. async setSnapshot(snapshot: PluginState.Snapshot) {
  58. await this.animation.stop();
  59. if (snapshot.behaviour) await this.plugin.runTask(this.behaviorState.setSnapshot(snapshot.behaviour));
  60. if (snapshot.data) await this.plugin.runTask(this.dataState.setSnapshot(snapshot.data));
  61. if (snapshot.canvas3d) {
  62. if (snapshot.canvas3d.props) await PluginCommands.Canvas3D.SetSettings.dispatch(this.plugin, { settings: snapshot.canvas3d.props });
  63. }
  64. if (snapshot.cameraSnapshots) this.cameraSnapshots.setStateSnapshot(snapshot.cameraSnapshots);
  65. if (snapshot.animation) {
  66. this.animation.setSnapshot(snapshot.animation);
  67. }
  68. if (snapshot.camera) {
  69. PluginCommands.Camera.SetSnapshot.dispatch(this.plugin, {
  70. snapshot: snapshot.camera.current,
  71. durationMs: snapshot.camera.transitionStyle === 'animate'
  72. ? snapshot.camera.transitionDurationInMs
  73. : void 0
  74. });
  75. }
  76. if (snapshot.startAnimation) {
  77. this.animation.start();
  78. }
  79. }
  80. dispose() {
  81. this.ev.dispose();
  82. this.dataState.dispose();
  83. this.behaviorState.dispose();
  84. this.cameraSnapshots.dispose();
  85. this.animation.dispose();
  86. }
  87. constructor(private plugin: import('./context').PluginContext) {
  88. this.snapshots = new PluginStateSnapshotManager(plugin);
  89. this.dataState = State.create(new SO.Root({ }), { globalContext: plugin });
  90. this.behaviorState = State.create(new PluginBehavior.Root({ }), { globalContext: plugin, rootState: { isLocked: true } });
  91. this.dataState.behaviors.currentObject.subscribe(o => {
  92. if (this.behavior.kind.value === 'data') this.behavior.currentObject.next(o);
  93. });
  94. this.behaviorState.behaviors.currentObject.subscribe(o => {
  95. if (this.behavior.kind.value === 'behavior') this.behavior.currentObject.next(o);
  96. });
  97. this.behavior.currentObject.next(this.dataState.behaviors.currentObject.value);
  98. this.animation = new PluginAnimationManager(plugin);
  99. }
  100. }
  101. namespace PluginState {
  102. export type Kind = 'data' | 'behavior'
  103. export type CameraTransitionStyle = 'instant' | 'animate'
  104. export const GetSnapshotParams = {
  105. durationInMs: PD.Numeric(1500, { min: 100, max: 15000, step: 100 }, { label: 'Duration in ms' }),
  106. data: PD.Boolean(true),
  107. behavior: PD.Boolean(false),
  108. animation: PD.Boolean(true),
  109. startAnimation: PD.Boolean(false),
  110. canvas3d: PD.Boolean(true),
  111. camera: PD.Boolean(true),
  112. // TODO: make camera snapshots same as the StateSnapshots with "child states?"
  113. cameraSnapshots: PD.Boolean(false),
  114. cameraTranstion: PD.MappedStatic('animate', {
  115. animate: PD.Group({
  116. durationInMs: PD.Numeric(250, { min: 100, max: 5000, step: 500 }, { label: 'Duration in ms' }),
  117. }),
  118. instant: PD.Group({ })
  119. }, { options: [['animate', 'Animate'], ['instant', 'Instant']] })
  120. };
  121. export type GetSnapshotParams = Partial<PD.Values<typeof GetSnapshotParams>>
  122. export const DefaultGetSnapshotParams = PD.getDefaultValues(GetSnapshotParams);
  123. export interface Snapshot {
  124. id: UUID,
  125. data?: State.Snapshot,
  126. behaviour?: State.Snapshot,
  127. animation?: PluginAnimationManager.Snapshot,
  128. startAnimation?: boolean,
  129. camera?: {
  130. current: Camera.Snapshot,
  131. transitionStyle: CameraTransitionStyle,
  132. transitionDurationInMs?: number
  133. },
  134. cameraSnapshots?: CameraSnapshotManager.StateSnapshot,
  135. canvas3d?: {
  136. props?: Canvas3DProps
  137. },
  138. durationInMs?: number
  139. }
  140. }