state.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { State, StateTransform, StateTransformer } from '../mol-state';
  7. import { PluginStateObject as SO } from '../mol-plugin-state/objects';
  8. import { Camera } from '../mol-canvas3d/camera';
  9. import { PluginBehavior } from './behavior';
  10. import { Canvas3DProps } from '../mol-canvas3d/canvas3d';
  11. import { PluginCommands } from './commands';
  12. import { PluginAnimationManager } from '../mol-plugin-state/manager/animation';
  13. import { ParamDefinition as PD } from '../mol-util/param-definition';
  14. import { UUID } from '../mol-util';
  15. import { InteractivityManager } from '../mol-plugin-state/manager/interactivity';
  16. import { produce } from 'immer';
  17. import { StructureFocusSnapshot } from '../mol-plugin-state/manager/structure/focus';
  18. import { merge } from 'rxjs';
  19. import { PluginContext } from './context';
  20. import { PluginComponent } from '../mol-plugin-state/component';
  21. export { PluginState };
  22. class PluginState extends PluginComponent {
  23. private get animation() { return this.plugin.managers.animation; }
  24. readonly data = State.create(new SO.Root({ }), { runTask: this.plugin.runTask, globalContext: this.plugin });
  25. readonly behaviors = State.create(new PluginBehavior.Root({ }), { runTask: this.plugin.runTask, globalContext: this.plugin, rootState: { isLocked: true } });
  26. readonly events = {
  27. cell: {
  28. stateUpdated: merge(this.data.events.cell.stateUpdated, this.behaviors.events.cell.stateUpdated),
  29. created: merge(this.data.events.cell.created, this.behaviors.events.cell.created),
  30. removed: merge(this.data.events.cell.removed, this.behaviors.events.cell.removed),
  31. },
  32. object: {
  33. created: merge(this.data.events.object.created, this.behaviors.events.object.created),
  34. removed: merge(this.data.events.object.removed, this.behaviors.events.object.removed),
  35. updated: merge(this.data.events.object.updated, this.behaviors.events.object.updated)
  36. }
  37. } as const;
  38. readonly snapshotParams = this.ev.behavior<PluginState.SnapshotParams>(PluginState.DefaultSnapshotParams);
  39. setSnapshotParams = (params?: PluginState.SnapshotParams) => {
  40. this.snapshotParams.next({ ...PluginState.DefaultSnapshotParams, ...params });
  41. }
  42. getSnapshot(params?: PluginState.SnapshotParams): PluginState.Snapshot {
  43. const p = { ...this.snapshotParams.value, ...params };
  44. return {
  45. id: UUID.create22(),
  46. data: p.data ? this.data.getSnapshot() : void 0,
  47. behaviour: p.behavior ? this.behaviors.getSnapshot() : void 0,
  48. animation: p.animation ? this.animation.getSnapshot() : void 0,
  49. startAnimation: p.startAnimation ? !!p.startAnimation : void 0,
  50. camera: p.camera ? {
  51. current: this.plugin.canvas3d!.camera.getSnapshot(),
  52. transitionStyle: p.cameraTransition!.name,
  53. transitionDurationInMs: p?.cameraTransition?.name === 'animate' ? p.cameraTransition.params.durationInMs : void 0
  54. } : void 0,
  55. canvas3d: p.canvas3d ? { props: this.plugin.canvas3d?.props } : void 0,
  56. interactivity: p.interactivity ? { props: this.plugin.managers.interactivity.props } : void 0,
  57. structureFocus: this.plugin.managers.structure.focus.getSnapshot(),
  58. durationInMs: p?.durationInMs
  59. };
  60. }
  61. async setSnapshot(snapshot: PluginState.Snapshot) {
  62. await this.animation.stop();
  63. if (snapshot.behaviour) await this.plugin.runTask(this.behaviors.setSnapshot(snapshot.behaviour));
  64. if (snapshot.data) await this.plugin.runTask(this.data.setSnapshot(snapshot.data));
  65. if (snapshot.canvas3d) {
  66. if (snapshot.canvas3d.props) await PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: snapshot.canvas3d.props });
  67. }
  68. if (snapshot.interactivity) {
  69. if (snapshot.interactivity.props) this.plugin.managers.interactivity.setProps(snapshot.interactivity.props);
  70. }
  71. if (snapshot.structureFocus) {
  72. this.plugin.managers.structure.focus.setSnapshot(snapshot.structureFocus);
  73. }
  74. if (snapshot.animation) {
  75. this.animation.setSnapshot(snapshot.animation);
  76. }
  77. if (snapshot.camera) {
  78. PluginCommands.Camera.Reset(this.plugin, {
  79. snapshot: snapshot.camera.current,
  80. durationMs: snapshot.camera.transitionStyle === 'animate'
  81. ? snapshot.camera.transitionDurationInMs
  82. : void 0
  83. });
  84. }
  85. if (snapshot.startAnimation) {
  86. this.animation.start();
  87. }
  88. }
  89. updateTransform(state: State, a: StateTransform.Ref, params: any, canUndo?: string | boolean) {
  90. const tree = state.build().to(a).update(params);
  91. return PluginCommands.State.Update(this.plugin, { state, tree, options: { canUndo } });
  92. }
  93. updateBehavior<T extends StateTransformer>(behavior: T, params: (old: StateTransformer.Params<T>) => (void | StateTransformer.Params<T>)) {
  94. const tree = this.behaviors.build();
  95. if (!this.behaviors.tree.transforms.has(behavior.id)) {
  96. const defaultParams = behavior.createDefaultParams(void 0 as any, this.plugin);
  97. tree.to(PluginBehavior.getCategoryId(behavior)).apply(behavior, produce(defaultParams, params) as any, { ref: behavior.id });
  98. } else {
  99. tree.to(behavior.id).update(params);
  100. }
  101. return this.plugin.runTask(this.behaviors.updateTree(tree));
  102. }
  103. dispose() {
  104. super.dispose();
  105. this.data.dispose();
  106. this.behaviors.dispose();
  107. this.animation.dispose();
  108. }
  109. constructor(private plugin: PluginContext) {
  110. super();
  111. }
  112. }
  113. namespace PluginState {
  114. export type CameraTransitionStyle = 'instant' | 'animate'
  115. export const SnapshotParams = {
  116. durationInMs: PD.Numeric(1500, { min: 100, max: 15000, step: 100 }, { label: 'Duration in ms' }),
  117. data: PD.Boolean(true),
  118. behavior: PD.Boolean(false),
  119. animation: PD.Boolean(true),
  120. startAnimation: PD.Boolean(false),
  121. canvas3d: PD.Boolean(true),
  122. interactivity: PD.Boolean(true),
  123. camera: PD.Boolean(true),
  124. cameraTransition: PD.MappedStatic('animate', {
  125. animate: PD.Group({
  126. durationInMs: PD.Numeric(250, { min: 100, max: 5000, step: 500 }, { label: 'Duration in ms' }),
  127. }),
  128. instant: PD.Group({ })
  129. }, { options: [['animate', 'Animate'], ['instant', 'Instant']] })
  130. };
  131. export type SnapshotParams = Partial<PD.Values<typeof SnapshotParams>>
  132. export const DefaultSnapshotParams = PD.getDefaultValues(SnapshotParams);
  133. export interface Snapshot {
  134. id: UUID,
  135. data?: State.Snapshot,
  136. behaviour?: State.Snapshot,
  137. animation?: PluginAnimationManager.Snapshot,
  138. startAnimation?: boolean,
  139. camera?: {
  140. current: Camera.Snapshot,
  141. transitionStyle: CameraTransitionStyle,
  142. transitionDurationInMs?: number
  143. },
  144. canvas3d?: {
  145. props?: Canvas3DProps
  146. },
  147. interactivity?: {
  148. props?: InteractivityManager.Props
  149. },
  150. structureFocus?: StructureFocusSnapshot,
  151. durationInMs?: number
  152. }
  153. }