From 365a5c5ce9ac8656b217e370726d11370cd5a295 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 15 Apr 2022 23:45:35 +0200 Subject: [PATCH] Add component trait and vsplit --- src/frame.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ 2 files changed, 84 insertions(+) create mode 100644 src/frame.rs diff --git a/src/frame.rs b/src/frame.rs new file mode 100644 index 0000000..34e0307 --- /dev/null +++ b/src/frame.rs @@ -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 { + pub top_left: Point, + pub size: Size, + pub left: T, + pub right: U, + pub ratio: f32, +} + +impl VSplit { + 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 Component for VSplit +where + C: PixelColor + From, + T: Drawable, + U: Drawable, +{ + 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 Drawable for VSplit +where + C: PixelColor + From, + T: Drawable + Component + Clone, + U: Drawable + Component + Clone, +{ + type Color = C; + type Output = (); + + fn draw(&self, target: &mut D) -> Result + where + D: DrawTarget, + { + 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 { + pub top_left: Point, + pub size: Size, + pub top: T, + pub bottom: T, +} diff --git a/src/lib.rs b/src/lib.rs index e08a390..1c6586a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,13 @@ mod bullet_counter; mod calendar; +mod frame; mod gauge; mod list_item; mod style; pub use bullet_counter::BulletCounter; pub use calendar::{Calendar, ScrollingCalendar}; +pub use frame::{HSplit, VSplit}; pub use gauge::Gauge; pub use list_item::{List, ListItem, ListItemData}; pub use style::ComponentStyle;