selection.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import * as React from 'react';
  8. import { CollapsableControls, CollapsableState } from '../base';
  9. import { StructureSelectionQuery, SelectionModifier, StructureSelectionQueryList } from '../../mol-plugin/util/structure-selection-helper';
  10. import { PluginCommands } from '../../mol-plugin/command';
  11. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  12. import { Interactivity } from '../../mol-plugin/util/interactivity';
  13. import { ParameterControls } from '../controls/parameters';
  14. import { stripTags } from '../../mol-util/string';
  15. import { StructureElement } from '../../mol-model/structure';
  16. import { ActionMenu } from '../controls/action-menu';
  17. import { ToggleButton } from '../controls/common';
  18. export const DefaultQueries = ActionMenu.createItems(StructureSelectionQueryList, {
  19. label: q => q.label,
  20. category: q => q.category
  21. });
  22. const StructureSelectionParams = {
  23. granularity: Interactivity.Params.granularity,
  24. }
  25. interface StructureSelectionControlsState extends CollapsableState {
  26. minRadius: number,
  27. extraRadius: number,
  28. durationMs: number,
  29. isDisabled: boolean,
  30. queryAction?: SelectionModifier
  31. }
  32. export class StructureSelectionControls<P, S extends StructureSelectionControlsState> extends CollapsableControls<P, S> {
  33. componentDidMount() {
  34. this.subscribe(this.plugin.events.interactivity.selectionUpdated, () => {
  35. this.forceUpdate()
  36. });
  37. this.subscribe(this.plugin.events.interactivity.propsUpdated, () => {
  38. this.forceUpdate()
  39. });
  40. this.subscribe(this.plugin.state.dataState.events.isUpdating, v => {
  41. this.setState({ isDisabled: v, queryAction: void 0 })
  42. })
  43. }
  44. get stats() {
  45. const stats = this.plugin.helpers.structureSelectionManager.stats
  46. if (stats.structureCount === 0 || stats.elementCount === 0) {
  47. return 'Selected nothing'
  48. } else {
  49. return `Selected ${stripTags(stats.label)}`
  50. }
  51. }
  52. focus = () => {
  53. const { extraRadius, minRadius, durationMs } = this.state
  54. if (this.plugin.helpers.structureSelectionManager.stats.elementCount === 0) return
  55. const principalAxes = this.plugin.helpers.structureSelectionManager.getPrincipalAxes();
  56. const { origin, dirA, dirC } = principalAxes.boxAxes
  57. const { sphere } = this.plugin.helpers.structureSelectionManager.getBoundary()
  58. const radius = Math.max(sphere.radius + extraRadius, minRadius);
  59. this.plugin.canvas3d?.camera.focus(origin, radius, this.plugin.canvas3d.boundingSphere.radius, durationMs, dirA, dirC);
  60. }
  61. focusLoci(loci: StructureElement.Loci) {
  62. return () => {
  63. const { extraRadius, minRadius, durationMs } = this.state
  64. if (this.plugin.helpers.structureSelectionManager.stats.elementCount === 0) return
  65. const { sphere } = StructureElement.Loci.getBoundary(loci)
  66. const radius = Math.max(sphere.radius + extraRadius, minRadius);
  67. this.plugin.canvas3d?.camera.focus(sphere.center, radius, this.plugin.canvas3d.boundingSphere.radius, durationMs);
  68. }
  69. }
  70. measureDistance = () => {
  71. const loci = this.plugin.helpers.structureSelectionManager.latestLoci;
  72. this.plugin.helpers.measurement.addDistance(loci[0].loci, loci[1].loci);
  73. }
  74. measureAngle = () => {
  75. const loci = this.plugin.helpers.structureSelectionManager.latestLoci;
  76. this.plugin.helpers.measurement.addAngle(loci[0].loci, loci[1].loci, loci[2].loci);
  77. }
  78. measureDihedral = () => {
  79. const loci = this.plugin.helpers.structureSelectionManager.latestLoci;
  80. this.plugin.helpers.measurement.addDihedral(loci[0].loci, loci[1].loci, loci[2].loci, loci[3].loci);
  81. }
  82. addLabel = () => {
  83. const loci = this.plugin.helpers.structureSelectionManager.latestLoci;
  84. this.plugin.helpers.measurement.addLabel(loci[0].loci);
  85. }
  86. addOrientation = () => {
  87. const loci = this.plugin.helpers.structureSelectionManager.latestLoci;
  88. this.plugin.helpers.measurement.addOrientation(loci[0].loci);
  89. }
  90. setProps = (p: { param: PD.Base<any>, name: string, value: any }) => {
  91. if (p.name === 'granularity') {
  92. PluginCommands.Interactivity.SetProps.dispatch(this.plugin, { props: { granularity: p.value } });
  93. }
  94. }
  95. get values () {
  96. return {
  97. granularity: this.plugin.interactivity.props.granularity,
  98. }
  99. }
  100. set = (modifier: SelectionModifier, selectionQuery: StructureSelectionQuery) => {
  101. this.plugin.helpers.structureSelection.set(modifier, selectionQuery, false)
  102. }
  103. selectQuery: ActionMenu.OnSelect = item => {
  104. if (!item || !this.state.queryAction) {
  105. this.setState({ queryAction: void 0 });
  106. return;
  107. }
  108. const q = this.state.queryAction!;
  109. this.setState({ queryAction: void 0 }, () => {
  110. this.set(q, item.value as StructureSelectionQuery);
  111. })
  112. }
  113. queries = DefaultQueries
  114. private showQueries(q: SelectionModifier) {
  115. return () => this.setState({ queryAction: this.state.queryAction === q ? void 0 : q });
  116. }
  117. toggleAdd = this.showQueries('add')
  118. toggleRemove = this.showQueries('remove')
  119. toggleOnly = this.showQueries('only')
  120. get controls() {
  121. return <div>
  122. <div className='msp-control-row msp-button-row'>
  123. <ToggleButton label='Select' toggle={this.toggleAdd} isSelected={this.state.queryAction === 'add'} disabled={this.state.isDisabled} />
  124. <ToggleButton label='Deselect' toggle={this.toggleRemove} isSelected={this.state.queryAction === 'remove'} disabled={this.state.isDisabled} />
  125. <ToggleButton label='Only' toggle={this.toggleOnly} isSelected={this.state.queryAction === 'only'} disabled={this.state.isDisabled} />
  126. </div>
  127. {this.state.queryAction && <ActionMenu items={this.queries} onSelect={this.selectQuery} />}
  128. </div>
  129. }
  130. defaultState() {
  131. return {
  132. isCollapsed: false,
  133. header: 'Selection',
  134. minRadius: 8,
  135. extraRadius: 4,
  136. durationMs: 250,
  137. queryAction: void 0,
  138. isDisabled: false
  139. } as S
  140. }
  141. renderControls() {
  142. const latest: JSX.Element[] = [];
  143. const mng = this.plugin.helpers.structureSelectionManager;
  144. // TODO: fix the styles, move them to CSS
  145. for (let i = 0, _i = Math.min(4, mng.latestLoci.length); i < _i; i++) {
  146. const e = mng.latestLoci[i];
  147. latest.push(<li key={e!.label}>
  148. <button className='msp-btn msp-btn-block msp-form-control' style={{ borderRight: '6px solid transparent', overflow: 'hidden' }}
  149. title='Click to focus.' onClick={this.focusLoci(e.loci)}>
  150. <span dangerouslySetInnerHTML={{ __html: e.label }} />
  151. </button>
  152. {/* <div>
  153. <IconButton icon='remove' title='Remove' onClick={() => {}} />
  154. </div> */}
  155. </li>)
  156. }
  157. return <div>
  158. <div className='msp-control-row msp-row-text'>
  159. <button className='msp-btn msp-btn-block' onClick={this.focus}>
  160. <span className={`msp-icon msp-icon-focus-on-visual`} style={{ position: 'absolute', left: '5px' }} />
  161. {this.stats}
  162. </button>
  163. </div>
  164. <ParameterControls params={StructureSelectionParams} values={this.values} onChange={this.setProps} isDisabled={this.state.isDisabled} />
  165. {this.controls}
  166. { latest.length > 0 &&
  167. <>
  168. <div className='msp-control-group-header' style={{ marginTop: '1px' }}><span>Latest Selections &amp; Measurement</span></div>
  169. <ul style={{ listStyle: 'none', marginTop: '1px', marginBottom: '0' }} className='msp-state-list'>
  170. {latest}
  171. </ul>
  172. {latest.length >= 1 && <div className='msp-control-row msp-row-text'>
  173. <button className='msp-btn msp-btn-block' onClick={this.addLabel} title='Add label for latest selection'>
  174. Add Label
  175. </button>
  176. </div>}
  177. {latest.length >= 1 && <div className='msp-control-row msp-row-text'>
  178. <button className='msp-btn msp-btn-block' onClick={this.addOrientation} title='Add orientation box/axes for latest selection'>
  179. Add Orientation
  180. </button>
  181. </div>}
  182. {latest.length >= 2 && <div className='msp-control-row msp-row-text'>
  183. <button className='msp-btn msp-btn-block' onClick={this.measureDistance} title='Measure distance between latest 2 selections'>
  184. Measure Distance
  185. </button>
  186. </div>}
  187. {latest.length >= 3 && <div className='msp-control-row msp-row-text'>
  188. <button className='msp-btn msp-btn-block' onClick={this.measureAngle} title='Measure angle between latest 3 selections'>
  189. Measure Angle
  190. </button>
  191. </div>}
  192. {latest.length >= 4 && <div className='msp-control-row msp-row-text'>
  193. <button className='msp-btn msp-btn-block' onClick={this.measureDihedral} title='Measure dihedral between latest 4 selections'>
  194. Measure Dihedral
  195. </button>
  196. </div>}
  197. </>}
  198. </div>
  199. }
  200. }