add some Color3 methods

This commit is contained in:
Quaternions 2024-10-05 12:19:55 -07:00
parent eccf7243c4
commit ff9c122470
2 changed files with 36 additions and 11 deletions

View File

@ -33,11 +33,36 @@ pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Err
Ok(()) Ok(())
} }
fn lerp(lhs:f32,rhs:f32,t:f32)->f32{
lhs+(rhs-lhs)*t
}
impl mlua::UserData for Color3{ impl mlua::UserData for Color3{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){ fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
fields.add_field_method_get("r",|_,this|Ok(this.r));
fields.add_field_method_set("r",|_,this,val|{
this.r=val;
Ok(())
});
fields.add_field_method_get("g",|_,this|Ok(this.g));
fields.add_field_method_set("g",|_,this,val|{
this.g=val;
Ok(())
});
fields.add_field_method_get("b",|_,this|Ok(this.b));
fields.add_field_method_set("b",|_,this,val|{
this.b=val;
Ok(())
});
} }
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){ fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
methods.add_method("Lerp",|_,this,(other,t):(Self,f32)|
Ok(Color3::new(
lerp(this.r,other.r,t),
lerp(this.g,other.g,t),
lerp(this.b,other.b,t),
))
)
} }
} }
type_from_lua_userdata!(Color3); type_from_lua_userdata!(Color3);

View File

@ -35,21 +35,21 @@ impl From<rbx_types::Vector3> for Vector3{
} }
impl mlua::UserData for Vector3{ impl mlua::UserData for Vector3{
fn add_fields<'lua,F: mlua::UserDataFields<'lua,Self>>(fields: &mut F) { fn add_fields<'lua,F: mlua::UserDataFields<'lua,Self>>(fields: &mut F){
fields.add_field_method_get("magnitude",|_,this| Ok(this.0.length())); fields.add_field_method_get("magnitude",|_,this|Ok(this.0.length()));
fields.add_field_method_get("x",|_,this| Ok(this.0.x)); fields.add_field_method_get("x",|_,this|Ok(this.0.x));
fields.add_field_method_set("x",|_,this,val| { fields.add_field_method_set("x",|_,this,val|{
this.0.x = val; this.0.x=val;
Ok(()) Ok(())
}); });
fields.add_field_method_get("y",|_,this| Ok(this.0.y)); fields.add_field_method_get("y",|_,this|Ok(this.0.y));
fields.add_field_method_set("y",|_,this,val| { fields.add_field_method_set("y",|_,this,val|{
this.0.y = val; this.0.y=val;
Ok(()) Ok(())
}); });
fields.add_field_method_get("z",|_,this| Ok(this.0.z)); fields.add_field_method_get("z",|_,this|Ok(this.0.z));
fields.add_field_method_set("z",|_,this,val| { fields.add_field_method_set("z",|_,this,val|{
this.0.z = val; this.0.z=val;
Ok(()) Ok(())
}); });
} }