FromLua type stuff + find_first_descendant_of_class

This commit is contained in:
Quaternions 2024-10-04 20:48:47 -07:00
parent ef78778708
commit 3a716373db

View File

@ -32,6 +32,9 @@ fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance)->S
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>{ 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) instance.children().iter().filter_map(|&r|dom.get_by_ref(r)).find(|inst|inst.class==class)
} }
pub fn find_first_descendant_of_class<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,class:&str)->Option<&'a rbx_dom_weak::Instance>{
dom.descendants_of(instance.referent()).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);
@ -162,17 +165,12 @@ impl Instance{
Ok(children) Ok(children)
}) })
); );
let ffc=|lua,this:&T,(name,search_descendants):(mlua::String,mlua::Value)|{ let ffc=|lua,this:&T,(name,search_descendants):(mlua::String,Option<bool>)|{
let name_str=name.to_str()?; let name_str=name.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"))?,
};
dom(lua,|dom|{ dom(lua,|dom|{
let instance=this.get(dom)?; let instance=this.get(dom)?;
Ok( Ok(
match search_descendants{ match search_descendants.unwrap_or(false){
true=>dom.descendants_of(this.referent()).find(|inst|inst.name==name_str), true=>dom.descendants_of(this.referent()).find(|inst|inst.name==name_str),
false=>instance.children().iter().filter_map(|&r| false=>instance.children().iter().filter_map(|&r|
dom.get_by_ref(r) dom.get_by_ref(r)
@ -186,19 +184,15 @@ 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)|{ methods.add_method("FindFirstChildOfClass",|lua,this,(class,search_descendants):(mlua::String,Option<bool>)|{
let class_str=class.to_str()?; 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|{ dom(lua,|dom|{
Ok(find_first_child_of_class(dom,this.get(dom)?,class_str) Ok(
.map(|inst|(Instance::new(inst.referent()))) match search_descendants.unwrap_or(false){
true=>find_first_descendant_of_class(dom,this.get(dom)?,class_str),
false=>find_first_child_of_class(dom,this.get(dom)?,class_str),
}
.map(|inst|(Instance::new(inst.referent())))
) )
}) })
}); });