state.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 { Canvas3DParams, 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. import { PluginConfig } from './config';
  22. export { PluginState };
  23. class PluginState extends PluginComponent {
  24. private get animation() { return this.plugin.managers.animation; }
  25. readonly data = State.create(new SO.Root({ }), { runTask: this.plugin.runTask, globalContext: this.plugin, historyCapacity: this.plugin.config.get(PluginConfig.State.HistoryCapacity) });
  26. readonly behaviors = State.create(new PluginBehavior.Root({ }), { runTask: this.plugin.runTask, globalContext: this.plugin, rootState: { isLocked: true } });
  27. readonly events = {
  28. cell: {
  29. stateUpdated: merge(this.data.events.cell.stateUpdated, this.behaviors.events.cell.stateUpdated),
  30. created: merge(this.data.events.cell.created, this.behaviors.events.cell.created),
  31. removed: merge(this.data.events.cell.removed, this.behaviors.events.cell.removed),
  32. },
  33. object: {
  34. created: merge(this.data.events.object.created, this.behaviors.events.object.created),
  35. removed: merge(this.data.events.object.removed, this.behaviors.events.object.removed),
  36. updated: merge(this.data.events.object.updated, this.behaviors.events.object.updated)
  37. }
  38. } as const;
  39. readonly snapshotParams = this.ev.behavior<PluginState.SnapshotParams>(PluginState.DefaultSnapshotParams);
  40. setSnapshotParams = (params?: PluginState.SnapshotParams) => {
  41. this.snapshotParams.next({ ...PluginState.DefaultSnapshotParams, ...params });
  42. }
  43. getSnapshot(params?: PluginState.SnapshotParams): PluginState.Snapshot {
  44. const p = { ...this.snapshotParams.value, ...params };
  45. return {
  46. id: UUID.create22(),
  47. data: p.data ? this.data.getSnapshot() : void 0,
  48. behaviour: p.behavior ? this.behaviors.getSnapshot() : void 0,
  49. animation: p.animation ? this.animation.getSnapshot() : void 0,
  50. startAnimation: p.startAnimation ? !!p.startAnimation : void 0,
  51. camera: p.camera ? {
  52. current: this.plugin.canvas3d!.camera.getSnapshot(),
  53. transitionStyle: p.cameraTransition!.name,
  54. transitionDurationInMs: p?.cameraTransition?.name === 'animate' ? p.cameraTransition.params.durationInMs : void 0
  55. } : void 0,
  56. canvas3d: p.canvas3d ? { props: this.plugin.canvas3d?.props } : void 0,
  57. interactivity: p.interactivity ? { props: this.plugin.managers.interactivity.props } : void 0,
  58. structureFocus: this.plugin.managers.structure.focus.getSnapshot(),
  59. durationInMs: p?.durationInMs
  60. };
  61. }
  62. async setSnapshot(snapshot: PluginState.Snapshot) {
  63. await this.animation.stop();
  64. if (snapshot.behaviour) await this.plugin.runTask(this.behaviors.setSnapshot(snapshot.behaviour));
  65. if (snapshot.data) await this.plugin.runTask(this.data.setSnapshot(snapshot.data));
  66. if (snapshot.canvas3d?.props) {
  67. const settings = PD.normalizeParams(Canvas3DParams, snapshot.canvas3d.props, 'children');
  68. await PluginCommands.Canvas3D.SetSettings(this.plugin, { settings });
  69. }
  70. if (snapshot.interactivity) {
  71. if (snapshot.interactivity.props) this.plugin.managers.interactivity.setProps(snapshot.interactivity.props);
  72. }
  73. if (snapshot.structureFocus) {
  74. this.plugin.managers.structure.focus.setSnapshot(snapshot.structureFocus);
  75. }
  76. if (snapshot.animation) {
  77. this.animation.setSnapshot(snapshot.animation);
  78. }
  79. if (snapshot.camera) {
  80. PluginCommands.Camera.Reset(this.plugin, {
  81. snapshot: snapshot.camera.current,
  82. durationMs: snapshot.camera.transitionStyle === 'animate'
  83. ? snapshot.camera.transitionDurationInMs
  84. : void 0
  85. });
  86. }
  87. if (snapshot.startAnimation) {
  88. this.animation.start();
  89. }
  90. }
  91. updateTransform(state: State, a: StateTransform.Ref, params: any, canUndo?: string | boolean) {
  92. const tree = state.build().to(a).update(params);
  93. return PluginCommands.State.Update(this.plugin, { state, tree, options: { canUndo } });
  94. }
  95. updateBehavior<T extends StateTransformer>(behavior: T, params: (old: StateTransformer.Params<T>) => (void | StateTransformer.Params<T>)) {
  96. const tree = this.behaviors.build();
  97. if (!this.behaviors.tree.transforms.has(behavior.id)) {
  98. const defaultParams = behavior.createDefaultParams(void 0 as any, this.plugin);
  99. tree.to(PluginBehavior.getCategoryId(behavior)).apply(behavior, produce(defaultParams, params) as any, { ref: behavior.id });
  100. } else {
  101. tree.to(behavior.id).update(params);
  102. }
  103. return this.plugin.runTask(this.behaviors.updateTree(tree));
  104. }
  105. dispose() {
  106. this.behaviors.cells.forEach(cell => {
  107. if (PluginBehavior.Behavior.is(cell.obj)) {
  108. cell.obj.data.unregister();
  109. }
  110. });
  111. super.dispose();
  112. this.data.dispose();
  113. this.behaviors.dispose();
  114. this.animation.dispose();
  115. }
  116. constructor(private plugin: PluginContext) {
  117. super();
  118. }
  119. }
  120. namespace PluginState {
  121. export type CameraTransitionStyle = 'instant' | 'animate'
  122. export const SnapshotParams = {
  123. durationInMs: PD.Numeric(1500, { min: 100, max: 15000, step: 100 }, { label: 'Duration in ms' }),
  124. data: PD.Boolean(true),
  125. behavior: PD.Boolean(false),
  126. animation: PD.Boolean(true),
  127. startAnimation: PD.Boolean(false),
  128. canvas3d: PD.Boolean(true),
  129. interactivity: PD.Boolean(true),
  130. camera: PD.Boolean(true),
  131. cameraTransition: PD.MappedStatic('animate', {
  132. animate: PD.Group({
  133. durationInMs: PD.Numeric(250, { min: 100, max: 5000, step: 500 }, { label: 'Duration in ms' }),
  134. }),
  135. instant: PD.Group({ })
  136. }, { options: [['animate', 'Animate'], ['instant', 'Instant']] })
  137. };
  138. export type SnapshotParams = Partial<PD.Values<typeof SnapshotParams>>
  139. export const DefaultSnapshotParams = PD.getDefaultValues(SnapshotParams);
  140. export interface Snapshot {
  141. id: UUID,
  142. data?: State.Snapshot,
  143. behaviour?: State.Snapshot,
  144. animation?: PluginAnimationManager.Snapshot,
  145. startAnimation?: boolean,
  146. camera?: {
  147. current: Camera.Snapshot,
  148. transitionStyle: CameraTransitionStyle,
  149. transitionDurationInMs?: number
  150. },
  151. canvas3d?: {
  152. props?: Canvas3DProps
  153. },
  154. interactivity?: {
  155. props?: InteractivityManager.Props
  156. },
  157. structureFocus?: StructureFocusSnapshot,
  158. durationInMs?: number
  159. }
  160. export type SnapshotType = 'json' | 'molj' | 'zip' | 'molx'
  161. }