built-in.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 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: 30, step: 1 })
  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. if (models.length === 0) {
  33. // nothing more to do here
  34. return { kind: 'finished' };
  35. }
  36. const update = state.build();
  37. const params = ctx.params;
  38. const palindromeDirections = animState.palindromeDirections || { };
  39. let isEnd = false, allSingles = true;
  40. for (const m of models) {
  41. const parent = StateSelection.findAncestorOfType(state.tree, state.cells, m.transform.ref, [PluginStateObject.Molecule.Trajectory]);
  42. if (!parent || !parent.obj) continue;
  43. const traj = parent.obj as PluginStateObject.Molecule.Trajectory;
  44. update.to(m.transform.ref).update(StateTransforms.Model.ModelFromTrajectory,
  45. old => {
  46. const len = traj.data.length;
  47. if (len !== 1) {
  48. allSingles = false;
  49. } else {
  50. return old;
  51. }
  52. let dir: -1 | 1 = 1;
  53. if (params.mode.name === 'once') {
  54. dir = params.mode.params.direction === 'backward' ? -1 : 1;
  55. // if we are at start or end already, do nothing.
  56. if ((dir === -1 && old.modelIndex === 0) || (dir === 1 && old.modelIndex === len - 1)) {
  57. isEnd = true;
  58. return old;
  59. }
  60. } else if (params.mode.name === 'palindrome') {
  61. if (old.modelIndex === 0) dir = 1;
  62. else if (old.modelIndex === len - 1) dir = -1;
  63. else dir = palindromeDirections[m.transform.ref] || 1;
  64. }
  65. palindromeDirections[m.transform.ref] = dir;
  66. let modelIndex = (old.modelIndex + dir) % len;
  67. if (modelIndex < 0) modelIndex += len;
  68. isEnd = isEnd || (dir === -1 && modelIndex === 0) || (dir === 1 && modelIndex === len - 1);
  69. return { modelIndex };
  70. });
  71. }
  72. await PluginCommands.State.Update.dispatch(ctx.plugin, { state, tree: update, options: { doNotLogTiming: true } });
  73. if (allSingles || (params.mode.name === 'once' && isEnd)) return { kind: 'finished' };
  74. if (params.mode.name === 'palindrome') return { kind: 'next', state: { palindromeDirections } };
  75. return { kind: 'next', state: {} };
  76. }
  77. })
  78. export const AnimateAssemblyUnwind = PluginStateAnimation.create({
  79. name: 'built-in.animate-assembly-unwind',
  80. display: { name: 'Unwind Assembly' },
  81. params: () => ({
  82. durationInMs: PD.Numeric(3000, { min: 100, max: 10000, step: 100})
  83. }),
  84. initialState: () => ({ t: 0 }),
  85. async setup(_, plugin) {
  86. const state = plugin.state.dataState;
  87. const reprs = state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure.Representation3D));
  88. const update = state.build();
  89. let changed = false;
  90. for (const r of reprs) {
  91. const unwinds = state.select(StateSelection.Generators.byValue(r)
  92. .children()
  93. .filter(c => c.transform.transformer === StateTransforms.Representation.UnwindStructureAssemblyRepresentation3D));
  94. if (unwinds.length > 0) continue;
  95. changed = true;
  96. update.to(r.transform.ref)
  97. .apply(StateTransforms.Representation.UnwindStructureAssemblyRepresentation3D, { t: 0 }, { props: { tag: 'animate-assembly-unwind' } });
  98. }
  99. if (!changed) return;
  100. return plugin.runTask(state.updateTree(update));
  101. },
  102. async teardown(_, plugin) {
  103. const state = plugin.state.dataState;
  104. const reprs = state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure.Representation3DState)
  105. .filter(c => c.transform.props.tag === 'animate-assembly-unwind'));
  106. if (reprs.length === 0) return;
  107. const update = state.build();
  108. for (const r of reprs) update.delete(r.transform.ref);
  109. return plugin.runTask(state.updateTree(update));
  110. },
  111. async apply(animState, t, ctx) {
  112. const state = ctx.plugin.state.dataState;
  113. const anims = state.selectQ(q => q.ofType(PluginStateObject.Molecule.Structure.Representation3DState)
  114. .filter(c => c.transform.transformer === StateTransforms.Representation.UnwindStructureAssemblyRepresentation3D));
  115. if (anims.length === 0) {
  116. // nothing more to do here
  117. return { kind: 'finished' };
  118. }
  119. const update = state.build();
  120. const d = (t.current - t.lastApplied) / ctx.params.durationInMs;
  121. const newTime = (animState.t + d) % 1;
  122. for (const m of anims) {
  123. update.to(m.transform.ref).update(StateTransforms.Representation.UnwindStructureAssemblyRepresentation3D, _ => ({ t: newTime }));
  124. }
  125. await PluginCommands.State.Update.dispatch(ctx.plugin, { state, tree: update, options: { doNotLogTiming: true } });
  126. return { kind: 'next', state: { t: newTime } };
  127. }
  128. })
  129. export const AnimateUnitsExplode = PluginStateAnimation.create({
  130. name: 'built-in.animate-units-explode',
  131. display: { name: 'Explode Units' },
  132. params: () => ({
  133. durationInMs: PD.Numeric(3000, { min: 100, max: 10000, step: 100})
  134. }),
  135. initialState: () => ({ t: 0 }),
  136. async setup(_, plugin) {
  137. const state = plugin.state.dataState;
  138. const reprs = state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure.Representation3D));
  139. const update = state.build();
  140. let changed = false;
  141. for (const r of reprs) {
  142. const unwinds = state.select(StateSelection.Generators.byValue(r)
  143. .children()
  144. .filter(c => c.transform.transformer === StateTransforms.Representation.UnwindStructureAssemblyRepresentation3D));
  145. if (unwinds.length > 0) continue;
  146. changed = true;
  147. update.to(r.transform.ref)
  148. .apply(StateTransforms.Representation.ExplodeStructureRepresentation3D, { t: 0 }, { props: { tag: 'animate-units-explode' } });
  149. }
  150. if (!changed) return;
  151. return plugin.runTask(state.updateTree(update));
  152. },
  153. async teardown(_, plugin) {
  154. const state = plugin.state.dataState;
  155. const reprs = state.select(StateSelection.Generators.ofType(PluginStateObject.Molecule.Structure.Representation3DState)
  156. .filter(c => c.transform.props.tag === 'animate-units-explode'));
  157. if (reprs.length === 0) return;
  158. const update = state.build();
  159. for (const r of reprs) update.delete(r.transform.ref);
  160. return plugin.runTask(state.updateTree(update));
  161. },
  162. async apply(animState, t, ctx) {
  163. const state = ctx.plugin.state.dataState;
  164. const anims = state.selectQ(q => q.rootsOfType(PluginStateObject.Molecule.Structure.Representation3DState)
  165. .filter(c => c.transform.transformer === StateTransforms.Representation.ExplodeStructureRepresentation3D));
  166. if (anims.length === 0) {
  167. // nothing more to do here
  168. return { kind: 'finished' };
  169. }
  170. const update = state.build();
  171. const d = (t.current - t.lastApplied) / ctx.params.durationInMs;
  172. const newTime = (animState.t + d) % 1;
  173. for (const m of anims) {
  174. update.to(m.transform.ref).update(StateTransforms.Representation.ExplodeStructureRepresentation3D, _ => ({ t: newTime }));
  175. }
  176. await PluginCommands.State.Update.dispatch(ctx.plugin, { state, tree: update, options: { doNotLogTiming: true } });
  177. return { kind: 'next', state: { t: newTime } };
  178. }
  179. })