I can haz Label?

This commit is contained in:
2026-02-14 20:13:16 +01:00
parent 81971410ce
commit f6ec4492f1
6 changed files with 133 additions and 34 deletions

View File

@@ -7,7 +7,7 @@ use g13_driver::{G13, G13Event};
use crate::app::pages::StartPage;
use super::app::widgets::Page;
use super::app::pages::Page;
pub struct App {
pages: VecDeque<Box<dyn Page<Display = G13>>>,
@@ -17,7 +17,7 @@ impl App {
pub fn new() -> Self {
let mut pages: VecDeque<Box<dyn Page<Display = G13>>> = VecDeque::new();
pages.push_back(Box::new(StartPage::default()));
pages.push_back(Box::new(StartPage::new()));
Self { pages }
}

View File

@@ -1,3 +1,7 @@
use crate::app::widgets::Widget;
mod start;
pub trait Page: Widget {}
pub use start::*;

View File

@@ -1,25 +1,34 @@
use embedded_graphics::{
Drawable,
mono_font::{MonoTextStyle, ascii::FONT_6X10},
mono_font::ascii::FONT_6X10,
pixelcolor::BinaryColor,
prelude::Point,
text::{Alignment, Baseline, Text, TextStyle, TextStyleBuilder},
text::{Alignment, Baseline},
};
use g13_driver::{G13, G13Event, Vec2};
use crate::app::widgets::{Page, Widget};
use crate::app::widgets::{Widget, label::Label};
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>,
children: Vec<Box<dyn Widget<Display = G13>>>,
}
impl StartPage {
pub fn new() -> Self {
let children: Vec<Box<dyn Widget<Display = G13>>> = vec![Box::new(Label::new(
"Hello World!",
&FONT_6X10,
BinaryColor::On,
Point::new(10, 0),
Alignment::Left,
Baseline::Top,
))];
Self {
joy: Vec2::default(),
children,
}
}
}
impl Widget for StartPage {
@@ -30,24 +39,35 @@ impl Widget for StartPage {
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()
// This is stupid.
// Need a way to keep some reference to widgets to manipulate them later
// preferably as their original type so we don't need this downcast bs
self.children[0]
.as_any_mut()
.downcast_mut::<Label>()
.unwrap()
.text(&format!(
"{} {}",
(self.joy.x * 512.0) as i32,
(self.joy.y * 512.0) as i32
));
for child in self.children.iter_mut() {
child.update(event);
}
}
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);
for child in self.children.iter_mut() {
child.draw(display);
}
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
}
impl Page for StartPage {
// ..
}
// Marker to indicate page
impl super::Page for StartPage {}

View File

@@ -0,0 +1,73 @@
use embedded_graphics::{
mono_font::{MonoFont, MonoTextStyle},
pixelcolor::BinaryColor,
prelude::*,
text::{Alignment, Baseline, Text, TextStyle, TextStyleBuilder},
};
use g13_driver::G13;
// 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 Label {
text: String,
position: Point,
character_style: MonoTextStyle<'static, BinaryColor>,
text_style: TextStyle,
}
impl Label {
pub fn new(
text: &str,
font: &'static MonoFont<'static>,
color: BinaryColor,
position: Point,
alignment: Alignment,
baseline: Baseline,
) -> Self {
let character_style: MonoTextStyle<'static, BinaryColor> = MonoTextStyle::new(font, color);
let text_style: TextStyle = TextStyleBuilder::new()
.baseline(baseline)
.alignment(alignment)
.build();
Self {
text: text.to_owned(),
position,
character_style,
text_style,
}
}
pub fn text(&mut self, text: &str) {
self.text = text.to_owned();
}
}
impl super::Widget for Label {
type Display = G13;
fn update(&mut self, _: g13_driver::G13Event) {
// noop
}
fn draw(&mut self, display: &mut Self::Display) {
let _ = Text::with_text_style(
&self.text,
self.position,
self.character_style,
self.text_style,
)
.draw(display);
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
}

View File

@@ -1,14 +1,16 @@
pub mod label;
use std::any::Any;
use embedded_graphics::draw_target::DrawTarget;
use g13_driver::G13Event;
pub trait Page: Widget {}
pub trait Widget {
type Display: DrawTarget;
fn update(&mut self, event: G13Event);
fn children(&self) -> Vec<Box<dyn Widget<Display = Self::Display>>>;
fn draw(&mut self, display: &mut Self::Display);
fn as_any_mut(&mut self) -> &mut dyn Any;
}

View File

@@ -26,7 +26,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
start = Instant::now();
g13.clear(BinaryColor::Off)?;
app.draw(&mut g13);
g13.render()?; // 33 fps rendering
g13.render()?; // 30 fps rendering
}
}
}