From 856f24e65e8adae2acf5ff868bf29d6e40d5fb38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=BCbner?= Date: Tue, 4 Mar 2025 15:00:59 +0100 Subject: [PATCH] Use new ui crate version --- context/src/core/context.rs | 4 +- engine/src/engine/engine.rs | 10 ++-- engine/src/engine/engine_event_handling.rs | 24 ++++----- engine/src/scene/scene/scene_base.rs | 4 +- loading-screen/src/loadingscreen.rs | 8 +-- lua-wrapper/src/lib.rs | 61 +++++++--------------- presentation/src/traits.rs | 4 +- skybox/src/lib.rs | 4 +- 8 files changed, 49 insertions(+), 70 deletions(-) diff --git a/context/src/core/context.rs b/context/src/core/context.rs index fae29df..4a49a3a 100644 --- a/context/src/core/context.rs +++ b/context/src/core/context.rs @@ -57,10 +57,10 @@ impl Context { &mut self.sound_handler } - pub fn next_frame(&mut self, world: &mut World, mut f: F) -> Result + pub fn next_frame<'a, C, F>(&mut self, world: &mut World, mut f: F) -> Result where C: Send + Sync + 'static, - F: FnMut(&mut World, &mut C, Event<'_>) -> Result<()> + Send + Sync + 'static, + F: FnMut(&mut World, &mut C, Event<'_>) -> Result<()> + Send + Sync + 'a, { let render_core = self.render_core.clone(); let consumer = world.resources.get_mut_unchecked::(); diff --git a/engine/src/engine/engine.rs b/engine/src/engine/engine.rs index 6ad4a52..297726b 100644 --- a/engine/src/engine/engine.rs +++ b/engine/src/engine/engine.rs @@ -21,11 +21,11 @@ impl TScene for GuiHandlerRenderer { buffer_recorder: &mut CommandBufferRecorder<'_>, _images: &TargetMode>>, indices: &TargetMode, - world: &World, + world: &mut World, ) -> Result<()> { world .resources - .get::>() + .get_mut::() .process(buffer_recorder, indices) } @@ -34,9 +34,9 @@ impl TScene for GuiHandlerRenderer { window_width: f32, window_height: f32, images: &TargetMode>>, - world: &World, + world: &mut World, ) -> Result<()> { - world.resources.get::>().resize( + world.resources.get_mut::().resize( window_width as u32, window_height as u32, images, @@ -249,7 +249,7 @@ impl Engine { impl Engine { fn main_system(world: &mut World) -> Result { - let gui_handler = world.resources.get_unchecked::>(); + let gui_handler = world.resources.get_mut_unchecked::(); let input_map = world.resources.get_unchecked::(); let context = world.resources.get_unchecked::(); diff --git a/engine/src/engine/engine_event_handling.rs b/engine/src/engine/engine_event_handling.rs index a0b79fb..7b3bcb7 100644 --- a/engine/src/engine/engine_event_handling.rs +++ b/engine/src/engine/engine_event_handling.rs @@ -39,7 +39,7 @@ impl Engine { pub(crate) fn event( world: &mut World, context: &Context, - gui_handler: &GuiHandler, + gui_handler: &mut GuiHandler, input: &InputMap, consumer: &mut T, event: Event<'_>, @@ -78,7 +78,7 @@ impl Engine { Self::button_up_event(world, consumer, controller, button)?; } Event::ControllerAxis(controller) => { - if !gui_handler.check_navigatable()? { + if !gui_handler.check_navigatable() { Self::axis_event(world, consumer, &controller)? } else { gui_handler.update_selection(controller.direction())?; @@ -140,7 +140,7 @@ impl Engine { #[inline] fn key_up_event( world: &mut World, - gui_handler: &GuiHandler, + gui_handler: &mut GuiHandler, input: &InputMap, consumer: &mut T, keycode: Keycode, @@ -160,7 +160,7 @@ impl Engine { fn key_down_event( world: &mut World, context: &Context, - gui_handler: &GuiHandler, + gui_handler: &mut GuiHandler, input: &InputMap, consumer: &mut T, keycode: Keycode, @@ -188,9 +188,9 @@ impl Engine { } } Keycode::V => { - if let Some(writeable) = gui_handler.writeable()? { + if let Some(writeable) = gui_handler.writeable() { if let Some(content) = context.window_config().clipboard_content()? { - writeable.set_text(content)?; + writeable.set_text(gui_handler, content)?; } } } @@ -218,12 +218,12 @@ impl Engine { #[inline] fn button_down_event( world: &mut World, - gui_handler: &GuiHandler, + gui_handler: &mut GuiHandler, consumer: &mut T, controller: &Controller, button: ControllerButton, ) -> Result<()> { - if gui_handler.check_navigatable()? { + if gui_handler.check_navigatable() { Self::check_button_down(world, gui_handler, consumer, controller, button)?; } else { consumer.event(world, EngineEvent::ControllerButtonDown(controller, button))?; @@ -249,7 +249,7 @@ impl Engine { #[inline] fn check_button_down( world: &mut World, - gui_handler: &GuiHandler, + gui_handler: &mut GuiHandler, consumer: &mut T, controller: &Controller, button: ControllerButton, @@ -338,10 +338,10 @@ impl Engine { } #[inline] - fn text_input(gui_handler: &GuiHandler, text: String) -> Result<()> { - if let Some(writeable) = gui_handler.writeable()? { + fn text_input(gui_handler: &mut GuiHandler, text: String) -> Result<()> { + if let Some(writeable) = gui_handler.writeable() { for c in text.chars() { - writeable.add_letter(c)?; + writeable.add_letter(gui_handler, c)?; } } diff --git a/engine/src/scene/scene/scene_base.rs b/engine/src/scene/scene/scene_base.rs index 960f798..e7d28d8 100644 --- a/engine/src/scene/scene/scene_base.rs +++ b/engine/src/scene/scene/scene_base.rs @@ -383,7 +383,7 @@ impl TScene for Scene { buffer_recorder: &mut CommandBufferRecorder<'_>, images: &TargetMode>>, indices: &TargetMode, - world: &World, + world: &mut World, ) -> anyhow::Result<()> { self.frame_time = { let now = world.now(); @@ -479,7 +479,7 @@ impl TScene for Scene { window_width: f32, window_height: f32, images: &TargetMode>>, - _world: &World, + _world: &mut World, ) -> anyhow::Result<()> { self.screen_width = window_width; self.screen_height = window_height; diff --git a/loading-screen/src/loadingscreen.rs b/loading-screen/src/loadingscreen.rs index 3bb3e46..3df443b 100644 --- a/loading-screen/src/loadingscreen.rs +++ b/loading-screen/src/loadingscreen.rs @@ -16,20 +16,22 @@ pub struct LoadingScreen { } impl LoadingScreen { - pub fn load(gui: Arc, loader: L) -> Result + pub fn load(gui_handler: &mut GuiHandler, gui: Arc, loader: L) -> Result where R: Fn(T) -> Result<()> + Send + Sync + 'static, T: Send + Sync + 'static, L: FnOnce() -> T + Send + Sync + 'static, G: TopLevelGui + TopGui + Send + Sync + 'static, { - gui.enable()?; + gui.enable(gui_handler)?; let (sender, receiver) = channel(); thread::spawn(move || { let _ = sender.send(loader()); - let _ = gui.disable(); + + // TODO: disable loading screen gui + // let _ = gui.disable(); }); Ok(Self { diff --git a/lua-wrapper/src/lib.rs b/lua-wrapper/src/lib.rs index b7b8020..77c7913 100644 --- a/lua-wrapper/src/lib.rs +++ b/lua-wrapper/src/lib.rs @@ -4,95 +4,81 @@ use assetpath::AssetPath; use mlua::{FromLua, FromLuaMulti, Function, IntoLua, IntoLuaMulti, Lua, Table as MLuaTable}; pub struct Globals<'a> { - table: MLuaTable<'a>, + table: MLuaTable, lua: &'a Lua, } impl<'a> Globals<'a> { - pub fn set, V: IntoLua<'a>>( - &self, - key: K, - value: V, - ) -> Result<(), anyhow::Error> { + pub fn set(&self, key: K, value: V) -> Result<(), anyhow::Error> { self.table.set(key, value)?; Ok(()) } - pub fn get, V: FromLua<'a>>(&self, key: K) -> Result { + pub fn get(&self, key: K) -> Result { Ok(self.table.get(key)?) } - pub fn create_table(&self) -> Result, anyhow::Error> { + pub fn create_table(&self) -> Result { Ok(Table { table: self.lua.create_table()?, }) } - pub fn set_table(&self, name: &str, table: Table<'_>) -> Result<(), anyhow::Error> { + pub fn set_table(&self, name: &str, table: Table) -> Result<(), anyhow::Error> { self.set(name, table.table)?; Ok(()) } - pub fn get_table(&self, name: &str) -> Result, anyhow::Error> { + pub fn get_table(&self, name: &str) -> Result { Ok(Table { table: self.get(name)?, }) } } -pub struct Table<'a> { - table: MLuaTable<'a>, +pub struct Table { + table: MLuaTable, } -impl<'a> Table<'a> { - pub fn set, V: IntoLua<'a>>( - &self, - key: K, - value: V, - ) -> Result<(), anyhow::Error> { +impl Table { + pub fn set(&self, key: K, value: V) -> Result<(), anyhow::Error> { self.table.set(key, value)?; Ok(()) } - pub fn get, V: FromLua<'a>>(&self, key: K) -> Result { + pub fn get(&self, key: K) -> Result { Ok(self.table.get(key)?) } } pub struct LuaFunction { - lua: Option<&'static Lua>, - function: ManuallyDrop>, + lua: Lua, + function: ManuallyDrop, } impl LuaFunction { pub fn new(path: &AssetPath) -> Result { - // into_static leaks the object - let lua = Lua::new().into_static(); + let lua = Lua::new(); let function = ManuallyDrop::new( lua.load(fs::read_to_string(&path.full_path())?.as_bytes()) .eval()?, ); - Ok(Self { - lua: Some(lua), - function, - }) + Ok(Self { lua, function }) } pub fn globals(&self, mut change_globals: F) -> Result<(), anyhow::Error> where - F: FnMut(&Globals<'_>) -> Result<(), E>, + F: FnMut(&Globals) -> Result<(), E>, E: std::error::Error + Send + Sync + 'static, { - let lua = self.lua.as_ref().unwrap(); - let globals = Globals { - table: lua.globals(), - lua, + table: self.lua.globals(), + lua: &self.lua, }; change_globals(&globals)?; @@ -100,10 +86,7 @@ impl LuaFunction { Ok(()) } - pub fn execute<'a, A: IntoLuaMulti<'a>, R: FromLuaMulti<'a>>( - &self, - input: A, - ) -> Result { + pub fn execute(&self, input: A) -> Result { Ok(self.function.call(input)?) } } @@ -118,11 +101,5 @@ impl Drop for LuaFunction { unsafe { ManuallyDrop::drop(&mut self.function); } - - if let Some(lua) = self.lua.take() { - // we need to drop it, due to leakage from static cast - let l = unsafe { Lua::from_static(lua) }; - drop(l); - } } } diff --git a/presentation/src/traits.rs b/presentation/src/traits.rs index 9c66da6..1208c5a 100644 --- a/presentation/src/traits.rs +++ b/presentation/src/traits.rs @@ -17,7 +17,7 @@ pub trait TScene: Send + Sync + 'static { buffer_recorder: &mut CommandBufferRecorder<'_>, images: &TargetMode>>, indices: &TargetMode, - world: &World, + world: &mut World, ) -> Result<()>; fn resize( @@ -25,7 +25,7 @@ pub trait TScene: Send + Sync + 'static { window_width: f32, window_height: f32, images: &TargetMode>>, - world: &World, + world: &mut World, ) -> Result<()>; } diff --git a/skybox/src/lib.rs b/skybox/src/lib.rs index ba022e7..4d66291 100644 --- a/skybox/src/lib.rs +++ b/skybox/src/lib.rs @@ -266,7 +266,7 @@ impl TScene for SkyBox { buffer_recorder: &mut CommandBufferRecorder<'_>, _images: &TargetMode>>, indices: &TargetMode, - _world: &World, + _world: &mut World, ) -> Result<()> { if !self.enabled { return Ok(()); @@ -298,7 +298,7 @@ impl TScene for SkyBox { _window_width: f32, _window_height: f32, _images: &TargetMode>>, - world: &World, + world: &mut World, ) -> Result<()> { let sample_count = world .resources