Trait embedded_graphics_core::Drawable[][src]

pub trait Drawable {
    type Color: PixelColor;
    fn draw<D>(&self, display: &mut D) -> Result<(), D::Error>
    where
        D: DrawTarget<Color = Self::Color>
; }

Marks an object as "drawable". Must be implemented for all graphics objects

The Drawable trait describes how a particular graphical object is drawn. A Drawable object can define its draw method as a collection of graphical primitives or as an iterator over pixels being rendered with DrawTarget's draw_iter method.

use embedded_graphics::{
    mono_font::{ascii::Font6x9, MonoTextStyle},
    pixelcolor::{BinaryColor, PixelColor, Rgb888},
    prelude::*,
    primitives::{Rectangle, PrimitiveStyle},
    text::Text,
};

struct Button<'a, C: PixelColor> {
    top_left: Point,
    size: Size,
    bg_color: C,
    fg_color: C,
    text: &'a str,
}

impl<C> Drawable for Button<'_, C>
where
    C: PixelColor + From<BinaryColor>,
{
    type Color = C;

    fn draw<D>(&self, display: &mut D) -> Result<(), D::Error>
    where
        D: DrawTarget<Color = C>,
    {
        Rectangle::new(self.top_left, self.size)
            .into_styled(PrimitiveStyle::with_fill(self.bg_color))
            .draw(display)?;

        Text::new(self.text, Point::new(6, 13))
            .into_styled(MonoTextStyle::new(Font6x9, self.fg_color))
            .draw(display)
    }
}

let mut button = Button {
    top_left: Point::zero(),
    size: Size::new(60, 20),
    bg_color: Rgb888::RED,
    fg_color: Rgb888::BLUE,
    text: "Click me!",
};

button.draw(&mut display)?;

Associated Types

type Color: PixelColor[src]

The pixel color type.

Loading content...

Required methods

fn draw<D>(&self, display: &mut D) -> Result<(), D::Error> where
    D: DrawTarget<Color = Self::Color>, 
[src]

Draw the graphics object using the supplied DrawTarget.

Loading content...

Implementors

impl<C> Drawable for Pixel<C> where
    C: PixelColor
[src]

type Color = C

Loading content...