From 05ce3d9a212bc9c69e88bc1d157cea4b1f9fc636 Mon Sep 17 00:00:00 2001 From: Alexander Rose <alex.rose@rcsb.org> Date: Wed, 21 Nov 2018 12:01:53 -0800 Subject: [PATCH] wip, repr state --- .../behavior/static/representation.ts | 2 +- src/mol-repr/representation.ts | 38 +++++++++++++------ src/mol-repr/shape/representation.ts | 16 ++++---- .../structure/complex-representation.ts | 18 ++++----- .../structure/units-representation.ts | 20 ++++------ src/mol-repr/volume/representation.ts | 20 +++++----- 6 files changed, 63 insertions(+), 51 deletions(-) diff --git a/src/mol-plugin/behavior/static/representation.ts b/src/mol-plugin/behavior/static/representation.ts index a89699641..cde79a5b6 100644 --- a/src/mol-plugin/behavior/static/representation.ts +++ b/src/mol-plugin/behavior/static/representation.ts @@ -58,5 +58,5 @@ export function UpdateRepresentationVisibility(ctx: PluginContext) { } function updateVisibility(e: State.ObjectEvent, r: Representation<any>) { - r.setVisibility(!e.state.cellStates.get(e.ref).isHidden); + r.setState({ visible: !e.state.cellStates.get(e.ref).isHidden }); } \ No newline at end of file diff --git a/src/mol-repr/representation.ts b/src/mol-repr/representation.ts index 3243f1fa6..bff58847c 100644 --- a/src/mol-repr/representation.ts +++ b/src/mol-repr/representation.ts @@ -16,6 +16,7 @@ import { ColorTheme } from 'mol-theme/color'; import { SizeTheme } from 'mol-theme/size'; import { Theme, ThemeRegistryContext } from 'mol-theme/theme'; import { Subject } from 'rxjs'; +import { Mat4 } from 'mol-math/linear-algebra'; // export interface RepresentationProps { // visuals?: string[] @@ -87,22 +88,37 @@ interface Representation<D, P extends PD.Params = {}> { readonly renderObjects: ReadonlyArray<RenderObject> readonly props: Readonly<PD.Values<P>> readonly params: Readonly<P> + readonly state: Readonly<Representation.State> createOrUpdate: (props?: Partial<PD.Values<P>>, data?: D) => Task<void> + setState: (state: Partial<Representation.State>) => void getLoci: (pickingId: PickingId) => Loci mark: (loci: Loci, action: MarkerAction) => boolean - setVisibility: (value: boolean) => void - setPickable: (value: boolean) => void destroy: () => void } namespace Representation { + export interface State { + visible: boolean + pickable: boolean + syncManually: boolean + transform: Mat4 + } + export function createState() { + return { visible: false, pickable: false, syncManually: false, transform: Mat4.identity() } + } + export function updateState(state: State, update: Partial<State>) { + if (update.visible !== undefined) state.visible = update.visible + if (update.pickable !== undefined) state.pickable = update.pickable + if (update.syncManually !== undefined) state.syncManually = update.syncManually + if (update.transform !== undefined) Mat4.copy(state.transform, update.transform) + } + export type Any = Representation<any> export const Empty: Any = { - label: '', groupCount: 0, renderObjects: [], props: {}, params: {}, updated: new Subject(), + label: '', groupCount: 0, renderObjects: [], props: {}, params: {}, updated: new Subject(), state: createState(), createOrUpdate: () => Task.constant('', undefined), + setState: () => {}, getLoci: () => EmptyLoci, mark: () => false, - setVisibility: () => {}, - setPickable: () => {}, destroy: () => {} } @@ -111,6 +127,7 @@ namespace Representation { export function createMulti<D, P extends PD.Params = {}>(label: string, ctx: RepresentationContext, getParams: RepresentationParamsGetter<D, P>, reprDefs: Def<D, P>): Representation<D, P> { let version = 0 const updated = new Subject<number>() + const currentState = Representation.createState() let currentParams: P let currentProps: PD.Values<P> @@ -174,6 +191,7 @@ namespace Representation { updated.next(version++) }) }, + get state() { return currentState }, getLoci: (pickingId: PickingId) => { for (let i = 0, il = reprList.length; i < il; ++i) { const loci = reprList[i].getLoci(pickingId) @@ -188,15 +206,11 @@ namespace Representation { } return marked }, - setVisibility: (value: boolean) => { - for (let i = 0, il = reprList.length; i < il; ++i) { - reprList[i].setVisibility(value) - } - }, - setPickable: (value: boolean) => { + setState: (state: Partial<State>) => { for (let i = 0, il = reprList.length; i < il; ++i) { - reprList[i].setPickable(value) + reprList[i].setState(state) } + Representation.updateState(currentState, state) }, destroy() { for (let i = 0, il = reprList.length; i < il; ++i) { diff --git a/src/mol-repr/shape/representation.ts b/src/mol-repr/shape/representation.ts index 27be5e00d..a6a04a224 100644 --- a/src/mol-repr/shape/representation.ts +++ b/src/mol-repr/shape/representation.ts @@ -33,6 +33,7 @@ export type ShapeParams = typeof ShapeParams export function ShapeRepresentation<P extends ShapeParams>(ctx: RepresentationContext): ShapeRepresentation<P> { let version = 0 const updated = new Subject<number>() + const _state = Representation.createState() const renderObjects: RenderObject[] = [] let _renderObject: MeshRenderObject | undefined let _shape: Shape @@ -65,11 +66,12 @@ export function ShapeRepresentation<P extends ShapeParams>(ctx: RepresentationCo return { label: 'Shape mesh', - updated, get groupCount () { return locationIt ? locationIt.count : 0 }, get renderObjects () { return renderObjects }, - get params () { return currentParams }, get props () { return currentProps }, + get params () { return currentParams }, + get state() { return _state }, + updated, createOrUpdate, getLoci(pickingId: PickingId) { const { objectId, groupId } = pickingId @@ -103,11 +105,11 @@ export function ShapeRepresentation<P extends ShapeParams>(ctx: RepresentationCo } return changed }, - setVisibility(value: boolean) { - renderObjects.forEach(ro => ro.state.visible = value) - }, - setPickable(value: boolean) { - renderObjects.forEach(ro => ro.state.pickable = value) + setState(state: Partial<Representation.State>) { + if (state.visible !== undefined) renderObjects.forEach(ro => ro.state.visible = state.visible!) + if (state.pickable !== undefined) renderObjects.forEach(ro => ro.state.pickable = state.pickable!) + + Representation.updateState(_state, state) }, destroy() { // TODO diff --git a/src/mol-repr/structure/complex-representation.ts b/src/mol-repr/structure/complex-representation.ts index 9b29dc5bc..b069bb13f 100644 --- a/src/mol-repr/structure/complex-representation.ts +++ b/src/mol-repr/structure/complex-representation.ts @@ -12,7 +12,7 @@ import { StructureRepresentation, StructureParams } from './representation'; import { ComplexVisual } from './complex-visual'; import { PickingId } from 'mol-geo/geometry/picking'; import { MarkerAction } from 'mol-geo/geometry/marker-data'; -import { RepresentationContext, RepresentationParamsGetter } from 'mol-repr/representation'; +import { RepresentationContext, RepresentationParamsGetter, Representation } from 'mol-repr/representation'; import { Theme, createTheme } from 'mol-theme/theme'; import { ParamDefinition as PD } from 'mol-util/param-definition'; import { Subject } from 'rxjs'; @@ -20,6 +20,7 @@ import { Subject } from 'rxjs'; export function ComplexRepresentation<P extends StructureParams>(label: string, ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, P>, visualCtor: () => ComplexVisual<P>): StructureRepresentation<P> { let version = 0 const updated = new Subject<number>() + const _state = Representation.createState() let visual: ComplexVisual<P> | undefined let _structure: Structure @@ -51,12 +52,11 @@ export function ComplexRepresentation<P extends StructureParams>(label: string, return visual ? visual.mark(loci, action) : false } - function setVisibility(value: boolean) { - if (visual) visual.setVisibility(value) - } + function setState(state: Partial<Representation.State>) { + if (state.visible !== undefined && visual) visual.setVisibility(state.visible) + if (state.pickable !== undefined && visual) visual.setPickable(state.pickable) - function setPickable(value: boolean) { - if (visual) visual.setPickable(value) + Representation.updateState(_state, state) } function destroy() { @@ -73,12 +73,12 @@ export function ComplexRepresentation<P extends StructureParams>(label: string, }, get props() { return _props }, get params() { return _params }, - get updated() { return updated }, + get state() { return _state }, + updated, createOrUpdate, + setState, getLoci, mark, - setVisibility, - setPickable, destroy } } \ No newline at end of file diff --git a/src/mol-repr/structure/units-representation.ts b/src/mol-repr/structure/units-representation.ts index 53d8a391a..706af09d5 100644 --- a/src/mol-repr/structure/units-representation.ts +++ b/src/mol-repr/structure/units-representation.ts @@ -8,7 +8,7 @@ import { Structure, Unit } from 'mol-model/structure'; import { Task } from 'mol-task' import { RenderObject } from 'mol-gl/render-object'; -import { Visual, RepresentationContext, RepresentationParamsGetter } from '../representation'; +import { Visual, RepresentationContext, RepresentationParamsGetter, Representation } from '../representation'; import { Loci, EmptyLoci, isEmptyLoci } from 'mol-model/loci'; import { StructureGroup } from './units-visual'; import { StructureRepresentation, StructureParams } from './representation'; @@ -30,6 +30,7 @@ export interface UnitsVisual<P extends UnitsParams> extends Visual<StructureGrou export function UnitsRepresentation<P extends UnitsParams>(label: string, ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, P>, visualCtor: () => UnitsVisual<P>): StructureRepresentation<P> { let version = 0 const updated = new Subject<number>() + const _state = Representation.createState() let visuals = new Map<number, { group: Unit.SymmetryGroup, visual: UnitsVisual<P> }>() let _structure: Structure @@ -151,16 +152,11 @@ export function UnitsRepresentation<P extends UnitsParams>(label: string, ctx: R return changed } - function setVisibility(value: boolean) { - visuals.forEach(({ visual }) => { - visual.setVisibility(value) - }) - } + function setState(state: Partial<Representation.State>) { + if (state.visible !== undefined) visuals.forEach(({ visual }) => visual.setVisibility(state.visible!)) + if (state.pickable !== undefined) visuals.forEach(({ visual }) => visual.setPickable(state.pickable!)) - function setPickable(value: boolean) { - visuals.forEach(({ visual }) => { - visual.setPickable(value) - }) + Representation.updateState(_state, state) } function destroy() { @@ -186,12 +182,12 @@ export function UnitsRepresentation<P extends UnitsParams>(label: string, ctx: R }, get props() { return _props }, get params() { return _params }, + get state() { return _state }, updated, createOrUpdate, + setState, getLoci, mark, - setVisibility, - setPickable, destroy } } \ No newline at end of file diff --git a/src/mol-repr/volume/representation.ts b/src/mol-repr/volume/representation.ts index b866b00c5..4b33b0b3b 100644 --- a/src/mol-repr/volume/representation.ts +++ b/src/mol-repr/volume/representation.ts @@ -146,6 +146,7 @@ export type VolumeParams = typeof VolumeParams export function VolumeRepresentation<P extends VolumeParams>(label: string, ctx: RepresentationContext, getParams: RepresentationParamsGetter<VolumeData, P>, visualCtor: (volume: VolumeData) => VolumeVisual<P>): VolumeRepresentation<P> { let version = 0 const updated = new Subject<number>() + const _state = Representation.createState() let visual: VolumeVisual<P> let _volume: VolumeData @@ -191,16 +192,15 @@ export function VolumeRepresentation<P extends VolumeParams>(label: string, ctx: return visual ? visual.mark(loci, action) : false } - function destroy() { - if (visual) visual.destroy() - } + function setState(state: Partial<Representation.State>) { + if (state.visible !== undefined && visual) visual.setVisibility(state.visible) + if (state.pickable !== undefined && visual) visual.setPickable(state.pickable) - function setVisibility(value: boolean) { - if (visual) visual.setVisibility(value) + Representation.updateState(_state, state) } - function setPickable(value: boolean) { - if (visual) visual.setPickable(value) + function destroy() { + if (visual) visual.destroy() } return { @@ -213,12 +213,12 @@ export function VolumeRepresentation<P extends VolumeParams>(label: string, ctx: }, get props () { return _props }, get params() { return _params }, - get updated() { return updated }, + get state() { return _state }, + updated, createOrUpdate, + setState, getLoci, mark, - setVisibility, - setPickable, destroy } } \ No newline at end of file -- GitLab