Refactor split SinglePlot from Drawable

This commit is contained in:
Michał Chodzikiewicz 2021-02-16 20:51:09 +01:00
parent f0909be13d
commit 547b5f13d2
3 changed files with 34 additions and 28 deletions

View file

@ -24,11 +24,12 @@ fn main() -> Result<(), core::convert::Infallible> {
let plot = SinglePlot::new( let plot = SinglePlot::new(
&curve, &curve,
BinaryColor::On,
Point { x: 9, y: 3 },
Point { x: 120, y: 45 },
Scale::RangeFraction(3), 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)?; plot.draw(&mut display)?;

View file

@ -7,7 +7,6 @@ use embedded_graphics_simulator::{
SimulatorDisplay, SimulatorDisplay,
Window, Window,
OutputSettingsBuilder, OutputSettingsBuilder,
}; };
use embedded_plots::{ use embedded_plots::{
@ -29,11 +28,13 @@ fn main() -> Result<(), core::convert::Infallible> {
let plot = SinglePlot::new( let plot = SinglePlot::new(
&curve, &curve,
Scale::RangeFraction(3),
Scale::RangeFraction(2),
)
.into_drawable(
RgbColor::YELLOW, RgbColor::YELLOW,
Point { x: 50, y: 10 }, Point { x: 50, y: 10 },
Point { x: 430, y: 250 }, Point { x: 430, y: 250 }
Scale::RangeFraction(3),
Scale::RangeFraction(2)
); );
plot.draw(&mut display)?; plot.draw(&mut display)?;

View file

@ -7,46 +7,50 @@ use crate::axis::{Scale, Placement, Axis};
use embedded_graphics::style::TextStyleBuilder; use embedded_graphics::style::TextStyleBuilder;
use embedded_graphics::fonts::Font6x8; use embedded_graphics::fonts::Font6x8;
pub struct SinglePlot<'a, C> pub struct SinglePlot<'a> {
where
C: PixelColor
{
curve: &'a Curve<'a>, curve: &'a Curve<'a>,
x_scale: Scale, x_scale: Scale,
y_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<C: PixelColor>(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, color: C,
top_left: Point, top_left: Point,
bottom_right: Point, bottom_right: Point,
} }
impl<'a, C> SinglePlot<'a, C> impl<'a, C> Drawable<C> for DrawableSinglePlot<'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<C> for SinglePlot<'a, C>
where where
C: PixelColor C: PixelColor
{ {
fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error> { fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error> {
let text_style = TextStyleBuilder::new(Font6x8) let text_style = TextStyleBuilder::new(Font6x8)
.text_color(self.color) .text_color(self.color)
.build(); .build();
Axis::new("X",self.curve.x_range.clone(),self.x_scale) 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) .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)?; .draw(display)?;
Axis::new("Y", self.curve.y_range.clone(), self.y_scale) 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) .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)?; .draw(display)?;
self.curve.into_drawable_curve( self.plot.curve.into_drawable_curve(
&self.top_left, &self.top_left,
&self.bottom_right, &self.bottom_right,
self.color, self.color,