Skip to content
Snippets Groups Projects
model.ts 1.76 KiB
Newer Older
David Sehnal's avatar
David Sehnal committed
/**
 * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
 *
 * @author David Sehnal <david.sehnal@gmail.com>
 */

import { ParamDefinition as PD } from 'mol-util/param-definition';
import { PluginContext } from 'mol-plugin/context';

export { PluginStateAnimation }

David Sehnal's avatar
David Sehnal committed
// TODO: helpers for building animations (once more animations are added)
//       for example "composite animation"

interface PluginStateAnimation<P = any, S = any> {
David Sehnal's avatar
David Sehnal committed
    name: string,
    readonly display: { readonly name: string, readonly description?: string },
    params: (ctx: PluginContext) => PD.For<P>,
    initialState(params: P, ctx: PluginContext): S,
David Sehnal's avatar
David Sehnal committed

    // TODO: support state in setup/teardown?
    setup?(params: P, ctx: PluginContext): void | Promise<void>,
    teardown?(params: P, ctx: PluginContext): void | Promise<void>,

David Sehnal's avatar
David Sehnal committed
    /**
     * Apply the current frame and modify the state.
     * @param t Current absolute time since the animation started.
     */
David Sehnal's avatar
David Sehnal committed
    apply(state: S, t: PluginStateAnimation.Time, ctx: PluginStateAnimation.Context<P>): Promise<PluginStateAnimation.ApplyResult<S>>,
David Sehnal's avatar
David Sehnal committed

    /**
     * The state must be serializable to JSON. If JSON.stringify is not enough,
     * custom converted to an object that works with JSON.stringify can be provided.
David Sehnal's avatar
David Sehnal committed
     */
    stateSerialization?: { toJSON(state: S): any, fromJSON(data: any): S }
David Sehnal's avatar
David Sehnal committed
}

namespace PluginStateAnimation {
David Sehnal's avatar
David Sehnal committed
    export interface Time {
        lastApplied: number,
        current: number
    }

    export type ApplyResult<S> = { kind: 'finished' } | { kind: 'skip' } | { kind: 'next', state: S }
    export interface Context<P> {
        params: P,
David Sehnal's avatar
David Sehnal committed
        plugin: PluginContext
    }

    export function create<P, S>(params: PluginStateAnimation<P, S>) {
David Sehnal's avatar
David Sehnal committed
        return params;
    }
}