model.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 { ParamDefinition as PD } from 'mol-util/param-definition';
  7. import { PluginContext } from 'mol-plugin/context';
  8. export { PluginStateAnimation }
  9. // TODO: helpers for building animations (once more animations are added)
  10. // for example "composite animation"
  11. interface PluginStateAnimation<P = any, S = any> {
  12. name: string,
  13. readonly display: { readonly name: string, readonly description?: string },
  14. params: (ctx: PluginContext) => PD.For<P>,
  15. initialState(params: P, ctx: PluginContext): S,
  16. /**
  17. * Apply the current frame and modify the state.
  18. * @param t Current absolute time since the animation started.
  19. */
  20. apply(state: S, t: PluginStateAnimation.Time, ctx: PluginStateAnimation.Context<P>): Promise<PluginStateAnimation.ApplyResult<S>>,
  21. /**
  22. * The state must be serializable to JSON. If JSON.stringify is not enough,
  23. * custom converted to an object that works with JSON.stringify can be provided.
  24. */
  25. stateSerialization?: { toJSON(state: S): any, fromJSON(data: any): S }
  26. }
  27. namespace PluginStateAnimation {
  28. export interface Time {
  29. lastApplied: number,
  30. current: number
  31. }
  32. export type ApplyResult<S> = { kind: 'finished' } | { kind: 'skip' } | { kind: 'next', state: S }
  33. export interface Context<P> {
  34. params: P,
  35. plugin: PluginContext
  36. }
  37. export function create<P, S>(params: PluginStateAnimation<P, S>) {
  38. return params;
  39. }
  40. }