molstar
Version:
A comprehensive macromolecular library.
184 lines (183 loc) • 8.8 kB
JavaScript
/**
* Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author David Sehnal <david.sehnal@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComplexRepresentation = void 0;
var tslib_1 = require("tslib");
var param_definition_1 = require("../../mol-util/param-definition");
var representation_1 = require("./representation");
var representation_2 = require("../representation");
var structure_1 = require("../../mol-model/structure");
var rxjs_1 = require("rxjs");
var render_object_1 = require("../../mol-gl/render-object");
var theme_1 = require("../../mol-theme/theme");
var mol_task_1 = require("../../mol-task");
var loci_1 = require("../../mol-model/loci");
var marker_action_1 = require("../../mol-util/marker-action");
var overpaint_1 = require("../../mol-theme/overpaint");
var clipping_1 = require("../../mol-theme/clipping");
var transparency_1 = require("../../mol-theme/transparency");
var substance_1 = require("../../mol-theme/substance");
function ComplexRepresentation(label, ctx, getParams, visualCtor) {
var version = 0;
var webgl = ctx.webgl;
var updated = new rxjs_1.Subject();
var geometryState = new representation_2.Representation.GeometryState();
var materialId = (0, render_object_1.getNextMaterialId)();
var renderObjects = [];
var _state = representation_1.StructureRepresentationStateBuilder.create();
var visual;
var _structure;
var _params;
var _props;
var _theme = theme_1.Theme.createEmpty();
function createOrUpdate(props, structure) {
var _this = this;
if (props === void 0) { props = {}; }
if (structure && structure !== _structure) {
_params = getParams(ctx, structure);
_structure = structure;
if (!_props)
_props = param_definition_1.ParamDefinition.getDefaultValues(_params);
}
_props = Object.assign({}, _props, props);
return mol_task_1.Task.create('Creating or updating ComplexRepresentation', function (runtime) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var newVisual, promise;
var _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
newVisual = false;
if (!visual) {
visual = visualCtor(materialId, _structure, _props, webgl);
newVisual = true;
}
else if ((_a = visual.mustRecreate) === null || _a === void 0 ? void 0 : _a.call(visual, _structure, _props, webgl)) {
visual.destroy();
visual = visualCtor(materialId, _structure, _props, webgl);
newVisual = true;
}
promise = visual.createOrUpdate({ webgl: webgl, runtime: runtime }, _theme, _props, structure);
if (!promise) return [3 /*break*/, 2];
return [4 /*yield*/, promise];
case 1:
_b.sent();
_b.label = 2;
case 2:
if (newVisual)
setState(_state); // current state for new visual
// update list of renderObjects
renderObjects.length = 0;
if (visual && visual.renderObject) {
renderObjects.push(visual.renderObject);
geometryState.add(visual.renderObject.id, visual.geometryVersion);
}
geometryState.snapshot();
// increment version
version += 1;
updated.next(version);
return [2 /*return*/];
}
});
}); });
}
function getLoci(pickingId) {
return visual ? visual.getLoci(pickingId) : loci_1.EmptyLoci;
}
function getAllLoci() {
return [structure_1.Structure.Loci(_structure.target)];
}
function mark(loci, action) {
if (!_structure)
return false;
if (!marker_action_1.MarkerActions.is(_state.markerActions, action))
return false;
if (structure_1.Structure.isLoci(loci) || structure_1.StructureElement.Loci.is(loci) || structure_1.Bond.isLoci(loci)) {
if (!structure_1.Structure.areRootsEquivalent(loci.structure, _structure))
return false;
// Remap `loci` from equivalent structure to the current `_structure`
loci = loci_1.Loci.remap(loci, _structure);
if (structure_1.Structure.isLoci(loci) || (structure_1.StructureElement.Loci.is(loci) && structure_1.StructureElement.Loci.isWholeStructure(loci))) {
// Change to `EveryLoci` to allow for downstream optimizations
loci = loci_1.EveryLoci;
}
}
else if (!(0, loci_1.isEveryLoci)(loci) && !(0, loci_1.isDataLoci)(loci)) {
return false;
}
if (loci_1.Loci.isEmpty(loci))
return false;
return visual ? visual.mark(loci, action) : false;
}
function setState(state) {
representation_1.StructureRepresentationStateBuilder.update(_state, state);
if (state.visible !== undefined && visual) {
// hide visual when _unitTransforms is set and not the identity
visual.setVisibility(state.visible && (_state.unitTransforms === null || _state.unitTransforms.isIdentity));
}
if (state.alphaFactor !== undefined && visual)
visual.setAlphaFactor(state.alphaFactor);
if (state.pickable !== undefined && visual)
visual.setPickable(state.pickable);
if (state.overpaint !== undefined && visual) {
// Remap loci from equivalent structure to the current structure
var remappedOverpaint = overpaint_1.Overpaint.remap(state.overpaint, _structure);
visual.setOverpaint(remappedOverpaint, webgl);
}
if (state.transparency !== undefined && visual) {
// Remap loci from equivalent structure to the current structure
var remappedTransparency = transparency_1.Transparency.remap(state.transparency, _structure);
visual.setTransparency(remappedTransparency, webgl);
}
if (state.substance !== undefined && visual) {
// Remap loci from equivalent structure to the current structure
var remappedSubstance = substance_1.Substance.remap(state.substance, _structure);
visual.setSubstance(remappedSubstance, webgl);
}
if (state.clipping !== undefined && visual) {
// Remap loci from equivalent structure to the current structure
var remappedClipping = clipping_1.Clipping.remap(state.clipping, _structure);
visual.setClipping(remappedClipping);
}
if (state.transform !== undefined && visual)
visual.setTransform(state.transform);
if (state.unitTransforms !== undefined && visual) {
// Since ComplexVisuals always renders geometries between units, the application
// of `unitTransforms` does not make sense. When given here and not the identity,
// it is ignored and sets the visual's visibility to `false`.
visual.setVisibility(_state.visible && (state.unitTransforms === null || state.unitTransforms.isIdentity));
}
}
function setTheme(theme) {
_theme = theme;
}
function destroy() {
if (visual)
visual.destroy();
}
return {
label: label,
get groupCount() {
return visual ? visual.groupCount : 0;
},
get props() { return _props; },
get params() { return _params; },
get state() { return _state; },
get theme() { return _theme; },
get geometryVersion() { return geometryState.version; },
renderObjects: renderObjects,
updated: updated,
createOrUpdate: createOrUpdate,
setState: setState,
setTheme: setTheme,
getLoci: getLoci,
getAllLoci: getAllLoci,
mark: mark,
destroy: destroy
};
}
exports.ComplexRepresentation = ComplexRepresentation;
;