UNPKG

molstar

Version:

A comprehensive macromolecular library.

83 lines (82 loc) 4.16 kB
/** * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> */ import { __awaiter, __generator } from "tslib"; import { PluginCommands } from '../../../mol-plugin/commands'; import { StateSelection } from '../../../mol-state'; import { ParamDefinition as PD } from '../../../mol-util/param-definition'; import { PluginStateObject } from '../../objects'; import { StateTransforms } from '../../transforms'; import { PluginStateAnimation } from '../model'; export var AnimateUnitsExplode = PluginStateAnimation.create({ name: 'built-in.animate-units-explode', display: { name: 'Explode Units' }, params: function () { return ({ durationInMs: PD.Numeric(3000, { min: 100, max: 10000, step: 100 }) }); }, initialState: function () { return ({ t: 0 }); }, setup: function (_, __, plugin) { return __awaiter(this, void 0, void 0, function () { var state, reprs, update, changed, _i, reprs_1, r, explodes; return __generator(this, function (_a) { state = plugin.state.data; reprs = state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure.Representation3D)); update = state.build(); changed = false; for (_i = 0, reprs_1 = reprs; _i < reprs_1.length; _i++) { r = reprs_1[_i]; explodes = state.select(StateSelection.Generators.ofTransformer(StateTransforms.Representation.ExplodeStructureRepresentation3D, r.transform.ref)); if (explodes.length > 0) continue; changed = true; update.to(r.transform.ref) .apply(StateTransforms.Representation.ExplodeStructureRepresentation3D, { t: 0 }, { tags: 'animate-units-explode' }); } if (!changed) return [2 /*return*/]; return [2 /*return*/, update.commit({ doNotUpdateCurrent: true })]; }); }); }, teardown: function (_, __, plugin) { var state = plugin.state.data; var reprs = state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure.Representation3DState) .withTag('animate-units-explode')); if (reprs.length === 0) return; var update = state.build(); for (var _i = 0, reprs_2 = reprs; _i < reprs_2.length; _i++) { var r = reprs_2[_i]; update.delete(r.transform.ref); } return update.commit(); }, apply: function (animState, t, ctx) { return __awaiter(this, void 0, void 0, function () { var state, anims, update, d, newTime, _i, anims_1, m; return __generator(this, function (_a) { switch (_a.label) { case 0: state = ctx.plugin.state.data; anims = state.select(StateSelection.Generators.ofTransformer(StateTransforms.Representation.ExplodeStructureRepresentation3D)); if (anims.length === 0) { return [2 /*return*/, { kind: 'finished' }]; } update = state.build(); d = (t.current - t.lastApplied) / ctx.params.durationInMs; newTime = (animState.t + d) % 1; for (_i = 0, anims_1 = anims; _i < anims_1.length; _i++) { m = anims_1[_i]; update.to(m).update({ t: newTime }); } return [4 /*yield*/, PluginCommands.State.Update(ctx.plugin, { state: state, tree: update, options: { doNotLogTiming: true } })]; case 1: _a.sent(); return [2 /*return*/, { kind: 'next', state: { t: newTime } }]; } }); }); } });