structure-element-selection.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Copyright (c) 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 { OrderedSet } from '../../mol-data/int';
  8. import { EmptyLoci, Loci } from '../../mol-model/loci';
  9. import { Structure, StructureElement } from '../../mol-model/structure';
  10. import { StateObject } from '../../mol-state';
  11. import { PluginContext } from '../context';
  12. import { PluginStateObject } from '../state/objects';
  13. export { StructureElementSelectionManager };
  14. class StructureElementSelectionManager {
  15. private entries = new Map<string, SelectionEntry>();
  16. private getEntry(s: Structure) {
  17. const cell = this.plugin.helpers.substructureParent.get(s);
  18. if (!cell) return;
  19. const ref = cell.transform.ref;
  20. if (!this.entries.has(ref)) {
  21. const entry = SelectionEntry(s);
  22. this.entries.set(ref, entry);
  23. return entry;
  24. }
  25. return this.entries.get(ref)!;
  26. }
  27. add(loci: Loci): Loci {
  28. if (StructureElement.isLoci(loci)) {
  29. const entry = this.getEntry(loci.structure);
  30. if (entry) {
  31. entry.selection = StructureElement.Loci.union(entry.selection, loci);
  32. return entry.selection;
  33. }
  34. }
  35. return EmptyLoci
  36. }
  37. remove(loci: Loci): Loci {
  38. if (StructureElement.isLoci(loci)) {
  39. const entry = this.getEntry(loci.structure);
  40. if (entry) {
  41. entry.selection = StructureElement.Loci.subtract(entry.selection, loci);
  42. return entry.selection.elements.length === 0 ? EmptyLoci : entry.selection;
  43. }
  44. }
  45. return EmptyLoci
  46. }
  47. set(loci: Loci): Loci {
  48. if (StructureElement.isLoci(loci)) {
  49. const entry = this.getEntry(loci.structure);
  50. if (entry) {
  51. entry.selection = loci;
  52. return entry.selection.elements.length === 0 ? EmptyLoci : entry.selection;
  53. }
  54. }
  55. return EmptyLoci;
  56. }
  57. clear() {
  58. const keys = this.entries.keys();
  59. const selections: StructureElement.Loci[] = [];
  60. while (true) {
  61. const k = keys.next();
  62. if (k.done) break;
  63. const s = this.entries.get(k.value)!;
  64. if (s.selection.elements.length > 0) selections.push(s.selection);
  65. s.selection = StructureElement.Loci(s.selection.structure, []);
  66. }
  67. return selections;
  68. }
  69. get(structure: Structure) {
  70. const entry = this.getEntry(structure);
  71. if (!entry) return EmptyLoci;
  72. return entry.selection;
  73. }
  74. has(loci: Loci) {
  75. if (StructureElement.isLoci(loci)) {
  76. const entry = this.getEntry(loci.structure);
  77. if (entry) {
  78. return StructureElement.Loci.areIntersecting(loci, entry.selection);
  79. }
  80. }
  81. return false;
  82. }
  83. tryGetRange(loci: Loci): StructureElement.Loci | undefined {
  84. if (!StructureElement.isLoci(loci)) return;
  85. if (loci.elements.length !== 1) return;
  86. const entry = this.getEntry(loci.structure);
  87. if (!entry) return;
  88. let xs = loci.elements[0];
  89. let e: StructureElement.Loci['elements'][0] | undefined;
  90. for (const _e of entry.selection.elements) {
  91. if (xs.unit === _e.unit) {
  92. e = _e;
  93. break;
  94. }
  95. }
  96. if (!e) return;
  97. let predIdx = OrderedSet.findPredecessorIndex(e.indices, OrderedSet.min(xs.indices));
  98. if (predIdx === 0) return;
  99. let fst;
  100. if (predIdx < OrderedSet.size(e.indices)) {
  101. fst = OrderedSet.getAt(e.indices, predIdx)
  102. if (fst > OrderedSet.min(xs.indices)) fst = OrderedSet.getAt(e.indices, predIdx - 1) + 1 as StructureElement.UnitIndex;
  103. } else {
  104. fst = OrderedSet.getAt(e.indices, predIdx - 1) + 1 as StructureElement.UnitIndex;
  105. }
  106. return StructureElement.Loci(entry.selection.structure, [{
  107. unit: e.unit,
  108. indices: OrderedSet.ofRange(fst, OrderedSet.max(xs.indices))
  109. }]);
  110. }
  111. private prevHighlight: StructureElement.Loci | undefined = void 0;
  112. accumulateInteractiveHighlight(loci: Loci) {
  113. if (StructureElement.isLoci(loci)) {
  114. if (this.prevHighlight) {
  115. this.prevHighlight = StructureElement.Loci.union(this.prevHighlight, loci);
  116. } else {
  117. this.prevHighlight = loci;
  118. }
  119. }
  120. return this.prevHighlight;
  121. }
  122. clearInteractiveHighlight() {
  123. const ret = this.prevHighlight;
  124. this.prevHighlight = void 0;
  125. return ret || EmptyLoci;
  126. }
  127. private onRemove(ref: string) {
  128. if (this.entries.has(ref)) this.entries.delete(ref);
  129. }
  130. private onUpdate(ref: string, oldObj: StateObject | undefined, obj: StateObject) {
  131. if (!PluginStateObject.Molecule.Structure.is(obj)) return;
  132. if (this.entries.has(ref)) {
  133. if (!PluginStateObject.Molecule.Structure.is(oldObj) || oldObj === obj || oldObj.data === obj.data) return;
  134. // remap the old selection to be related to the new object if possible.
  135. if (Structure.areUnitAndIndicesEqual(oldObj.data, obj.data)) {
  136. this.entries.set(ref, remapSelectionEntry(this.entries.get(ref)!, obj.data));
  137. return;
  138. }
  139. // clear the selection
  140. this.entries.set(ref, SelectionEntry(obj.data));
  141. }
  142. }
  143. constructor(private plugin: PluginContext) {
  144. plugin.state.dataState.events.object.removed.subscribe(e => this.onRemove(e.ref));
  145. plugin.state.dataState.events.object.updated.subscribe(e => this.onUpdate(e.ref, e.oldObj, e.obj));
  146. }
  147. }
  148. interface SelectionEntry {
  149. selection: StructureElement.Loci
  150. }
  151. function SelectionEntry(s: Structure): SelectionEntry {
  152. return {
  153. selection: StructureElement.Loci(s, [])
  154. };
  155. }
  156. function remapSelectionEntry(e: SelectionEntry, s: Structure): SelectionEntry {
  157. return {
  158. selection: StructureElement.Loci.remap(e.selection, s)
  159. };
  160. }