[][src]Trait embedded_graphics::Drawable

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::{
    fonts::{Font6x8, Text},
    pixelcolor::{BinaryColor, PixelColor, Rgb888},
    prelude::*,
    primitives::Rectangle,
    style::{PrimitiveStyle, TextStyle},
};

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, 6))
            .into_styled(TextStyle::new(Font6x8, 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

The pixel color type.

Loading content...

Required methods

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

Draw the graphics object using the supplied DrawTarget.

Loading content...

Implementors

impl<'a, C> Drawable for Styled<Polyline<'a>, PrimitiveStyle<C>> where
    C: PixelColor
[src]

type Color = C

impl<'a, T> Drawable for Image<'a, T> where
    T: ImageDrawable
[src]

type Color = T::Color

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

type Color = C

impl<C> Drawable for Styled<Arc, PrimitiveStyle<C>> where
    C: PixelColor
[src]

type Color = C

impl<C> Drawable for Styled<Circle, PrimitiveStyle<C>> where
    C: PixelColor
[src]

type Color = C

impl<C> Drawable for Styled<Ellipse, PrimitiveStyle<C>> where
    C: PixelColor
[src]

type Color = C

impl<C> Drawable for Styled<Line, PrimitiveStyle<C>> where
    C: PixelColor
[src]

type Color = C

impl<C> Drawable for Styled<Rectangle, PrimitiveStyle<C>> where
    C: PixelColor
[src]

type Color = C

impl<C> Drawable for Styled<RoundedRectangle, PrimitiveStyle<C>> where
    C: PixelColor
[src]

type Color = C

impl<C> Drawable for Styled<Sector, PrimitiveStyle<C>> where
    C: PixelColor
[src]

type Color = C

impl<C> Drawable for Styled<Triangle, PrimitiveStyle<C>> where
    C: PixelColor
[src]

type Color = C

impl<C, F, '_> Drawable for Styled<Text<'_>, TextStyle<C, F>> where
    C: PixelColor,
    F: Font + Copy
[src]

type Color = C

Loading content...