1786010767

This commit is contained in:
2026-08-06 12:06:07 +02:00
parent 1b2db4c426
commit 2a07e79233

View File

@@ -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<S> {
state: S,
ctx: Context,
window: Rc<MinimalSoftwareWindow>,
last_time: Instant,
current_time: Instant,
update_timestep: Duration,
@@ -26,10 +33,12 @@ impl<S> Trekstor<S> {
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<S> Trekstor<S> {
}
}
pub fn update(&mut self, update: UpdateFn<S>) {
pub fn update(&mut self) {
loop {
self.current_time = Instant::now();
let dt = self.current_time - self.last_time;
@@ -49,8 +58,28 @@ impl<S> Trekstor<S> {
}
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<S> Trekstor<S> {
}
}
}
impl<S> Platform for Trekstor<S> {
fn create_window_adapter(
&self,
) -> Result<Rc<dyn slint::platform::WindowAdapter>, slint::PlatformError> {
Ok(self.window.clone())
}
// ..
}