Trait embedded_graphics::drawable::Drawable
source · [−]pub trait Drawable<C> where
C: PixelColor, {
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)?;