Skip to main content

il2cpp_bridge_rs/structs/components/rendering/
material.rs

1use crate::structs::collections::Il2cppString;
2use crate::structs::components::rendering::shader::Shader;
3use crate::structs::core::Object;
4use crate::structs::math::Color;
5use std::ffi::c_void;
6use std::ops::Deref;
7
8#[repr(C)]
9#[derive(Debug, Clone, Copy)]
10pub struct Material {
11    pub object: Object,
12}
13
14impl Material {
15    /// Creates a Material from a raw pointer
16    ///
17    /// # Arguments
18    /// * `ptr` - The raw pointer to the material
19    ///
20    /// # Returns
21    /// * `Self` - The created Material wrapper
22    pub fn from_ptr(ptr: *mut c_void) -> Self {
23        Self {
24            object: unsafe { Object::from_ptr(ptr) },
25        }
26    }
27}
28
29impl Deref for Material {
30    type Target = Object;
31    fn deref(&self) -> &Self::Target {
32        &self.object
33    }
34}
35
36impl Material {
37    /// Gets the main color of the material.
38    ///
39    /// # Returns
40    /// * `Color` - The main color (returns Black if retrieval fails)
41    pub fn get_color(&self) -> Color {
42        unsafe {
43            self.method(("get_color", 0))
44                .and_then(|method| method.call::<Color>(&[]).ok())
45                .unwrap_or(Color::BLACK) // if the method fails, return black
46        }
47    }
48
49    /// Sets the main color of the material.
50    ///
51    /// # Arguments
52    /// * `color` - The new color to set
53    ///
54    /// # Returns
55    /// * `Result<(), String>` - Ok if success
56    pub fn set_color(&self, color: Color) -> Result<(), String> {
57        unsafe {
58            self.method(("set_color", 1))
59                .ok_or("Method 'set_color' not found")?
60                .call::<()>(&[&mut (color.clone()) as *mut Color as *mut c_void])?;
61        }
62        Ok(())
63    }
64
65    /// Sets a named color value.
66    ///
67    /// # Arguments
68    /// * `name` - The property name of the color
69    /// * `color` - The color value to set
70    ///
71    /// # Returns
72    /// * `Result<(), String>` - Ok if success
73    pub fn set_color_named(&self, name: &str, color: Color) -> Result<(), String> {
74        unsafe {
75            let string_new_ptr = Il2cppString::new(name);
76            self.method(("SetColor", 2))
77                .ok_or("Method 'SetColor' not found")?
78                .call::<()>(&[
79                    string_new_ptr as *mut c_void,
80                    &mut (color.clone()) as *mut Color as *mut c_void,
81                ])?;
82        }
83        Ok(())
84    }
85
86    /// Sets a float value for a shader property.
87    ///
88    /// # Arguments
89    /// * `name` - The property name
90    /// * `value` - The float value
91    ///
92    /// # Returns
93    /// * `Result<(), String>` - Ok if success
94    pub fn set_float(&self, name: &str, value: f32) -> Result<(), String> {
95        unsafe {
96            let string_new_ptr = Il2cppString::new(name);
97            self.method(("SetFloat", 2))
98                .ok_or("Method 'SetFloat' not found")?
99                .call::<()>(&[string_new_ptr as *mut c_void, &mut { value } as *mut f32
100                    as *mut c_void])?;
101        }
102        Ok(())
103    }
104
105    /// Sets an integer value for a shader property.
106    ///
107    /// # Arguments
108    /// * `name` - The property name
109    /// * `value` - The integer value
110    ///
111    /// # Returns
112    /// * `Result<(), String>` - Ok if success
113    pub fn set_int(&self, name: &str, value: i32) -> Result<(), String> {
114        unsafe {
115            let string_new_ptr = Il2cppString::new(name);
116            self.method(("SetInt", 2))
117                .ok_or("Method 'SetInt' not found")?
118                .call::<()>(&[string_new_ptr as *mut c_void, &mut { value } as *mut i32
119                    as *mut c_void])?;
120        }
121        Ok(())
122    }
123
124    /// Gets the shader used by the material.
125    ///
126    /// # Returns
127    /// * `Result<Shader, String>` - The shader assigned to this material
128    pub fn get_shader(&self) -> Result<Shader, String> {
129        unsafe {
130            let ptr = self
131                .method(("get_shader", 0))
132                .ok_or("Method 'get_shader' not found")?
133                .call::<*mut c_void>(&[])?;
134
135            if ptr.is_null() {
136                return Err("Shader is null".to_string());
137            }
138            Ok(Shader::from_ptr(ptr))
139        }
140    }
141
142    /// Sets the shader used by the material.
143    ///
144    /// # Arguments
145    /// * `shader` - The new shader to assign
146    ///
147    /// # Returns
148    /// * `Result<(), String>` - Ok if success
149    pub fn set_shader(&self, shader: Shader) -> Result<(), String> {
150        unsafe {
151            self.method(("set_shader", 1))
152                .ok_or("Method 'set_shader' not found")?
153                .call::<()>(&[shader.object.ptr as *mut c_void])?;
154        }
155        Ok(())
156    }
157}