commit 8b07082e318e434a35779eef9cf66b844c0925da
parent 339e4bb931f44dd311d51fb7111e6778a6253dff
Author: Ryan Jeffrey <ryan@ryanmj.xyz>
Date: Wed, 12 Oct 2022 09:57:55 -0700
Fix bug where events were consumed before handled
Diffstat:
5 files changed, 114 insertions(+), 71 deletions(-)
diff --git a/src/config.rs b/src/config.rs
@@ -56,6 +56,9 @@ pub struct Config {
/// Height of the window.
#[serde(default = "default_height")]
debug_win_height: u32,
+ /// True if in debug mode, false otherwise.
+ #[serde(default = "default_debug_mode")]
+ debug_mode: bool,
}
// Struct impls.
@@ -82,6 +85,7 @@ impl Config {
win_height: 0,
debug_win_width: 0,
debug_win_height: 0,
+ debug_mode: false,
})
}
@@ -231,6 +235,11 @@ impl Config {
pub fn get_ncpus(&self) -> u32 {
self.ncpu
}
+
+ /// Get the debug mode option.
+ pub fn is_debug_mode(&self) -> bool {
+ self.debug_mode
+ }
}
// Local functions.
@@ -332,3 +341,7 @@ fn default_width() -> u32 {
fn default_height() -> u32 {
900
}
+
+fn default_debug_mode() -> bool {
+ true
+}
diff --git a/src/debug_window.rs b/src/debug_window.rs
@@ -16,6 +16,7 @@
use config::Config;
use sdl::{Context, Drawable, Pane};
use sdl2::event::{Event, WindowEvent};
+use sdl2::keyboard::Keycode;
use sdl2::pixels::*;
use system::System;
use util::Result;
@@ -49,32 +50,10 @@ impl<'a> Drawable for DebugWindow<'a> {
//
self.pane.canvas.present();
}
- fn handle_events(&mut self, context: &mut Context) -> bool {
- let event_pump = &mut context.event_pump;
- let window_id = self.pane.get_id();
- for event in event_pump.poll_iter() {
- match event {
- Event::Quit { .. } => {
- return true;
- }
- Event::Window {
- win_event: WindowEvent::Close,
- window_id: id,
- ..
- } => {
- if id == window_id {
- return true;
- }
- }
- Event::KeyDown {
- keycode: Some(kc), ..
- } => {}
- Event::KeyUp {
- keycode: Some(kc), ..
- } => {}
- _ => {}
- }
- }
- return false;
+
+ fn handle_key_down(&mut self, kc: Keycode) {}
+ fn handle_key_up(&mut self, kc: Keycode) {}
+ fn get_window_id(&self) -> u32 {
+ self.pane.get_id()
}
}
diff --git a/src/main.rs b/src/main.rs
@@ -39,12 +39,85 @@ pub mod windows;
use config::Config;
use debug_window::DebugWindow;
use sdl::{Context, Drawable};
+use sdl2::event::{Event, WindowEvent};
+use sdl2::keyboard::Keycode;
use std::boxed::Box;
use std::error::Error;
use system::System;
use windows::MainWindow;
+
// Struct/enum declarations.
+enum GlobalAction {
+ None,
+ QuitProgram,
+ CloseDebugWindow,
+}
+
+fn handle_events(
+ context: &mut Context,
+ win: &mut MainWindow,
+ debug_window: &mut Option<DebugWindow>,
+) -> GlobalAction {
+ let event_pump = &mut context.event_pump;
+ let mut result = GlobalAction::None;
+ let main_win_id = win.get_window_id();
+ for event in event_pump.poll_iter() {
+ match event {
+ Event::Quit { .. } => {
+ return GlobalAction::QuitProgram;
+ }
+ Event::Window {
+ win_event: WindowEvent::Close,
+ window_id: id,
+ ..
+ } => {
+ if id == main_win_id {
+ return GlobalAction::QuitProgram;
+ } else if let Some(dwin) = debug_window {
+ if dwin.get_window_id() == id {
+ result = GlobalAction::CloseDebugWindow;
+ continue;
+ }
+ }
+ eprintln!("Close for window id {}, but it does not exist!", id);
+ }
+ Event::KeyDown {
+ keycode: Some(kc),
+ window_id: id,
+ ..
+ } => {
+ if id == main_win_id {
+ win.handle_key_down(kc);
+ } else if let Some(dwin) = debug_window {
+ if dwin.get_window_id() == id {
+ dwin.handle_key_down(kc);
+ continue;
+ }
+ }
+ eprintln!("Keydown event for window id {}, but it does not exist!", id);
+ }
+ Event::KeyUp {
+ keycode: Some(kc),
+ window_id: id,
+ ..
+ } => {
+ if id == main_win_id {
+ win.handle_key_up(kc);
+ } else if let Some(dwin) = debug_window {
+ if dwin.get_window_id() == id {
+ dwin.handle_key_up(kc);
+ continue;
+ }
+ }
+ eprintln!("Keyup event for window id {}, but it does not exist!", id);
+ }
+ _ => {}
+ }
+ }
+ return result;
+}
+
fn main() -> Result<(), Box<dyn Error>> {
let config = Config::init()?;
@@ -58,22 +131,22 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut sdl_context = Context::new()?;
let mut main_window = MainWindow::new(&config, &system, &mut sdl_context)?;
- let mut debug_window = Some(DebugWindow::new(&config, &system, &mut sdl_context)?);
+ let mut debug_window = if config.is_debug_mode() {
+ Some(DebugWindow::new(&config, &system, &mut sdl_context)?)
+ } else {
+ None
+ };
'running: loop {
- if main_window.handle_events(&mut sdl_context) {
- break 'running;
- }
- debug_window = if let Some(mut win) = debug_window {
- if win.handle_events(&mut sdl_context) {
- None
- } else {
- Some(win)
+ match handle_events(&mut sdl_context, &mut main_window, &mut debug_window) {
+ GlobalAction::QuitProgram => {
+ break 'running;
}
- } else {
- None
- };
-
+ GlobalAction::CloseDebugWindow => {
+ debug_window = None;
+ }
+ _ => {}
+ }
debug_window = if let Some(mut win) = debug_window {
win.draw(&mut sdl_context);
Some(win)
diff --git a/src/sdl.rs b/src/sdl.rs
@@ -18,7 +18,7 @@
extern crate sdl2;
use config::Config;
-use sdl2::event::Event;
+use sdl2::event::{Event, WindowEvent};
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
use sdl2::rect::Rect;
@@ -34,7 +34,9 @@ use util::Result;
pub trait Drawable {
fn draw(&mut self, context: &mut Context);
- fn handle_events(&mut self, context: &mut Context) -> bool;
+ fn handle_key_down(&mut self, kc: Keycode);
+ fn handle_key_up(&mut self, kc: Keycode);
+ fn get_window_id(&self) -> u32;
}
pub struct Context {
diff --git a/src/windows.rs b/src/windows.rs
@@ -18,6 +18,7 @@
use config::Config;
use sdl::{Context, Drawable, Pane};
use sdl2::event::{Event, WindowEvent};
+use sdl2::keyboard::Keycode;
use sdl2::pixels::*;
use system::System;
use util::Result;
@@ -34,39 +35,14 @@ impl<'a> Drawable for MainWindow<'a> {
fn draw(&mut self, context: &mut Context) {
self.pane.canvas.set_draw_color(Color::RGB(0, 0, 0));
self.pane.canvas.clear();
-
//
self.pane.canvas.present();
}
- fn handle_events(&mut self, context: &mut Context) -> bool {
- // TODO need to segregate events based off of windows ourself in main.
- let event_pump = &mut context.event_pump;
- let window_id = self.pane.get_id();
- for event in event_pump.poll_iter() {
- match event {
- Event::Quit { .. } => {
- return true;
- }
- Event::Window {
- win_event: WindowEvent::Close,
- window_id: id,
- ..
- } => {
- if id == window_id {
- return true;
- }
- }
- Event::KeyDown {
- keycode: Some(kc), ..
- } => {}
- Event::KeyUp {
- keycode: Some(kc), ..
- } => {}
- _ => {}
- }
- }
- return false;
+ fn handle_key_down(&mut self, kc: Keycode) {}
+ fn handle_key_up(&mut self, kc: Keycode) {}
+ fn get_window_id(&self) -> u32 {
+ self.pane.get_id()
}
}