use prometeu_drivers::hardware::Hardware; use prometeu_hal::InputSignals; use winit::event::{ElementState, MouseButton, WindowEvent}; use winit::keyboard::{KeyCode, PhysicalKey}; use winit::window::Window; pub struct HostInputHandler { pub signals: InputSignals, } impl Default for HostInputHandler { fn default() -> Self { Self::new() } } impl HostInputHandler { pub fn new() -> Self { Self { signals: InputSignals::default() } } pub fn handle_event(&mut self, event: &WindowEvent, window: &Window) { match event { WindowEvent::KeyboardInput { event, .. } => { if let PhysicalKey::Code(code) = event.physical_key { let is_down = event.state == ElementState::Pressed; match code { KeyCode::ArrowUp => self.signals.up_signal = is_down, KeyCode::ArrowDown => self.signals.down_signal = is_down, KeyCode::ArrowLeft => self.signals.left_signal = is_down, KeyCode::ArrowRight => self.signals.right_signal = is_down, KeyCode::KeyA => self.signals.a_signal = is_down, KeyCode::KeyD => self.signals.b_signal = is_down, KeyCode::KeyW => self.signals.x_signal = is_down, KeyCode::KeyS => self.signals.y_signal = is_down, KeyCode::KeyQ => self.signals.l_signal = is_down, KeyCode::KeyE => self.signals.r_signal = is_down, KeyCode::KeyZ => self.signals.start_signal = is_down, KeyCode::ShiftLeft | KeyCode::ShiftRight => { self.signals.select_signal = is_down } _ => {} } } } WindowEvent::CursorMoved { position, .. } => { let v = window_to_fb(position.x as f32, position.y as f32, window); self.signals.x_pos = v.0; self.signals.y_pos = v.1; } WindowEvent::MouseInput { state, button, .. } => { if *button == MouseButton::Left { match state { ElementState::Pressed => { self.signals.f_signal = true; } ElementState::Released => { self.signals.f_signal = false; } } } } _ => {} } } } pub fn window_to_fb(wx: f32, wy: f32, window: &Window) -> (i32, i32) { let size = window.inner_size(); let fb_w = Hardware::W as f32; let fb_h = Hardware::H as f32; let x = (wx * fb_w / size.width as f32).floor() as i32; let y = (wy * fb_h / size.height as f32).floor() as i32; (x.clamp(0, Hardware::W as i32 - 1), y.clamp(0, Hardware::H as i32 - 1)) }