label.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Loci } from '../../../mol-model/loci';
  7. import { RuntimeContext } from '../../../mol-task';
  8. import { Text } from '../../../mol-geo/geometry/text/text';
  9. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  10. import { ColorNames } from '../../../mol-util/color/names';
  11. import { ShapeRepresentation } from '../representation';
  12. import { Representation, RepresentationParamsGetter, RepresentationContext } from '../../representation';
  13. import { Shape } from '../../../mol-model/shape';
  14. import { TextBuilder } from '../../../mol-geo/geometry/text/text-builder';
  15. import { Sphere3D } from '../../../mol-math/geometry';
  16. import { lociLabel } from '../../../mol-theme/label';
  17. export interface LabelData {
  18. infos: { loci: Loci, label?: string }[]
  19. }
  20. const TextParams = {
  21. ...Text.Params,
  22. borderWidth: PD.Numeric(0.2, { min: 0, max: 0.5, step: 0.01 }),
  23. textColor: PD.Color(ColorNames.black),
  24. textSize: PD.Numeric(0.8, { min: 0.1, max: 5, step: 0.1 }),
  25. offsetZ: PD.Numeric(2, { min: 0, max: 10, step: 0.1 }),
  26. }
  27. type TextParams = typeof TextParams
  28. const LabelVisuals = {
  29. 'text': (ctx: RepresentationContext, getParams: RepresentationParamsGetter<LabelData, TextParams>) => ShapeRepresentation(getTextShape, Text.Utils),
  30. }
  31. export const LabelParams = {
  32. ...TextParams,
  33. visuals: PD.MultiSelect(['text'], PD.objectToOptions(LabelVisuals)),
  34. }
  35. export type LabelParams = typeof LabelParams
  36. export type LabelProps = PD.Values<LabelParams>
  37. //
  38. const tmpSphere = Sphere3D()
  39. function label(info: { loci: Loci, label?: string }) {
  40. return info.label || lociLabel(info.loci, { hidePrefix: true, htmlStyling: false })
  41. }
  42. function getLabelName(data: LabelData) {
  43. return data.infos.length === 1 ? label(data.infos[0]) : `${data.infos.length} Labels`
  44. }
  45. //
  46. function buildText(data: LabelData, props: LabelProps, text?: Text): Text {
  47. const builder = TextBuilder.create(props, 128, 64, text)
  48. for (let i = 0, il = data.infos.length; i < il; ++i) {
  49. const info = data.infos[i]
  50. const sphere = Loci.getBoundingSphere(info.loci, tmpSphere)
  51. if (!sphere) continue
  52. const { center, radius } = sphere
  53. const text = label(info)
  54. builder.add(text, center[0], center[1], center[2], radius, 1, i)
  55. }
  56. return builder.getText()
  57. }
  58. function getTextShape(ctx: RuntimeContext, data: LabelData, props: LabelProps, shape?: Shape<Text>) {
  59. const text = buildText(data, props, shape && shape.geometry);
  60. const name = getLabelName(data)
  61. const getLabel = function (groupId: number) {
  62. return label(data.infos[groupId])
  63. }
  64. return Shape.create(name, data, text, () => props.textColor, () => props.textSize, getLabel)
  65. }
  66. //
  67. export type LabelRepresentation = Representation<LabelData, LabelParams>
  68. export function LabelRepresentation(ctx: RepresentationContext, getParams: RepresentationParamsGetter<LabelData, LabelParams>): LabelRepresentation {
  69. return Representation.createMulti('Label', ctx, getParams, Representation.StateBuilder, LabelVisuals as unknown as Representation.Def<LabelData, LabelParams>)
  70. }