From 3209d65b9347ffcb62fa570b9a93ca0c6ee9280a Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Sun, 16 Jun 2024 15:34:06 -0400 Subject: [PATCH] push code for debugging --- .vscode/launch.json | 17 ++ examples/lua-scripting/scripts/test.lua | 13 +- lyra-game/src/game.rs | 8 +- .../lyra-scripting-derive/src/lua_macro.rs | 25 +- lyra-scripting/src/lua/wrappers/math.rs | 257 +++++++++++++++++- 5 files changed, 306 insertions(+), 14 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index a69d883..c368743 100755 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,23 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug example lua-scripting", + "cargo": { + "args": [ + "build", + "--manifest-path", "${workspaceFolder}/examples/lua-scripting/Cargo.toml" + ], + "filter": { + "name": "lua-scripting", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}/examples/lua-scripting" + }, { "type": "lldb", "request": "launch", diff --git a/examples/lua-scripting/scripts/test.lua b/examples/lua-scripting/scripts/test.lua index bac6e90..d3e7872 100644 --- a/examples/lua-scripting/scripts/test.lua +++ b/examples/lua-scripting/scripts/test.lua @@ -6,16 +6,21 @@ function udname(val) end function on_init() - local cube = world:request_res("../assets/cube-texture-embedded.gltf") + --[[ local cube = world:request_res("../assets/cube-texture-embedded.gltf") print("Loaded textured cube (" .. udname(cube) .. ")") cube:wait_until_loaded() local scenes = cube:scenes() - local cube_scene = scenes[1] + local cube_scene = scenes[1] ]] - local pos = Transform.from_translation(Vec3.new(0, 0, -8.0)) + print("creating vec3") + local v3 = Vec3.new(0, 0, -8.0) + print("creating Transform") + local pos = Transform.from_translation(v3) + print("created Transform") - local e = world:spawn(pos, cube_scene) + print("spawning cube") + local e = world:spawn(pos) print("spawned entity " .. tostring(e)) end diff --git a/lyra-game/src/game.rs b/lyra-game/src/game.rs index 8ccf6c3..c66fef7 100755 --- a/lyra-game/src/game.rs +++ b/lyra-game/src/game.rs @@ -345,13 +345,13 @@ impl Game { /// Start the game pub async fn run(&mut self) { // init logging - let (stdout_layer, _stdout_nb) = non_blocking(std::io::stdout()); + //let (stdout_layer, _stdout_nb) = non_blocking(std::io::stdout()); { let t = tracing_subscriber::registry() - .with(fmt::layer().with_writer(stdout_layer)); + .with(fmt::layer().with_writer(std::io::stdout)); - #[cfg(feature = "tracy")] - let t = t.with(tracing_tracy::TracyLayer::default()); + /* #[cfg(feature = "tracy")] + let t = t.with(tracing_tracy::TracyLayer::default()); */ t.with(filter::Targets::new() // done by prefix, so it includes all lyra subpackages diff --git a/lyra-scripting/lyra-scripting-derive/src/lua_macro.rs b/lyra-scripting/lyra-scripting-derive/src/lua_macro.rs index a4ad512..ac259f0 100644 --- a/lyra-scripting/lyra-scripting-derive/src/lua_macro.rs +++ b/lyra-scripting/lyra-scripting-derive/src/lua_macro.rs @@ -452,11 +452,32 @@ pub fn wrap_lua_struct_impl(input: proc_macro::TokenStream) -> proc_macro::Token quote!() } else { quote! { - builder.method(#FN_NAME_INTERNAL_REFLECT, |_, this, ()| { - Ok(crate::ScriptBorrow::from_component::<#path>(Some(this.0.clone()))) + builder.method(#FN_NAME_INTERNAL_REFLECT, |s, this, ()| { + println!("{} called for {}", #FN_NAME_INTERNAL_REFLECT, stringify!(#type_name)); + + println!("self is {:?}", this.0); + let opt_test = Some(&this.0); + println!("opt_test is {:?}", opt_test); + + println!("creating clone"); + let clone = this.0.clone(); + println!("cloned!"); + + println!("self clone is {:?}", clone); + println!("creating option"); + let opt = Some(clone); + println!("creating borrow"); + let borrow = crate::ScriptBorrow::from_component::<#path>(opt); + println!("into lua"); + let val = borrow.as_lua(s); + + let val = val.expect("Failed to convert"); + + Ok(val) }); builder.function(#FN_NAME_INTERNAL_REFLECT_TYPE, |_, ()| { + println!("{} called for {}", #FN_NAME_INTERNAL_REFLECT_TYPE, stringify!(#type_name)); Ok(crate::ScriptBorrow::from_component::<#path>(None)) }); } diff --git a/lyra-scripting/src/lua/wrappers/math.rs b/lyra-scripting/src/lua/wrappers/math.rs index 6a4bf9e..a5c844e 100644 --- a/lyra-scripting/src/lua/wrappers/math.rs +++ b/lyra-scripting/src/lua/wrappers/math.rs @@ -1,9 +1,11 @@ use lyra_scripting_derive::{lua_vec_wrap_extension, wrap_lua_struct}; -use crate::lyra_engine; -use lyra_game::math; +use crate::{lua::FN_NAME_INTERNAL_REFLECT, lyra_engine}; +use lyra_game::math::{self, Transform}; use crate as lyra_scripting; +use elua::AsLua; + wrap_lua_struct!( math::Vec2, derives(PartialEq, Copy), @@ -175,7 +177,7 @@ wrap_lua_struct!( } ); -wrap_lua_struct!( +/* wrap_lua_struct!( math::Transform, derives(PartialEq, Copy), metamethods(ToString, Eq), @@ -270,4 +272,251 @@ wrap_lua_struct!( Ok(t) }); } -); +); */ + +#[derive(lyra_reflect::Reflect, PartialEq, Clone)] +pub struct LuaTransform(#[reflect(skip)] pub(crate) math::Transform); + +/* impl Clone for LuaTransform { + fn clone(&self) -> Self { + let tran = self.0.translation.clone(); + println!("Cloned translation"); + let scale = self.0.scale.clone(); + println!("Cloned scale"); + let rot = math::Quat::IDENTITY; //self.0.rotation.clone(); + println!("Cloned rot"); + + println!("Returning cloned shit"); + Self(math::Transform { + translation: tran, + rotation: rot, + scale, + }) + } +} */ + +impl std::ops::Deref for LuaTransform { + type Target = math::Transform; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl std::ops::DerefMut for LuaTransform { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl<'lua> elua::FromLua<'lua> for LuaTransform { + fn from_lua( + _lua: &'lua elua::State, + value: elua::Value<'lua>, + ) -> elua::Result { + match value { + elua::Value::Userdata(ud) => Ok(ud.as_ref::()?.clone()), + _ => { + panic!("Attempt to get {0} from a {1} value", "LuaTransform", value.type_name()); + } + } + } +} + +impl elua::Userdata for LuaTransform { + fn name() -> String { + "Transform".to_string() + } + + fn build<'a>(builder: &mut elua::UserdataBuilder<'a, Self>) { + builder + .method( + "__lyra_internal_reflect", + |s, this, ()| { + println!("{} called for {}", FN_NAME_INTERNAL_REFLECT, stringify!(#type_name)); + + println!("self is {:?}", this.0); + let opt_test = Some(&this.0); + println!("opt_test is {:?}", opt_test); + + println!("creating clone"); + let clone = this.clone(); + let clone = clone.0; + println!("cloned!"); + + println!("self clone is {:?}", clone); + println!("creating option"); + let opt = Some(clone); + println!("creating borrow"); + let borrow = crate::ScriptBorrow::from_component::(opt); + println!("into lua"); + let val = borrow.as_lua(s); + + let val = val.expect("Failed to convert"); + + Ok(val) + }, + ); + builder + .function( + "__lyra_internal_reflect_type", + |_, ()| { + println!("__lyra_internal_reflect_type called for Transform"); + Ok(crate::ScriptBorrow::from_component::(None)) + }, + ); + builder + .meta_method( + elua::MetaMethod::ToString, + |_, this, ()| { + Ok(format!("{:?}", this.0)) + }, + ); + builder + .meta_method( + elua::MetaMethod::Eq, + |_, this, (v,): (LuaTransform,)| { Ok(this.0 == v.0) }, + ); + { + builder + .function( + "default", + |_, ()| { Ok(Self(math::Transform::default())) }, + ); + builder + .function( + "new", + |_, (pos, rot, scale): (LuaVec3, LuaQuat, LuaVec3)| { + Ok(Self(math::Transform::new(*pos, *rot, *scale))) + }, + ); + builder + .function( + "from_translation", + |_, (pos,): (LuaVec3,)| { + Ok(Self(math::Transform::from_translation(*pos))) + }, + ); + builder + .function( + "from_xyz", + |_, (x, y, z)| { Ok(Self(math::Transform::from_xyz(x, y, z))) }, + ); + builder.method("clone", |_, this, ()| { Ok(this.clone()) }); + builder.method("forward", |_, this, ()| { Ok(LuaVec3(this.forward())) }); + builder.method("left", |_, this, ()| { Ok(LuaVec3(this.left())) }); + builder.method("up", |_, this, ()| { Ok(LuaVec3(this.up())) }); + builder + .method_mut( + "rotate", + |_, this, (quat,): (LuaQuat,)| { + this.rotate(*quat); + Ok(()) + }, + ); + builder + .method_mut( + "rotate_x", + |_, this, (deg,): (f32,)| { + this.rotate_x(math::Angle::Degrees(deg)); + Ok(()) + }, + ); + builder + .method_mut( + "rotate_y", + |_, this, (deg,): (f32,)| { + this.rotate_y(math::Angle::Degrees(deg)); + Ok(()) + }, + ); + builder + .method_mut( + "rotate_z", + |_, this, (deg,): (f32,)| { + this.rotate_z(math::Angle::Degrees(deg)); + Ok(()) + }, + ); + builder + .method_mut( + "rotate_x_rad", + |_, this, (rad,): (f32,)| { + this.rotate_x(math::Angle::Radians(rad)); + Ok(()) + }, + ); + builder + .method_mut( + "rotate_y_rad", + |_, this, (rad,): (f32,)| { + this.rotate_y(math::Angle::Radians(rad)); + Ok(()) + }, + ); + builder + .method_mut( + "rotate_z_rad", + |_, this, (rad,): (f32,)| { + this.rotate_z(math::Angle::Radians(rad)); + Ok(()) + }, + ); + builder + .method_mut( + "translate", + |_, this, (x, y, z): (f32, f32, f32)| { + this.translate(x, y, z); + Ok(()) + }, + ); + builder + .method( + "lerp", + |_, this, (rhs, alpha): (Self, f32)| { + Ok(Self(this.lerp(*rhs, alpha))) + }, + ); + builder + .meta_method( + elua::MetaMethod::Mul, + |_, this, (quat,): (LuaQuat,)| { + let mut t = this.clone(); + t.rotation *= *quat; + Ok(t) + }, + ); + builder + .meta_method( + elua::MetaMethod::Add, + |_, this, (pos,): (LuaVec3,)| { + let mut t = this.clone(); + t.translation += *pos; + Ok(t) + }, + ); + } + } +} + +impl lyra_scripting::lua::LuaWrapper for LuaTransform { + fn wrapped_type_id() -> std::any::TypeId { + std::any::TypeId::of::() + } +} + +impl<'a> elua::FromLuaVec<'a> for LuaTransform { + fn from_lua_value_vec( + state: &'a elua::State, + mut values: elua::ValueVec<'a>, + ) -> elua::Result { + use elua::FromLua; + if let Some(val) = values.pop_front() { + LuaTransform::from_lua(state, val) + } else { + Err(elua::Error::IncorrectArgCount { + arg_expected: 1, + arg_count: values.len() as i32, + }) + } + } +} \ No newline at end of file