molstar
Version:
A comprehensive macromolecular library.
86 lines (85 loc) • 4.39 kB
JavaScript
/**
* Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { __assign, __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 AnimateStructureSpin = PluginStateAnimation.create({
name: 'built-in.animate-structure-spin',
display: { name: 'Spin Structure' },
isExportable: true,
params: function () { return ({
durationInMs: PD.Numeric(3000, { min: 100, max: 10000, step: 100 })
}); },
initialState: function () { return ({ t: 0 }); },
getDuration: function (p) { return ({ kind: 'fixed', durationMs: p.durationInMs }); },
setup: function (_, __, plugin) {
return __awaiter(this, void 0, void 0, function () {
var state, reprs, update, changed, _i, reprs_1, r, spins;
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];
spins = state.select(StateSelection.Generators.ofTransformer(StateTransforms.Representation.SpinStructureRepresentation3D, r.transform.ref));
if (spins.length > 0)
continue;
changed = true;
update.to(r.transform.ref)
.apply(StateTransforms.Representation.SpinStructureRepresentation3D, { t: 0 }, { tags: 'animate-structure-spin' });
}
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-structure-spin'));
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) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var state, anims, update, d, newTime, _i, anims_1, m;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
state = ctx.plugin.state.data;
anims = state.select(StateSelection.Generators.ofTransformer(StateTransforms.Representation.SpinStructureRepresentation3D));
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(__assign(__assign({}, (_a = m.params) === null || _a === void 0 ? void 0 : _a.values), { t: newTime }));
}
return [4 /*yield*/, PluginCommands.State.Update(ctx.plugin, { state: state, tree: update, options: { doNotLogTiming: true } })];
case 1:
_b.sent();
return [2 /*return*/, { kind: 'next', state: { t: newTime } }];
}
});
});
}
});