@benjamin.brienen held
This commit is contained in:
@@ -3,65 +3,34 @@ pub(super) mod widgets;
|
||||
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use embedded_graphics::{
|
||||
Drawable,
|
||||
draw_target::DrawTarget,
|
||||
mono_font::{MonoTextStyle, ascii::FONT_6X10},
|
||||
pixelcolor::BinaryColor,
|
||||
prelude::Point,
|
||||
text::{Alignment, Baseline, LineHeight, Text, TextStyle, TextStyleBuilder},
|
||||
};
|
||||
use g13_driver::{G13Event, Vec2};
|
||||
use g13_driver::{G13, G13Event};
|
||||
|
||||
use crate::app::pages::StartPage;
|
||||
|
||||
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 {
|
||||
pages: VecDeque<Box<dyn Page>>,
|
||||
joy: Vec2<f32>,
|
||||
pages: VecDeque<Box<dyn Page<Display = G13>>>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
pages: VecDeque::new(),
|
||||
joy: Vec2::new(0.0, 0.0),
|
||||
}
|
||||
let mut pages: VecDeque<Box<dyn Page<Display = G13>>> = VecDeque::new();
|
||||
|
||||
pages.push_back(Box::new(StartPage::default()));
|
||||
|
||||
Self { pages }
|
||||
}
|
||||
|
||||
pub fn update(&mut self, event: G13Event) {
|
||||
match event {
|
||||
G13Event::Axis(x, y) => {
|
||||
self.joy.x = x;
|
||||
self.joy.y = y;
|
||||
}
|
||||
G13Event::Button(_button, (_prev, _value)) => { /* noop */ }
|
||||
if let Some(last_page) = self.pages.iter_mut().last() {
|
||||
last_page.update(event);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw<D>(&mut self, display: &mut D)
|
||||
where
|
||||
D: DrawTarget<Color = Color>,
|
||||
{
|
||||
if let Some(last_page) = self.pages.iter().last() {
|
||||
pub fn draw(&mut self, display: &mut G13) {
|
||||
if let Some(last_page) = self.pages.iter_mut().last() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
mod start;
|
||||
|
||||
pub use start::*;
|
||||
|
||||
53
g13-os/src/app/pages/start.rs
Normal file
53
g13-os/src/app/pages/start.rs
Normal 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 {
|
||||
// ..
|
||||
}
|
||||
@@ -2,15 +2,13 @@ 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() -> Vec<Box<dyn Widget>>; // the trait `app::widgets::Widget` is not dyn compatible
|
||||
// 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>
|
||||
fn children(&self) -> Vec<Box<dyn Widget<Display = Self::Display>>>;
|
||||
|
||||
// Generics aren't allowed, but I need that drawtarget here >_<
|
||||
fn draw<D>(&mut self, display: &mut D)
|
||||
where
|
||||
D: DrawTarget;
|
||||
fn draw(&mut self, display: &mut Self::Display);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user