This commit is contained in:
2026-02-14 18:31:46 +01:00
parent 87490152bd
commit 81971410ce
4 changed files with 74 additions and 51 deletions

View File

@@ -3,65 +3,34 @@ pub(super) mod widgets;
use std::collections::VecDeque; use std::collections::VecDeque;
use embedded_graphics::{ use g13_driver::{G13, G13Event};
Drawable,
draw_target::DrawTarget, use crate::app::pages::StartPage;
mono_font::{MonoTextStyle, ascii::FONT_6X10},
pixelcolor::BinaryColor,
prelude::Point,
text::{Alignment, Baseline, LineHeight, Text, TextStyle, TextStyleBuilder},
};
use g13_driver::{G13Event, Vec2};
use super::app::widgets::Page; use super::app::widgets::Page;
type Color = BinaryColor;
const CHARACTER_STYLE: MonoTextStyle<'static, BinaryColor> =
MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
const TEXT_STYLE: TextStyle = TextStyleBuilder::new()
.baseline(Baseline::Top)
.alignment(Alignment::Left)
.build();
pub struct App { pub struct App {
pages: VecDeque<Box<dyn Page>>, pages: VecDeque<Box<dyn Page<Display = G13>>>,
joy: Vec2<f32>,
} }
impl App { impl App {
pub fn new() -> Self { pub fn new() -> Self {
Self { let mut pages: VecDeque<Box<dyn Page<Display = G13>>> = VecDeque::new();
pages: VecDeque::new(),
joy: Vec2::new(0.0, 0.0), pages.push_back(Box::new(StartPage::default()));
}
Self { pages }
} }
pub fn update(&mut self, event: G13Event) { pub fn update(&mut self, event: G13Event) {
match event { if let Some(last_page) = self.pages.iter_mut().last() {
G13Event::Axis(x, y) => { last_page.update(event);
self.joy.x = x;
self.joy.y = y;
}
G13Event::Button(_button, (_prev, _value)) => { /* noop */ }
} }
} }
pub fn draw<D>(&mut self, display: &mut D) pub fn draw(&mut self, display: &mut G13) {
where if let Some(last_page) = self.pages.iter_mut().last() {
D: DrawTarget<Color = Color>,
{
if let Some(last_page) = self.pages.iter().last() {
last_page.draw(display); last_page.draw(display);
} }
// let string = format!(
// "{}x{}",
// (self.joy.x * 256.0) as i32,
// (self.joy.y * 256.0) as i32
// );
// let text = Text::with_text_style(&string, Point::new(1, 1), CHARACTER_STYLE, TEXT_STYLE);
// let _ = text.draw(display);
} }
} }

View File

@@ -0,0 +1,3 @@
mod start;
pub use start::*;

View File

@@ -0,0 +1,53 @@
use embedded_graphics::{
Drawable,
mono_font::{MonoTextStyle, ascii::FONT_6X10},
pixelcolor::BinaryColor,
prelude::Point,
text::{Alignment, Baseline, Text, TextStyle, TextStyleBuilder},
};
use g13_driver::{G13, G13Event, Vec2};
use crate::app::widgets::{Page, Widget};
const CHARACTER_STYLE: MonoTextStyle<'static, BinaryColor> =
MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
const TEXT_STYLE: TextStyle = TextStyleBuilder::new()
.baseline(Baseline::Top)
.alignment(Alignment::Left)
.build();
#[derive(Default)]
pub struct StartPage {
joy: Vec2<f32>,
}
impl Widget for StartPage {
type Display = G13;
fn update(&mut self, event: g13_driver::G13Event) {
if let G13Event::Axis(x, y) = event {
self.joy.x = x;
self.joy.y = y;
}
}
fn children(&self) -> Vec<Box<dyn Widget<Display = Self::Display>>> {
// .. idk if we need this yet
Vec::new()
}
fn draw(&mut self, display: &mut Self::Display) {
let string = format!(
"{}x{}",
(self.joy.x * 256.0) as i32,
(self.joy.y * 256.0) as i32
);
let text = Text::with_text_style(&string, Point::new(1, 1), CHARACTER_STYLE, TEXT_STYLE);
let _ = text.draw(display);
}
}
impl Page for StartPage {
// ..
}

View File

@@ -2,15 +2,13 @@ use embedded_graphics::draw_target::DrawTarget;
use g13_driver::G13Event; use g13_driver::G13Event;
pub trait Page: Widget {} pub trait Page: Widget {}
pub trait Widget { pub trait Widget {
type Display: DrawTarget;
fn update(&mut self, event: G13Event); fn update(&mut self, event: G13Event);
fn children() -> Vec<Box<dyn Widget>>; // the trait `app::widgets::Widget` is not dyn compatible fn children(&self) -> Vec<Box<dyn Widget<Display = Self::Display>>>;
// for a trait to be dyn compatible it needs to allow building a vtable
// for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
// Generics aren't allowed, but I need that drawtarget here >_< fn draw(&mut self, display: &mut Self::Display);
fn draw<D>(&mut self, display: &mut D)
where
D: DrawTarget;
} }