wrend/uniforms/
uniform_js.rs

1use crate::{utils, StringArray, Uniform, UniformLocationsMap};
2
3use std::ops::{Deref, DerefMut};
4use wasm_bindgen::{prelude::wasm_bindgen, JsCast};
5
6pub type UniformJsInner = Uniform<String, String>;
7
8#[wasm_bindgen(inspectable, js_name = Uniform)]
9pub struct UniformJs(UniformJsInner);
10
11#[wasm_bindgen(js_class = Uniform)]
12impl UniformJs {
13    #[wasm_bindgen(js_name = programIds)]
14    pub fn program_ids(&self) -> StringArray {
15        utils::strings_to_js_array(self.deref().program_ids())
16    }
17
18    #[wasm_bindgen(js_name = uniformId)]
19    pub fn uniform_id(&self) -> String {
20        self.deref().uniform_id().to_owned()
21    }
22
23    #[wasm_bindgen(js_name = uniformLocations)]
24    pub fn uniform_locations(&self) -> UniformLocationsMap {
25        utils::hash_map_to_js_map(self.deref().uniform_locations())
26            .dyn_into()
27            .expect("Should be able to convert Map into UniformLocationsMap")
28    }
29}
30
31impl UniformJs {
32    pub fn into_inner(self) -> UniformJsInner {
33        self.0
34    }
35}
36
37impl From<UniformJsInner> for UniformJs {
38    fn from(js_uniform_inner: UniformJsInner) -> Self {
39        Self(js_uniform_inner)
40    }
41}
42
43impl From<&UniformJsInner> for UniformJs {
44    fn from(js_uniform_inner: &UniformJsInner) -> Self {
45        Self(js_uniform_inner.to_owned())
46    }
47}
48
49impl Deref for UniformJs {
50    type Target = UniformJsInner;
51
52    fn deref(&self) -> &Self::Target {
53        &self.0
54    }
55}
56
57impl DerefMut for UniformJs {
58    fn deref_mut(&mut self) -> &mut UniformJsInner {
59        &mut self.0
60    }
61}