state.ts 6.9 KB

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