From 7790a4fa4dfd40609bb43791d16528d31962a2fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Chodzikiewicz?= Date: Thu, 31 Dec 2020 00:04:24 +0100 Subject: [PATCH] Move text style building out of Axis --- examples/free_axis.rs | 24 +++++++++++++++--------- src/axis.rs | 36 +++++++++++++++++++++--------------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/examples/free_axis.rs b/examples/free_axis.rs index 2376c61..23c2928 100644 --- a/examples/free_axis.rs +++ b/examples/free_axis.rs @@ -13,37 +13,43 @@ use embedded_graphics_simulator::{ use embedded_plots::{ axis::{Axis,Orientation,Scale}, }; +use embedded_graphics::style::TextStyleBuilder; +use embedded_graphics::fonts::Font6x8; fn main() -> Result<(), core::convert::Infallible> { let mut display: SimulatorDisplay = SimulatorDisplay::new(Size::new(480, 272)); - Axis::new("X Fixed 0-100(10)",Orientation::X{x1: 10, x2: 230, y: 10},0..100,Scale::Fixed(10),RgbColor::WHITE) + let text_style = TextStyleBuilder::new(Font6x8) + .text_color(RgbColor::WHITE) + .build(); + + Axis::new("X Fixed 0-100(10)",Orientation::X{x1: 10, x2: 230, y: 10},0..100,Scale::Fixed(10),RgbColor::WHITE, text_style) .draw(&mut display)?; - Axis::new("X Fixed 0-200(100)",Orientation::X{x1: 240, x2: 470, y: 10},0..200,Scale::Fixed(100),RgbColor::YELLOW) + Axis::new("X Fixed 0-200(100)",Orientation::X{x1: 240, x2: 470, y: 10},0..200,Scale::Fixed(100),RgbColor::YELLOW, text_style) .draw(&mut display)?; - Axis::new("X Fixed 0-100(7)",Orientation::X{x1: 20, x2: 220, y: 30},0..100,Scale::RangeFraction(7),RgbColor::BLUE) + Axis::new("X Frac 0-100(7)",Orientation::X{x1: 20, x2: 220, y: 30},0..100,Scale::RangeFraction(7),RgbColor::BLUE, text_style) .draw(&mut display)?; - Axis::new("X Fixed 0-200(4)",Orientation::X{x1: 250, x2: 460, y: 30},0..200,Scale::RangeFraction(4),RgbColor::RED) + Axis::new("X Frac 0-200(4)",Orientation::X{x1: 250, x2: 460, y: 30},0..200,Scale::RangeFraction(4),RgbColor::RED, text_style) .draw(&mut display)?; - Axis::new("Y Fixed 0-100(10)",Orientation::Y{y1: 60, y2: 250, x: 90},0..100,Scale::Fixed(10),RgbColor::WHITE) + Axis::new("Y Fixed 0-100(10)",Orientation::Y{y1: 60, y2: 250, x: 110},0..100,Scale::Fixed(10),RgbColor::WHITE, text_style) .draw(&mut display)?; - Axis::new("Y Fixed 0-200(100)",Orientation::Y{y1: 70, y2: 230, x: 210},0..200,Scale::Fixed(100),RgbColor::YELLOW) + Axis::new("Y Fixed 0-200(100)",Orientation::Y{y1: 70, y2: 230, x: 230},0..200,Scale::Fixed(100),RgbColor::YELLOW, text_style) .draw(&mut display)?; - Axis::new("Y Fixed 0-100(7)",Orientation::Y{y1: 60, y2: 180, x: 330},0..100,Scale::RangeFraction(7),RgbColor::BLUE) + Axis::new("Y Frac 0-100(7)",Orientation::Y{y1: 60, y2: 180, x: 350},0..100,Scale::RangeFraction(7),RgbColor::BLUE, text_style) .draw(&mut display)?; - Axis::new("Y Fixed 0-200(4)",Orientation::Y{y1: 90, y2: 260, x: 450},0..200,Scale::RangeFraction(4),RgbColor::RED) + Axis::new("Y Frac 0-200(4)",Orientation::Y{y1: 90, y2: 260, x: 470},0..200,Scale::RangeFraction(4),RgbColor::RED, text_style) .draw(&mut display)?; let output_settings = OutputSettingsBuilder::new() - // .pixel_spacing(1) + .pixel_spacing(1) .build(); Window::new("Basic plot", &output_settings).show_static(&display); diff --git a/src/axis.rs b/src/axis.rs index 32e48ec..177087d 100644 --- a/src/axis.rs +++ b/src/axis.rs @@ -3,9 +3,9 @@ use embedded_graphics::drawable::Drawable; use embedded_graphics::DrawTarget; use core::ops::Range; use embedded_graphics::prelude::*; -use embedded_graphics::style::{PrimitiveStyle, TextStyleBuilder}; +use embedded_graphics::style::{PrimitiveStyle, TextStyle}; use crate::range_conv::Scalable; -use embedded_graphics::fonts::{Text, Font6x8}; +use embedded_graphics::fonts::Text; use heapless::{consts::*, String}; use core::fmt::Write; @@ -27,27 +27,37 @@ pub enum Scale { RangeFraction(usize), } -pub struct Axis<'a, C> { +pub struct Axis<'a, C, F> + where + C: PixelColor, + F: Font, + TextStyle: Clone, +{ title: &'a str, orientation: Orientation, range: Range, scale: Scale, color: C, + text_style: TextStyle } -impl<'a, C> Axis<'a, C> +impl<'a, C, F> Axis<'a, C, F> where C: PixelColor, + F: Font, + TextStyle: Clone, { - pub fn new(title: &'a str, orientation: Orientation, range: Range, scale: Scale, color: C) -> Axis<'a, C> { - Axis { title, orientation, range, scale, color } + pub fn new(title: &'a str, orientation: Orientation, range: Range, scale: Scale, color: C, text_style: TextStyle) -> Axis<'a, C, F> { + Axis { title, orientation, range, scale, color, text_style } } } -impl<'a, C> Drawable for Axis<'a, C> +impl<'a, C, F> Drawable for Axis<'a, C, F> where C: PixelColor, + F: Font + Copy, + TextStyle: Clone, { fn draw>(self, display: &mut D) -> Result<(), D::Error> { let lines = match self.scale { @@ -59,17 +69,13 @@ impl<'a, C> Drawable for Axis<'a, C> self.range.clone().into_iter().step_by(len / fraction) } }; - // Create a new text style - let style = TextStyleBuilder::new(Font6x8) - .text_color(self.color) - .build(); match self.orientation { Orientation::X { x1, x2, y } => { Line { start: Point { x: x1, y }, end: Point { x: x2, y } } .into_styled(PrimitiveStyle::with_stroke(self.color, 1)) .draw(display)?; let title = Text::new(self.title, Point { x: x1, y: y + 10 }) - .into_styled(style); + .into_styled(self.text_style); let title = title.translate(Point { x: (x2 - x1) / 2 - title.size().width as i32 / 2, y: 0 }); title.draw(display)?; @@ -80,7 +86,7 @@ impl<'a, C> Drawable for Axis<'a, C> .draw(display)?; let mut buf: String:: = String::new(); write!(buf, "{}", line).unwrap(); - Text::new(&buf, Point { x: x + 1, y: y + 1 }).into_styled(style).draw(display)?; + Text::new(&buf, Point { x: x + 1, y: y + 1 }).into_styled(self.text_style).draw(display)?; } } Orientation::Y { y1, y2, x } => { @@ -88,7 +94,7 @@ impl<'a, C> Drawable for Axis<'a, C> .into_styled(PrimitiveStyle::with_stroke(self.color, 1)) .draw(display)?; let title = Text::new(self.title, Point { x, y: y1 }) - .into_styled(style); + .into_styled(self.text_style); let title = title.translate(Point { x: -(title.size().width as i32) - 5, y: (y2-y1)/2 }); title.draw(display)?; @@ -99,7 +105,7 @@ impl<'a, C> Drawable for Axis<'a, C> .draw(display)?; let mut buf: String:: = String::new(); write!(buf, "{}", line).unwrap(); - let tick = Text::new(&buf, Point { x, y}).into_styled(style); + let tick = Text::new(&buf, Point { x, y}).into_styled(self.text_style); let tick = tick.translate(Point{ x: -(tick.size().width as i32), y: 0 }); tick.draw(display)?; }