implements PLN-0106

This commit is contained in:
2026-06-15 06:00:39 +01:00
parent 1d6b799960
commit 5381ae02b1
3 changed files with 34 additions and 27 deletions
@@ -1,4 +1,3 @@
use prometeu_drivers::hardware::Hardware;
use prometeu_hal::InputSignals;
use winit::event::{ElementState, MouseButton, WindowEvent};
use winit::keyboard::{KeyCode, PhysicalKey};
@@ -14,7 +13,7 @@ struct FbViewport {
}
impl FbViewport {
fn framebuffer_viewport(window: &Window) -> Option<FbViewport> {
fn framebuffer_viewport(window: &Window, display_size: (usize, usize)) -> Option<FbViewport> {
let size = window.inner_size();
let win_w = size.width as f32;
@@ -24,8 +23,8 @@ impl FbViewport {
return None;
}
let fb_w = Hardware::W as f32;
let fb_h = Hardware::H as f32;
let fb_w = display_size.0 as f32;
let fb_h = display_size.1 as f32;
let scale_x = (win_w / fb_w).floor();
let scale_y = (win_h / fb_h).floor();
@@ -44,17 +43,18 @@ impl FbViewport {
pub struct HostInputHandler {
pub signals: InputSignals,
display_size: (usize, usize),
}
impl Default for HostInputHandler {
fn default() -> Self {
Self::new()
Self::new((480, 270))
}
}
impl HostInputHandler {
pub fn new() -> Self {
Self { signals: InputSignals::default() }
pub fn new(display_size: (usize, usize)) -> Self {
Self { signals: InputSignals::default(), display_size }
}
pub fn handle_event(&mut self, event: &WindowEvent, window: &Window) {
@@ -87,7 +87,9 @@ impl HostInputHandler {
}
WindowEvent::CursorMoved { position, .. } => {
if let Some((x, y)) = window_to_fb(position.x as f32, position.y as f32, window) {
if let Some((x, y)) =
window_to_fb(position.x as f32, position.y as f32, window, self.display_size)
{
self.signals.x_pos = x;
self.signals.y_pos = y;
}
@@ -107,8 +109,13 @@ impl HostInputHandler {
_ => {}
}
fn window_to_fb(wx: f32, wy: f32, window: &Window) -> Option<(i32, i32)> {
let viewport = FbViewport::framebuffer_viewport(window)?;
fn window_to_fb(
wx: f32,
wy: f32,
window: &Window,
display_size: (usize, usize),
) -> Option<(i32, i32)> {
let viewport = FbViewport::framebuffer_viewport(window, display_size)?;
let local_x = wx - viewport.x;
let local_y = wy - viewport.y;
@@ -120,7 +127,10 @@ impl HostInputHandler {
let fb_x = (local_x / viewport.scale).floor() as i32;
let fb_y = (local_y / viewport.scale).floor() as i32;
Some((fb_x.clamp(0, Hardware::W as i32 - 1), fb_y.clamp(0, Hardware::H as i32 - 1)))
Some((
fb_x.clamp(0, display_size.0 as i32 - 1),
fb_y.clamp(0, display_size.1 as i32 - 1),
))
}
}
}