diff --git a/src/trekstor.rs b/src/trekstor.rs index 4fa6815..f77c822 100644 --- a/src/trekstor.rs +++ b/src/trekstor.rs @@ -1,6 +1,12 @@ +use std::rc::Rc; use std::thread::sleep; use std::time::{Duration, Instant}; +use evdev::KeyCode; +use slint::LogicalPosition; +use slint::platform::software_renderer::{MinimalSoftwareWindow, RepaintBufferType}; +use slint::platform::{Platform, WindowEvent}; + use self::input::InputState; use self::screen::Screen; @@ -17,6 +23,7 @@ pub struct Context { pub struct Trekstor { state: S, ctx: Context, + window: Rc, last_time: Instant, current_time: Instant, update_timestep: Duration, @@ -26,10 +33,12 @@ impl Trekstor { pub fn new(update_fps: i32, state: S) -> Self { let screen = Screen::new(); let input = InputState::new(&screen); + let window = MinimalSoftwareWindow::new(RepaintBufferType::ReusedBuffer); Self { state, ctx: Context { screen, input }, + window, current_time: Instant::now(), last_time: Instant::now(), update_timestep: Duration::from_nanos( @@ -38,7 +47,7 @@ impl Trekstor { } } - pub fn update(&mut self, update: UpdateFn) { + pub fn update(&mut self) { loop { self.current_time = Instant::now(); let dt = self.current_time - self.last_time; @@ -49,8 +58,28 @@ impl Trekstor { } self.ctx.input.handle_event(); - if !update(&mut self.ctx, &mut self.state) { - return; + + slint::platform::update_timers_and_animations(); + + if self.ctx.input.is_just_pressed(KeyCode::BTN_TOUCH) { + self.window.try_dispatch_event(WindowEvent::PointerPressed { + position: LogicalPosition::new( + self.ctx.input.x as f32, + self.ctx.input.y as f32, + ), + button: slint::platform::PointerEventButton::Left, + }); + } + + if self.ctx.input.is_just_released(KeyCode::BTN_TOUCH) { + self.window + .try_dispatch_event(WindowEvent::PointerReleased { + position: LogicalPosition::new( + self.ctx.input.x as f32, + self.ctx.input.y as f32, + ), + button: slint::platform::PointerEventButton::Left, + }); } self.ctx.screen.flush(); @@ -58,3 +87,12 @@ impl Trekstor { } } } + +impl Platform for Trekstor { + fn create_window_adapter( + &self, + ) -> Result, slint::PlatformError> { + Ok(self.window.clone()) + } + // .. +}