Expose structs to Lua and write Lua type annotations #28

Merged
SeanOMik merged 15 commits from feat/lua-type-defs into main 2024-10-19 15:17:00 +00:00
7 changed files with 56 additions and 16 deletions
Showing only changes of commit b90e19161d - Show all commits

View File

@ -77,7 +77,7 @@ function on_update()
end, Transform
) ]]
world:view(
--[[ world:view(
---@param c Camera
function (c)
c.transform:translate(0, 0.15 * dt, 0)
@ -86,7 +86,7 @@ function on_update()
return c
end, Camera
)
) ]]
end
--[[ function on_post_update()

View File

@ -1,5 +1,6 @@
use glam::{EulerRot, Quat, Vec3};
use lyra_ecs::{query::{Res, View}, Component};
use lyra_reflect::Reflect;
use crate::{game::App, input::ActionHandler, plugin::Plugin, DeltaTime};
@ -12,7 +13,7 @@ pub const ACTLBL_LOOK_LEFT_RIGHT: &str = "LookLeftRight";
pub const ACTLBL_LOOK_UP_DOWN: &str = "LookUpDown";
pub const ACTLBL_LOOK_ROLL: &str = "LookRoll";
#[derive(Clone, Component)]
#[derive(Clone, Component, Reflect)]
pub struct FreeFlyCamera {
pub speed: f32,
pub slow_speed_factor: f32,

View File

@ -1,3 +1,4 @@
use proc_macro2::Span;
use syn::{braced, parenthesized, parse_macro_input, punctuated::Punctuated, token, Token};
use quote::{quote, ToTokens};
use crate::{field::Field, FN_NAME_INTERNAL_REFLECT, FN_NAME_INTERNAL_REFLECT_TYPE};
@ -116,10 +117,17 @@ struct IntoLuaUsage {
impl syn::parse::Parse for IntoLuaUsage {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let type_path: syn::Path = input.parse()?;
let type_ident = &type_path
.segments
.last()
.expect("Failure to find typename in macro usage!")
.ident;
let lua_name = type_ident.to_string();
let mut s = Self {
type_path,
override_name: None,
table_name: String::new(),
table_name: lua_name,
derives: vec![],
fields: vec![],
create: None,
@ -226,7 +234,8 @@ pub fn to_lua_struct_impl(input: proc_macro::TokenStream) -> proc_macro::TokenSt
.last()
.expect("Failure to find typename in macro usage!")
.ident;
let wrapper = input.override_name.unwrap_or_else(|| syn::Ident::new(format!("Lua{}", type_name.to_string()).as_str(), type_name.span()));
let wrapper = input.override_name
.unwrap_or_else(|| syn::Ident::new(&format!("Lua{}", type_name), Span::call_site()));
let derives_iter = input.derives.into_iter();

View File

@ -4,7 +4,7 @@ use lyra_ecs::ResourceObject;
use lyra_reflect::{Reflect, TypeRegistry};
use lyra_resource::gltf::Gltf;
use crate::{lua::{wrappers::*, LuaContext, LuaTableProxyLookup, LuaWrapper, ReflectLuaProxy, RegisterLuaType, FN_NAME_INTERNAL_REFLECT, FN_NAME_INTERNAL_REFLECT_TYPE}, ScriptApiProvider, ScriptBorrow, ScriptData, ScriptDynamicBundle, ScriptWorldPtr};
use crate::{lua::{wrappers::*, LuaContext, LuaWrapper, ReflectLuaProxy, RegisterLuaType, FN_NAME_INTERNAL_REFLECT, FN_NAME_INTERNAL_REFLECT_TYPE}, ScriptApiProvider, ScriptBorrow, ScriptData, ScriptDynamicBundle, ScriptWorldPtr};
//fn register_lua_proxy::<T:
@ -19,12 +19,8 @@ impl ScriptApiProvider for LyraEcsApiProvider {
world.register_lua_wrapper::<LuaSceneHandle>();
world.register_lua_wrapper::<LuaActionHandler>();
world.register_lua_wrapper::<LuaWindow>();
world.register_lua_convert::<LuaCamera>();
let mut lookup = world.get_resource_or_default::<LuaTableProxyLookup>();
lookup.comp_info_from_name.insert("Camera".into(), lyra_ecs::ComponentInfo::new::<lyra_game::scene::CameraComponent>());
lookup.typeid_from_name.insert("Camera".into(), std::any::TypeId::of::<lyra_game::scene::CameraComponent>());
drop(lookup);
world.register_lua_convert_component::<LuaCamera>("Camera");
world.register_lua_convert_component::<LuaFreeFlyCamera>("FreeFlyCamera");
let mut registry = world.get_resource_mut::<TypeRegistry>().unwrap();
@ -57,8 +53,8 @@ impl ScriptApiProvider for LyraEcsApiProvider {
globals.set("ActionHandler", ctx.create_proxy::<LuaActionHandler>()?)?;
globals.set("Window", ctx.create_proxy::<LuaWindow>()?)?;
let cam_table = create_reflect_comp_table::<LuaCamera>(&ctx, "Camera")?;
globals.set("Camera", cam_table)?;
expose_table_wrapper::<LuaCamera>(&ctx, &globals, "Camera")?;
expose_table_wrapper::<LuaFreeFlyCamera>(&ctx, &globals, "FreeFlyCamera")?;
let dt_table = create_reflect_table::<lyra_game::DeltaTime>(&ctx)?;
globals.set("DeltaTime", dt_table)?;
@ -102,4 +98,18 @@ where
Ok(table)
}
/// Expose a wrapper that converts to/from a lua type.
///
/// This creates the reflection functions on a table specified in globals.
/// The table name is set to `name`, which is also how the script will use the table.
fn expose_table_wrapper<T>(lua: &mlua::Lua, globals: &mlua::Table, name: &str) -> mlua::Result<()>
where
T: LuaWrapper + mlua::FromLua,
T::Wrap: lyra_ecs::Component + Reflect
{
let table = create_reflect_comp_table::<T>(&lua, name)?;
globals.set(name, table)?;
Ok(())
}

View File

@ -18,7 +18,7 @@ fn projection_mode_from_str(s: &str) -> Option<CameraProjectionMode> {
}
to_lua_convert!(
// Struct that is being wrapped[]
// Struct that is being wrapped
CameraComponent,
// Name of wrapping struct
name=LuaCamera,

View File

@ -0,0 +1,17 @@
use crate::{lua::LuaWrapper, ScriptBorrow};
use lyra_game::scene::FreeFlyCamera;
use lyra_scripting_derive::to_lua_convert;
to_lua_convert!(
// Struct that is being wrapped
FreeFlyCamera,
// Reflection type, can be 'component' or 'resource'
reflect=component,
fields={
speed: f32,
slow_speed_factor: f32,
look_speed: f32,
mouse_sensitivity: f32,
look_with_keys: bool,
}
);

View File

@ -14,4 +14,7 @@ mod window;
pub use window::*;
mod camera;
pub use camera::*;
pub use camera::*;
mod free_fly_camera;
pub use free_fly_camera::*;