From 547b5f13d22e533e959a4b682e4f048bcfbc604a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Chodzikiewicz?= Date: Tue, 16 Feb 2021 20:51:09 +0100 Subject: [PATCH] Refactor split SinglePlot from Drawable --- examples/single_plot_mono.rs | 9 ++++---- examples/single_plot_rgb.rs | 9 ++++---- src/single_plot.rs | 44 ++++++++++++++++++++---------------- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/examples/single_plot_mono.rs b/examples/single_plot_mono.rs index 803ab3e..569c6ff 100644 --- a/examples/single_plot_mono.rs +++ b/examples/single_plot_mono.rs @@ -24,11 +24,12 @@ fn main() -> Result<(), core::convert::Infallible> { let plot = SinglePlot::new( &curve, - BinaryColor::On, - Point { x: 9, y: 3 }, - Point { x: 120, y: 45 }, Scale::RangeFraction(3), - Scale::RangeFraction(2) + Scale::RangeFraction(2), + ).into_drawable( + BinaryColor::On, + Point { x: 18, y: 2 }, + Point { x: 120, y: 30 }, ); plot.draw(&mut display)?; diff --git a/examples/single_plot_rgb.rs b/examples/single_plot_rgb.rs index 922d16b..87ed2c5 100644 --- a/examples/single_plot_rgb.rs +++ b/examples/single_plot_rgb.rs @@ -7,7 +7,6 @@ use embedded_graphics_simulator::{ SimulatorDisplay, Window, OutputSettingsBuilder, - }; use embedded_plots::{ @@ -29,11 +28,13 @@ fn main() -> Result<(), core::convert::Infallible> { let plot = SinglePlot::new( &curve, + Scale::RangeFraction(3), + Scale::RangeFraction(2), + ) + .into_drawable( RgbColor::YELLOW, Point { x: 50, y: 10 }, - Point { x: 430, y: 250 }, - Scale::RangeFraction(3), - Scale::RangeFraction(2) + Point { x: 430, y: 250 } ); plot.draw(&mut display)?; diff --git a/src/single_plot.rs b/src/single_plot.rs index e885baa..a1a6d0a 100644 --- a/src/single_plot.rs +++ b/src/single_plot.rs @@ -7,46 +7,50 @@ use crate::axis::{Scale, Placement, Axis}; use embedded_graphics::style::TextStyleBuilder; use embedded_graphics::fonts::Font6x8; -pub struct SinglePlot<'a, C> - where - C: PixelColor -{ +pub struct SinglePlot<'a> { curve: &'a Curve<'a>, x_scale: Scale, y_scale: Scale, +} + +impl<'a> SinglePlot<'a> { + pub fn new(curve: &'a Curve<'a>, x_scale: Scale, y_scale: Scale) -> SinglePlot { + SinglePlot { curve, x_scale, y_scale } + } + + pub fn into_drawable(self, color: C, top_left: Point, bottom_right: Point) -> DrawableSinglePlot<'a, C> { + DrawableSinglePlot { plot: self, color, top_left, bottom_right } + } +} + +pub struct DrawableSinglePlot<'a, C> + where + C: PixelColor +{ + plot: SinglePlot<'a>, color: C, top_left: Point, bottom_right: Point, } -impl<'a, C> SinglePlot<'a, C> - where - C: PixelColor -{ - pub fn new(curve: &'a Curve<'a>, color: C, top_left: Point, bottom_right: Point, x_scale: Scale, y_scale: Scale) -> SinglePlot<'a, C> { - SinglePlot { curve, color, top_left, bottom_right, x_scale, y_scale} - } -} - -impl<'a, C> Drawable for SinglePlot<'a, C> +impl<'a, C> Drawable for DrawableSinglePlot<'a, C> where C: PixelColor { fn draw>(self, display: &mut D) -> Result<(), D::Error> { - let text_style = TextStyleBuilder::new(Font6x8) .text_color(self.color) .build(); - Axis::new("X",self.curve.x_range.clone(),self.x_scale) - .into_drawable_axis(Placement::X{x1: self.top_left.x, x2: self.bottom_right.x, y: self.bottom_right.y},self.color,text_style,2) + Axis::new("X", self.plot.curve.x_range.clone(), self.plot.x_scale) + .into_drawable_axis(Placement::X { x1: self.top_left.x, x2: self.bottom_right.x, y: self.bottom_right.y }, self.color, text_style, 2) .draw(display)?; - Axis::new("Y", self.curve.y_range.clone(), self.y_scale) - .into_drawable_axis(Placement::Y{y1: self.top_left.y, y2: self.bottom_right.y, x: self.top_left.x},self.color,text_style,2) + Axis::new("Y", self.plot.curve.y_range.clone(), self.plot.y_scale) + .into_drawable_axis(Placement::Y { y1: self.top_left.y, y2: self.bottom_right.y, x: self.top_left.x }, self.color, text_style, 2) .draw(display)?; - self.curve.into_drawable_curve( + self.plot.curve.into_drawable_curve( &self.top_left, &self.bottom_right, self.color,