molstar
Version:
A comprehensive macromolecular library.
61 lines • 3.05 kB
JavaScript
/**
* Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { __assign } from "tslib";
import { Loci } from '../../../mol-model/loci';
import { Text } from '../../../mol-geo/geometry/text/text';
import { ParamDefinition as PD } from '../../../mol-util/param-definition';
import { ShapeRepresentation } from '../representation';
import { Representation } from '../../representation';
import { Shape } from '../../../mol-model/shape';
import { TextBuilder } from '../../../mol-geo/geometry/text/text-builder';
import { Sphere3D } from '../../../mol-math/geometry';
import { lociLabel } from '../../../mol-theme/label';
import { LociLabelTextParams } from './common';
var TextParams = __assign(__assign({}, LociLabelTextParams), { offsetZ: PD.Numeric(2, { min: 0, max: 10, step: 0.1 }) });
var LabelVisuals = {
'text': function (ctx, getParams) { return ShapeRepresentation(getTextShape, Text.Utils, { modifyState: function (s) { return (__assign(__assign({}, s), { pickable: false })); } }); },
};
export var LabelParams = __assign(__assign({}, TextParams), { scaleByRadius: PD.Boolean(true), visuals: PD.MultiSelect(['text'], PD.objectToOptions(LabelVisuals)) });
//
var tmpSphere = Sphere3D();
function label(info, condensed) {
if (condensed === void 0) { condensed = false; }
return info.label || lociLabel(info.loci, { hidePrefix: true, htmlStyling: false, condensed: condensed });
}
function getLabelName(data) {
return data.infos.length === 1 ? label(data.infos[0]) : data.infos.length + " Labels";
}
//
function buildText(data, props, text) {
var builder = TextBuilder.create(props, 128, 64, text);
var customLabel = props.customText.trim();
for (var i = 0, il = data.infos.length; i < il; ++i) {
var info = data.infos[i];
var sphere = Loci.getBoundingSphere(info.loci, tmpSphere);
if (!sphere)
continue;
var center = sphere.center, radius = sphere.radius;
var text_1 = customLabel || label(info, true);
builder.add(text_1, center[0], center[1], center[2], props.scaleByRadius ? radius / 0.9 : 0, props.scaleByRadius ? Math.max(1, radius) : 1, i);
}
return builder.getText();
}
function getTextShape(ctx, data, props, shape) {
var text = buildText(data, props, shape && shape.geometry);
var name = getLabelName(data);
var customLabel = props.customText.trim();
var getLabel = customLabel
? function (groupId) {
return customLabel;
} : function (groupId) {
return label(data.infos[groupId]);
};
return Shape.create(name, data, text, function () { return props.textColor; }, function () { return props.textSize; }, getLabel);
}
export function LabelRepresentation(ctx, getParams) {
return Representation.createMulti('Label', ctx, getParams, Representation.StateBuilder, LabelVisuals);
}
//# sourceMappingURL=label.js.map