molstar
Version:
A comprehensive macromolecular library.
137 lines • 7 kB
JavaScript
/**
* Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { __assign, __awaiter, __extends, __generator } from "tslib";
import { debounceTime } from 'rxjs/operators';
import { PluginComponent } from '../../mol-plugin-state/component';
import { Task } from '../../mol-task';
import { ParamDefinition as PD } from '../../mol-util/param-definition';
import { encodeMp4Animation } from './encoder';
export var Mp4AnimationParams = {
quantization: PD.Numeric(18, { min: 10, max: 51 }, { description: 'Lower is better, but slower.' })
};
var Mp4Controls = /** @class */ (function (_super) {
__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(PD.getDefaultValues(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 = PD.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(__assign(__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 = Task.create('Export Animation', function (ctx) { return __awaiter(_this, void 0, void 0, function () {
var resolution, anim, movie, filename, e_1;
var _a, _b, _c;
return __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*/, encodeMp4Animation(this.plugin, ctx, __assign(__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: PD.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(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;
}(PluginComponent));
export { Mp4Controls };
//# sourceMappingURL=controls.js.map