From fa3d391fed01598cb17fab46da2cf079f75b7182 Mon Sep 17 00:00:00 2001 From: Ralf Fuest Date: Mon, 16 Nov 2020 21:34:34 +0100 Subject: [PATCH 1/2] Remove chaining example --- examples/chaining.rs | 74 -------------------------------------------- 1 file changed, 74 deletions(-) delete mode 100644 examples/chaining.rs diff --git a/examples/chaining.rs b/examples/chaining.rs deleted file mode 100644 index db7f7b5..0000000 --- a/examples/chaining.rs +++ /dev/null @@ -1,74 +0,0 @@ -//! # Example: Chaining -//! -//! Demonstrate the chaining abilities of embedded graphics iterators -//! -//! This example displays the same end result as the `hello-world.rs` example, but does it using a -//! single chained iterator. - -use embedded_graphics::{ - fonts::{Font6x8, Text}, - pixelcolor::BinaryColor, - prelude::*, - primitives::{Circle, Rectangle, Triangle}, - style::{MonoTextStyle, PrimitiveStyle}, -}; -use embedded_graphics_simulator::{ - BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, Window, -}; - -fn main() -> Result<(), core::convert::Infallible> { - let mut display: SimulatorDisplay = SimulatorDisplay::new(Size::new(128, 64)); - - // Create styles used by the drawing operations. - let thin_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 1); - let thick_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 3); - let fill = PrimitiveStyle::with_fill(BinaryColor::On); - let text_style = MonoTextStyle::new(Font6x8, BinaryColor::On); - - let yoffset = 10; - - // Draw an 3px wide outline around the display. - Rectangle::new(Point::zero(), display.size()) - .into_styled(thick_stroke) - .into_pixels() - .chain( - // Draw a triangle. - Triangle::new( - Point::new(16, 16 + yoffset), - Point::new(16 + 16, 16 + yoffset), - Point::new(16 + 8, yoffset), - ) - .into_styled(thin_stroke) - .into_pixels(), - ) - .chain( - // Draw a filled square - Rectangle::new(Point::new(52, yoffset), Size::new(16, 16)) - .into_styled(fill) - .into_pixels(), - ) - .chain( - // Draw a circle with a 3px wide stroke. - Circle::new(Point::new(88, yoffset), 17) - .into_styled(thick_stroke) - .into_pixels(), - ) - .chain({ - // Draw centered text. - let text = "embedded-graphics"; - let width = text.len() as i32 * 6; - - Text::new(text, Point::new(64 - width / 2, 40)) - .into_styled(text_style) - .into_pixels() - }) - .draw(&mut display)?; - - let output_settings = OutputSettingsBuilder::new() - .theme(BinaryColorTheme::OledBlue) - .build(); - - Window::new("Chained drawing", &output_settings).show_static(&display); - - Ok(()) -} From 0f93f5d1d431d58b135cded9ef19d34e4e8840b9 Mon Sep 17 00:00:00 2001 From: Ralf Fuest Date: Mon, 16 Nov 2020 21:34:48 +0100 Subject: [PATCH 2/2] Update primitives-stroke example --- examples/primitives-stroke.rs | 145 ++++++++++++---------------------- 1 file changed, 51 insertions(+), 94 deletions(-) diff --git a/examples/primitives-stroke.rs b/examples/primitives-stroke.rs index 85087a5..878c7f8 100644 --- a/examples/primitives-stroke.rs +++ b/examples/primitives-stroke.rs @@ -12,109 +12,66 @@ use embedded_graphics_simulator::{OutputSettings, SimulatorDisplay, Window}; const PADDING: i32 = 16; -fn main() -> Result<(), core::convert::Infallible> { - let mut display: SimulatorDisplay = SimulatorDisplay::new(Size::new(512, 256)); +/// Draws all embedded-graphics primitives. +fn draw_primitives(target: &mut D, style: PrimitiveStyle) -> Result<(), D::Error> +where + D: DrawTarget, +{ + Triangle::new(Point::new(0, 64), Point::new(64, 0), Point::new(64, 64)) + .into_styled(style) + .draw(target)?; - let thin_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 1); - let medium_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 3); - let thick_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 10); + Rectangle::new(Point::new(0, 0), Size::new(64, 64)) + .translate(Point::new(64 + PADDING, 0)) + .into_styled(style) + .draw(target)?; - let triangle = Triangle::new(Point::new(0, 64), Point::new(64, 0), Point::new(64, 64)); - let rectangle = - Rectangle::new(Point::new(0, 0), Size::new(64, 64)).translate(Point::new(64 + PADDING, 0)); - let line = Line::new(Point::new(0, 0), Point::new(64, 64)) - .translate(Point::new((64 + PADDING) * 2, 0)); - let circle = Circle::new(Point::new(0, 0), 64).translate(Point::new((64 + PADDING) * 3, 0)); - let rounded_rectangle = RoundedRectangle::new( + Line::new(Point::new(0, 0), Point::new(64, 64)) + .translate(Point::new((64 + PADDING) * 2, 0)) + .into_styled(style) + .draw(target)?; + + Circle::new(Point::new(0, 0), 64) + .translate(Point::new((64 + PADDING) * 3, 0)) + .into_styled(style) + .draw(target)?; + + RoundedRectangle::new( Rectangle::new(Point::new(0, 0), Size::new(64, 64)), CornerRadii::new(Size::new(16, 16)), ) - .translate(Point::new((64 + PADDING) * 4, 0)); + .translate(Point::new((64 + PADDING) * 4, 0)) + .into_styled(style) + .draw(target)?; - let ellipse = Ellipse::new(Point::new(0, 0), Size::new(96, 64)) - .translate(Point::new((64 + PADDING) * 5, 0)); + Ellipse::new(Point::new(0, 0), Size::new(96, 64)) + .translate(Point::new((64 + PADDING) * 5, 0)) + .into_styled(style) + .draw(target) +} + +fn main() -> Result<(), core::convert::Infallible> { + let mut display = SimulatorDisplay::::new(Size::new(512, 256)); + + let thin_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 1); + let medium_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 3); + let thick_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 10); - circle - .into_styled(thin_stroke) - .into_pixels() - .chain(rectangle.into_styled(thin_stroke).into_pixels()) - .chain(line.into_styled(thin_stroke).into_pixels()) - .chain(triangle.into_styled(thin_stroke).into_pixels()) - .chain(ellipse.into_styled(thin_stroke).into_pixels()) - .chain(rounded_rectangle.into_styled(thin_stroke).into_pixels()) - .draw(&mut display)?; + // Draw the primitives using a thin stroke. + // + // Instead of directly drawing to the display a `TranslatedDrawTarget` is created by + // using `display.translated(position)`. This translates all drawing operations in `draw_shapes` + // by 10 pixels in the x and y direction. + let mut position = Point::new(10, 10); + draw_primitives(&mut display.translated(position), thin_stroke)?; - let offset = Point::new(0, 64 + PADDING); - circle - .translate(offset) - .into_styled(medium_stroke) - .into_pixels() - .chain( - rectangle - .translate(offset) - .into_styled(medium_stroke) - .into_pixels(), - ) - .chain( - line.translate(offset) - .into_styled(medium_stroke) - .into_pixels(), - ) - .chain( - triangle - .translate(offset) - .into_styled(medium_stroke) - .into_pixels(), - ) - .chain( - ellipse - .translate(offset) - .into_styled(medium_stroke) - .into_pixels(), - ) - .chain( - rounded_rectangle - .translate(offset) - .into_styled(medium_stroke) - .into_pixels(), - ) - .draw(&mut display)?; + // Draw the primitives using a medium stroke. + position.y += 64 + PADDING; + draw_primitives(&mut display.translated(position), medium_stroke)?; - let offset = Point::new(0, (64 + PADDING) * 2); - circle - .translate(offset) - .into_styled(thick_stroke) - .into_pixels() - .chain( - rectangle - .translate(offset) - .into_styled(thick_stroke) - .into_pixels(), - ) - .chain( - line.translate(offset) - .into_styled(thick_stroke) - .into_pixels(), - ) - .chain( - triangle - .translate(offset) - .into_styled(thick_stroke) - .into_pixels(), - ) - .chain( - ellipse - .translate(offset) - .into_styled(thick_stroke) - .into_pixels(), - ) - .chain( - rounded_rectangle - .translate(offset) - .into_styled(thick_stroke) - .into_pixels(), - ) - .draw(&mut display)?; + // Draw the primitives using a thick stroke. + position.y += 64 + PADDING; + draw_primitives(&mut display.translated(position), thick_stroke)?; Window::new("Strokes", &OutputSettings::default()).show_static(&display);