Browse Source

wip, repr provider

Alexander Rose 6 years ago
parent
commit
4924d434e7

+ 2 - 1
src/apps/canvas/structure-view.ts

@@ -197,7 +197,8 @@ export async function StructureView(app: App, canvas3d: Canvas3D, models: Readon
                     if (structureRepresentations[k]) {
                         repr = structureRepresentations[k]
                     } else {
-                        repr = app.structureRepresentationRegistry.create(k, app.reprCtx, structure)
+                        const provider = app.structureRepresentationRegistry.get(k)
+                        repr = provider.factory(provider.getParams)
                         structureRepresentations[k] = repr
                         canvas3d.add(repr)
                     }

+ 1 - 1
src/mol-gl/renderer.ts

@@ -44,7 +44,7 @@ interface Renderer {
 }
 
 export const DefaultRendererProps = {
-    clearColor: 0x000000 as Color,
+    clearColor: Color(0x000000),
     viewport: Viewport.create(0, 0, 0, 0)
 }
 export type RendererProps = typeof DefaultRendererProps

+ 5 - 4
src/mol-plugin/state/transforms/visuals.ts

@@ -29,21 +29,22 @@ const CreateStructureRepresentation = PluginStateTransform.Create<SO.Molecule.St
         default: (a, ctx: PluginContext) => ({
             type: {
                 name: ctx.structureReprensentation.registry.default.name,
-                params: PD.getDefaultValues(ctx.structureReprensentation.registry.default.provider.getParams(ctx.structureReprensentation.themeCtx, a.data))
+                params: ctx.structureReprensentation.registry.default.provider.defaultValues
             }
         }),
         definition: (a, ctx: PluginContext) => ({
             type: PD.Mapped('Type', '',
                 ctx.structureReprensentation.registry.default.name,
                 ctx.structureReprensentation.registry.types,
-                name => ctx.structureReprensentation.registry.get(name)!.getParams(ctx.structureReprensentation.themeCtx, a.data))
+                name => ctx.structureReprensentation.registry.get(name).getParams(ctx.structureReprensentation.themeCtx, a.data))
         })
     },
     apply({ a, params }, plugin: PluginContext) {
         return Task.create('Structure Representation', async ctx => {
-            const repr = plugin.structureReprensentation.registry.create(params.type.name, plugin.structureReprensentation.themeCtx, a.data)
+            const provider = plugin.structureReprensentation.registry.get(params.type.name)
+            const repr = provider.factory(provider.getParams)
             await repr.createOrUpdate({ webgl: plugin.canvas3d.webgl, ...plugin.structureReprensentation.themeCtx }, params.type.params || {}, {}, a.data).runInContext(ctx);
-            return new SO.Molecule.Representation3D(repr, { label: params.type.name });
+            return new SO.Molecule.Representation3D(repr, { label: provider.label });
         });
     },
     update({ a, b, oldParams, newParams }, plugin: PluginContext) {

+ 18 - 13
src/mol-repr/representation.ts

@@ -27,10 +27,21 @@ export type RepresentationParamsGetter<D, P extends PD.Params> = (ctx: ThemeRegi
 //
 
 export interface RepresentationProvider<D, P extends PD.Params> {
+    readonly label: string
+    readonly description: string
     readonly factory: (getParams: RepresentationParamsGetter<D, P>) => Representation<D, P>
     readonly getParams: (ctx: ThemeRegistryContext, data: D) => P
-    // TODO
-    // readonly defaultParams: PD.Values<P>
+    readonly defaultValues: PD.Values<P>
+}
+
+export type AnyRepresentationProvider = RepresentationProvider<any, {}>
+
+export const EmptyRepresentationProvider = {
+    label: '',
+    description: '',
+    factory: () => Representation.Empty,
+    getParams: () => ({}),
+    defaultValues: {}
 }
 
 export class RepresentationRegistry<D> {
@@ -39,24 +50,18 @@ export class RepresentationRegistry<D> {
 
     get default() { return this._list[0]; }
     get types(): [string, string][] {
-        return this._list.map(e => [e.name, e.name] as [string, string]);
+        return this._list.map(e => [e.name, e.provider.label] as [string, string]);
     }
 
     constructor() {};
 
-    add<P extends PD.Params>(name: string, factory: RepresentationProvider<D, P>['factory'], getParams: RepresentationProvider<D, P>['getParams']) {
-        const provider = { factory, getParams } as RepresentationProvider<D, P>
+    add<P extends PD.Params>(name: string, provider: RepresentationProvider<D, P>) {
         this._list.push({ name, provider })
         this._map.set(name, provider)
     }
 
-    get(id: string) {
-        return this._map.get(id)
-    }
-
-    create(id: string, ctx: ThemeRegistryContext, data: D, props = {}): Representation<D, any> {
-        const provider = this.get(id)
-        return provider ? provider.factory(provider.getParams) : Representation.Empty
+    get<P extends PD.Params>(name: string): RepresentationProvider<D, P> {
+        return this._map.get(name) || EmptyRepresentationProvider as unknown as RepresentationProvider<D, P>
     }
 
     get list() {
@@ -86,7 +91,7 @@ interface Representation<D, P extends PD.Params = {}> {
 }
 namespace Representation {
     export type Any = Representation<any>
-    export const Empty: Representation<any> = {
+    export const Empty: Any = {
         label: '', renderObjects: [], props: {}, params: {}, updated: new BehaviorSubject(0),
         createOrUpdate: () => Task.constant('', undefined),
         getLoci: () => EmptyLoci,

+ 1 - 1
src/mol-repr/structure/registry.ts

@@ -14,7 +14,7 @@ export class StructureRepresentationRegistry extends RepresentationRegistry<Stru
         super()
         Object.keys(BuiltInStructureRepresentations).forEach(name => {
             const p = (BuiltInStructureRepresentations as { [k: string]: RepresentationProvider<Structure, any> })[name]
-            this.add(name, p.factory, p.getParams)
+            this.add(name, p)
         })
     }
 }

+ 5 - 1
src/mol-repr/structure/representation/ball-and-stick.ts

@@ -47,5 +47,9 @@ export function BallAndStickRepresentation(getParams: RepresentationParamsGetter
 }
 
 export const BallAndStickRepresentationProvider: StructureRepresentationProvider<typeof BallAndStickParams> = {
-    factory: BallAndStickRepresentation, getParams: getBallAndStickParams
+    label: 'Ball & Stick',
+    description: 'Displays atoms as spheres and bonds as cylinders.',
+    factory: BallAndStickRepresentation,
+    getParams: getBallAndStickParams,
+    defaultValues: PD.getDefaultValues(BallAndStickParams)
 }

+ 5 - 1
src/mol-repr/structure/representation/cartoon.ts

@@ -47,5 +47,9 @@ export function CartoonRepresentation(getParams: RepresentationParamsGetter<Stru
 }
 
 export const CartoonRepresentationProvider: StructureRepresentationProvider<CartoonParams> = {
-    factory: CartoonRepresentation, getParams: getCartoonParams
+    label: 'Cartoon',
+    description: 'Displays a ribbon smoothly following the trace atom of polymers.',
+    factory: CartoonRepresentation,
+    getParams: getCartoonParams,
+    defaultValues: PD.getDefaultValues(CartoonParams)
 }

+ 1 - 1
src/mol-repr/volume/registry.ts

@@ -12,7 +12,7 @@ export class VolumeRepresentationRegistry extends RepresentationRegistry<VolumeD
         super()
         Object.keys(BuiltInVolumeRepresentations).forEach(name => {
             const p = (BuiltInVolumeRepresentations as { [k: string]: RepresentationProvider<VolumeData, any> })[name]
-            this.add(name, p.factory, p.getParams)
+            this.add(name, p)
         })
     }
 }