I can haz Label?
This commit is contained in:
@@ -7,7 +7,7 @@ use g13_driver::{G13, G13Event};
|
|||||||
|
|
||||||
use crate::app::pages::StartPage;
|
use crate::app::pages::StartPage;
|
||||||
|
|
||||||
use super::app::widgets::Page;
|
use super::app::pages::Page;
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
pages: VecDeque<Box<dyn Page<Display = G13>>>,
|
pages: VecDeque<Box<dyn Page<Display = G13>>>,
|
||||||
@@ -17,7 +17,7 @@ impl App {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let mut pages: VecDeque<Box<dyn Page<Display = G13>>> = VecDeque::new();
|
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 }
|
Self { pages }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
use crate::app::widgets::Widget;
|
||||||
|
|
||||||
mod start;
|
mod start;
|
||||||
|
|
||||||
|
pub trait Page: Widget {}
|
||||||
|
|
||||||
pub use start::*;
|
pub use start::*;
|
||||||
|
|||||||
@@ -1,25 +1,34 @@
|
|||||||
use embedded_graphics::{
|
use embedded_graphics::{
|
||||||
Drawable,
|
mono_font::ascii::FONT_6X10,
|
||||||
mono_font::{MonoTextStyle, ascii::FONT_6X10},
|
|
||||||
pixelcolor::BinaryColor,
|
pixelcolor::BinaryColor,
|
||||||
prelude::Point,
|
prelude::Point,
|
||||||
text::{Alignment, Baseline, Text, TextStyle, TextStyleBuilder},
|
text::{Alignment, Baseline},
|
||||||
};
|
};
|
||||||
use g13_driver::{G13, G13Event, Vec2};
|
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 {
|
pub struct StartPage {
|
||||||
joy: Vec2<f32>,
|
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 {
|
impl Widget for StartPage {
|
||||||
@@ -30,24 +39,35 @@ impl Widget for StartPage {
|
|||||||
self.joy.x = x;
|
self.joy.x = x;
|
||||||
self.joy.y = y;
|
self.joy.y = y;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn children(&self) -> Vec<Box<dyn Widget<Display = Self::Display>>> {
|
// This is stupid.
|
||||||
// .. idk if we need this yet
|
// Need a way to keep some reference to widgets to manipulate them later
|
||||||
Vec::new()
|
// 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) {
|
fn draw(&mut self, display: &mut Self::Display) {
|
||||||
let string = format!(
|
for child in self.children.iter_mut() {
|
||||||
"{}x{}",
|
child.draw(display);
|
||||||
(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 {
|
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
||||||
// ..
|
self
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marker to indicate page
|
||||||
|
impl super::Page for StartPage {}
|
||||||
|
|||||||
73
g13-os/src/app/widgets/label.rs
Normal file
73
g13-os/src/app/widgets/label.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
|
pub mod label;
|
||||||
|
|
||||||
|
use std::any::Any;
|
||||||
|
|
||||||
use embedded_graphics::draw_target::DrawTarget;
|
use embedded_graphics::draw_target::DrawTarget;
|
||||||
use g13_driver::G13Event;
|
use g13_driver::G13Event;
|
||||||
|
|
||||||
pub trait Page: Widget {}
|
|
||||||
|
|
||||||
pub trait Widget {
|
pub trait Widget {
|
||||||
type Display: DrawTarget;
|
type Display: DrawTarget;
|
||||||
|
|
||||||
fn update(&mut self, event: G13Event);
|
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 draw(&mut self, display: &mut Self::Display);
|
||||||
|
|
||||||
|
fn as_any_mut(&mut self) -> &mut dyn Any;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
start = Instant::now();
|
start = Instant::now();
|
||||||
g13.clear(BinaryColor::Off)?;
|
g13.clear(BinaryColor::Off)?;
|
||||||
app.draw(&mut g13);
|
app.draw(&mut g13);
|
||||||
g13.render()?; // 33 fps rendering
|
g13.render()?; // 30 fps rendering
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user