controls.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 * as React from 'react';
  7. import { PluginCommands } from 'mol-plugin/command';
  8. import { UpdateTrajectory } from 'mol-plugin/state/actions/structure';
  9. import { PluginUIComponent } from './base';
  10. import { LociLabelEntry } from 'mol-plugin/util/loci-label-manager';
  11. import { IconButton } from './controls/common';
  12. import { PluginStateObject } from 'mol-plugin/state/objects';
  13. import { StateTransforms } from 'mol-plugin/state/transforms';
  14. import { StateTransformer } from 'mol-state';
  15. import { ModelFromTrajectory } from 'mol-plugin/state/transforms/model';
  16. import { AnimationControls } from './state/animation';
  17. export class TrajectoryViewportControls extends PluginUIComponent<{}, { show: boolean, label: string }> {
  18. state = { show: false, label: '' }
  19. private update = () => {
  20. const state = this.plugin.state.dataState;
  21. const models = state.selectQ(q => q.ofTransformer(StateTransforms.Model.ModelFromTrajectory));
  22. if (models.length === 0) {
  23. this.setState({ show: false });
  24. return;
  25. }
  26. let label = '', count = 0, parents = new Set<string>();
  27. for (const m of models) {
  28. if (!m.sourceRef) continue;
  29. const parent = state.cells.get(m.sourceRef)!.obj as PluginStateObject.Molecule.Trajectory;
  30. if (!parent) continue;
  31. if (parent.data.length > 1) {
  32. if (parents.has(m.sourceRef)) {
  33. // do not show the controls if there are 2 models of the same trajectory present
  34. this.setState({ show: false });
  35. }
  36. parents.add(m.sourceRef);
  37. count++;
  38. if (!label) {
  39. const idx = (m.transform.params! as StateTransformer.Params<ModelFromTrajectory>).modelIndex;
  40. label = `Model ${idx + 1} / ${parent.data.length}`;
  41. }
  42. }
  43. }
  44. if (count > 1) label = '';
  45. this.setState({ show: count > 0, label });
  46. }
  47. componentDidMount() {
  48. this.subscribe(this.plugin.state.dataState.events.changed, this.update);
  49. this.subscribe(this.plugin.behaviors.state.isAnimating, this.update);
  50. }
  51. reset = () => PluginCommands.State.ApplyAction.dispatch(this.plugin, {
  52. state: this.plugin.state.dataState,
  53. action: UpdateTrajectory.create({ action: 'reset' })
  54. });
  55. prev = () => PluginCommands.State.ApplyAction.dispatch(this.plugin, {
  56. state: this.plugin.state.dataState,
  57. action: UpdateTrajectory.create({ action: 'advance', by: -1 })
  58. });
  59. next = () => PluginCommands.State.ApplyAction.dispatch(this.plugin, {
  60. state: this.plugin.state.dataState,
  61. action: UpdateTrajectory.create({ action: 'advance', by: 1 })
  62. });
  63. // stopAnimation = () => {
  64. // this.plugin.state.animation.stop();
  65. // }
  66. // playAnimation = () => {
  67. // const anim = this.plugin.state.animation;
  68. // if (anim.state.params.current === AnimateModelIndex.name) {
  69. // anim.start();
  70. // } else {
  71. // anim.play(AnimateModelIndex, ParamDefinition.getDefaultValues(AnimateModelIndex.params(this.plugin) as any as ParamDefinition.Params))
  72. // }
  73. // }
  74. render() {
  75. const isAnimating = this.plugin.behaviors.state.isAnimating.value;
  76. if (!this.state.show || (isAnimating && !this.state.label)) return null;
  77. return <div className='msp-traj-controls'>
  78. {/* <IconButton icon={isAnimating ? 'stop' : 'play'} title={isAnimating ? 'Stop' : 'Play'} onClick={isAnimating ? this.stopAnimation : this.playAnimation} /> */}
  79. {!isAnimating && <IconButton icon='model-first' title='First Model' onClick={this.reset} disabled={isAnimating} />}
  80. {!isAnimating && <IconButton icon='model-prev' title='Previous Model' onClick={this.prev} disabled={isAnimating} />}
  81. {!isAnimating && <IconButton icon='model-next' title='Next Model' onClick={this.next} disabled={isAnimating} />}
  82. {!!this.state.label && <span>{this.state.label}</span> }
  83. </div>;
  84. }
  85. }
  86. export class StateSnapshotViewportControls extends PluginUIComponent<{}, { isBusy: boolean, show: boolean }> {
  87. state = { isBusy: false, show: true }
  88. componentDidMount() {
  89. // TODO: this needs to be diabled when the state is updating!
  90. this.subscribe(this.plugin.state.snapshots.events.changed, () => this.forceUpdate());
  91. this.subscribe(this.plugin.behaviors.state.isUpdating, isBusy => this.setState({ isBusy }));
  92. this.subscribe(this.plugin.behaviors.state.isAnimating, isBusy => this.setState({ isBusy }))
  93. window.addEventListener('keyup', this.keyUp, false);
  94. }
  95. componentWillUnmount() {
  96. super.componentWillUnmount();
  97. window.removeEventListener('keyup', this.keyUp, false);
  98. }
  99. keyUp = (e: KeyboardEvent) => {
  100. if (!e.ctrlKey || this.state.isBusy || e.target !== document.body) return;
  101. const snapshots = this.plugin.state.snapshots;
  102. if (e.keyCode === 37) { // left
  103. if (snapshots.state.isPlaying) snapshots.stop();
  104. this.prev();
  105. } else if (e.keyCode === 38) { // up
  106. if (snapshots.state.isPlaying) snapshots.stop();
  107. if (snapshots.state.entries.size === 0) return;
  108. const e = snapshots.state.entries.get(0);
  109. this.update(e.snapshot.id);
  110. } else if (e.keyCode === 39) { // right
  111. if (snapshots.state.isPlaying) snapshots.stop();
  112. this.next();
  113. } else if (e.keyCode === 40) { // down
  114. if (snapshots.state.isPlaying) snapshots.stop();
  115. if (snapshots.state.entries.size === 0) return;
  116. const e = snapshots.state.entries.get(snapshots.state.entries.size - 1);
  117. this.update(e.snapshot.id);
  118. }
  119. };
  120. async update(id: string) {
  121. this.setState({ isBusy: true });
  122. await PluginCommands.State.Snapshots.Apply.dispatch(this.plugin, { id });
  123. this.setState({ isBusy: false });
  124. }
  125. change = (e: React.ChangeEvent<HTMLSelectElement>) => {
  126. if (e.target.value === 'none') return;
  127. this.update(e.target.value);
  128. }
  129. prev = () => {
  130. const s = this.plugin.state.snapshots;
  131. const id = s.getNextId(s.state.current, -1);
  132. if (id) this.update(id);
  133. }
  134. next = () => {
  135. const s = this.plugin.state.snapshots;
  136. const id = s.getNextId(s.state.current, 1);
  137. if (id) this.update(id);
  138. }
  139. togglePlay = () => {
  140. this.plugin.state.snapshots.togglePlay();
  141. }
  142. render() {
  143. const snapshots = this.plugin.state.snapshots;
  144. const count = snapshots.state.entries.size;
  145. if (count < 2 || !this.state.show) {
  146. return null;
  147. }
  148. const current = snapshots.state.current;
  149. const isPlaying = snapshots.state.isPlaying;
  150. return <div className='msp-state-snapshot-viewport-controls'>
  151. <select className='msp-form-control' value={current || 'none'} onChange={this.change} disabled={this.state.isBusy || isPlaying}>
  152. {!current && <option key='none' value='none'></option>}
  153. {snapshots.state.entries.valueSeq().map((e, i) => <option key={e!.snapshot.id} value={e!.snapshot.id}>{`[${i! + 1}/${count}]`} {e!.name || new Date(e!.timestamp).toLocaleString()}</option>)}
  154. </select>
  155. <IconButton icon={isPlaying ? 'stop' : 'play'} title={isPlaying ? 'Pause' : 'Cycle States'} onClick={this.togglePlay}
  156. disabled={isPlaying ? false : this.state.isBusy} />
  157. {!isPlaying && <>
  158. <IconButton icon='left-open' title='Previous State' onClick={this.prev} disabled={this.state.isBusy || isPlaying} />
  159. <IconButton icon='right-open' title='Next State' onClick={this.next} disabled={this.state.isBusy || isPlaying} />
  160. </>}
  161. </div>;
  162. }
  163. }
  164. export class AnimationViewportControls extends PluginUIComponent<{}, { isEmpty: boolean, isExpanded: boolean, isUpdating: boolean, isAnimating: boolean, isPlaying: boolean }> {
  165. state = { isEmpty: true, isExpanded: false, isUpdating: false, isAnimating: false, isPlaying: false };
  166. componentDidMount() {
  167. this.subscribe(this.plugin.state.snapshots.events.changed, () => {
  168. if (this.plugin.state.snapshots.state.isPlaying) this.setState({ isPlaying: true, isExpanded: false });
  169. else this.setState({ isPlaying: false });
  170. });
  171. this.subscribe(this.plugin.behaviors.state.isUpdating, isUpdating => {
  172. if (isUpdating) this.setState({ isUpdating: true, isExpanded: false, isEmpty: this.plugin.state.dataState.tree.transforms.size < 2 });
  173. else this.setState({ isUpdating: false, isEmpty: this.plugin.state.dataState.tree.transforms.size < 2 });
  174. });
  175. this.subscribe(this.plugin.behaviors.state.isAnimating, isAnimating => {
  176. if (isAnimating) this.setState({ isAnimating: true, isExpanded: false });
  177. else this.setState({ isAnimating: false });
  178. });
  179. }
  180. toggleExpanded = () => this.setState({ isExpanded: !this.state.isExpanded });
  181. stop = () => {
  182. this.plugin.state.animation.stop();
  183. this.plugin.state.snapshots.stop();
  184. }
  185. render() {
  186. // if (!this.state.show) return null;
  187. const isPlaying = this.plugin.state.snapshots.state.isPlaying;
  188. if (isPlaying) return null;
  189. const isAnimating = this.state.isAnimating;
  190. return <div className='msp-animation-viewport-controls'>
  191. <IconButton icon={isAnimating || isPlaying ? 'stop' : 'play'} title={isAnimating ? 'Stop' : 'Select Animation'}
  192. onClick={isAnimating || isPlaying ? this.stop : this.toggleExpanded}
  193. disabled={isAnimating|| isPlaying ? false : this.state.isUpdating || this.state.isPlaying || this.state.isEmpty} />
  194. {(this.state.isExpanded && !this.state.isUpdating) && <div className='msp-animation-viewport-controls-select'>
  195. <AnimationControls onStart={this.toggleExpanded} />
  196. </div>}
  197. </div>;
  198. }
  199. }
  200. export class LociLabelControl extends PluginUIComponent<{}, { entries: ReadonlyArray<LociLabelEntry> }> {
  201. state = { entries: [] }
  202. componentDidMount() {
  203. this.subscribe(this.plugin.behaviors.labels.highlight, e => this.setState({ entries: e.entries }));
  204. }
  205. render() {
  206. if (this.state.entries.length === 0) return null;
  207. return <div className='msp-highlight-info'>
  208. {this.state.entries.map((e, i) => <div key={'' + i}>{e}</div>)}
  209. </div>;
  210. }
  211. }