Compare commits

...

5 Commits

Author SHA1 Message Date
e02e455990 CFrame.new 2024-10-05 12:34:30 -07:00
ff9c122470 add some Color3 methods 2024-10-05 12:19:55 -07:00
eccf7243c4 fake out nil parent 2024-10-05 12:09:57 -07:00
b457da64a1 Instance globals 2024-10-05 12:02:15 -07:00
ed3f9f9b30 reduce globals code 2024-10-05 11:52:18 -07:00
5 changed files with 108 additions and 22 deletions

View File

@ -64,15 +64,55 @@ pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Err
//CFrame.new
cframe_table.raw_set("new",
lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
Ok(ctx.create_userdata(CFrame::point(x,y,z)))
)?
lua.create_function(|_,tuple:(
mlua::Value,mlua::Value,Option<f32>,
Option<f32>,Option<f32>,Option<f32>,
Option<f32>,Option<f32>,Option<f32>,
Option<f32>,Option<f32>,Option<f32>,
)|match tuple{
//CFrame.new(pos)
(
mlua::Value::UserData(ud),mlua::Value::Nil,None,
None,None,None,
None,None,None,
None,None,None,
)=>{
let v:Vector3=ud.take()?;
Ok(CFrame::point(v.0.x,v.0.y,v.0.z))
},
//TODO: CFrame.new(pos,look)
(
mlua::Value::UserData(_pos),mlua::Value::UserData(_look),None,
None,None,None,
None,None,None,
None,None,None,
)=>Err(mlua::Error::runtime("Not yet implemented")),
//CFrame.new(x,y,z)
(
mlua::Value::Number(x),mlua::Value::Number(y),Some(z),
None,None,None,
None,None,None,
None,None,None,
)=>Ok(CFrame::point(x as f32,y as f32,z)),
//CFrame.new(x,y,z,xx,yx,zx,xy,yy,zy,xz,yz,zz)
(
mlua::Value::Number(x),mlua::Value::Number(y),Some(z),
Some(xx),Some(yx),Some(zx),
Some(xy),Some(yy),Some(zy),
Some(xz),Some(yz),Some(zz),
)=>Ok(CFrame::new(x as f32,y as f32,z,
xx,yx,zx,
xy,yy,zy,
xz,yz,zz,
)),
_=>Err(mlua::Error::runtime("Invalid arguments"))
})?
)?;
//CFrame.Angles
cframe_table.raw_set("Angles",
lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
Ok(ctx.create_userdata(CFrame::angles(x,y,z)))
lua.create_function(|_,(x,y,z):(f32,f32,f32)|
Ok(CFrame::angles(x,y,z))
)?
)?;

View File

@ -19,13 +19,13 @@ pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Err
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)))
lua.create_function(|_,(r,g,b):(f32,f32,f32)|
Ok(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)))
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))
)?
)?;
@ -33,11 +33,36 @@ pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Err
Ok(())
}
fn lerp(lhs:f32,rhs:f32,t:f32)->f32{
lhs+(rhs-lhs)*t
}
impl mlua::UserData for Color3{
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){
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);

View File

@ -4,6 +4,26 @@ use rbx_dom_weak::{InstanceBuilder,WeakDom};
use super::vector3::Vector3;
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{
let instance_table=lua.create_table()?;
//Instance.new
instance_table.raw_set("new",
lua.create_function(|lua,(class_name,parent):(mlua::String,Option<Instance>)|{
let class_name_str=class_name.to_str()?;
let parent=parent.ok_or(mlua::Error::runtime("Nil Parent not yet supported"))?;
dom_mut(lua,|dom|{
//TODO: Nil instances
Ok(Instance::new(dom.insert(parent.referent,InstanceBuilder::new(class_name_str))))
})
})?
)?;
globals.set("Instance",instance_table)?;
Ok(())
}
// LMAO look at this function!
fn dom_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut WeakDom)->mlua::Result<T>)->mlua::Result<T>{
let mut dom=lua.app_data_mut::<&'static mut WeakDom>().ok_or(mlua::Error::runtime("DataModel missing"))?;

View File

@ -33,6 +33,7 @@ fn init(lua:&mlua::Lua)->mlua::Result<()>{
super::color3::set_globals(lua,&globals)?;
super::vector3::set_globals(lua,&globals)?;
super::cframe::set_globals(lua,&globals)?;
super::instance::set_globals(lua,&globals)?;
Ok(())
}

View File

@ -12,8 +12,8 @@ pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Err
//Vector3.new
vector3_table.raw_set("new",
lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
Ok(ctx.create_userdata(Vector3::new(x,y,z)))
lua.create_function(|_,(x,y,z):(f32,f32,f32)|
Ok(Vector3::new(x,y,z))
)?
)?;
@ -35,21 +35,21 @@ impl From<rbx_types::Vector3> for Vector3{
}
impl mlua::UserData for Vector3{
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("x",|_,this| Ok(this.0.x));
fields.add_field_method_set("x",|_,this,val| {
this.0.x = val;
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("x",|_,this|Ok(this.0.x));
fields.add_field_method_set("x",|_,this,val|{
this.0.x=val;
Ok(())
});
fields.add_field_method_get("y",|_,this| Ok(this.0.y));
fields.add_field_method_set("y",|_,this,val| {
this.0.y = val;
fields.add_field_method_get("y",|_,this|Ok(this.0.y));
fields.add_field_method_set("y",|_,this,val|{
this.0.y=val;
Ok(())
});
fields.add_field_method_get("z",|_,this| Ok(this.0.z));
fields.add_field_method_set("z",|_,this,val| {
this.0.z = val;
fields.add_field_method_get("z",|_,this|Ok(this.0.z));
fields.add_field_method_set("z",|_,this,val|{
this.0.z=val;
Ok(())
});
}