structure-overpaint-helper.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 { PluginStateObject } from '../../mol-plugin/state/objects';
  7. import { StateTransforms } from '../../mol-plugin/state/transforms';
  8. import { StateSelection, StateObjectCell, StateTransform, StateBuilder } from '../../mol-state';
  9. import { Structure, StructureElement } from '../../mol-model/structure';
  10. import { PluginContext } from '../context';
  11. import { Color } from '../../mol-util/color';
  12. type OverpaintEachReprCallback = (update: StateBuilder.Root, repr: StateObjectCell<PluginStateObject.Molecule.Structure.Representation3D, StateTransform<typeof StateTransforms.Representation.StructureRepresentation3D>>, overpaint?: StateObjectCell<any, StateTransform<typeof StateTransforms.Representation.OverpaintStructureRepresentation3DFromBundle>>) => void
  13. const OverpaintManagerTag = 'overpaint-controls'
  14. export class StructureOverpaintHelper {
  15. private async eachRepr(callback: OverpaintEachReprCallback) {
  16. const state = this.plugin.state.dataState;
  17. const reprs = state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure.Representation3D));
  18. const update = state.build();
  19. for (const r of reprs) {
  20. const overpaint = state.select(StateSelection.Generators.ofTransformer(StateTransforms.Representation.OverpaintStructureRepresentation3DFromBundle, r.transform.ref).withTag(OverpaintManagerTag))
  21. callback(update, r, overpaint[0])
  22. }
  23. await this.plugin.runTask(state.updateTree(update, { doNotUpdateCurrent: true }));
  24. }
  25. async set(color: Color | -1, lociGetter: (structure: Structure) => StructureElement.Loci, types?: string[]) {
  26. await this.eachRepr((update, repr, overpaint) => {
  27. if (types && !types.includes(repr.params!.values.type.name)) return
  28. // TODO merge overpaint layers, delete shadowed ones
  29. // TODO filter overpaint layers for given structure
  30. const structure = repr.obj!.data.source.data
  31. // always use the root structure to get the loci so the overpaint
  32. // stays applicable as long as the root structure does not change
  33. const loci = lociGetter(structure.root)
  34. if (StructureElement.Loci.isEmpty(loci)) return
  35. const layer = {
  36. bundle: StructureElement.Bundle.fromLoci(loci),
  37. color: color === -1 ? Color(0) : color,
  38. clear: color === -1
  39. }
  40. if (overpaint) {
  41. update.to(overpaint).update({ layers: [ ...overpaint.params!.values.layers, layer ], alpha: 1 })
  42. } else {
  43. update.to(repr.transform.ref)
  44. .apply(StateTransforms.Representation.OverpaintStructureRepresentation3DFromBundle, { layers: [ layer ], alpha: 1 }, { tags: OverpaintManagerTag });
  45. }
  46. })
  47. }
  48. constructor(private plugin: PluginContext) {
  49. }
  50. }