Skip to content
Snippets Groups Projects
state.ts 2.05 KiB
Newer Older
/**
 * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
 *
 * @author David Sehnal <david.sehnal@gmail.com>
 */

David Sehnal's avatar
David Sehnal committed
import { State, StateTree } from 'mol-state';
import { PluginStateObjects as SO } from './state/objects';
David Sehnal's avatar
David Sehnal committed
import { CombinedCamera } from 'mol-canvas3d/camera/combined';

export { PluginState }

class PluginState {
David Sehnal's avatar
David Sehnal committed
    readonly data: State;
David Sehnal's avatar
David Sehnal committed
    readonly behavior: State;

    getSnapshot(): PluginState.Snapshot {
David Sehnal's avatar
David Sehnal committed
        return {
            data: this.data.getSnapshot(),
David Sehnal's avatar
David Sehnal committed
            behaviour: this.behavior.getSnapshot(),
            canvas3d: {
                camera: { ...this.plugin.canvas3d.camera }
David Sehnal's avatar
David Sehnal committed
            }
        };
    async setSnapshot(snapshot: PluginState.Snapshot) {
        await this.behavior.setSnapshot(snapshot.behaviour);
        await this.data.setSnapshot(snapshot.data);
David Sehnal's avatar
David Sehnal committed

        // TODO: handle camera
        // console.log({ old: { ...this.plugin.canvas3d.camera  }, new: snapshot.canvas3d.camera });
        // CombinedCamera.copy(snapshot.canvas3d.camera, this.plugin.canvas3d.camera);
        // CombinedCamera.update(this.plugin.canvas3d.camera);
        // this.plugin.canvas3d.center
        // console.log({ copied: { ...this.plugin.canvas3d.camera  } });
        this.plugin.canvas3d.requestDraw(true);
David Sehnal's avatar
David Sehnal committed
        // console.log('updated camera');
    }

    updateData(tree: StateTree) {
        return this.plugin.runTask(this.data.update(tree));
David Sehnal's avatar
David Sehnal committed
    updateBehaviour(tree: StateTree) {
        return this.plugin.runTask(this.behavior.update(tree));
    }

    dispose() {
        this.data.dispose();
    }
David Sehnal's avatar
David Sehnal committed

David Sehnal's avatar
David Sehnal committed
    constructor(private plugin: import('./context').PluginContext) {
        this.data = State.create(new SO.DataRoot({ label: 'Root' }, { }), { globalContext: plugin });
        this.behavior = State.create(new SO.BehaviorRoot({ label: 'Root' }, { }), { globalContext: plugin });
David Sehnal's avatar
David Sehnal committed
    }
}

namespace PluginState {
David Sehnal's avatar
David Sehnal committed
    export interface Snapshot {
        data: State.Snapshot,
        behaviour: State.Snapshot,
David Sehnal's avatar
David Sehnal committed
        canvas3d: {
            camera: CombinedCamera
        }
David Sehnal's avatar
David Sehnal committed
    }