From 227240fbe6c05a8f0ce65393b6f68bff70188387 Mon Sep 17 00:00:00 2001
From: Alexander Rose <alexander.rose@weirdbyte.de>
Date: Thu, 22 Nov 2018 00:13:55 -0800
Subject: [PATCH] wip highlighting

---
 .../behavior/dynamic/representation.ts        |  8 ++---
 src/mol-plugin/behavior/static/state.ts       | 32 +++++++------------
 src/mol-repr/structure/complex-visual.ts      |  2 +-
 src/mol-repr/structure/units-visual.ts        |  2 +-
 4 files changed, 18 insertions(+), 26 deletions(-)

diff --git a/src/mol-plugin/behavior/dynamic/representation.ts b/src/mol-plugin/behavior/dynamic/representation.ts
index e6b511604..5119494f8 100644
--- a/src/mol-plugin/behavior/dynamic/representation.ts
+++ b/src/mol-plugin/behavior/dynamic/representation.ts
@@ -19,8 +19,8 @@ export const HighlightLoci = PluginBehavior.create({
                 if (!this.ctx.canvas3d) return;
 
                 if (current.repr !== prevRepr || !areLociEqual(current.loci, prevLoci)) {
-                    this.ctx.canvas3d.mark(prevLoci, MarkerAction.RemoveHighlight);
-                    this.ctx.canvas3d.mark(current.loci, MarkerAction.Highlight);
+                    this.ctx.canvas3d.mark(prevLoci, MarkerAction.RemoveHighlight, prevRepr);
+                    this.ctx.canvas3d.mark(current.loci, MarkerAction.Highlight, current.repr);
                     prevLoci = current.loci;
                     prevRepr = current.repr;
                 }
@@ -38,8 +38,8 @@ export const SelectLoci = PluginBehavior.create({
             this.subscribeObservable(this.ctx.behaviors.canvas.selectLoci, current => {
                 if (!this.ctx.canvas3d) return;
                 if (current.repr !== prevRepr || !areLociEqual(current.loci, prevLoci)) {
-                    this.ctx.canvas3d.mark(prevLoci, MarkerAction.Deselect);
-                    this.ctx.canvas3d.mark(current.loci, MarkerAction.Select);
+                    this.ctx.canvas3d.mark(prevLoci, MarkerAction.Deselect, prevRepr);
+                    this.ctx.canvas3d.mark(current.loci, MarkerAction.Select, current.repr);
                     prevLoci = current.loci;
                     prevRepr = current.repr;
                 } else {
diff --git a/src/mol-plugin/behavior/static/state.ts b/src/mol-plugin/behavior/static/state.ts
index 612800e4f..5a6e6be01 100644
--- a/src/mol-plugin/behavior/static/state.ts
+++ b/src/mol-plugin/behavior/static/state.ts
@@ -8,9 +8,9 @@ import { PluginCommands } from '../../command';
 import { PluginContext } from '../../context';
 import { StateTree, Transform, State } from 'mol-state';
 import { PluginStateSnapshotManager } from 'mol-plugin/state/snapshots';
-import { PluginStateObject as SO } from '../../state/objects';
+import { PluginStateObject as SO, PluginStateObject } from '../../state/objects';
 import { EmptyLoci, EveryLoci } from 'mol-model/loci';
-import { MarkerAction } from 'mol-geo/geometry/marker-data';
+import { Structure } from 'mol-model/structure';
 
 export function registerDefault(ctx: PluginContext) {
     SyncBehaviors(ctx);
@@ -79,32 +79,24 @@ function setVisibilityVisitor(t: Transform, tree: StateTree, ctx: { state: State
     ctx.state.updateCellState(t.ref, { isHidden: ctx.value });
 }
 
+// TODO make isHighlighted and isSelect part of StateObjectCell.State and subscribe from there???
+// TODO select structures of subtree
+// TODO should also work for volumes and shapes
 export function Highlight(ctx: PluginContext) {
     PluginCommands.State.Highlight.subscribe(ctx, ({ state, ref }) => {
-        setHighlight(state, ref)
-        ctx.canvas3d.update()
-        ctx.canvas3d.requestDraw(true);
+        const cell = state.select(ref)[0]
+        const repr = cell && SO.isRepresentation3D(cell.obj) ? cell.obj.data : undefined
+        if (cell && cell.obj && cell.obj.type === PluginStateObject.Molecule.Structure.type) {
+            ctx.behaviors.canvas.highlightLoci.next({ loci: Structure.Loci(cell.obj.data) })
+        } else if (repr) {
+            ctx.behaviors.canvas.highlightLoci.next({ loci: EveryLoci, repr })
+        }
     });
 }
 
-function setHighlight(state: State, root: Transform.Ref) {
-    StateTree.doPreOrder(state.tree, state.transforms.get(root), { state }, setHighlightVisitor);
-}
-
-function setHighlightVisitor(t: Transform, tree: StateTree, ctx: { state: State }) {
-    const cell = ctx.state.select(t.ref)[0]
-    if (!cell) return
-    if (!SO.isRepresentation3D(cell.obj)) return;
-    cell.obj.data.mark(EveryLoci, MarkerAction.Highlight)
-
-}
-
 export function ClearHighlight(ctx: PluginContext) {
     PluginCommands.State.ClearHighlight.subscribe(ctx, ({ state, ref }) => {
         ctx.behaviors.canvas.highlightLoci.next({ loci: EmptyLoci })
-        ctx.canvas3d.mark(EveryLoci, MarkerAction.RemoveHighlight);
-        ctx.canvas3d.update()
-        ctx.canvas3d.requestDraw(true);
     });
 }
 
diff --git a/src/mol-repr/structure/complex-visual.ts b/src/mol-repr/structure/complex-visual.ts
index a5adc76f9..c8121e01f 100644
--- a/src/mol-repr/structure/complex-visual.ts
+++ b/src/mol-repr/structure/complex-visual.ts
@@ -153,7 +153,7 @@ export function ComplexVisual<P extends ComplexParams>(builder: ComplexVisualGeo
             }
 
             let changed = false
-            if (isEveryLoci(loci)) {
+            if (isEveryLoci(loci) || (Structure.isLoci(loci) && loci.structure === currentStructure)) {
                 changed = apply(Interval.ofBounds(0, groupCount * instanceCount))
             } else {
                 changed = mark(loci, currentStructure, apply)
diff --git a/src/mol-repr/structure/units-visual.ts b/src/mol-repr/structure/units-visual.ts
index a80885a74..dfd9384eb 100644
--- a/src/mol-repr/structure/units-visual.ts
+++ b/src/mol-repr/structure/units-visual.ts
@@ -184,7 +184,7 @@ export function UnitsVisual<P extends UnitsParams>(builder: UnitsVisualGeometryB
             }
 
             let changed = false
-            if (isEveryLoci(loci)) {
+            if (isEveryLoci(loci) || (Structure.isLoci(loci) && loci.structure === currentStructure)) {
                 changed = apply(Interval.ofBounds(0, groupCount * instanceCount))
             } else {
                 changed = mark(loci, { structure: currentStructure, group: currentGroup }, apply)
-- 
GitLab