rpgx_wasm/eucl/
coordinates.rs

1use crate::{
2    prelude::{WasmDelta, WasmShape},
3    traits::WasmWrapper,
4};
5use rpgx::prelude::Coordinates;
6use wasm_bindgen::prelude::*;
7
8#[wasm_bindgen(js_name = Coordinates)]
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub struct WasmCoordinates {
11    inner: Coordinates,
12}
13
14impl WasmWrapper<Coordinates> for WasmCoordinates {
15    fn from_inner(inner: Coordinates) -> Self {
16        WasmCoordinates { inner }
17    }
18
19    fn inner(&self) -> &Coordinates {
20        &self.inner
21    }
22
23    fn into_inner(self) -> Coordinates {
24        self.inner
25    }
26}
27
28#[wasm_bindgen(js_class = Coordinates)]
29impl WasmCoordinates {
30    #[wasm_bindgen(constructor)]
31    pub fn new(x: u32, y: u32) -> WasmCoordinates {
32        WasmCoordinates {
33            inner: Coordinates::new(x, y),
34        }
35    }
36
37    #[wasm_bindgen(getter)]
38    pub fn x(&self) -> u32 {
39        self.inner.x
40    }
41
42    #[wasm_bindgen(getter)]
43    pub fn y(&self) -> u32 {
44        self.inner.y
45    }
46
47    #[wasm_bindgen(js_name = isOrigin)]
48    pub fn is_origin(&self) -> bool {
49        self.inner.is_origin()
50    }
51
52    #[wasm_bindgen(js_name = isAlignedWith)]
53    pub fn is_aligned_with(&self, other: &WasmCoordinates) -> bool {
54        self.inner.is_aligned_with(other.inner)
55    }
56
57    #[wasm_bindgen(js_name = isWithin)]
58    pub fn is_within(&self, origin: &WasmCoordinates, shape: &WasmShape) -> bool {
59        self.inner.is_within(origin.inner, *shape.inner())
60    }
61
62    #[wasm_bindgen]
63    pub fn offseted(&self, delta: &WasmDelta) -> WasmCoordinates {
64        WasmCoordinates {
65            inner: self.inner.offseted(*delta.inner()),
66        }
67    }
68
69    #[wasm_bindgen(js_name = tryOffseted)]
70    pub fn try_offseted(&self, delta: &WasmDelta) -> Option<WasmCoordinates> {
71        self.inner
72            .try_offseted(*delta.inner())
73            .map(|c| WasmCoordinates { inner: c })
74    }
75
76    #[wasm_bindgen(js_name = toDelta)]
77    pub fn to_delta(&self) -> WasmDelta {
78        WasmDelta::from_inner(self.inner.to_delta())
79    }
80
81    #[wasm_bindgen(js_name = addCoordinates)]
82    pub fn add_coordinates(&self, other: &WasmCoordinates) -> WasmCoordinates {
83        WasmCoordinates {
84            inner: self.inner + other.inner,
85        }
86    }
87
88    #[wasm_bindgen(js_name = subCoordinates)]
89    pub fn sub_coordinates(&self, other: &WasmCoordinates) -> WasmCoordinates {
90        WasmCoordinates {
91            inner: self.inner - other.inner,
92        }
93    }
94
95    #[wasm_bindgen(js_name = addShape)]
96    pub fn add_shape(&self, shape: &WasmShape) -> WasmCoordinates {
97        WasmCoordinates {
98            inner: self.inner + *shape.inner(),
99        }
100    }
101
102    #[wasm_bindgen(js_name = subShape)]
103    pub fn sub_shape(&self, shape: &WasmShape) -> WasmCoordinates {
104        WasmCoordinates {
105            inner: self.inner - *shape.inner(),
106        }
107    }
108
109    #[wasm_bindgen(js_name = addDelta)]
110    pub fn add_delta(&self, delta: &WasmDelta) -> Option<WasmCoordinates> {
111        (self.inner + *delta.inner()).map(|c| WasmCoordinates { inner: c })
112    }
113
114    #[wasm_bindgen(js_name = subDelta)]
115    pub fn sub_delta(&self, delta: &WasmDelta) -> Option<WasmCoordinates> {
116        (self.inner - *delta.inner()).map(|c| WasmCoordinates { inner: c })
117    }
118
119    #[wasm_bindgen(js_name = boundingBox)]
120    pub fn bounding_box(coords: Vec<WasmCoordinates>) -> Option<js_sys::Array> {
121        let inner: Vec<Coordinates> = coords.into_iter().map(|c| c.inner).collect();
122        Coordinates::bounding_box(&inner).map(|(min, max)| {
123            let array = js_sys::Array::new();
124            array.push(&WasmCoordinates { inner: min }.into());
125            array.push(&WasmCoordinates { inner: max }.into());
126            array
127        })
128    }
129}