Browse Source

mol-plugin: custom text structure label

David Sehnal 6 years ago
parent
commit
7b8b2c1085

+ 7 - 2
src/mol-plugin/state/transforms/representation.ts

@@ -194,7 +194,12 @@ const StructureLabels3D = PluginStateTransform.BuiltIn({
     to: SO.Molecule.Representation3D,
     params: {
         // TODO: other targets
-        target: PD.Select<'elements' | 'residues'>('residues', [['residues', 'Residues'], ['elements', 'Elements']]),
+        target: PD.MappedStatic('residues', {
+            'elements': PD.Group({ }),
+            'residues': PD.Group({ }),
+            'static-text': PD.Group({ value: PD.Text('') }, { isFlat: true })
+        }),
+         // PD.Select<'elements' | 'residues'>('residues', [['residues', 'Residues'], ['elements', 'Elements']]),
         options: PD.Group({
             ...Text.Params,
 
@@ -212,7 +217,7 @@ const StructureLabels3D = PluginStateTransform.BuiltIn({
     apply({ a, params }) {
         return Task.create('Structure Labels', async ctx => {
             const repr = await getLabelRepresentation(ctx, a.data, params);
-            return new SO.Molecule.Representation3D(repr, { label: `Labels`, description: params.target });
+            return new SO.Molecule.Representation3D(repr, { label: `Labels`, description: params.target.name });
         });
     },
     update({ a, b, newParams }) {

+ 21 - 2
src/mol-plugin/util/structure-labels.ts

@@ -36,7 +36,7 @@ function getLabelsText(data: LabelsData, props: PD.Values<Text.Params>, text?: T
 
 export async function getLabelRepresentation(ctx: RuntimeContext, structure: Structure, params: StateTransformer.Params<StructureLabels3D>, prev?: ShapeRepresentation<LabelsData, Text, Text.Params>) {
     const repr = prev || ShapeRepresentation(getLabelsShape, Text.Utils);
-    const data = getLabelData(structure, params.target);
+    const data = getLabelData(structure, params);
     await repr.createOrUpdate(params.options, data).runInContext(ctx);
     return repr;
 }
@@ -47,7 +47,26 @@ function getLabelsShape(ctx: RuntimeContext, data: LabelsData, props: PD.Values<
 }
 
 const boundaryHelper = new BoundaryHelper();
-function getLabelData(structure: Structure, level: 'elements' | 'residues'): LabelsData {
+function getLabelData(structure: Structure, params: StateTransformer.Params<StructureLabels3D>): LabelsData {
+    if (params.target.name === 'static-text') {
+        return getLabelDataStatic(structure, params.target.params.value);
+    } else {
+        return getLabelDataComputed(structure, params.target.name);
+    }
+
+}
+
+function getLabelDataStatic(structure: Structure, text: string): LabelsData {
+    const boundary = structure.boundary.sphere;
+    return {
+        texts: [text],
+        positions: [boundary.center],
+        sizes: [1],
+        depths: [boundary.radius]
+    };
+}
+
+function getLabelDataComputed(structure: Structure, level: 'elements' | 'residues'): LabelsData {
     const data: LabelsData = { texts: [], positions: [], sizes: [], depths: [] };
 
     const l = StructureElement.create();