controls.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. }
  94. async update(id: string) {
  95. this.setState({ isBusy: true });
  96. await PluginCommands.State.Snapshots.Apply.dispatch(this.plugin, { id });
  97. this.setState({ isBusy: false });
  98. }
  99. change = (e: React.ChangeEvent<HTMLSelectElement>) => {
  100. if (e.target.value === 'none') return;
  101. this.update(e.target.value);
  102. }
  103. prev = () => {
  104. const s = this.plugin.state.snapshots;
  105. const id = s.getNextId(s.state.current, -1);
  106. if (id) this.update(id);
  107. }
  108. next = () => {
  109. const s = this.plugin.state.snapshots;
  110. const id = s.getNextId(s.state.current, 1);
  111. if (id) this.update(id);
  112. }
  113. togglePlay = () => {
  114. this.plugin.state.snapshots.togglePlay();
  115. }
  116. render() {
  117. const snapshots = this.plugin.state.snapshots;
  118. const count = snapshots.state.entries.size;
  119. if (count < 2 || !this.state.show) {
  120. return null;
  121. }
  122. const current = snapshots.state.current;
  123. const isPlaying = snapshots.state.isPlaying;
  124. return <div className='msp-state-snapshot-viewport-controls'>
  125. <select className='msp-form-control' value={current || 'none'} onChange={this.change} disabled={this.state.isBusy || isPlaying}>
  126. {!current && <option key='none' value='none'></option>}
  127. {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>)}
  128. </select>
  129. <IconButton icon={isPlaying ? 'pause' : 'play'} title={isPlaying ? 'Pause' : 'Cycle States'} onClick={this.togglePlay}
  130. disabled={isPlaying ? false : this.state.isBusy} />
  131. {!isPlaying && <>
  132. <IconButton icon='left-open' title='Previous State' onClick={this.prev} disabled={this.state.isBusy || isPlaying} />
  133. <IconButton icon='right-open' title='Next State' onClick={this.next} disabled={this.state.isBusy || isPlaying} />
  134. </>}
  135. </div>;
  136. }
  137. }
  138. export class AnimationViewportControls extends PluginUIComponent<{}, { isEmpty: boolean, isExpanded: boolean, isUpdating: boolean, isAnimating: boolean, isPlaying: boolean }> {
  139. state = { isEmpty: true, isExpanded: false, isUpdating: false, isAnimating: false, isPlaying: false };
  140. componentDidMount() {
  141. this.subscribe(this.plugin.state.snapshots.events.changed, () => {
  142. if (this.plugin.state.snapshots.state.isPlaying) this.setState({ isPlaying: true, isExpanded: false });
  143. else this.setState({ isPlaying: false });
  144. });
  145. this.subscribe(this.plugin.behaviors.state.isUpdating, isUpdating => {
  146. if (isUpdating) this.setState({ isUpdating: true, isExpanded: false, isEmpty: this.plugin.state.dataState.tree.transforms.size < 2 });
  147. else this.setState({ isUpdating: false, isEmpty: this.plugin.state.dataState.tree.transforms.size < 2 });
  148. });
  149. this.subscribe(this.plugin.behaviors.state.isAnimating, isAnimating => {
  150. if (isAnimating) this.setState({ isAnimating: true, isExpanded: false });
  151. else this.setState({ isAnimating: false });
  152. });
  153. }
  154. toggleExpanded = () => this.setState({ isExpanded: !this.state.isExpanded });
  155. stop = () => this.plugin.state.animation.stop();
  156. render() {
  157. // if (!this.state.show) return null;
  158. const isAnimating = this.state.isAnimating;
  159. return <div className='msp-animation-viewport-controls'>
  160. <IconButton icon={isAnimating ? 'stop' : 'play'} title={isAnimating ? 'Stop' : 'Select Animation'}
  161. onClick={isAnimating ? this.stop : this.toggleExpanded}
  162. disabled={isAnimating ? false : this.state.isUpdating || this.state.isPlaying || this.state.isEmpty} />
  163. {(this.state.isExpanded && !this.state.isUpdating) && <div className='msp-animation-viewport-controls-select'>
  164. <AnimationControls onStart={this.toggleExpanded} />
  165. </div>}
  166. </div>;
  167. }
  168. }
  169. export class LociLabelControl extends PluginUIComponent<{}, { entries: ReadonlyArray<LociLabelEntry> }> {
  170. state = { entries: [] }
  171. componentDidMount() {
  172. this.subscribe(this.plugin.behaviors.labels.highlight, e => this.setState({ entries: e.entries }));
  173. }
  174. render() {
  175. if (this.state.entries.length === 0) return null;
  176. return <div className='msp-highlight-info'>
  177. {this.state.entries.map((e, i) => <div key={'' + i}>{e}</div>)}
  178. </div>;
  179. }
  180. }