use super::{InputDriver, MouseButton}; use ggez::{ event::{EventHandler, KeyCode, KeyMods, MouseButton as EzBtn}, Context, GameResult, }; /// Incoming event handler core component /// /// An input event handler component that receives events from APIs /// and maps them onto an input subscriber collection. Events can be /// consumed, or passed through priority trees. pub struct InputArbiter { map: Vec>, } impl InputArbiter { pub fn new() -> Self { Self { map: vec![] } } /// Add a new input handler subscription pub(crate) fn add_handler(&mut self, dri: impl InputDriver + 'static) { self.map.push(Box::new(dri)); } } /// Implement the ggez event trait for the input arbiter. These /// function calls map onto a little-input system which maps them /// across event handlers. An event handler is attached to a UI /// element, which was subscribed to a partical type and scope. impl EventHandler for InputArbiter { fn draw(&mut self, _: &mut Context) -> GameResult<()> { Ok(()) } fn update(&mut self, _: &mut Context) -> GameResult<()> { Ok(()) } fn mouse_button_down_event(&mut self, _: &mut Context, button: EzBtn, x: f32, y: f32) { self.map.iter_mut().for_each(|dri| { // TODO: add drag event handling dri.mouse_down(x, y, button.into()); }); } fn mouse_button_up_event(&mut self, _: &mut Context, button: EzBtn, x: f32, y: f32) { self.map.iter_mut().for_each(|dri| { dri.mouse_up(x, y, button.into()); }); } fn mouse_motion_event(&mut self, _ctx: &mut Context, _x: f32, _y: f32, _dx: f32, _dy: f32) {} /// A keyboard button was pressed. /// /// The default implementation of this will call `ggez::event::quit()` /// when the escape key is pressed. If you override this with /// your own event handler you have to re-implment that /// functionality yourself. fn key_down_event( &mut self, ctx: &mut Context, keycode: KeyCode, _keymods: KeyMods, _repeat: bool, ) { } /// A keyboard button was released. fn key_up_event(&mut self, _ctx: &mut Context, _keycode: KeyCode, _keymods: KeyMods) {} /// A unicode character was received, usually from keyboard input. /// This is the intended way of facilitating text input. fn text_input_event(&mut self, _ctx: &mut Context, _character: char) {} }