selection.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import * as React from 'react';
  7. import { CollapsableControls, CollapsableState } from '../base';
  8. import { StructureSelectionQueries, SelectionModifier } from '../../util/structure-selection-helper';
  9. import { ButtonSelect, Options } from '../controls/common';
  10. import { PluginCommands } from '../../command';
  11. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  12. import { Interactivity } from '../../util/interactivity';
  13. import { ParameterControls } from '../controls/parameters';
  14. import { camelCaseToWords } from '../../../mol-util/string';
  15. const StructureSelectionParams = {
  16. granularity: Interactivity.Params.granularity,
  17. }
  18. interface StructureSelectionControlsState extends CollapsableState {
  19. minRadius: number,
  20. extraRadius: number,
  21. durationMs: number
  22. }
  23. export class StructureSelectionControls<P, S extends StructureSelectionControlsState> extends CollapsableControls<P, S> {
  24. componentDidMount() {
  25. this.subscribe(this.plugin.events.interactivity.selectionUpdated, () => {
  26. this.forceUpdate()
  27. });
  28. this.subscribe(this.plugin.events.interactivity.propsUpdated, () => {
  29. this.forceUpdate()
  30. });
  31. }
  32. get stats() {
  33. const stats = this.plugin.helpers.structureSelectionManager.stats
  34. if (stats.structureCount === 0 || stats.elementCount === 0) {
  35. return 'Selected nothing'
  36. } else {
  37. return `Selected ${stats.label}`
  38. }
  39. }
  40. focus = () => {
  41. const { extraRadius, minRadius, durationMs } = this.state
  42. const { sphere } = this.plugin.helpers.structureSelectionManager.getBoundary();
  43. if (sphere.radius === 0) return
  44. const radius = Math.max(sphere.radius + extraRadius, minRadius);
  45. this.plugin.canvas3d.camera.focus(sphere.center, radius, durationMs);
  46. }
  47. setProps = (p: { param: PD.Base<any>, name: string, value: any }) => {
  48. if (p.name === 'granularity') {
  49. PluginCommands.Interactivity.SetProps.dispatch(this.plugin, { props: { granularity: p.value } });
  50. }
  51. }
  52. get values () {
  53. return {
  54. granularity: this.plugin.interactivity.props.granularity,
  55. }
  56. }
  57. set = (modifier: SelectionModifier, value: string) => {
  58. const query = StructureSelectionQueries[value as keyof typeof StructureSelectionQueries]
  59. this.plugin.helpers.structureSelection.set(modifier, query)
  60. }
  61. add = (value: string) => this.set('add', value)
  62. remove = (value: string) => this.set('remove', value)
  63. only = (value: string) => this.set('only', value)
  64. defaultState() {
  65. return {
  66. isCollapsed: false,
  67. header: 'Selection',
  68. minRadius: 8,
  69. extraRadius: 4,
  70. durationMs: 250
  71. } as S
  72. }
  73. renderControls() {
  74. const queries = Object.keys(StructureSelectionQueries).map(name => {
  75. return [name, camelCaseToWords(name)] as [string, string]
  76. })
  77. return <div>
  78. <div className='msp-control-row msp-row-text'>
  79. <button className='msp-btn msp-btn-block' onClick={this.focus}>
  80. <span className={`msp-icon msp-icon-focus-on-visual`} style={{ position: 'absolute', left: '10px' }} />
  81. {this.stats}
  82. </button>
  83. </div>
  84. <ParameterControls params={StructureSelectionParams} values={this.values} onChange={this.setProps} />
  85. <div className='msp-control-row'>
  86. <div className='msp-select-row'>
  87. <ButtonSelect label='Add' onChange={this.add}>
  88. <optgroup label='Add'>
  89. {Options(queries)}
  90. </optgroup>
  91. </ButtonSelect>
  92. <ButtonSelect label='Remove' onChange={this.remove}>
  93. <optgroup label='Remove'>
  94. {Options(queries)}
  95. </optgroup>
  96. </ButtonSelect>
  97. <ButtonSelect label='Only' onChange={this.only}>
  98. <optgroup label='Only'>
  99. {Options(queries)}
  100. </optgroup>
  101. </ButtonSelect>
  102. </div>
  103. </div>
  104. </div>
  105. }
  106. }