representation.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 { MarkerAction } from '../../../mol-util/marker-action';
  8. import { PluginContext } from '../../../mol-plugin/context';
  9. import { PluginStateObject as SO } from '../../state/objects';
  10. import { lociLabel } from '../../../mol-theme/label';
  11. import { PluginBehavior } from '../behavior';
  12. import { Interactivity } from '../../util/interactivity';
  13. import { StateTreeSpine } from '../../../mol-state/tree/spine';
  14. import { StateSelection } from '../../../mol-state';
  15. import { ButtonsType, ModifiersKeys } from '../../../mol-util/input/input-observer';
  16. import { Binding } from '../../../mol-util/binding';
  17. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  18. import { EmptyLoci, Loci } from '../../../mol-model/loci';
  19. import { Structure } from '../../../mol-model/structure';
  20. import { arrayMax } from '../../../mol-util/array';
  21. const B = ButtonsType
  22. const M = ModifiersKeys
  23. const Trigger = Binding.Trigger
  24. //
  25. const DefaultHighlightLociBindings = {
  26. hoverHighlightOnly: Binding([Trigger(B.Flag.None)], 'Highlight hovered element using ${triggers}'),
  27. hoverHighlightOnlyExtend: Binding([Trigger(B.Flag.None, M.create({ shift: true }))], 'Extend highlight from selected to hovered element along polymer using ${triggers}'),
  28. }
  29. const HighlightLociParams = {
  30. bindings: PD.Value(DefaultHighlightLociBindings, { isHidden: true }),
  31. }
  32. type HighlightLociProps = PD.Values<typeof HighlightLociParams>
  33. export const HighlightLoci = PluginBehavior.create({
  34. name: 'representation-highlight-loci',
  35. category: 'interaction',
  36. ctor: class extends PluginBehavior.Handler<HighlightLociProps> {
  37. private lociMarkProvider = (interactionLoci: Interactivity.Loci, action: MarkerAction) => {
  38. if (!this.ctx.canvas3d) return;
  39. this.ctx.canvas3d.mark({ loci: interactionLoci.loci }, action)
  40. }
  41. register() {
  42. this.subscribeObservable(this.ctx.behaviors.interaction.hover, ({ current, buttons, modifiers }) => {
  43. if (!this.ctx.canvas3d) return
  44. let matched = false
  45. if (Binding.match(this.params.bindings.hoverHighlightOnly, buttons, modifiers)) {
  46. this.ctx.interactivity.lociHighlights.highlightOnly(current)
  47. matched = true
  48. }
  49. if (Binding.match(this.params.bindings.hoverHighlightOnlyExtend, buttons, modifiers)) {
  50. this.ctx.interactivity.lociHighlights.highlightOnlyExtend(current)
  51. matched = true
  52. }
  53. if (!matched) {
  54. this.ctx.interactivity.lociHighlights.highlightOnly({ repr: current.repr, loci: EmptyLoci })
  55. }
  56. });
  57. this.ctx.interactivity.lociHighlights.addProvider(this.lociMarkProvider)
  58. }
  59. unregister() {
  60. this.ctx.interactivity.lociHighlights.removeProvider(this.lociMarkProvider)
  61. }
  62. },
  63. params: () => HighlightLociParams,
  64. display: { name: 'Highlight Loci on Canvas' }
  65. });
  66. //
  67. const DefaultSelectLociBindings = {
  68. clickSelect: Binding.Empty,
  69. clickSelectExtend: Binding([Trigger(B.Flag.Primary, M.create({ shift: true }))], 'Extend selection to clicked element along polymer using ${triggers}.'),
  70. clickSelectOnly: Binding.Empty,
  71. clickSelectToggle: Binding([Trigger(B.Flag.Primary)], 'Toggle selection of clicked element using ${triggers}.'),
  72. clickDeselect: Binding.Empty,
  73. clickDeselectAllOnEmpty: Binding([Trigger(B.Flag.Secondary)], 'Deselect all when clicking on nothing using ${triggers}.'),
  74. }
  75. const SelectLociParams = {
  76. bindings: PD.Value(DefaultSelectLociBindings, { isHidden: true }),
  77. }
  78. type SelectLociProps = PD.Values<typeof SelectLociParams>
  79. export const SelectLoci = PluginBehavior.create({
  80. name: 'representation-select-loci',
  81. category: 'interaction',
  82. ctor: class extends PluginBehavior.Handler<SelectLociProps> {
  83. private spine: StateTreeSpine.Impl
  84. private lociMarkProvider = (interactionLoci: Interactivity.Loci, action: MarkerAction) => {
  85. if (!this.ctx.canvas3d) return;
  86. this.ctx.canvas3d.mark({ loci: interactionLoci.loci }, action)
  87. }
  88. private applySelectMark(ref: string, clear?: boolean) {
  89. const cell = this.ctx.state.dataState.cells.get(ref)
  90. if (cell && SO.isRepresentation3D(cell.obj)) {
  91. this.spine.current = cell
  92. const so = this.spine.getRootOfType(SO.Molecule.Structure)
  93. if (so) {
  94. if (clear) {
  95. this.lociMarkProvider({ loci: Structure.Loci(so.data) }, MarkerAction.Deselect)
  96. }
  97. const loci = this.ctx.helpers.structureSelectionManager.get(so.data)
  98. this.lociMarkProvider({ loci }, MarkerAction.Select)
  99. }
  100. }
  101. }
  102. register() {
  103. const actions: [keyof typeof DefaultSelectLociBindings, (current: Interactivity.Loci) => void][] = [
  104. ['clickSelect', current => this.ctx.interactivity.lociSelects.select(current)],
  105. ['clickSelectToggle', current => this.ctx.interactivity.lociSelects.selectToggle(current)],
  106. ['clickSelectExtend', current => this.ctx.interactivity.lociSelects.selectExtend(current)],
  107. ['clickSelectOnly', current => this.ctx.interactivity.lociSelects.selectOnly(current)],
  108. ['clickDeselect', current => this.ctx.interactivity.lociSelects.deselect(current)],
  109. ['clickDeselectAllOnEmpty', current => {
  110. if (Loci.isEmpty(current.loci)) this.ctx.interactivity.lociSelects.deselectAll()
  111. }],
  112. ];
  113. // sort the action so that the ones with more modifiers trigger sooner.
  114. actions.sort((a, b) => {
  115. const x = this.params.bindings[a[0]], y = this.params.bindings[b[0]];
  116. const k = x.triggers.length === 0 ? 0 : arrayMax(x.triggers.map(t => M.size(t.modifiers)));
  117. const l = y.triggers.length === 0 ? 0 : arrayMax(y.triggers.map(t => M.size(t.modifiers)));
  118. return l - k;
  119. })
  120. this.subscribeObservable(this.ctx.behaviors.interaction.click, ({ current, buttons, modifiers }) => {
  121. if (!this.ctx.canvas3d) return
  122. // only trigger the 1st action that matches
  123. for (const [binding, action] of actions) {
  124. if (Binding.match(this.params.bindings[binding], buttons, modifiers)) {
  125. action(current);
  126. break;
  127. }
  128. }
  129. });
  130. this.ctx.interactivity.lociSelects.addProvider(this.lociMarkProvider)
  131. this.subscribeObservable(this.ctx.events.state.object.created, ({ ref }) => this.applySelectMark(ref));
  132. // re-apply select-mark to all representation of an updated structure
  133. this.subscribeObservable(this.ctx.events.state.object.updated, ({ ref }) => {
  134. const cell = this.ctx.state.dataState.cells.get(ref)
  135. if (cell && SO.Molecule.Structure.is(cell.obj)) {
  136. const reprs = this.ctx.state.dataState.select(StateSelection.Generators.ofType(SO.Molecule.Structure.Representation3D, ref))
  137. for (const repr of reprs) this.applySelectMark(repr.transform.ref, true)
  138. }
  139. });
  140. }
  141. unregister() {
  142. this.ctx.interactivity.lociSelects.removeProvider(this.lociMarkProvider)
  143. }
  144. constructor(ctx: PluginContext, params: SelectLociProps) {
  145. super(ctx, params)
  146. this.spine = new StateTreeSpine.Impl(ctx.state.dataState.cells)
  147. }
  148. },
  149. params: () => SelectLociParams,
  150. display: { name: 'Select Loci on Canvas' }
  151. });
  152. export const DefaultLociLabelProvider = PluginBehavior.create({
  153. name: 'default-loci-label-provider',
  154. category: 'interaction',
  155. ctor: class implements PluginBehavior<undefined> {
  156. private f = lociLabel;
  157. register() { this.ctx.lociLabels.addProvider(this.f); }
  158. unregister() { this.ctx.lociLabels.removeProvider(this.f); }
  159. constructor(protected ctx: PluginContext) { }
  160. },
  161. display: { name: 'Provide Default Loci Label' }
  162. });