structure-representation-params.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { Structure } from '../../mol-model/structure';
  7. import { PluginContext } from '../../mol-plugin/context';
  8. import { RepresentationProvider } from '../../mol-repr/representation';
  9. import { BuiltInStructureRepresentations, BuiltInStructureRepresentationsName } from '../../mol-repr/structure/registry'
  10. import { StateTransformer } from '../../mol-state';
  11. import { BuiltInColorThemeName, BuiltInColorThemes, ColorTheme } from '../../mol-theme/color';
  12. import { BuiltInSizeThemeName, BuiltInSizeThemes, SizeTheme } from '../../mol-theme/size';
  13. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  14. import { StructureRepresentation3D } from '../transforms/representation';
  15. export interface StructureRepresentationBuiltInProps<
  16. R extends BuiltInStructureRepresentationsName = BuiltInStructureRepresentationsName,
  17. C extends BuiltInColorThemeName = BuiltInColorThemeName,
  18. S extends BuiltInSizeThemeName = BuiltInSizeThemeName> {
  19. /** Using any registered name will work, but code completion will break */
  20. type?: R,
  21. typeParams?: Partial<RepresentationProvider.ParamValues<BuiltInStructureRepresentations[R]>>,
  22. /** Using any registered name will work, but code completion will break */
  23. color?: C,
  24. colorParams?: Partial<ColorTheme.ParamValues<BuiltInColorThemes[C]>>,
  25. /** Using any registered name will work, but code completion will break */
  26. size?: S,
  27. sizeParams?: Partial<SizeTheme.ParamValues<BuiltInSizeThemes[S]>>
  28. }
  29. export interface StructureRepresentationProps<
  30. R extends RepresentationProvider<Structure> = RepresentationProvider<Structure>,
  31. C extends ColorTheme.Provider = ColorTheme.Provider,
  32. S extends SizeTheme.Provider = SizeTheme.Provider> {
  33. type?: R,
  34. typeParams?: Partial<RepresentationProvider.ParamValues<R>>,
  35. color?: C,
  36. colorParams?: Partial<ColorTheme.ParamValues<C>>,
  37. size?: S,
  38. sizeParams?: Partial<SizeTheme.ParamValues<S>>
  39. }
  40. export function createStructureRepresentationParams
  41. <R extends BuiltInStructureRepresentationsName, C extends BuiltInColorThemeName, S extends BuiltInSizeThemeName>
  42. (ctx: PluginContext, structure?: Structure, props?: StructureRepresentationBuiltInProps<R, C, S>): StateTransformer.Params<StructureRepresentation3D>
  43. export function createStructureRepresentationParams
  44. <R extends RepresentationProvider<Structure>, C extends ColorTheme.Provider, S extends SizeTheme.Provider>
  45. (ctx: PluginContext, structure?: Structure, props?: StructureRepresentationProps<R, C, S>): StateTransformer.Params<StructureRepresentation3D>
  46. export function createStructureRepresentationParams(ctx: PluginContext, structure?: Structure, props: any = {}): StateTransformer.Params<StructureRepresentation3D> {
  47. const p = props as StructureRepresentationBuiltInProps;
  48. if (typeof p.type === 'string' || typeof p.color === 'string' || typeof p.size === 'string') return createParamsByName(ctx, structure || Structure.Empty, props);
  49. return createParamsProvider(ctx, structure || Structure.Empty, props);
  50. }
  51. function createParamsByName(ctx: PluginContext, structure: Structure, props: StructureRepresentationBuiltInProps): StateTransformer.Params<StructureRepresentation3D> {
  52. const typeProvider = (props.type && ctx.structureRepresentation.registry.get(props.type))
  53. || ctx.structureRepresentation.registry.default.provider;
  54. const colorProvider = (props.color && ctx.structureRepresentation.themeCtx.colorThemeRegistry.get(props.color))
  55. || ctx.structureRepresentation.themeCtx.colorThemeRegistry.get(typeProvider.defaultColorTheme.name);
  56. const sizeProvider = (props.size && ctx.structureRepresentation.themeCtx.sizeThemeRegistry.get(props.size))
  57. || ctx.structureRepresentation.themeCtx.sizeThemeRegistry.get(typeProvider.defaultSizeTheme.name);
  58. return createParamsProvider(ctx, structure, {
  59. type: typeProvider,
  60. typeParams: props.typeParams,
  61. color: colorProvider,
  62. colorParams: props.colorParams,
  63. size: sizeProvider,
  64. sizeParams: props.sizeParams
  65. });
  66. }
  67. function createParamsProvider(ctx: PluginContext, structure: Structure, props: StructureRepresentationProps = {}): StateTransformer.Params<StructureRepresentation3D> {
  68. const { themeCtx } = ctx.structureRepresentation
  69. const themeDataCtx = { structure };
  70. const repr = props.type || ctx.structureRepresentation.registry.default.provider;
  71. const reprDefaultParams = PD.getDefaultValues(repr.getParams(themeCtx, structure));
  72. const reprParams = Object.assign(reprDefaultParams, props.typeParams);
  73. const color = props.color || themeCtx.colorThemeRegistry.get(repr.defaultColorTheme.name);
  74. const colorName = themeCtx.colorThemeRegistry.getName(color);
  75. const colorDefaultParams = PD.getDefaultValues(color.getParams(themeDataCtx));
  76. if (colorName === repr.defaultColorTheme.name) Object.assign(colorDefaultParams, repr.defaultColorTheme.props);
  77. const colorParams = Object.assign(colorDefaultParams, props.colorParams);
  78. const size = props.size || themeCtx.sizeThemeRegistry.get(repr.defaultSizeTheme.name);
  79. const sizeName = themeCtx.sizeThemeRegistry.getName(size);
  80. const sizeDefaultParams = PD.getDefaultValues(size.getParams(themeDataCtx));
  81. if (sizeName === repr.defaultSizeTheme.name) Object.assign(sizeDefaultParams, repr.defaultSizeTheme.props);
  82. const sizeParams = Object.assign(sizeDefaultParams, props.sizeParams);
  83. return ({
  84. type: { name: ctx.structureRepresentation.registry.getName(repr), params: reprParams },
  85. colorTheme: { name: colorName, params: colorParams },
  86. sizeTheme: { name: sizeName, params: sizeParams }
  87. });
  88. }