rpgx_wasm/engine/
mod.rs

1pub mod pawn;
2pub mod scene;
3
4use crate::{engine::scene::WasmScene, traits::WasmWrapper};
5use js_sys::Array;
6use rpgx::prelude::Engine;
7use wasm_bindgen::prelude::*;
8
9#[wasm_bindgen(js_name = Engine)]
10pub struct WasmEngine {
11    inner: Engine,
12}
13
14impl WasmWrapper<Engine> for WasmEngine {
15    fn into_inner(self) -> Engine {
16        self.inner
17    }
18
19    fn from_inner(inner: Engine) -> WasmEngine {
20        WasmEngine { inner }
21    }
22
23    fn inner(&self) -> &Engine {
24        &self.inner
25    }
26}
27
28#[wasm_bindgen(js_class = Engine)]
29impl WasmEngine {
30    /// Create a new engine from an initial scene
31    #[wasm_bindgen(constructor)]
32    pub fn new(scene: WasmScene) -> WasmEngine {
33        WasmEngine {
34            inner: Engine::new(scene.into_inner()),
35        }
36    }
37
38    /// Get the active scene
39    #[wasm_bindgen(js_name = getActiveScene)]
40    pub fn get_active_scene(&self) -> Option<WasmScene> {
41        self.inner
42            .get_active_scene()
43            .cloned()
44            .map(WasmScene::from_inner)
45    }
46
47    /// Get the active scene mutably
48    #[wasm_bindgen(js_name = getActiveSceneMut)]
49    pub fn get_active_scene_mut(&mut self) -> Option<WasmScene> {
50        self.inner
51            .get_active_scene_mut()
52            .cloned()
53            .map(WasmScene::from_inner)
54    }
55
56    /// Push a new scene and set it active
57    #[wasm_bindgen(js_name = pushScene)]
58    pub fn push_scene(&mut self, scene: WasmScene) {
59        self.inner.push_scene(scene.into_inner());
60    }
61
62    /// Pop the last scene if possible
63    #[wasm_bindgen(js_name = popScene)]
64    pub fn pop_scene(&mut self) {
65        self.inner.pop_scene();
66    }
67
68    /// Rollback timeline to a given index
69    #[wasm_bindgen(js_name = rollbackTo)]
70    pub fn rollback_to(&mut self, index: usize) {
71        self.inner.rollback_to(index);
72    }
73
74    /// Rewind to a specific index without truncating
75    #[wasm_bindgen(js_name = rewindTo)]
76    pub fn rewind_to(&mut self, index: usize) -> Result<(), JsValue> {
77        self.inner
78            .rewind_to(index)
79            .map_err(|e| JsValue::from_str(e))
80    }
81
82    /// Get a scene at a specific index
83    #[wasm_bindgen(js_name = getSceneAt)]
84    pub fn get_scene_at(&self, index: usize) -> Option<WasmScene> {
85        self.inner
86            .get_scene_at(index)
87            .cloned()
88            .map(WasmScene::from_inner)
89    }
90
91    /// Get full timeline (cloned)
92    #[wasm_bindgen(js_name = getTimeline)]
93    pub fn get_timeline(&self) -> Array {
94        self.inner
95            .timeline
96            .iter()
97            .cloned()
98            .map(WasmScene::from_inner)
99            .map(JsValue::from)
100            .collect()
101    }
102
103    /// Get current time index
104    #[wasm_bindgen(js_name = getCurrentIndex)]
105    pub fn get_current_index(&self) -> usize {
106        self.inner.timenow
107    }
108}