embedded_graphics::drawable

Trait Drawable

Source
pub trait Drawable<C>
where C: PixelColor,
{ // Required method fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error>; }
Expand description

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::{
    egrectangle, egtext,
    fonts::Font6x8,
    geometry::Point,
    pixelcolor::{BinaryColor, PixelColor, Rgb888},
    prelude::*,
    primitive_style, text_style,
};

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

impl<'a, C: 'a> Drawable<C> for &Button<'a, C>
where
    C: PixelColor + From<BinaryColor>,
{
    fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error> {
        egrectangle!(
            top_left = self.top_left,
            bottom_right = self.bottom_right,
            style = primitive_style!(fill_color = self.bg_color)
        )
        .draw(display);
        egtext!(
            text = self.text,
            top_left = (20, 20),
            style = text_style!(font = Font6x8, text_color = self.fg_color)
        )
        .draw(display)
    }
}

let mut button = Button {
    top_left: Point::zero(),
    bottom_right: Point::new(100, 50),
    bg_color: Rgb888::RED,
    fg_color: Rgb888::BLUE,
    text: "Click me!",
};

button.draw(&mut display)?;

Required Methods§

Source

fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error>

Draw the graphics object using the supplied DrawTarget.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<C, T> Drawable<C> for &mut T
where C: PixelColor, T: Iterator<Item = Pixel<C>>,

Source§

fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error>

Implementors§

Source§

impl<'a, 'b, I, C> Drawable<C> for &'a Image<'b, I, C>

Source§

impl<'a, C> Drawable<C> for &Styled<Circle, PrimitiveStyle<C>>
where C: PixelColor + 'a,

Source§

impl<'a, C> Drawable<C> for &Styled<Line, PrimitiveStyle<C>>
where C: PixelColor + 'a,

Source§

impl<'a, C> Drawable<C> for &Styled<Triangle, PrimitiveStyle<C>>
where C: PixelColor + 'a,

Source§

impl<C> Drawable<C> for &Styled<Rectangle, PrimitiveStyle<C>>
where C: PixelColor,

Source§

impl<C> Drawable<C> for Pixel<C>
where C: PixelColor,

Source§

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