state.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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) || void 0
  51. } : void 0,
  52. cameraSnapshots: p.cameraSnapshots ? this.cameraSnapshots.getStateSnapshot() : void 0,
  53. canvas3d: p.canvas3d ? {
  54. props: this.plugin.canvas3d.props
  55. } : 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.cameraSnapshots) this.cameraSnapshots.setStateSnapshot(snapshot.cameraSnapshots);
  67. if (snapshot.animation) {
  68. this.animation.setSnapshot(snapshot.animation);
  69. }
  70. if (snapshot.camera) {
  71. PluginCommands.Camera.SetSnapshot.dispatch(this.plugin, {
  72. snapshot: snapshot.camera.current,
  73. durationMs: snapshot.camera.transitionStyle === 'animate'
  74. ? snapshot.camera.transitionDurationInMs
  75. : void 0
  76. });
  77. }
  78. if (snapshot.startAnimation) {
  79. this.animation.start();
  80. }
  81. }
  82. dispose() {
  83. this.ev.dispose();
  84. this.dataState.dispose();
  85. this.behaviorState.dispose();
  86. this.cameraSnapshots.dispose();
  87. this.animation.dispose();
  88. }
  89. constructor(private plugin: import('./context').PluginContext) {
  90. this.snapshots = new PluginStateSnapshotManager(plugin);
  91. this.dataState = State.create(new SO.Root({ }), { globalContext: plugin });
  92. this.behaviorState = State.create(new PluginBehavior.Root({ }), { globalContext: plugin, rootProps: { isLocked: true } });
  93. this.dataState.behaviors.currentObject.subscribe(o => {
  94. if (this.behavior.kind.value === 'data') this.behavior.currentObject.next(o);
  95. });
  96. this.behaviorState.behaviors.currentObject.subscribe(o => {
  97. if (this.behavior.kind.value === 'behavior') this.behavior.currentObject.next(o);
  98. });
  99. this.behavior.currentObject.next(this.dataState.behaviors.currentObject.value);
  100. this.animation = new PluginAnimationManager(plugin);
  101. }
  102. }
  103. namespace PluginState {
  104. export type Kind = 'data' | 'behavior'
  105. export type CameraTransitionStyle = 'instant' | 'animate'
  106. export const GetSnapshotParams = {
  107. durationInMs: PD.Numeric(1500, { min: 100, max: 15000, step: 100 }, { label: 'Duration in ms' }),
  108. data: PD.Boolean(true),
  109. behavior: PD.Boolean(false),
  110. animation: PD.Boolean(true),
  111. startAnimation: PD.Boolean(false),
  112. canvas3d: PD.Boolean(true),
  113. camera: PD.Boolean(true),
  114. // TODO: make camera snapshots same as the StateSnapshots with "child states?"
  115. cameraSnapshots: PD.Boolean(false),
  116. cameraTranstion: PD.MappedStatic('animate', {
  117. animate: PD.Group({
  118. durationInMs: PD.Numeric(250, { min: 100, max: 5000, step: 500 }, { label: 'Duration in ms' }),
  119. }),
  120. instant: PD.Group({ })
  121. }, { options: [['animate', 'Animate'], ['instant', 'Instant']] })
  122. };
  123. export type GetSnapshotParams = Partial<PD.Values<typeof GetSnapshotParams>>
  124. export const DefaultGetSnapshotParams = PD.getDefaultValues(GetSnapshotParams);
  125. export interface Snapshot {
  126. id: UUID,
  127. data?: State.Snapshot,
  128. behaviour?: State.Snapshot,
  129. animation?: PluginAnimationManager.Snapshot,
  130. startAnimation?: boolean,
  131. camera?: {
  132. current: Camera.Snapshot,
  133. transitionStyle: CameraTransitionStyle,
  134. transitionDurationInMs?: number
  135. },
  136. cameraSnapshots?: CameraSnapshotManager.StateSnapshot,
  137. canvas3d?: {
  138. props?: Canvas3DProps
  139. },
  140. durationInMs?: number
  141. }
  142. }