index.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Task, RuntimeContext } from 'mol-task'
  7. import { RenderObject } from 'mol-gl/render-object'
  8. import { PickingId } from '../mol-geo/geometry/picking';
  9. import { Loci, isEmptyLoci, EmptyLoci } from 'mol-model/loci';
  10. import { MarkerAction } from '../mol-geo/geometry/marker-data';
  11. import { Params, MultiSelectParam } from 'mol-util/parameter';
  12. import { WebGLContext } from 'mol-gl/webgl/context';
  13. import { getQualityProps } from './util';
  14. import { Theme } from 'mol-geo/geometry/geometry';
  15. // import { ColorTheme } from 'mol-theme/color';
  16. // export interface RepresentationProps {
  17. // visuals?: string[]
  18. // }
  19. export type RepresentationProps = { [k: string]: any }
  20. export interface RepresentationContext {
  21. webgl?: WebGLContext
  22. }
  23. export interface Representation<D, P extends RepresentationProps = {}> {
  24. readonly label: string
  25. readonly params: Params
  26. readonly renderObjects: ReadonlyArray<RenderObject>
  27. readonly props: Readonly<P>
  28. createOrUpdate: (ctx: RepresentationContext, props?: Partial<P>, data?: D) => Task<void>
  29. getLoci: (pickingId: PickingId) => Loci
  30. mark: (loci: Loci, action: MarkerAction) => boolean
  31. destroy: () => void
  32. }
  33. export namespace Representation {
  34. export function createMulti<D, P extends RepresentationProps = {}>(label: string, params: Params, defaultProps: P, reprList: Representation<D, P>[]): Representation<D, P> {
  35. let currentProps: P
  36. let currentData: D
  37. const visualsOptions: [string, string][] = []
  38. for (let i = 0, il = reprList.length; i < il; ++i) {
  39. visualsOptions.push([ i.toString(), reprList[i].label ])
  40. }
  41. params['visuals'] = MultiSelectParam<string>('Visuals', '', ['surface'], visualsOptions)
  42. if (!defaultProps.visuals) {
  43. defaultProps.visuals = reprList.map((r, i) => i.toString())
  44. }
  45. return {
  46. label,
  47. params,
  48. get renderObjects() {
  49. const { visuals } = currentProps
  50. const renderObjects: RenderObject[] = []
  51. for (let i = 0, il = reprList.length; i < il; ++i) {
  52. if (!visuals || visuals.includes(i.toString())) {
  53. renderObjects.push(...reprList[i].renderObjects)
  54. }
  55. }
  56. return renderObjects
  57. },
  58. get props() {
  59. const props = {}
  60. reprList.forEach(r => Object.assign(props, r.props))
  61. return props as P
  62. },
  63. createOrUpdate: (ctx: RepresentationContext, props: Partial<P> = {}, data?: D) => {
  64. if (data) currentData = data
  65. const qualityProps = getQualityProps(Object.assign({}, currentProps, props), data)
  66. currentProps = Object.assign({}, defaultProps, currentProps, props, qualityProps)
  67. const { visuals } = currentProps
  68. return Task.create(`Creating '${label}' representation`, async runtime => {
  69. for (let i = 0, il = reprList.length; i < il; ++i) {
  70. if (!visuals || visuals.includes(i.toString())) {
  71. await reprList[i].createOrUpdate(ctx, currentProps, currentData).runInContext(runtime)
  72. }
  73. }
  74. })
  75. },
  76. getLoci: (pickingId: PickingId) => {
  77. for (let i = 0, il = reprList.length; i < il; ++i) {
  78. const loci = reprList[i].getLoci(pickingId)
  79. if (!isEmptyLoci(loci)) return loci
  80. }
  81. return EmptyLoci
  82. },
  83. mark: (loci: Loci, action: MarkerAction) => {
  84. let marked = false
  85. for (let i = 0, il = reprList.length; i < il; ++i) {
  86. marked = reprList[i].mark(loci, action) || marked
  87. }
  88. return marked
  89. },
  90. destroy() {
  91. for (let i = 0, il = reprList.length; i < il; ++i) {
  92. reprList[i].destroy()
  93. }
  94. }
  95. }
  96. }
  97. }
  98. //
  99. export interface VisualContext extends RepresentationContext {
  100. runtime: RuntimeContext,
  101. }
  102. export interface Visual<D, P extends RepresentationProps> {
  103. readonly renderObject: RenderObject | undefined
  104. createOrUpdate: (ctx: VisualContext, theme: Theme, props?: Partial<P>, data?: D) => Promise<void>
  105. getLoci: (pickingId: PickingId) => Loci
  106. mark: (loci: Loci, action: MarkerAction) => boolean
  107. destroy: () => void
  108. }