diff --git a/src/mol-plugin/behavior/static/representation.ts b/src/mol-plugin/behavior/static/representation.ts index 0bd4ec276ddbf7f2be67231bdcfc86e6c4f51ac7..d62cb8ea8af9bfe6333ee49c323d1ea584e38b9a 100644 --- a/src/mol-plugin/behavior/static/representation.ts +++ b/src/mol-plugin/behavior/static/representation.ts @@ -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 diff --git a/src/mol-plugin/behavior/static/state.ts b/src/mol-plugin/behavior/static/state.ts index 4c4124f913246b77ee1a7e0356baa2585dfe07ca..6e6643a398448bad1fdc354133d9188dad74d0f4 100644 --- a/src/mol-plugin/behavior/static/state.ts +++ b/src/mol-plugin/behavior/static/state.ts @@ -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)); } diff --git a/src/mol-plugin/context.ts b/src/mol-plugin/context.ts index 8eda5baa8fa611bb50dde28f9cec1a9d333b97f4..19dfe141da26b466c0619f7ab7d75b6a9c0fa382 100644 --- a/src/mol-plugin/context.ts +++ b/src/mol-plugin/context.ts @@ -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