Expose structs to Lua and write Lua type annotations #28
|
@ -3,6 +3,7 @@ use std::{collections::VecDeque, sync::Arc};
|
||||||
use async_std::task::block_on;
|
use async_std::task::block_on;
|
||||||
use glam::{DVec2, IVec2, UVec2};
|
use glam::{DVec2, IVec2, UVec2};
|
||||||
use lyra_ecs::Entity;
|
use lyra_ecs::Entity;
|
||||||
|
use lyra_reflect::Reflect;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use tracing::{debug, error, warn};
|
use tracing::{debug, error, warn};
|
||||||
use winit::{
|
use winit::{
|
||||||
|
@ -22,9 +23,11 @@ use super::WindowOptions;
|
||||||
|
|
||||||
/// A struct that contains a [`DeviceEvent`](winit::event::DeviceEvent) with its source
|
/// A struct that contains a [`DeviceEvent`](winit::event::DeviceEvent) with its source
|
||||||
/// [`DeviceId`](winit::event::DeviceId).
|
/// [`DeviceId`](winit::event::DeviceId).
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Reflect)]
|
||||||
pub struct DeviceEventPair {
|
pub struct DeviceEventPair {
|
||||||
|
#[reflect(skip)]
|
||||||
pub device_src: winit::event::DeviceId,
|
pub device_src: winit::event::DeviceId,
|
||||||
|
#[reflect(skip)]
|
||||||
pub event: winit::event::DeviceEvent,
|
pub event: winit::event::DeviceEvent,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -132,10 +132,18 @@ pub trait RegisterLuaType {
|
||||||
where
|
where
|
||||||
T: Clone + mlua::FromLua + mlua::IntoLua + LuaWrapper + 'static;
|
T: Clone + mlua::FromLua + mlua::IntoLua + LuaWrapper + 'static;
|
||||||
|
|
||||||
|
/// Registers a type that can be converted to and from lua and adds a lookup entry.
|
||||||
|
///
|
||||||
|
/// This is a shortcut for `register_lua_convert` and `add_component_lookup_entry`.
|
||||||
fn register_lua_convert_component<T>(&mut self, name: &str)
|
fn register_lua_convert_component<T>(&mut self, name: &str)
|
||||||
where
|
where
|
||||||
T: Clone + mlua::FromLua + mlua::IntoLua + LuaWrapper + 'static,
|
T: Clone + mlua::FromLua + mlua::IntoLua + LuaWrapper + 'static,
|
||||||
T::Wrap: lyra_ecs::Component + Reflect;
|
T::Wrap: lyra_ecs::Component + Reflect;
|
||||||
|
|
||||||
|
/// Add an entry for a component in the [`TypeLookup`] table.
|
||||||
|
fn add_component_lookup_entry<T>(&mut self, name: &str)
|
||||||
|
where
|
||||||
|
T: lyra_ecs::Component;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RegisterLuaType for World {
|
impl RegisterLuaType for World {
|
||||||
|
@ -177,10 +185,16 @@ impl RegisterLuaType for World {
|
||||||
T::Wrap: lyra_ecs::Component + Reflect
|
T::Wrap: lyra_ecs::Component + Reflect
|
||||||
{
|
{
|
||||||
self.register_lua_convert::<T>();
|
self.register_lua_convert::<T>();
|
||||||
|
self.add_component_lookup_entry::<T::Wrap>(name);
|
||||||
|
}
|
||||||
|
|
||||||
let mut lookup = self.get_resource_or_default::<LuaTableProxyLookup>();
|
fn add_component_lookup_entry<T>(&mut self, name: &str)
|
||||||
lookup.comp_info_from_name.insert(name.into(), lyra_ecs::ComponentInfo::new::<T::Wrap>());
|
where
|
||||||
lookup.typeid_from_name.insert(name.into(), std::any::TypeId::of::<T::Wrap>());
|
T: lyra_ecs::Component
|
||||||
|
{
|
||||||
|
let mut lookup = self.get_resource_or_default::<TypeLookup>();
|
||||||
|
lookup.comp_info_from_name.insert(name.into(), lyra_ecs::ComponentInfo::new::<T>());
|
||||||
|
lookup.typeid_from_name.insert(name.into(), std::any::TypeId::of::<T>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -58,9 +58,11 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A struct that is used for retrieving rust type ids of types that implement `TableProxy`.
|
/// ECS resource that can be used to lookup types via name.
|
||||||
|
///
|
||||||
|
/// You can get the [`TypeId`] of the type via name, or the [`ComponentInfo`].
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct LuaTableProxyLookup {
|
pub struct TypeLookup {
|
||||||
pub(crate) typeid_from_name: HashMap<String, TypeId>,
|
pub(crate) typeid_from_name: HashMap<String, TypeId>,
|
||||||
pub(crate) comp_info_from_name: HashMap<String, ComponentInfo>,
|
pub(crate) comp_info_from_name: HashMap<String, ComponentInfo>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ use lyra_resource::ResourceManager;
|
||||||
use mlua::{IntoLua, ObjectLike};
|
use mlua::{IntoLua, ObjectLike};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
reflect_user_data, wrappers::LuaResHandleToComponent, Error, LuaTableProxyLookup, ReflectLuaProxy, ReflectedIterator, FN_NAME_INTERNAL_AS_COMPONENT, FN_NAME_INTERNAL_REFLECT, FN_NAME_INTERNAL_REFLECT_TYPE
|
reflect_user_data, wrappers::LuaResHandleToComponent, Error, TypeLookup, ReflectLuaProxy, ReflectedIterator, FN_NAME_INTERNAL_AS_COMPONENT, FN_NAME_INTERNAL_REFLECT, FN_NAME_INTERNAL_REFLECT_TYPE
|
||||||
};
|
};
|
||||||
|
|
||||||
impl mlua::FromLua for ScriptEntity {
|
impl mlua::FromLua for ScriptEntity {
|
||||||
|
@ -115,7 +115,7 @@ impl mlua::UserData for ScriptWorldPtr {
|
||||||
mlua::Value::Table(t) => {
|
mlua::Value::Table(t) => {
|
||||||
let name: String = t.get(mlua::MetaMethod::Type.name())?;
|
let name: String = t.get(mlua::MetaMethod::Type.name())?;
|
||||||
|
|
||||||
let lookup = world.get_resource::<LuaTableProxyLookup>().ok_or(
|
let lookup = world.get_resource::<TypeLookup>().ok_or(
|
||||||
mlua::Error::runtime(
|
mlua::Error::runtime(
|
||||||
"Unable to lookup table proxy, none were ever registered!",
|
"Unable to lookup table proxy, none were ever registered!",
|
||||||
),
|
),
|
||||||
|
@ -195,7 +195,7 @@ impl mlua::UserData for ScriptWorldPtr {
|
||||||
mlua::Value::Table(tbl) => {
|
mlua::Value::Table(tbl) => {
|
||||||
let name: String = tbl.get(mlua::MetaMethod::Type.name())?;
|
let name: String = tbl.get(mlua::MetaMethod::Type.name())?;
|
||||||
|
|
||||||
let lookup = world.get_resource::<LuaTableProxyLookup>().unwrap();
|
let lookup = world.get_resource::<TypeLookup>().unwrap();
|
||||||
*lookup.typeid_from_name.get(&name).unwrap()
|
*lookup.typeid_from_name.get(&name).unwrap()
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
|
Loading…
Reference in New Issue