Trait embedded_graphics_core::Drawable [−][src]
pub trait Drawable {
type Color: PixelColor;
type Output;
fn draw<D>(&self, target: &mut D) -> Result<Self::Output, D::Error>
where
D: DrawTarget<Color = Self::Color>;
}
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::{
mono_font::{ascii::FONT_6X9, 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;
type Output = ();
fn draw<D>(&self, target: &mut D) -> Result<Self::Output, D::Error>
where
D: DrawTarget<Color = C>,
{
Rectangle::new(self.top_left, self.size)
.into_styled(PrimitiveStyle::with_fill(self.bg_color))
.draw(target)?;
let style = MonoTextStyle::new(&FONT_6X9, self.fg_color);
Text::new(self.text, Point::new(6, 13), style).draw(target)?;
Ok(())
}
}
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
type Color: PixelColor
The pixel color type.
The return type of the draw
method.
The Output
type can be used to return results and values produced from the drawing of the
current item. For example, rendering two differently styled text items next to each other
can make use of a returned value, allowing the next text to be positioned after the first:
use embedded_graphics::{
mono_font::{
ascii::{FONT_10X20, FONT_6X10},
MonoTextStyle,
},
pixelcolor::BinaryColor,
prelude::*,
text::Text,
};
let label_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
let value_style = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
let next_point = Text::new("Label ", Point::new(10, 20), label_style)
.draw(&mut display)?;
Text::new("1234", next_point, value_style).draw(&mut display)?;
Use ()
if no value should be returned.