il2cpp_bridge_rs/structs/components/rendering/
material.rs1use 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 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 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) }
47 }
48
49 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 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 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 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 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 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}