representation.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 { State } 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. const events = ctx.state.dataState.events;
  18. events.object.created.subscribe(e => {
  19. if (!SO.isRepresentation3D(e.obj)) return;
  20. updateVisibility(e, e.obj.data.repr);
  21. e.obj.data.repr.setState({ syncManually: true });
  22. ctx.canvas3d.add(e.obj.data.repr);
  23. if (reprCount === 0) ctx.canvas3d.resetCamera();
  24. reprCount++;
  25. });
  26. events.object.updated.subscribe(e => {
  27. if (e.oldObj && SO.isRepresentation3D(e.oldObj)) {
  28. ctx.canvas3d.remove(e.oldObj.data.repr);
  29. ctx.canvas3d.requestDraw(true);
  30. e.oldObj.data.repr.destroy();
  31. }
  32. if (!SO.isRepresentation3D(e.obj)) {
  33. return;
  34. }
  35. updateVisibility(e, e.obj.data.repr);
  36. if (e.action === 'recreate') {
  37. e.obj.data.repr.setState({ syncManually: true });
  38. }
  39. ctx.canvas3d.add(e.obj.data.repr);
  40. });
  41. events.object.removed.subscribe(e => {
  42. if (!SO.isRepresentation3D(e.obj)) return;
  43. ctx.canvas3d.remove(e.obj.data.repr);
  44. ctx.canvas3d.requestDraw(true);
  45. e.obj.data.repr.destroy();
  46. reprCount--;
  47. });
  48. }
  49. export function SyncStructureRepresentation3DState(ctx: PluginContext) {
  50. // TODO: figure out how to do transform composition here?
  51. const events = ctx.state.dataState.events;
  52. events.object.created.subscribe(e => {
  53. if (!SO.Molecule.Structure.Representation3DState.is(e.obj)) return;
  54. const data = e.obj.data as SO.Molecule.Structure.Representation3DStateData;
  55. data.source.data.repr.setState(data.state);
  56. ctx.canvas3d.update(data.source.data.repr);
  57. ctx.canvas3d.requestDraw(true);
  58. });
  59. events.object.updated.subscribe(e => {
  60. if (!SO.Molecule.Structure.Representation3DState.is(e.obj)) return;
  61. const data = e.obj.data as SO.Molecule.Structure.Representation3DStateData;
  62. data.source.data.repr.setState(data.state);
  63. ctx.canvas3d.update(data.source.data.repr);
  64. ctx.canvas3d.requestDraw(true);
  65. });
  66. events.object.removed.subscribe(e => {
  67. if (!SO.Molecule.Structure.Representation3DState.is(e.obj)) return;
  68. const data = e.obj.data as SO.Molecule.Structure.Representation3DStateData;
  69. data.source.data.repr.setState(data.initialState);
  70. ctx.canvas3d.update(data.source.data.repr);
  71. ctx.canvas3d.requestDraw(true);
  72. });
  73. }
  74. export function UpdateRepresentationVisibility(ctx: PluginContext) {
  75. ctx.state.dataState.events.cell.stateUpdated.subscribe(e => {
  76. const cell = e.state.cells.get(e.ref)!;
  77. if (!SO.isRepresentation3D(cell.obj)) return;
  78. updateVisibility(e, cell.obj.data.repr);
  79. ctx.canvas3d.requestDraw(true);
  80. })
  81. }
  82. function updateVisibility(e: State.ObjectEvent, r: Representation<any>) {
  83. r.setState({ visible: !e.state.cellStates.get(e.ref).isHidden });
  84. }