diff --git a/src/api.rs b/src/api.rs index 9ba5c5c..745ea8d 100644 --- a/src/api.rs +++ b/src/api.rs @@ -61,6 +61,7 @@ pub fn register_globals(lua: &Lua) -> LuaResult<()> { // general globals.set("GetTime", lua.create_function(get_time)?)?; globals.set("StripEscapes", lua.create_function(strip_escapes)?)?; + globals.set("Exit", lua.create_function(exit)?)?; globals.set("Restart", lua.create_function(restart)?)?; globals.set("OpenURL", lua.create_function(open_url)?)?; globals.set("RenderInit", lua.create_function(render_init)?)?; @@ -128,6 +129,15 @@ fn strip_escapes(_: &Lua, text: String) -> LuaResult { Ok(PoBString(&text).strip_escapes()) } +fn exit(l: &Lua, exit_msg: Option) -> LuaResult<()> { + if let Some(exit_msg) = exit_msg { + println!("{exit_msg}"); + } + let ctx = l.app_data_ref::<&'static Context>().unwrap(); + *ctx.should_exit() = true; + Ok(()) +} + fn restart(l: &Lua, _: ()) -> LuaResult<()> { let ctx = l.app_data_ref::<&'static Context>().unwrap(); *ctx.needs_restart() = true; diff --git a/src/app.rs b/src/app.rs index ebce02b..a3512cf 100644 --- a/src/app.rs +++ b/src/app.rs @@ -29,6 +29,7 @@ pub struct AppState { pub fonts: Fonts, pub texture_manager: WrappedTextureManager, pub script_dir: PathBuf, + pub should_exit: bool, } impl AppState { @@ -59,6 +60,7 @@ impl App { fonts: Fonts::new(pob_font_definitions()), texture_manager: WrappedTextureManager::new(), script_dir, + should_exit: false, }; let current_mode = if uses_custom_script_dir { @@ -188,8 +190,10 @@ impl ApplicationHandler for App { ) { match event { WindowEvent::CloseRequested => { - self.handle_event(AppEvent::Exit); - event_loop.exit(); + self.state.should_exit = self.current_mode.can_exit(&mut self.state); + if !self.state.should_exit { + self.state.window.request_redraw(); + } } WindowEvent::RedrawRequested => { profiling::scope!("RedrawRequested"); @@ -200,6 +204,12 @@ impl ApplicationHandler for App { return; } + if self.state.should_exit { + self.handle_event(AppEvent::Exit); + event_loop.exit(); + return; + } + if self.needs_reconfigure { if let Some(ref mut gfx) = self.gfx_context { let size = gfx.window.inner_size(); diff --git a/src/lua.rs b/src/lua.rs index d061daa..d47ca98 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -52,6 +52,7 @@ pub struct Context { current_working_dir: Cell<*mut PathBuf>, layers: Cell<*mut Layers>, needs_restart: Cell<*mut bool>, + should_exit: Cell<*mut bool>, is_dpi_aware: Cell<*mut bool>, } @@ -66,6 +67,7 @@ impl Context { current_working_dir: Cell::new(std::ptr::null_mut()), layers: Cell::new(std::ptr::null_mut()), needs_restart: Cell::new(std::ptr::null_mut()), + should_exit: Cell::new(std::ptr::null_mut()), is_dpi_aware: Cell::new(std::ptr::null_mut()), })) } @@ -80,6 +82,7 @@ impl Context { .set(&mut ctx.pob.current_working_dir); self.layers.set(&mut ctx.pob.layers); self.needs_restart.set(&mut ctx.pob.needs_restart); + self.should_exit.set(&mut ctx.app.should_exit); self.is_dpi_aware.set(&mut ctx.pob.is_dpi_aware); } @@ -92,6 +95,7 @@ impl Context { self.current_working_dir.set(std::ptr::null_mut()); self.layers.set(std::ptr::null_mut()); self.needs_restart.set(std::ptr::null_mut()); + self.should_exit.set(std::ptr::null_mut()); self.is_dpi_aware.set(std::ptr::null_mut()); } @@ -103,6 +107,7 @@ impl Context { ctx_accessor!(current_working_dir: &mut PathBuf); ctx_accessor!(layers: &mut Layers); ctx_accessor!(needs_restart: &mut bool); + ctx_accessor!(should_exit: &mut bool); ctx_accessor!(is_dpi_aware: &mut bool); } @@ -259,6 +264,18 @@ impl LuaInstance { list_func.call::(()) } + pub fn can_exit(&self, pob_ctx: &mut PoBContext) -> bool { + let ctx = self.lua.app_data_ref::<&'static Context>().unwrap(); + ctx.set(pob_ctx); + + let can_exit = get_callback(&self.lua, "CanExit") + .and_then(|f| f.call(())) + .unwrap_or(true); + + ctx.clear(); + can_exit + } + pub fn handle_event(&self, event: PoBEvent, pob_ctx: &mut PoBContext) -> LuaResult<()> { profiling::scope!("handle_event", format!("{}", event)); diff --git a/src/mode.rs b/src/mode.rs index 1895134..2da4478 100644 --- a/src/mode.rs +++ b/src/mode.rs @@ -65,4 +65,11 @@ impl AppMode { AppMode::PoB(mode) => mode.handle_event(state, event), } } + + pub fn can_exit(&mut self, state: &mut AppState) -> bool { + match self { + AppMode::Install(_) => true, + AppMode::PoB(mode) => mode.can_exit(state), + } + } } diff --git a/src/pob.rs b/src/pob.rs index 7ea3624..a219e21 100644 --- a/src/pob.rs +++ b/src/pob.rs @@ -147,6 +147,11 @@ impl PoBMode { Ok(()) } + pub fn can_exit(&mut self, app_state: &mut AppState) -> bool { + let mut ctx = PoBContext::new(app_state, &mut self.state); + self.lua_instance.can_exit(&mut ctx) + } + fn reset_viewport(&mut self, size: LogicalSize) { self.state .layers