the composition paradigm
This commit is contained in:
parent
ff76f3c865
commit
90609f4ad4
@ -1,29 +1,8 @@
|
||||
use rbx_types::Ref;
|
||||
use rbx_dom_weak::WeakDom;
|
||||
|
||||
use super::vector3::Vector3;
|
||||
|
||||
pub struct Instance{
|
||||
referent:rbx_types::Ref,
|
||||
}
|
||||
|
||||
impl From<crate::script::Script> for Instance{
|
||||
fn from(value:crate::script::Script)->Self{
|
||||
Self{referent:value.script}
|
||||
}
|
||||
}
|
||||
|
||||
impl Instance{
|
||||
pub const fn new(referent:rbx_types::Ref)->Self{
|
||||
Self{referent}
|
||||
}
|
||||
pub fn get<'a>(&'a self,dom:&'a WeakDom)->mlua::Result<&'a rbx_dom_weak::Instance>{
|
||||
dom.get_by_ref(self.referent).ok_or(mlua::Error::runtime("Instance missing"))
|
||||
}
|
||||
pub fn get_mut<'a>(&'a self,dom:&'a mut WeakDom)->mlua::Result<&'a mut rbx_dom_weak::Instance>{
|
||||
dom.get_by_ref_mut(self.referent).ok_or(mlua::Error::runtime("Instance missing"))
|
||||
}
|
||||
}
|
||||
|
||||
// LMAO look at this function!
|
||||
fn dom<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"))?;
|
||||
@ -38,8 +17,36 @@ fn coerce_float32(value:&mlua::Value)->Option<f32>{
|
||||
}
|
||||
}
|
||||
|
||||
impl mlua::UserData for Instance{
|
||||
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
|
||||
trait Referent{
|
||||
fn referent(&self)->Ref;
|
||||
fn get<'a>(&self,dom:&'a WeakDom)->mlua::Result<&'a rbx_dom_weak::Instance>{
|
||||
dom.get_by_ref(self.referent()).ok_or(mlua::Error::runtime("Instance missing"))
|
||||
}
|
||||
fn get_mut<'a>(&self,dom:&'a mut WeakDom)->mlua::Result<&'a mut rbx_dom_weak::Instance>{
|
||||
dom.get_by_ref_mut(self.referent()).ok_or(mlua::Error::runtime("Instance missing"))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Instance{
|
||||
referent:Ref,
|
||||
}
|
||||
impl Referent for Instance{
|
||||
fn referent(&self)->Ref{
|
||||
self.referent
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::script::Script> for Instance{
|
||||
fn from(value:crate::script::Script)->Self{
|
||||
Self{referent:value.script}
|
||||
}
|
||||
}
|
||||
|
||||
impl Instance{
|
||||
pub const fn new(referent:Ref)->Self{
|
||||
Self{referent}
|
||||
}
|
||||
fn add_fields<'lua,T:Referent,F:mlua::UserDataFields<'lua,T>>(fields:&mut F){
|
||||
fields.add_field_method_get("Parent",|lua,this|{
|
||||
dom(lua,|dom|{
|
||||
let instance=this.get(dom)?;
|
||||
@ -48,7 +55,7 @@ impl mlua::UserData for Instance{
|
||||
});
|
||||
fields.add_field_method_set("Parent",|lua,this,val:Self|{
|
||||
dom(lua,|dom|{
|
||||
dom.transfer_within(this.referent,val.referent);
|
||||
dom.transfer_within(this.referent(),val.referent);
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
@ -68,7 +75,7 @@ impl mlua::UserData for Instance{
|
||||
});
|
||||
}
|
||||
|
||||
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
||||
fn add_methods<'lua,T:Referent,M:mlua::UserDataMethods<'lua,T>>(methods:&mut M){
|
||||
methods.add_method("GetChildren",|lua,this,_:()|
|
||||
dom(lua,|dom|{
|
||||
let instance=this.get(dom)?;
|
||||
@ -84,7 +91,7 @@ impl mlua::UserData for Instance{
|
||||
methods.add_method("GetDescendants",|lua,this,_:()|
|
||||
dom(lua,|dom|{
|
||||
let children:Vec<_>=dom
|
||||
.descendants_of(this.referent)
|
||||
.descendants_of(this.referent())
|
||||
.map(|instance|
|
||||
Instance::new(instance.referent())
|
||||
)
|
||||
@ -100,6 +107,7 @@ impl mlua::UserData for Instance{
|
||||
);
|
||||
methods.add_meta_function(mlua::MetaMethod::NewIndex,|lua,(this,index,value):(Self,mlua::String,mlua::Value)|
|
||||
dom(lua,|dom|{
|
||||
//println!("__newindex t={this:?} i={index:?} v={value:?}");
|
||||
let instance=this.get_mut(dom)?;
|
||||
let index_str=index.to_str()?;
|
||||
let db=rbx_reflection_database::get();
|
||||
@ -121,7 +129,14 @@ impl mlua::UserData for Instance{
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl mlua::UserData for Instance{
|
||||
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
|
||||
Instance::add_fields(fields);
|
||||
}
|
||||
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
||||
Instance::add_methods(methods);
|
||||
}
|
||||
}
|
||||
impl<'lua> mlua::FromLua<'lua> for Instance{
|
||||
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
|
||||
match value{
|
||||
@ -130,3 +145,51 @@ impl<'lua> mlua::FromLua<'lua> for Instance{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DataModel{
|
||||
referent:Ref,
|
||||
}
|
||||
impl Referent for DataModel{
|
||||
fn referent(&self)->Ref{
|
||||
self.referent
|
||||
}
|
||||
}
|
||||
impl DataModel{
|
||||
fn add_methods<'lua,T,M:mlua::UserDataMethods<'lua,T>>(methods:&mut M){
|
||||
methods.add_method("GetService",|lua,this,service:String|
|
||||
dom(lua,|dom|{
|
||||
match service.as_str(){
|
||||
//"Lighting"=>Ok(Lighting::new()),
|
||||
other=>Err::<(),_>(mlua::Error::runtime("Service '{other}' not supported")),
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
impl mlua::UserData for DataModel{
|
||||
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
|
||||
Instance::add_fields(fields);
|
||||
//DataModel::add_fields(fields);
|
||||
}
|
||||
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
||||
Instance::add_methods(methods);
|
||||
DataModel::add_methods(methods);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Lighting{
|
||||
referent:Ref,
|
||||
}
|
||||
impl Referent for Lighting{
|
||||
fn referent(&self)->Ref{
|
||||
self.referent
|
||||
}
|
||||
}
|
||||
impl mlua::UserData for Lighting{
|
||||
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
|
||||
Instance::add_fields(fields);
|
||||
}
|
||||
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
||||
Instance::add_methods(methods);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user