controls.tsx 11 KB

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