controls.tsx 11 KB

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