ball-and-stick.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, StructureUnitsRepresentation } 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, Unit } 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. import { InterUnitLinkVisual } from './visual/inter-unit-link-cylinder';
  16. export const DefaultBallAndStickProps = {
  17. ...DefaultElementSphereProps,
  18. ...DefaultIntraUnitLinkProps,
  19. sizeTheme: { name: 'uniform', value: 0.25 } as SizeTheme,
  20. unitKinds: [ Unit.Kind.Atomic ] as Unit.Kind[]
  21. }
  22. export type BallAndStickProps = Partial<typeof DefaultBallAndStickProps>
  23. export function BallAndStickRepresentation(): StructureRepresentation<BallAndStickProps> {
  24. const elmementRepr = StructureUnitsRepresentation(ElementSphereVisual)
  25. const intraLinkRepr = StructureUnitsRepresentation(IntraUnitLinkVisual)
  26. const interLinkRepr = StructureRepresentation(InterUnitLinkVisual)
  27. return {
  28. get renderObjects() {
  29. return [ ...elmementRepr.renderObjects, ...intraLinkRepr.renderObjects, ...interLinkRepr.renderObjects ]
  30. },
  31. get props() {
  32. return { ...elmementRepr.props, ...intraLinkRepr.props, ...interLinkRepr.props }
  33. },
  34. create: (structure: Structure, props: BallAndStickProps = {} as BallAndStickProps) => {
  35. const p = Object.assign({}, DefaultBallAndStickProps, props)
  36. return Task.create('DistanceRestraintRepresentation', async ctx => {
  37. await elmementRepr.create(structure, p).runInContext(ctx)
  38. await intraLinkRepr.create(structure, p).runInContext(ctx)
  39. await interLinkRepr.create(structure, p).runInContext(ctx)
  40. })
  41. },
  42. update: (props: BallAndStickProps) => {
  43. const p = Object.assign({}, props)
  44. return Task.create('Updating BallAndStickRepresentation', async ctx => {
  45. await elmementRepr.update(p).runInContext(ctx)
  46. await intraLinkRepr.update(p).runInContext(ctx)
  47. await interLinkRepr.update(p).runInContext(ctx)
  48. })
  49. },
  50. getLoci: (pickingId: PickingId) => {
  51. const sphereLoci = elmementRepr.getLoci(pickingId)
  52. const intraLinkLoci = intraLinkRepr.getLoci(pickingId)
  53. const interLinkLoci = interLinkRepr.getLoci(pickingId)
  54. if (isEmptyLoci(sphereLoci)) {
  55. if (isEmptyLoci(intraLinkLoci)) {
  56. return interLinkLoci
  57. } else {
  58. return intraLinkLoci
  59. }
  60. } else {
  61. return sphereLoci
  62. }
  63. },
  64. mark: (loci: Loci, action: MarkerAction) => {
  65. elmementRepr.mark(loci, action)
  66. intraLinkRepr.mark(loci, action)
  67. interLinkRepr.mark(loci, action)
  68. },
  69. destroy() {
  70. elmementRepr.destroy()
  71. intraLinkRepr.destroy()
  72. interLinkRepr.destroy()
  73. }
  74. }
  75. }