representation.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { PluginStateObject as SO } from '../../state/objects';
  7. import { PluginContext } from '../../../mol-plugin/context';
  8. import { Representation } from '../../../mol-repr/representation';
  9. import { StateObjectCell } from '../../../mol-state';
  10. export function registerDefault(ctx: PluginContext) {
  11. SyncRepresentationToCanvas(ctx);
  12. SyncStructureRepresentation3DState(ctx); // should be AFTER SyncRepresentationToCanvas
  13. UpdateRepresentationVisibility(ctx);
  14. }
  15. export function SyncRepresentationToCanvas(ctx: PluginContext) {
  16. let reprCount = 0;
  17. ctx.events.canvas3d.initialized.subscribe(() => {
  18. ctx.canvas3d?.reprCount.subscribe(v => {
  19. if (reprCount === 0) ctx.canvas3d?.requestCameraReset();
  20. reprCount = v;
  21. });
  22. })
  23. const events = ctx.state.dataState.events;
  24. events.object.created.subscribe(e => {
  25. if (!SO.isRepresentation3D(e.obj)) return;
  26. updateVisibility(e.state.cells.get(e.ref)!, e.obj.data.repr);
  27. e.obj.data.repr.setState({ syncManually: true });
  28. ctx.canvas3d?.add(e.obj.data.repr);
  29. });
  30. events.object.updated.subscribe(e => {
  31. if (e.oldObj && SO.isRepresentation3D(e.oldObj)) {
  32. ctx.canvas3d?.remove(e.oldObj.data.repr);
  33. ctx.canvas3d?.requestDraw(true);
  34. e.oldObj.data.repr.destroy();
  35. }
  36. if (!SO.isRepresentation3D(e.obj)) {
  37. return;
  38. }
  39. updateVisibility(e.state.cells.get(e.ref)!, e.obj.data.repr);
  40. if (e.action === 'recreate') {
  41. e.obj.data.repr.setState({ syncManually: true });
  42. }
  43. ctx.canvas3d?.add(e.obj.data.repr);
  44. });
  45. events.object.removed.subscribe(e => {
  46. if (!SO.isRepresentation3D(e.obj)) return;
  47. ctx.canvas3d?.remove(e.obj.data.repr);
  48. ctx.canvas3d?.requestDraw(true);
  49. e.obj.data.repr.destroy();
  50. });
  51. }
  52. export function SyncStructureRepresentation3DState(ctx: PluginContext) {
  53. // TODO: figure out how to do transform composition here?
  54. const events = ctx.state.dataState.events;
  55. events.object.created.subscribe(e => {
  56. if (!SO.Molecule.Structure.Representation3DState.is(e.obj)) return;
  57. const data = e.obj.data as SO.Molecule.Structure.Representation3DStateData;
  58. data.source.data.repr.setState(data.state);
  59. ctx.canvas3d?.update(data.source.data.repr);
  60. ctx.canvas3d?.requestDraw(true);
  61. });
  62. events.object.updated.subscribe(e => {
  63. if (!SO.Molecule.Structure.Representation3DState.is(e.obj)) return;
  64. const data = e.obj.data as SO.Molecule.Structure.Representation3DStateData;
  65. data.source.data.repr.setState(data.state);
  66. ctx.canvas3d?.update(data.source.data.repr);
  67. ctx.canvas3d?.requestDraw(true);
  68. });
  69. events.object.removed.subscribe(e => {
  70. if (!SO.Molecule.Structure.Representation3DState.is(e.obj)) return;
  71. const data = e.obj.data as SO.Molecule.Structure.Representation3DStateData;
  72. data.source.data.repr.setState(data.initialState);
  73. ctx.canvas3d?.update(data.source.data.repr);
  74. ctx.canvas3d?.requestDraw(true);
  75. });
  76. }
  77. export function UpdateRepresentationVisibility(ctx: PluginContext) {
  78. ctx.state.dataState.events.cell.stateUpdated.subscribe(e => {
  79. const cell = e.state.cells.get(e.ref)!;
  80. if (!SO.isRepresentation3D(cell.obj)) return;
  81. updateVisibility(cell, cell.obj.data.repr);
  82. ctx.canvas3d?.requestDraw(true);
  83. })
  84. }
  85. function updateVisibility(cell: StateObjectCell, r: Representation<any>) {
  86. r.setState({ visible: !cell.state.isHidden });
  87. }