Add component trait and vsplit
parent
5562b57ea5
commit
e731f907c4
|
|
@ -14,6 +14,8 @@ use embedded_graphics::{
|
||||||
use embedded_layout::prelude::*;
|
use embedded_layout::prelude::*;
|
||||||
use num_traits::cast::FromPrimitive;
|
use num_traits::cast::FromPrimitive;
|
||||||
|
|
||||||
|
use crate::Component;
|
||||||
|
|
||||||
fn font(height: u32) -> MonoFont<'static> {
|
fn font(height: u32) -> MonoFont<'static> {
|
||||||
if height < 8 {
|
if height < 8 {
|
||||||
return FONT_4X6;
|
return FONT_4X6;
|
||||||
|
|
@ -318,6 +320,15 @@ pub struct ScrollingCalendar<C> {
|
||||||
pub weeks: u32,
|
pub weeks: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<C> Component for ScrollingCalendar<C> {
|
||||||
|
fn set_size(&mut self, size: Size) {
|
||||||
|
self.size = size;
|
||||||
|
}
|
||||||
|
fn set_top_left(&mut self, top_left: Point) {
|
||||||
|
self.top_left = top_left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<C> Drawable for ScrollingCalendar<C>
|
impl<C> Drawable for ScrollingCalendar<C>
|
||||||
where
|
where
|
||||||
C: PixelColor + From<BinaryColor>,
|
C: PixelColor + From<BinaryColor>,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,82 @@
|
||||||
|
use embedded_graphics::{pixelcolor::BinaryColor, prelude::*};
|
||||||
|
use embedded_layout::View;
|
||||||
|
|
||||||
|
pub trait Component {
|
||||||
|
fn set_top_left(&mut self, top_left: Point);
|
||||||
|
fn set_size(&mut self, size: Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Frame {
|
||||||
|
//drawable: Component,
|
||||||
|
pub weight: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct VSplit<T, U> {
|
||||||
|
pub top_left: Point,
|
||||||
|
pub size: Size,
|
||||||
|
pub left: T,
|
||||||
|
pub right: U,
|
||||||
|
pub ratio: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, U> VSplit<T, U> {
|
||||||
|
pub fn new(left: T, right: U) -> Self {
|
||||||
|
VSplit {
|
||||||
|
top_left: Point::new(0, 0),
|
||||||
|
size: Size::new(0, 0),
|
||||||
|
left,
|
||||||
|
right,
|
||||||
|
ratio: 0.5,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<C, T, U> Component for VSplit<T, U>
|
||||||
|
where
|
||||||
|
C: PixelColor + From<BinaryColor>,
|
||||||
|
T: Drawable<Color = C>,
|
||||||
|
U: Drawable<Color = C>,
|
||||||
|
{
|
||||||
|
fn set_top_left(&mut self, top_left: Point) {
|
||||||
|
self.top_left = top_left;
|
||||||
|
}
|
||||||
|
fn set_size(&mut self, size: Size) {
|
||||||
|
self.size = size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<C, T, U> Drawable for VSplit<T, U>
|
||||||
|
where
|
||||||
|
C: PixelColor + From<BinaryColor>,
|
||||||
|
T: Drawable<Color = C> + Component + Clone,
|
||||||
|
U: Drawable<Color = C> + Component + Clone,
|
||||||
|
{
|
||||||
|
type Color = C;
|
||||||
|
type Output = ();
|
||||||
|
|
||||||
|
fn draw<D>(&self, target: &mut D) -> Result<Self::Output, D::Error>
|
||||||
|
where
|
||||||
|
D: DrawTarget<Color = C>,
|
||||||
|
{
|
||||||
|
let left_width = (self.size.width as f32 * self.ratio) as u32;
|
||||||
|
let mut left = self.left.clone();
|
||||||
|
left.set_top_left(self.top_left);
|
||||||
|
left.set_size(Size::new(self.size.height, left_width));
|
||||||
|
|
||||||
|
let mut right = self.right.clone();
|
||||||
|
right.set_top_left(Point::new(left_width as i32, 0));
|
||||||
|
right.set_size(Size::new(self.size.height, self.size.width - left_width));
|
||||||
|
|
||||||
|
left.draw(target)?;
|
||||||
|
right.draw(target)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct HSplit<T> {
|
||||||
|
pub top_left: Point,
|
||||||
|
pub size: Size,
|
||||||
|
pub top: T,
|
||||||
|
pub bottom: T,
|
||||||
|
}
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
mod bullet_counter;
|
mod bullet_counter;
|
||||||
mod calendar;
|
mod calendar;
|
||||||
|
mod frame;
|
||||||
mod gauge;
|
mod gauge;
|
||||||
mod list_item;
|
mod list_item;
|
||||||
mod style;
|
mod style;
|
||||||
|
|
||||||
pub use bullet_counter::BulletCounter;
|
pub use bullet_counter::BulletCounter;
|
||||||
pub use calendar::{Calendar, ScrollingCalendar};
|
pub use calendar::{Calendar, ScrollingCalendar};
|
||||||
|
pub use frame::{Component, HSplit, VSplit};
|
||||||
pub use gauge::Gauge;
|
pub use gauge::Gauge;
|
||||||
pub use list_item::{List, ListItem, ListItemData};
|
pub use list_item::{List, ListItem, ListItemData};
|
||||||
pub use style::ComponentStyle;
|
pub use style::ComponentStyle;
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@ use embedded_graphics::{
|
||||||
};
|
};
|
||||||
use embedded_layout::prelude::*;
|
use embedded_layout::prelude::*;
|
||||||
|
|
||||||
|
use crate::Component;
|
||||||
|
|
||||||
pub struct ListItemData {
|
pub struct ListItemData {
|
||||||
pub text: String,
|
pub text: String,
|
||||||
pub icon: String,
|
pub icon: String,
|
||||||
|
|
@ -39,6 +41,15 @@ pub struct List<C> {
|
||||||
pub list_items: Vec<ListItemData>,
|
pub list_items: Vec<ListItemData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<C> Component for List<C> {
|
||||||
|
fn set_size(&mut self, size: Size) {
|
||||||
|
self.size = size;
|
||||||
|
}
|
||||||
|
fn set_top_left(&mut self, top_left: Point) {
|
||||||
|
self.top_left = top_left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<C> Drawable for List<C>
|
impl<C> Drawable for List<C>
|
||||||
where
|
where
|
||||||
C: PixelColor + From<BinaryColor>,
|
C: PixelColor + From<BinaryColor>,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue