Browse Source

improved highlighting

- highlight SO.Molecule.Structure.Selections
- cleaned-up LociHighlightManager
Alexander Rose 5 years ago
parent
commit
f6ca679a57
2 changed files with 44 additions and 20 deletions
  1. 6 2
      src/mol-plugin/behavior/static/state.ts
  2. 38 18
      src/mol-plugin/util/interactivity.ts

+ 6 - 2
src/mol-plugin/behavior/static/state.ts

@@ -14,7 +14,6 @@ import { getFormattedTime } from '../../../mol-util/date';
 import { readFromFile } from '../../../mol-util/data-source';
 import { download } from '../../../mol-util/download';
 import { Structure } from '../../../mol-model/structure';
-import { EmptyLoci } from '../../../mol-model/loci';
 
 export function registerDefault(ctx: PluginContext) {
     SyncBehaviors(ctx);
@@ -111,6 +110,11 @@ export function Highlight(ctx: PluginContext) {
         } else if (cell && SO.isRepresentation3D(cell.obj)) {
             const { repr } = cell.obj.data
             ctx.interactivity.lociHighlights.highlightOnly({ loci: repr.getLoci(), repr }, false);
+        } else if (SO.Molecule.Structure.Selections.is(cell.obj)) {
+            ctx.interactivity.lociHighlights.clearHighlights();
+            for (const entry of cell.obj.data) {
+                ctx.interactivity.lociHighlights.highlight({ loci: entry.loci }, false);
+            }
         }
 
         // TODO: highlight volumes?
@@ -120,7 +124,7 @@ export function Highlight(ctx: PluginContext) {
 
 export function ClearHighlight(ctx: PluginContext) {
     PluginCommands.State.ClearHighlight.subscribe(ctx, ({ state, ref }) => {
-        ctx.interactivity.lociHighlights.highlightOnly({ loci: EmptyLoci }, false);
+        ctx.interactivity.lociHighlights.clearHighlights();
     });
 }
 

+ 38 - 18
src/mol-plugin/util/interactivity.ts

@@ -100,33 +100,53 @@ namespace Interactivity {
     //
 
     export class LociHighlightManager extends LociMarkManager {
-        private prev: Loci = { loci: EmptyLoci, repr: void 0 };
+        private prev: Loci[] = [];
+
+        private isHighlighted(loci: Loci) {
+            for (const p of this.prev) {
+                if (Loci.areEqual(p, loci)) return true
+            }
+            return false
+        }
+
+        private addHighlight(loci: Loci) {
+            this.mark(loci, MarkerAction.Highlight);
+            this.prev.push(loci)
+        }
+
+        clearHighlights() {
+            for (const p of this.prev) {
+                this.mark(p, MarkerAction.RemoveHighlight);
+            }
+            this.prev.length = 0
+        }
+
+        highlight(current: Loci, applyGranularity = true) {
+            const normalized = this.normalizedLoci(current, applyGranularity)
+            if (!this.isHighlighted(normalized)) {
+                this.addHighlight(normalized)
+            }
+        }
 
         highlightOnly(current: Loci, applyGranularity = true) {
             const normalized = this.normalizedLoci(current, applyGranularity)
-            if (StructureElement.Loci.is(normalized.loci)) {
-                const loci = normalized.loci;
-                this.mark(this.prev, MarkerAction.RemoveHighlight);
-                const toHighlight = { loci, repr: normalized.repr };
-                this.mark(toHighlight, MarkerAction.Highlight);
-                this.prev = toHighlight;
-            } else {
-                if (!Loci.areEqual(this.prev, normalized)) {
-                    this.mark(this.prev, MarkerAction.RemoveHighlight);
-                    this.mark(normalized, MarkerAction.Highlight);
-                    this.prev = normalized;
-                }
+            if (!this.isHighlighted(normalized)) {
+                this.clearHighlights()
+                this.addHighlight(normalized)
             }
         }
 
         highlightOnlyExtend(current: Loci, applyGranularity = true) {
             const normalized = this.normalizedLoci(current, applyGranularity)
             if (StructureElement.Loci.is(normalized.loci)) {
-                const loci = this.sel.tryGetRange(normalized.loci) || normalized.loci;
-                this.mark(this.prev, MarkerAction.RemoveHighlight);
-                const toHighlight = { loci, repr: normalized.repr };
-                this.mark(toHighlight, MarkerAction.Highlight);
-                this.prev = toHighlight;
+                const loci = {
+                    loci: this.sel.tryGetRange(normalized.loci) || normalized.loci,
+                    repr: normalized.repr
+                }
+                if (!this.isHighlighted(loci)) {
+                    this.clearHighlights()
+                    this.addHighlight(loci)
+                }
             }
         }
     }