diff --git a/lib/roblox_emulator/src/runner/brickcolor.rs b/lib/roblox_emulator/src/runner/brickcolor.rs new file mode 100644 index 0000000..b864684 --- /dev/null +++ b/lib/roblox_emulator/src/runner/brickcolor.rs @@ -0,0 +1,33 @@ +use super::color3::Color3; + +#[derive(Clone,Copy)] +pub struct BrickColor(rbx_types::BrickColor); +impl BrickColor{ + pub fn from_name(name:&str)->Option<Self>{ + Some(BrickColor(rbx_types::BrickColor::from_name(name)?)) + } +} + +pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{ + let brickcolor_table=lua.create_table()?; + + brickcolor_table.raw_set("new", + lua.create_function(|_,name:mlua::String| + Ok(BrickColor::from_name(&*name.to_str()?)) + )? + )?; + + globals.set("BrickColor",brickcolor_table)?; + + Ok(()) +} + +impl mlua::UserData for BrickColor{ + fn add_fields<F:mlua::UserDataFields<Self>>(fields:&mut F){ + fields.add_field_method_get("Color",|_,BrickColor(this)|{ + let rbx_types::Color3uint8{r,g,b}=this.to_color3uint8(); + Ok(Color3::from_rgb(r,g,b)) + }); + } +} +type_from_lua_userdata!(BrickColor); diff --git a/lib/roblox_emulator/src/runner/color3.rs b/lib/roblox_emulator/src/runner/color3.rs index 93b8c68..bae927f 100644 --- a/lib/roblox_emulator/src/runner/color3.rs +++ b/lib/roblox_emulator/src/runner/color3.rs @@ -8,6 +8,9 @@ impl Color3{ pub const fn new(r:f32,g:f32,b:f32)->Self{ Self{r,g,b} } + pub const fn from_rgb(r:u8,g:u8,b:u8)->Self{ + Color3::new(r as f32/255.0,g as f32/255.0,b as f32/255.0) + } } impl Into<rbx_types::Color3> for Color3{ fn into(self)->rbx_types::Color3{ @@ -25,7 +28,7 @@ pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{ )?; color3_table.raw_set("fromRGB", lua.create_function(|_,(r,g,b):(u8,u8,u8)| - Ok(Color3::new(r as f32/255.0,g as f32/255.0,b as f32/255.0)) + Ok(Color3::from_rgb(r,g,b)) )? )?; diff --git a/lib/roblox_emulator/src/runner/mod.rs b/lib/roblox_emulator/src/runner/mod.rs index a208868..8a8e5ba 100644 --- a/lib/roblox_emulator/src/runner/mod.rs +++ b/lib/roblox_emulator/src/runner/mod.rs @@ -6,6 +6,7 @@ mod r#enum; mod color3; mod cframe; mod vector3; +mod brickcolor; pub mod instance; mod script_signal; mod color_sequence; diff --git a/lib/roblox_emulator/src/runner/runner.rs b/lib/roblox_emulator/src/runner/runner.rs index 84d5ccb..da6f131 100644 --- a/lib/roblox_emulator/src/runner/runner.rs +++ b/lib/roblox_emulator/src/runner/runner.rs @@ -36,6 +36,7 @@ fn init(lua:&mlua::Lua)->mlua::Result<()>{ super::script_signal::set_globals(lua,&globals)?; super::r#enum::set_globals(lua,&globals)?; super::color3::set_globals(lua,&globals)?; + super::brickcolor::set_globals(lua,&globals)?; super::vector3::set_globals(lua,&globals)?; super::cframe::set_globals(lua,&globals)?; super::instance::instance::set_globals(lua,&globals)?;