complex-representation.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  8. import { StructureParams, ComplexVisual, StructureRepresentation, StructureRepresentationStateBuilder, StructureRepresentationState } from './representation';
  9. import { RepresentationContext, RepresentationParamsGetter } from '../representation';
  10. import { Structure } from '../../mol-model/structure';
  11. import { Subject } from 'rxjs';
  12. import { getNextMaterialId, GraphicsRenderObject } from '../../mol-gl/render-object';
  13. import { createEmptyTheme, Theme } from '../../mol-theme/theme';
  14. import { Task } from '../../mol-task';
  15. import { PickingId } from '../../mol-geo/geometry/picking';
  16. import { EmptyLoci, Loci } from '../../mol-model/loci';
  17. import { MarkerAction } from '../../mol-util/marker-action';
  18. export function ComplexRepresentation<P extends StructureParams>(label: string, ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, P>, visualCtor: (materialId: number) => ComplexVisual<P>): StructureRepresentation<P> {
  19. let version = 0
  20. const updated = new Subject<number>()
  21. const materialId = getNextMaterialId()
  22. const renderObjects: GraphicsRenderObject[] = []
  23. const _state = StructureRepresentationStateBuilder.create()
  24. let visual: ComplexVisual<P> | undefined
  25. let _structure: Structure
  26. let _params: P
  27. let _props: PD.Values<P>
  28. let _theme = createEmptyTheme()
  29. function createOrUpdate(props: Partial<PD.Values<P>> = {}, structure?: Structure) {
  30. if (structure && structure !== _structure) {
  31. _params = getParams(ctx, structure)
  32. _structure = structure
  33. if (!_props) _props = PD.getDefaultValues(_params)
  34. }
  35. _props = Object.assign({}, _props, props)
  36. return Task.create('Creating or updating ComplexRepresentation', async runtime => {
  37. if (!visual) visual = visualCtor(materialId)
  38. const promise = visual.createOrUpdate({ webgl: ctx.webgl, runtime }, _theme, _props, structure)
  39. if (promise) await promise
  40. // update list of renderObjects
  41. renderObjects.length = 0
  42. if (visual && visual.renderObject) renderObjects.push(visual.renderObject)
  43. // increment version
  44. updated.next(version++)
  45. });
  46. }
  47. function getLoci(pickingId: PickingId) {
  48. return visual ? visual.getLoci(pickingId) : EmptyLoci
  49. }
  50. function mark(loci: Loci, action: MarkerAction) {
  51. return visual ? visual.mark(loci, action) : false
  52. }
  53. function setState(state: Partial<StructureRepresentationState>) {
  54. StructureRepresentationStateBuilder.update(_state, state)
  55. if (state.visible !== undefined && visual) {
  56. // hide visual when _unitTransforms is set
  57. visual.setVisibility(state.visible && _state.unitTransforms === null)
  58. }
  59. if (state.alphaFactor !== undefined && visual) visual.setAlphaFactor(state.alphaFactor)
  60. if (state.pickable !== undefined && visual) visual.setPickable(state.pickable)
  61. if (state.overpaint !== undefined && visual) visual.setOverpaint(state.overpaint)
  62. if (state.transparency !== undefined && visual) visual.setTransparency(state.transparency)
  63. if (state.transform !== undefined && visual) visual.setTransform(state.transform)
  64. if (state.unitTransforms !== undefined && visual) {
  65. // Since ComplexVisuals always renders geometries between units the application of `unitTransforms`
  66. // does not make sense. When given it is ignored here and sets the visual's visibility to `false`.
  67. visual.setVisibility(_state.visible && state.unitTransforms === null)
  68. }
  69. }
  70. function setTheme(theme: Theme) {
  71. _theme = theme
  72. }
  73. function destroy() {
  74. if (visual) visual.destroy()
  75. }
  76. return {
  77. label,
  78. get groupCount() {
  79. return visual ? visual.groupCount : 0
  80. },
  81. get props() { return _props },
  82. get params() { return _params },
  83. get state() { return _state },
  84. get theme() { return _theme },
  85. renderObjects,
  86. updated,
  87. createOrUpdate,
  88. setState,
  89. setTheme,
  90. getLoci,
  91. mark,
  92. destroy
  93. }
  94. }