molstar
Version:
A comprehensive macromolecular library.
140 lines • 7.37 kB
JavaScript
"use strict";
/**
* Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mp4Controls = exports.Mp4AnimationParams = void 0;
var tslib_1 = require("tslib");
var operators_1 = require("rxjs/operators");
var component_1 = require("../../mol-plugin-state/component");
var mol_task_1 = require("../../mol-task");
var param_definition_1 = require("../../mol-util/param-definition");
var encoder_1 = require("./encoder");
exports.Mp4AnimationParams = {
quantization: param_definition_1.ParamDefinition.Numeric(18, { min: 10, max: 51 }, { description: 'Lower is better, but slower.' })
};
var Mp4Controls = /** @class */ (function (_super) {
(0, tslib_1.__extends)(Mp4Controls, _super);
function Mp4Controls(plugin) {
var _this = _super.call(this) || this;
_this.plugin = plugin;
_this.currentNames = new Set();
_this.animations = [];
_this.behaviors = {
animations: _this.ev.behavior({}),
current: _this.ev.behavior(void 0),
canApply: _this.ev.behavior({ canApply: false }),
info: _this.ev.behavior({ width: 0, height: 0 }),
params: _this.ev.behavior(param_definition_1.ParamDefinition.getDefaultValues(exports.Mp4AnimationParams))
};
_this.init();
return _this;
}
Mp4Controls.prototype.setCurrent = function (name) {
var _a, _b;
var anim = this.animations.find(function (a) { return a.name === name; });
if (!anim) {
this.behaviors.current.next(anim);
return;
}
var params = anim.params(this.plugin);
var values = param_definition_1.ParamDefinition.getDefaultValues(params);
this.behaviors.current.next({ anim: anim, params: params, values: values });
this.behaviors.canApply.next((_b = (_a = anim.canApply) === null || _a === void 0 ? void 0 : _a.call(anim, this.plugin)) !== null && _b !== void 0 ? _b : { canApply: true });
};
Mp4Controls.prototype.setCurrentParams = function (values) {
this.behaviors.current.next((0, tslib_1.__assign)((0, tslib_1.__assign)({}, this.behaviors.current.value), { values: values }));
};
Object.defineProperty(Mp4Controls.prototype, "current", {
get: function () {
return this.behaviors.current.value;
},
enumerable: false,
configurable: true
});
Mp4Controls.prototype.render = function () {
var _this = this;
var task = mol_task_1.Task.create('Export Animation', function (ctx) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
var resolution, anim, movie, filename, e_1;
var _a, _b, _c;
return (0, tslib_1.__generator)(this, function (_d) {
switch (_d.label) {
case 0:
_d.trys.push([0, 2, , 3]);
resolution = (_a = this.plugin.helpers.viewportScreenshot) === null || _a === void 0 ? void 0 : _a.getSizeAndViewport();
anim = this.current;
return [4 /*yield*/, (0, encoder_1.encodeMp4Animation)(this.plugin, ctx, (0, tslib_1.__assign)((0, tslib_1.__assign)({ animation: {
definition: anim.anim,
params: anim.values,
} }, resolution), { quantizationParameter: this.behaviors.params.value.quantization, pass: (_b = this.plugin.helpers.viewportScreenshot) === null || _b === void 0 ? void 0 : _b.imagePass }))];
case 1:
movie = _d.sent();
filename = anim.anim.display.name.toLowerCase().replace(/\s/g, '-').replace(/[^a-z0-9_\-]/g, '');
return [2 /*return*/, { movie: movie, filename: ((_c = this.plugin.helpers.viewportScreenshot) === null || _c === void 0 ? void 0 : _c.getFilename('')) + "_" + filename + ".mp4" }];
case 2:
e_1 = _d.sent();
this.plugin.log.error('' + e_1);
throw e_1;
case 3: return [2 /*return*/];
}
});
}); });
return this.plugin.runTask(task, { useOverlay: true });
};
Object.defineProperty(Mp4Controls.prototype, "manager", {
get: function () {
return this.plugin.managers.animation;
},
enumerable: false,
configurable: true
});
Mp4Controls.prototype.syncInfo = function () {
var helper = this.plugin.helpers.viewportScreenshot;
var size = helper === null || helper === void 0 ? void 0 : helper.getSizeAndViewport();
if (!size)
return;
this.behaviors.info.next({ width: size.viewport.width, height: size.viewport.height });
};
Mp4Controls.prototype.sync = function () {
var _this = this;
var _a, _b;
var animations = this.manager.animations.filter(function (a) { return a.isExportable; });
var hasAll = animations.every(function (a) { return _this.currentNames.has(a.name); });
if (hasAll && this.currentNames.size === animations.length) {
return;
}
var params = {
current: param_definition_1.ParamDefinition.Select((_a = animations[0]) === null || _a === void 0 ? void 0 : _a.name, animations.map(function (a) { return [a.name, a.display.name]; }), { label: 'Animation' })
};
var current = this.behaviors.current.value;
var hasCurrent = !!animations.find(function (a) { return a.name === (current === null || current === void 0 ? void 0 : current.anim.name); });
this.animations = animations;
if (!hasCurrent) {
this.setCurrent((_b = animations[0]) === null || _b === void 0 ? void 0 : _b.name);
}
this.behaviors.animations.next(params);
};
Mp4Controls.prototype.init = function () {
var _this = this;
var _a, _b;
this.subscribe(this.plugin.managers.animation.events.updated.pipe((0, operators_1.debounceTime)(16)), function () {
_this.sync();
});
this.subscribe((_a = this.plugin.canvas3d) === null || _a === void 0 ? void 0 : _a.resized, function () { return _this.syncInfo(); });
this.subscribe((_b = this.plugin.helpers.viewportScreenshot) === null || _b === void 0 ? void 0 : _b.events.previewed, function () { return _this.syncInfo(); });
this.subscribe(this.plugin.behaviors.state.isBusy, function (b) {
var _a, _b, _c;
var anim = _this.current;
if (!b && anim) {
_this.behaviors.canApply.next((_c = (_b = (_a = anim.anim).canApply) === null || _b === void 0 ? void 0 : _b.call(_a, _this.plugin)) !== null && _c !== void 0 ? _c : { canApply: true });
}
});
this.sync();
this.syncInfo();
};
return Mp4Controls;
}(component_1.PluginComponent));
exports.Mp4Controls = Mp4Controls;
//# sourceMappingURL=controls.js.map