built-in.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 { PluginStateAnimation } from './model';
  7. import { PluginStateObject } from '../objects';
  8. import { StateTransforms } from '../transforms';
  9. import { StateSelection } from 'mol-state';
  10. import { PluginCommands } from 'mol-plugin/command';
  11. import { ParamDefinition as PD } from 'mol-util/param-definition';
  12. export const AnimateModelIndex = PluginStateAnimation.create({
  13. name: 'built-in.animate-model-index',
  14. display: { name: 'Animate Model Index' },
  15. params: () => ({
  16. mode: PD.MappedStatic('once', {
  17. once: PD.Group({ direction: PD.Select('forward', [['forward', 'Forward'], ['backward', 'Backward']]) }, { isFlat: true }),
  18. palindrome: PD.Group({ }),
  19. loop: PD.Group({ }),
  20. }, { options: [['once', 'Once'], ['palindrome', 'Palindrome'], ['loop', 'Loop']] }),
  21. maxFPS: PD.Numeric(3, { min: 0.5, max: 30, step: 0.5 })
  22. }),
  23. initialState: () => ({} as { palindromeDirections?: { [id: string]: -1 | 1 | undefined } }),
  24. async apply(animState, t, ctx) {
  25. // limit fps
  26. if (t.current > 0 && t.current - t.lastApplied < 1000 / ctx.params.maxFPS) {
  27. return { kind: 'skip' };
  28. }
  29. const state = ctx.plugin.state.dataState;
  30. const models = state.selectQ(q => q.rootsOfType(PluginStateObject.Molecule.Model)
  31. .filter(c => c.transform.transformer === StateTransforms.Model.ModelFromTrajectory));
  32. const update = state.build();
  33. const params = ctx.params;
  34. const palindromeDirections = animState.palindromeDirections || { };
  35. let isEnd = false;
  36. for (const m of models) {
  37. const parent = StateSelection.findAncestorOfType(state.tree, state.cells, m.transform.ref, [PluginStateObject.Molecule.Trajectory]);
  38. if (!parent || !parent.obj) continue;
  39. const traj = parent.obj as PluginStateObject.Molecule.Trajectory;
  40. update.to(m.transform.ref).update(StateTransforms.Model.ModelFromTrajectory,
  41. old => {
  42. const len = traj.data.length;
  43. let dir: -1 | 1 = 1;
  44. if (params.mode.name === 'once') {
  45. dir = params.mode.params.direction === 'backward' ? -1 : 1;
  46. // if we are at start or end already, do nothing.
  47. if ((dir === -1 && old.modelIndex === 0) || (dir === 1 && old.modelIndex === len - 1)) {
  48. isEnd = true;
  49. return old;
  50. }
  51. } else if (params.mode.name === 'palindrome') {
  52. if (old.modelIndex === 0) dir = 1;
  53. else if (old.modelIndex === len - 1) dir = -1;
  54. else dir = palindromeDirections[m.transform.ref] || 1;
  55. }
  56. palindromeDirections[m.transform.ref] = dir;
  57. let modelIndex = (old.modelIndex + dir) % len;
  58. if (modelIndex < 0) modelIndex += len;
  59. isEnd = isEnd || (dir === -1 && modelIndex === 0) || (dir === 1 && modelIndex === len - 1);
  60. return { modelIndex };
  61. });
  62. }
  63. await PluginCommands.State.Update.dispatch(ctx.plugin, { state, tree: update, doNotLogTiming: true });
  64. if (params.mode.name === 'once' && isEnd) return { kind: 'finished' };
  65. if (params.mode.name === 'palindrome') return { kind: 'next', state: { palindromeDirections } };
  66. return { kind: 'next', state: {} };
  67. }
  68. })