model-index.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { PluginCommands } from '../../../mol-plugin/commands';
  7. import { StateSelection } from '../../../mol-state';
  8. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  9. import { PluginStateObject } from '../../objects';
  10. import { StateTransforms } from '../../transforms';
  11. import { PluginStateAnimation } from '../model';
  12. export const AnimateModelIndex = PluginStateAnimation.create({
  13. name: 'built-in.animate-model-index',
  14. display: { name: 'Animate Trajectory' },
  15. params: () => ({
  16. mode: PD.MappedStatic('palindrome', {
  17. palindrome: PD.Group({ }),
  18. loop: PD.Group({ }),
  19. once: PD.Group({ direction: PD.Select('forward', [['forward', 'Forward'], ['backward', 'Backward']]) }, { isFlat: true })
  20. }, { options: [['palindrome', 'Palindrome'], ['loop', 'Loop'], ['once', 'Once']] }),
  21. maxFPS: PD.Numeric(15, { min: 1, max: 60, step: 1 })
  22. }),
  23. canApply(ctx) {
  24. const state = ctx.state.data;
  25. const models = state.select(StateSelection.Generators.ofTransformer(StateTransforms.Model.ModelFromTrajectory));
  26. for (const m of models) {
  27. const parent = StateSelection.findAncestorOfType(state.tree, state.cells, m.transform.ref, [PluginStateObject.Molecule.Trajectory]);
  28. if (parent && parent.obj && parent.obj.data.frameCount > 1) return { canApply: true };
  29. }
  30. return { canApply: false, reason: 'No trajectory to animate' };
  31. },
  32. initialState: () => ({} as { palindromeDirections?: { [id: string]: -1 | 1 | undefined } }),
  33. async apply(animState, t, ctx) {
  34. // limit fps
  35. if (t.current > 0 && t.current - t.lastApplied < 1000 / ctx.params.maxFPS) {
  36. return { kind: 'skip' };
  37. }
  38. const state = ctx.plugin.state.data;
  39. const models = state.select(StateSelection.Generators.ofTransformer(StateTransforms.Model.ModelFromTrajectory));
  40. if (models.length === 0) {
  41. // nothing more to do here
  42. return { kind: 'finished' };
  43. }
  44. const update = state.build();
  45. const params = ctx.params;
  46. const palindromeDirections = animState.palindromeDirections || { };
  47. let isEnd = false, allSingles = true;
  48. for (const m of models) {
  49. const parent = StateSelection.findAncestorOfType(state.tree, state.cells, m.transform.ref, [PluginStateObject.Molecule.Trajectory]);
  50. if (!parent || !parent.obj) continue;
  51. const traj = parent.obj;
  52. if (traj.data.frameCount <= 1) continue;
  53. update.to(m).update(old => {
  54. const len = traj.data.frameCount;
  55. if (len !== 1) {
  56. allSingles = false;
  57. } else {
  58. return old;
  59. }
  60. let dir: -1 | 1 = 1;
  61. if (params.mode.name === 'once') {
  62. dir = params.mode.params.direction === 'backward' ? -1 : 1;
  63. // if we are at start or end already, do nothing.
  64. if ((dir === -1 && old.modelIndex === 0) || (dir === 1 && old.modelIndex === len - 1)) {
  65. isEnd = true;
  66. return old;
  67. }
  68. } else if (params.mode.name === 'palindrome') {
  69. if (old.modelIndex === 0) dir = 1;
  70. else if (old.modelIndex === len - 1) dir = -1;
  71. else dir = palindromeDirections[m.transform.ref] || 1;
  72. }
  73. palindromeDirections[m.transform.ref] = dir;
  74. let modelIndex = (old.modelIndex + dir) % len;
  75. if (modelIndex < 0) modelIndex += len;
  76. isEnd = isEnd || (dir === -1 && modelIndex === 0) || (dir === 1 && modelIndex === len - 1);
  77. return { modelIndex };
  78. });
  79. }
  80. if (!allSingles) {
  81. await PluginCommands.State.Update(ctx.plugin, { state, tree: update, options: { doNotLogTiming: true } });
  82. }
  83. if (allSingles || (params.mode.name === 'once' && isEnd)) return { kind: 'finished' };
  84. if (params.mode.name === 'palindrome') return { kind: 'next', state: { palindromeDirections } };
  85. return { kind: 'next', state: {} };
  86. }
  87. });