find_first_child_of_class

This commit is contained in:
Quaternions 2024-10-04 19:05:10 -07:00
parent 0148476648
commit 67223efa26

View File

@ -29,6 +29,10 @@ fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance)->S
full_name full_name
} }
pub fn find_first_child_of_class<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,class:&str)->Option<&'a rbx_dom_weak::Instance>{
instance.children().iter().filter_map(|&r|dom.get_by_ref(r)).find(|inst|inst.class==class)
}
//workaround until I have an enum of classes //workaround until I have an enum of classes
struct Dereferent(Ref); struct Dereferent(Ref);
impl mlua::UserData for Dereferent{} impl mlua::UserData for Dereferent{}
@ -181,6 +185,22 @@ impl Instance{
}; };
methods.add_method("FindFirstChild",ffc); methods.add_method("FindFirstChild",ffc);
methods.add_method("WaitForChild",ffc); methods.add_method("WaitForChild",ffc);
methods.add_method("FindFirstChildOfClass",|lua,this,(class,search_descendants):(mlua::String,mlua::Value)|{
let class_str=class.to_str()?;
let search_descendants=match search_descendants{
mlua::Value::Nil=>false,
mlua::Value::Boolean(b)=>b,
_=>Err(mlua::Error::runtime("Invalid argument #3 bool expected"))?,
};
if search_descendants==true{
return Err(mlua::Error::runtime("FFC of class searching descendants not supported get rekt"));
}
dom(lua,|dom|{
Ok(find_first_child_of_class(dom,this.get(dom)?,class_str)
.map(|inst|(Instance::new(inst.referent())))
)
})
});
methods.add_method("GetDescendants",|lua,this,_:()| methods.add_method("GetDescendants",|lua,this,_:()|
dom(lua,|dom|{ dom(lua,|dom|{
let children:Vec<_>=dom let children:Vec<_>=dom