Ver Fonte

mol-plugin: targetted unwind, ability to auto start animation

David Sehnal há 6 anos atrás
pai
commit
8b274c106a

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

@@ -44,12 +44,12 @@ class PluginState {
 
     getSnapshot(params?: PluginState.GetSnapshotParams): PluginState.Snapshot {
         const p = { ...PluginState.DefaultGetSnapshotParams, ...params };
-        console.log(p.animation, this.animation.getSnapshot());
         return {
             id: UUID.create22(),
             data: p.data ? this.dataState.getSnapshot() : void 0,
             behaviour: p.behavior ? this.behaviorState.getSnapshot() : void 0,
             animation: p.animation ? this.animation.getSnapshot() : void 0,
+            startAnimation: p.startAnimation ? !!p.startAnimation : void 0,
             camera: p.camera ? {
                 current: this.plugin.canvas3d.camera.getSnapshot(),
                 transitionStyle: p.cameraTranstion.name,
@@ -76,13 +76,16 @@ class PluginState {
             this.animation.setSnapshot(snapshot.animation);
         }
         if (snapshot.camera) {
-            await PluginCommands.Camera.SetSnapshot.dispatch(this.plugin, {
+            PluginCommands.Camera.SetSnapshot.dispatch(this.plugin, {
                 snapshot: snapshot.camera.current,
                 durationMs: snapshot.camera.transitionStyle === 'animate'
                     ? snapshot.camera.transitionDurationInMs
                     : void 0
             });
         }
+        if (snapshot.startAnimation) {
+            this.animation.start();
+        }
     }
 
     dispose() {
@@ -120,6 +123,7 @@ namespace PluginState {
         data: PD.Boolean(true),
         behavior: PD.Boolean(false),
         animation: PD.Boolean(true),
+        startAnimation: PD.Boolean(false),
         canvas3d: PD.Boolean(true),
         camera: PD.Boolean(true),
         // TODO: make camera snapshots same as the StateSnapshots with "child states?"
@@ -139,6 +143,7 @@ namespace PluginState {
         data?: State.Snapshot,
         behaviour?: State.Snapshot,
         animation?: PluginAnimationManager.Snapshot,
+        startAnimation?: boolean,
         camera?: {
             current: Camera.Snapshot,
             transitionStyle: CameraTransitionStyle,

+ 21 - 8
src/mol-plugin/state/animation/built-in.ts

@@ -7,9 +7,10 @@
 import { PluginStateAnimation } from './model';
 import { PluginStateObject } from '../objects';
 import { StateTransforms } from '../transforms';
-import { StateSelection } from 'mol-state';
+import { StateSelection, StateTransform } from 'mol-state';
 import { PluginCommands } from 'mol-plugin/command';
 import { ParamDefinition as PD } from 'mol-util/param-definition';
+import { PluginContext } from 'mol-plugin/context';
 
 export const AnimateModelIndex = PluginStateAnimation.create({
     name: 'built-in.animate-model-index',
@@ -89,14 +90,25 @@ export const AnimateModelIndex = PluginStateAnimation.create({
 export const AnimateAssemblyUnwind = PluginStateAnimation.create({
     name: 'built-in.animate-assembly-unwind',
     display: { name: 'Unwind Assembly' },
-    params: () => ({
-        durationInMs: PD.Numeric(3000, { min: 100, max: 10000, step: 100}),
-        playOnce: PD.Boolean(false)
-    }),
+    params: (plugin: PluginContext) => {
+        const targets: [string, string][] = [['all', 'All']];
+        const structures = plugin.state.dataState.select(StateSelection.Generators.rootsOfType(PluginStateObject.Molecule.Structure));
+
+        for (const s of structures) {
+            targets.push([s.transform.ref, s.obj!.data.models[0].label]);
+        }
+
+        return {
+            durationInMs: PD.Numeric(3000, { min: 100, max: 10000, step: 100}),
+            playOnce: PD.Boolean(false),
+            target: PD.Select(targets[0][0], targets)
+        };
+    },
     initialState: () => ({ t: 0 }),
-    async setup(_, plugin) {
+    async setup(params, plugin) {
         const state = plugin.state.dataState;
-        const reprs = state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure.Representation3D));
+        const root = !params.target || params.target === 'all' ? StateTransform.RootRef : params.target;
+        const reprs = state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure.Representation3D, root));
 
         const update = state.build();
         let changed = false;
@@ -125,7 +137,8 @@ export const AnimateAssemblyUnwind = PluginStateAnimation.create({
     },
     async apply(animState, t, ctx) {
         const state = ctx.plugin.state.dataState;
-        const anims = state.select(StateSelection.Generators.ofTransformer(StateTransforms.Representation.UnwindStructureAssemblyRepresentation3D));
+        const root = !ctx.params.target || ctx.params.target === 'all' ? StateTransform.RootRef : ctx.params.target;
+        const anims = state.select(StateSelection.Generators.ofTransformer(StateTransforms.Representation.UnwindStructureAssemblyRepresentation3D, root));
 
         if (anims.length === 0) {
             return { kind: 'finished' };

+ 1 - 1
src/mol-plugin/state/animation/model.ts

@@ -15,7 +15,7 @@ export { PluginStateAnimation }
 interface PluginStateAnimation<P = any, S = any> {
     name: string,
     readonly display: { readonly name: string, readonly description?: string },
-    params: (ctx: PluginContext) => PD.For<P>,
+    params(ctx: PluginContext): PD.For<P>,
     initialState(params: P, ctx: PluginContext): S,
 
     // TODO: support state in setup/teardown?