Skip to content
Snippets Groups Projects
Commit 494cf19b authored by David Sehnal's avatar David Sehnal
Browse files

mol-plugin: handle repr visibility

parent 2bc180f2
No related branches found
No related tags found
No related merge requests found
......@@ -6,19 +6,21 @@
import { PluginStateObject as SO } from '../../state/objects';
import { PluginContext } from 'mol-plugin/context';
import { Representation } from 'mol-repr/representation';
import { State } from 'mol-state';
export function registerDefault(ctx: PluginContext) {
SyncRepresentationToCanvas(ctx);
UpdateRepresentationVisibility(ctx);
}
export function SyncRepresentationToCanvas(ctx: PluginContext) {
const events = ctx.state.dataState.events;
events.object.created.subscribe(e => {
if (!SO.isRepresentation3D(e.obj)) return;
updateVisibility(e, e.obj.data);
ctx.canvas3d.add(e.obj.data);
ctx.canvas3d.requestDraw(true);
// TODO: update visiblity, e.obj.data.setVisibility()
});
events.object.updated.subscribe(e => {
if (e.oldObj && SO.isRepresentation3D(e.oldObj)) {
......@@ -29,16 +31,15 @@ export function SyncRepresentationToCanvas(ctx: PluginContext) {
if (!SO.isRepresentation3D(e.obj)) return;
// TODO: update visiblity, e.obj.data.setVisibility()
updateVisibility(e, e.obj.data);
ctx.canvas3d.add(e.obj.data);
ctx.canvas3d.requestDraw(true);
});
events.object.removed.subscribe(e => {
const oo = e.obj;
if (!SO.isRepresentation3D(oo)) return;
ctx.canvas3d.remove(oo.data);
if (!SO.isRepresentation3D(e.obj)) return;
ctx.canvas3d.remove(e.obj.data);
ctx.canvas3d.requestDraw(true);
oo.data.destroy();
e.obj.data.destroy();
});
}
......@@ -46,7 +47,11 @@ export function UpdateRepresentationVisibility(ctx: PluginContext) {
ctx.state.dataState.events.cell.stateUpdated.subscribe(e => {
const cell = e.state.cells.get(e.ref)!;
if (!SO.isRepresentation3D(cell.obj)) return;
// TODO: update visiblity, e.obj.data.setVisibility()
updateVisibility(e, cell.obj.data);
ctx.canvas3d.requestDraw(true);
})
}
function updateVisibility(e: State.ObjectEvent, r: Representation<any>) {
r.setVisibility(!e.state.tree.cellStates.get(e.ref).isHidden);
}
\ No newline at end of file
......@@ -8,8 +8,10 @@ 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';
export function registerDefault(ctx: PluginContext) {
SyncBehaviors(ctx);
SetCurrentObject(ctx);
Update(ctx);
ApplyAction(ctx);
......@@ -19,6 +21,25 @@ export function registerDefault(ctx: PluginContext) {
Snapshots(ctx);
}
export function SyncBehaviors(ctx: PluginContext) {
ctx.events.state.object.created.subscribe(o => {
if (!SO.isBehavior(o.obj)) return;
o.obj.data.register();
});
ctx.events.state.object.removed.subscribe(o => {
if (!SO.isBehavior(o.obj)) return;
o.obj.data.unregister();
});
ctx.events.state.object.updated.subscribe(o => {
if (o.action === 'recreate') {
if (o.oldObj && SO.isBehavior(o.oldObj)) o.oldObj.data.unregister();
if (o.obj && SO.isBehavior(o.obj)) o.obj.data.register();
}
});
}
export function SetCurrentObject(ctx: PluginContext) {
PluginCommands.State.SetCurrentObject.subscribe(ctx, ({ state, ref }) => state.setCurrent(ref));
}
......
......@@ -20,7 +20,6 @@ import { BuiltInPluginBehaviors } from './behavior';
import { PluginCommand, PluginCommands } from './command';
import { PluginSpec } from './spec';
import { PluginState } from './state';
import { PluginStateObject as SO } from './state/objects';
import { TaskManager } from './util/task-manager';
export class PluginContext {
......@@ -43,8 +42,6 @@ export class PluginContext {
removed: merge(this.state.dataState.events.object.removed, this.state.behaviorState.events.object.removed),
updated: merge(this.state.dataState.events.object.updated, this.state.behaviorState.events.object.updated)
},
// data: this.state.dataState.events,
// behavior: this.state.behaviorState.events,
cameraSnapshots: this.state.cameraSnapshots.events,
snapshots: this.state.snapshots.events,
},
......@@ -58,10 +55,6 @@ export class PluginContext {
}
readonly behaviors = {
// state: {
// data: this.state.dataState.behaviors,
// behavior: this.state.behaviorState.behaviors
// },
canvas: {
highlightLoci: this.ev.behavior<{ loci: Loci, repr?: Representation.Any }>({ loci: EmptyLoci }),
selectLoci: this.ev.behavior<{ loci: Loci, repr?: Representation.Any }>({ loci: EmptyLoci }),
......@@ -145,33 +138,12 @@ export class PluginContext {
return PluginCommands.State.Update.dispatch(this, { state, tree });
}
private initEvents() {
this.events.state.object.created.subscribe(o => {
if (!SO.isBehavior(o.obj)) return;
o.obj.data.register();
});
this.events.state.object.removed.subscribe(o => {
if (!SO.isBehavior(o.obj)) return;
o.obj.data.unregister();
});
this.events.state.object.updated.subscribe(o => {
if (o.action === 'recreate') {
if (o.oldObj && SO.isBehavior(o.oldObj)) o.oldObj.data.unregister();
if (o.obj && SO.isBehavior(o.obj)) o.obj.data.register();
}
});
}
constructor(public spec: PluginSpec) {
this.initEvents();
this.initBuiltInBehavior();
this.initBehaviors();
this.initDataActions();
}
// logger = ;
// settings = ;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment