rpgx_wasm/eucl/
shape.rs

1use rpgx::prelude::{Coordinates, Shape};
2use wasm_bindgen::prelude::*;
3
4#[wasm_bindgen(js_name = Shape)]
5#[derive(Clone, Debug)]
6pub struct WasmShape {
7    inner: Shape,
8}
9
10#[wasm_bindgen(js_class = Shape)]
11impl WasmShape {
12    #[wasm_bindgen(constructor)]
13    pub fn new(width: u32, height: u32) -> WasmShape {
14        WasmShape {
15            inner: Shape::from_rectangle(width, height),
16        }
17    }
18
19    #[wasm_bindgen(js_name = fromSquare)]
20    pub fn from_square(side: u32) -> WasmShape {
21        WasmShape {
22            inner: Shape::from_square(side),
23        }
24    }
25
26    #[wasm_bindgen(getter)]
27    pub fn width(&self) -> u32 {
28        self.inner.width
29    }
30
31    #[wasm_bindgen(getter)]
32    pub fn height(&self) -> u32 {
33        self.inner.height
34    }
35
36    #[wasm_bindgen(js_name = area)]
37    pub fn area(&self) -> u32 {
38        self.inner.area()
39    }
40
41    /// Returns a copy of the shape offset by the given x/y
42    #[wasm_bindgen(js_name = offsetBy)]
43    pub fn offset_by(&self, x: u32, y: u32) -> WasmShape {
44        let offset = Coordinates { x, y };
45        WasmShape {
46            inner: self.inner.offset_by(offset),
47        }
48    }
49}
50
51impl WasmShape {
52    // Access to inner for Rust interop (not exported to JS)
53    pub fn inner(&self) -> &Shape {
54        &self.inner
55    }
56
57    pub fn into_inner(self) -> Shape {
58        self.inner
59    }
60
61    pub fn from_inner(inner: Shape) -> Self {
62        Self { inner }
63    }
64}