rpgx_wasm/eucl/
shape.rs

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