rpgx_wasm/map/
mask.rs

1use rpgx::prelude::*;
2use wasm_bindgen::prelude::*;
3
4use crate::{
5    prelude::{WasmCoordinates, WasmDelta, WasmEffect, WasmRect, WasmShape},
6    traits::WasmWrapper,
7};
8
9#[wasm_bindgen(js_name = Mask)]
10pub struct WasmMask {
11    inner: Mask,
12}
13
14impl WasmWrapper<Mask> for WasmMask {
15    /// Get a reference to the inner Mask (Rust struct)
16    fn inner(&self) -> &Mask {
17        &self.inner
18    }
19
20    /// Consume WasmMask and return the inner Mask
21    fn into_inner(self) -> Mask {
22        self.inner
23    }
24
25    /// Create WasmMask from inner Mask directly
26    fn from_inner(inner: Mask) -> WasmMask {
27        WasmMask { inner }
28    }
29}
30
31#[wasm_bindgen(js_class = Mask)]
32impl WasmMask {
33    /// Create a new Mask from name, areas, and uniform effect
34    #[wasm_bindgen(constructor)]
35    pub fn new(name: String, areas: Vec<WasmRect>, effects: Vec<WasmEffect>) -> WasmMask {
36        let inner_areas = areas.into_iter().map(|r| r.into_inner()).collect();
37        WasmMask {
38            inner: Mask::new(
39                name,
40                inner_areas,
41                effects.iter().map(|e| e.into_inner()).collect(),
42            ),
43        }
44    }
45
46    /// Get the mask's name
47    #[wasm_bindgen(getter)]
48    pub fn name(&self) -> String {
49        self.inner.name.clone()
50    }
51
52    /// Offset all tiles and their effects by delta
53    #[wasm_bindgen]
54    pub fn offset(&mut self, delta: &WasmDelta) {
55        self.inner.offset(*delta.inner());
56    }
57
58    /// Get the bounding shape of all tiles
59    #[wasm_bindgen(js_name = getShape)]
60    pub fn get_shape(&self) -> WasmShape {
61        WasmShape::from_inner(self.inner.get_shape())
62    }
63
64    /// Returns true if any tile contains the coordinate
65    #[wasm_bindgen]
66    pub fn contains(&self, coord: &WasmCoordinates) -> bool {
67        self.inner.contains(coord.inner())
68    }
69
70    #[wasm_bindgen(js_name = getTexture)]
71    pub fn get_texture(&self) -> Option<u32> {
72        self.inner.get_texture()
73    }
74
75    #[wasm_bindgen]
76    pub fn tiles(&self) -> Vec<WasmRect> {
77        self.inner
78            .tiles
79            .iter()
80            .map(|t| WasmRect::from_inner(*t))
81            .collect()
82    }
83}