ball-and-stick.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 { StructureRepresentation } from '.';
  7. import { ElementSphereVisual, DefaultElementSphereProps } from './visual/element-sphere';
  8. import { IntraUnitLinkVisual, DefaultIntraUnitLinkProps } from './visual/intra-unit-link-cylinder';
  9. import { PickingId } from '../../util/picking';
  10. import { Structure } from 'mol-model/structure';
  11. import { Task } from 'mol-task';
  12. import { Loci, isEmptyLoci } from 'mol-model/loci';
  13. import { MarkerAction } from '../../util/marker-data';
  14. import { SizeTheme } from '../../theme';
  15. export const DefaultBallAndStickProps = {
  16. ...DefaultElementSphereProps,
  17. ...DefaultIntraUnitLinkProps,
  18. sizeTheme: { name: 'physical', factor: 0.2 } as SizeTheme,
  19. }
  20. export type BallAndStickProps = Partial<typeof DefaultBallAndStickProps>
  21. export function BallAndStickRepresentation(): StructureRepresentation<BallAndStickProps> {
  22. const sphereRepr = StructureRepresentation(ElementSphereVisual)
  23. const intraLinkRepr = StructureRepresentation(IntraUnitLinkVisual)
  24. return {
  25. get renderObjects() {
  26. return [ ...sphereRepr.renderObjects, ...intraLinkRepr.renderObjects ]
  27. },
  28. create: (structure: Structure, props: BallAndStickProps = {} as BallAndStickProps) => {
  29. const p = Object.assign({}, props, DefaultBallAndStickProps)
  30. return Task.create('Creating BallAndStickRepresentation', async ctx => {
  31. await sphereRepr.create(structure, p).runInContext(ctx)
  32. await intraLinkRepr.create(structure, p).runInContext(ctx)
  33. })
  34. },
  35. update: (props: BallAndStickProps) => {
  36. return Task.create('Updating BallAndStickRepresentation', async ctx => {
  37. await sphereRepr.update(props).runInContext(ctx)
  38. await intraLinkRepr.update(props).runInContext(ctx)
  39. })
  40. },
  41. getLoci: (pickingId: PickingId) => {
  42. const sphereLoci = sphereRepr.getLoci(pickingId)
  43. const intraLinkLoci = intraLinkRepr.getLoci(pickingId)
  44. if (isEmptyLoci(sphereLoci)) {
  45. return intraLinkLoci
  46. } else {
  47. return sphereLoci
  48. }
  49. },
  50. mark: (loci: Loci, action: MarkerAction) => {
  51. sphereRepr.mark(loci, action)
  52. intraLinkRepr.mark(loci, action)
  53. },
  54. destroy() {
  55. sphereRepr.destroy()
  56. intraLinkRepr.destroy()
  57. }
  58. }
  59. }