size.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 { SizeType, LocationSize } from 'mol-geo/geometry/size-data';
  7. import { UniformSizeTheme, UniformSizeThemeProvider } from './size/uniform';
  8. import { ParamDefinition as PD } from 'mol-util/param-definition';
  9. import { ThemeDataContext } from 'mol-theme/theme';
  10. import { PhysicalSizeThemeProvider } from './size/physical';
  11. import { deepEqual } from 'mol-util';
  12. export { SizeTheme }
  13. interface SizeTheme<P extends SizeTheme.Props = {}> {
  14. readonly granularity: SizeType
  15. readonly size: LocationSize
  16. readonly props: Readonly<P>
  17. readonly description?: string
  18. }
  19. namespace SizeTheme {
  20. export type Props = { [k: string]: any }
  21. export const Empty = UniformSizeTheme({}, { value: 1 })
  22. export function areEqual(themeA: SizeTheme, themeB: SizeTheme) {
  23. return themeA === themeB && deepEqual(themeA.props, themeB.props)
  24. }
  25. export interface Provider<P extends PD.Params> {
  26. readonly label: string
  27. readonly factory: (ctx: ThemeDataContext, props: PD.Values<P>) => SizeTheme<PD.Values<P>>
  28. readonly getParams: (ctx: ThemeDataContext) => P
  29. }
  30. export class Registry {
  31. private _list: { name: string, provider: Provider<any> }[] = []
  32. private _map = new Map<string, Provider<any>>()
  33. get default() { return this._list[0]; }
  34. get types(): [string, string][] {
  35. return this._list.map(e => [e.name, e.provider.label] as [string, string]);
  36. }
  37. constructor() {
  38. Object.keys(BuiltInSizeThemes).forEach(name => {
  39. const p = (BuiltInSizeThemes as { [k: string]: Provider<any> })[name]
  40. this.add(name, p)
  41. })
  42. }
  43. add<P extends PD.Params>(name: string, provider: Provider<P>) {
  44. this._list.push({ name, provider })
  45. this._map.set(name, provider)
  46. }
  47. get(id: string) {
  48. return this._map.get(id)
  49. }
  50. create(id: string, ctx: ThemeDataContext, props = {}) {
  51. const provider = this.get(id)
  52. return provider ? provider.factory(ctx, { ...PD.getDefaultValues(provider.getParams(ctx)), ...props }) : Empty
  53. }
  54. get list() {
  55. return this._list
  56. }
  57. }
  58. }
  59. export const BuiltInSizeThemes = {
  60. 'physical': PhysicalSizeThemeProvider,
  61. 'uniform': UniformSizeThemeProvider
  62. }
  63. export type BuiltInSizeThemeName = keyof typeof BuiltInSizeThemes
  64. export const BuiltInSizeThemeNames = Object.keys(BuiltInSizeThemes)
  65. export const BuiltInSizeThemeOptions = BuiltInSizeThemeNames.map(n => [n, n] as [BuiltInSizeThemeName, string])
  66. export const getBuiltInSizeThemeParams = (name: string, ctx: ThemeDataContext = {}) => PD.Group((BuiltInSizeThemes as { [k: string]: SizeTheme.Provider<any> })[name].getParams(ctx))