lua: expose FreeFlyCamera

This commit is contained in:
SeanOMik 2024-10-07 16:28:38 -04:00
parent e9cbb48653
commit b90e19161d
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
7 changed files with 56 additions and 16 deletions

View File

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

View File

@ -1,5 +1,6 @@
use glam::{EulerRot, Quat, Vec3}; use glam::{EulerRot, Quat, Vec3};
use lyra_ecs::{query::{Res, View}, Component}; use lyra_ecs::{query::{Res, View}, Component};
use lyra_reflect::Reflect;
use crate::{game::App, input::ActionHandler, plugin::Plugin, DeltaTime}; 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_UP_DOWN: &str = "LookUpDown";
pub const ACTLBL_LOOK_ROLL: &str = "LookRoll"; pub const ACTLBL_LOOK_ROLL: &str = "LookRoll";
#[derive(Clone, Component)] #[derive(Clone, Component, Reflect)]
pub struct FreeFlyCamera { pub struct FreeFlyCamera {
pub speed: f32, pub speed: f32,
pub slow_speed_factor: 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 syn::{braced, parenthesized, parse_macro_input, punctuated::Punctuated, token, Token};
use quote::{quote, ToTokens}; use quote::{quote, ToTokens};
use crate::{field::Field, FN_NAME_INTERNAL_REFLECT, FN_NAME_INTERNAL_REFLECT_TYPE}; 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 { impl syn::parse::Parse for IntoLuaUsage {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let type_path: syn::Path = input.parse()?; 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 { let mut s = Self {
type_path, type_path,
override_name: None, override_name: None,
table_name: String::new(), table_name: lua_name,
derives: vec![], derives: vec![],
fields: vec![], fields: vec![],
create: None, create: None,
@ -226,7 +234,8 @@ pub fn to_lua_struct_impl(input: proc_macro::TokenStream) -> proc_macro::TokenSt
.last() .last()
.expect("Failure to find typename in macro usage!") .expect("Failure to find typename in macro usage!")
.ident; .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(); let derives_iter = input.derives.into_iter();

View File

@ -4,7 +4,7 @@ use lyra_ecs::ResourceObject;
use lyra_reflect::{Reflect, TypeRegistry}; use lyra_reflect::{Reflect, TypeRegistry};
use lyra_resource::gltf::Gltf; 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: //fn register_lua_proxy::<T:
@ -19,12 +19,8 @@ impl ScriptApiProvider for LyraEcsApiProvider {
world.register_lua_wrapper::<LuaSceneHandle>(); world.register_lua_wrapper::<LuaSceneHandle>();
world.register_lua_wrapper::<LuaActionHandler>(); world.register_lua_wrapper::<LuaActionHandler>();
world.register_lua_wrapper::<LuaWindow>(); world.register_lua_wrapper::<LuaWindow>();
world.register_lua_convert::<LuaCamera>(); world.register_lua_convert_component::<LuaCamera>("Camera");
world.register_lua_convert_component::<LuaFreeFlyCamera>("FreeFlyCamera");
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);
let mut registry = world.get_resource_mut::<TypeRegistry>().unwrap(); 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("ActionHandler", ctx.create_proxy::<LuaActionHandler>()?)?;
globals.set("Window", ctx.create_proxy::<LuaWindow>()?)?; globals.set("Window", ctx.create_proxy::<LuaWindow>()?)?;
let cam_table = create_reflect_comp_table::<LuaCamera>(&ctx, "Camera")?; expose_table_wrapper::<LuaCamera>(&ctx, &globals, "Camera")?;
globals.set("Camera", cam_table)?; expose_table_wrapper::<LuaFreeFlyCamera>(&ctx, &globals, "FreeFlyCamera")?;
let dt_table = create_reflect_table::<lyra_game::DeltaTime>(&ctx)?; let dt_table = create_reflect_table::<lyra_game::DeltaTime>(&ctx)?;
globals.set("DeltaTime", dt_table)?; globals.set("DeltaTime", dt_table)?;
@ -103,3 +99,17 @@ where
Ok(table) 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!( to_lua_convert!(
// Struct that is being wrapped[] // Struct that is being wrapped
CameraComponent, CameraComponent,
// Name of wrapping struct // Name of wrapping struct
name=LuaCamera, 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

@ -15,3 +15,6 @@ pub use window::*;
mod camera; mod camera;
pub use camera::*; pub use camera::*;
mod free_fly_camera;
pub use free_fly_camera::*;