rpgx_wasm/engine/
mod.rs

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