From c06ba656624727c190cd562ec7dc5ad3e8ff1aaf Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 3 Oct 2024 18:05:39 -0700 Subject: [PATCH] Color3 --- src/runner/color3.rs | 50 ++++++++++++++++++++++++++++++++++++++++++ src/runner/instance.rs | 4 ++++ src/runner/mod.rs | 1 + src/runner/runner.rs | 1 + 4 files changed, 56 insertions(+) create mode 100644 src/runner/color3.rs diff --git a/src/runner/color3.rs b/src/runner/color3.rs new file mode 100644 index 0000000..f6a162d --- /dev/null +++ b/src/runner/color3.rs @@ -0,0 +1,50 @@ +#[derive(Clone,Copy)] +pub struct Color3{ + r:f32, + g:f32, + b:f32, +} +impl Color3{ + pub const fn new(r:f32,g:f32,b:f32)->Self{ + Self{r,g,b} + } +} +impl Into for Color3{ + fn into(self)->rbx_types::Color3{ + rbx_types::Color3::new(self.r,self.g,self.b) + } +} + +pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{ + let color3_table=lua.create_table()?; + + color3_table.raw_set("new", + lua.create_function(|ctx,(r,g,b):(f32,f32,f32)| + Ok(ctx.create_userdata(Color3::new(r,g,b))) + )? + )?; + color3_table.raw_set("fromRGB", + lua.create_function(|ctx,(r,g,b):(u8,u8,u8)| + Ok(ctx.create_userdata(Color3::new(r as f32/255.0,g as f32/255.0,b as f32/255.0))) + )? + )?; + + globals.set("Color3",color3_table)?; + + Ok(()) +} + +impl mlua::UserData for Color3{ + fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){ + } + fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){ + } +} +impl<'lua> mlua::FromLua<'lua> for Color3{ + fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult{ + match value{ + mlua::Value::UserData(ud)=>ud.take()?, + other=>Err(mlua::Error::runtime(format!("Expected Color3 got {:?}",other))), + } + } +} diff --git a/src/runner/instance.rs b/src/runner/instance.rs index 6face0b..b26e42d 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -160,6 +160,10 @@ impl Instance{ let typed_value:f32=coerce_float32(&value).ok_or(mlua::Error::runtime("Expected f32"))?; instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Float32(typed_value)); }, + rbx_types::Variant::Color3(_)=>{ + let typed_value:super::color3::Color3=value.as_userdata().ok_or(mlua::Error::runtime("Expected Userdata"))?.take()?; + instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Color3(typed_value.into())); + }, other=>return Err(mlua::Error::runtime(format!("Unimplemented property type: {other:?}"))), } Ok(()) diff --git a/src/runner/mod.rs b/src/runner/mod.rs index f832a6a..0b364fd 100644 --- a/src/runner/mod.rs +++ b/src/runner/mod.rs @@ -1,5 +1,6 @@ mod runner; +mod color3; mod cframe; mod vector3; pub mod instance; diff --git a/src/runner/runner.rs b/src/runner/runner.rs index df3c237..658da3f 100644 --- a/src/runner/runner.rs +++ b/src/runner/runner.rs @@ -29,6 +29,7 @@ fn init(lua:&mlua::Lua)->mlua::Result<()>{ //global environment let globals=lua.globals(); + super::color3::set_globals(lua,&globals)?; super::vector3::set_globals(lua,&globals)?; super::cframe::set_globals(lua,&globals)?;