Add Drawing trait

This commit is contained in:
Joscha 2024-03-30 12:12:09 +01:00
parent 3381c6e165
commit c85c4ee14b

View file

@ -16,9 +16,22 @@ use tokio::sync::mpsc;
use crate::printer::Printer; use crate::printer::Printer;
#[derive(Default)]
pub struct Context {
font_stuff: FontStuff,
}
pub trait Drawing {
fn draw(&self, printer: &mut Printer, ctx: &mut Context) -> anyhow::Result<()>;
}
pub struct BoxedDrawing(Box<dyn Drawing + Send>);
pub enum Command { pub enum Command {
Stop, Stop,
Rip, Rip,
Draw(BoxedDrawing),
Test, Test,
Text(String), Text(String),
Image { image: RgbaImage, bright: bool }, Image { image: RgbaImage, bright: bool },
@ -28,9 +41,10 @@ pub enum Command {
Cells { rule: u8, rows: u32, scale: u32 }, Cells { rule: u8, rows: u32, scale: u32 },
} }
#[derive(Default)] impl Command {
struct Context { pub fn draw<D: Drawing + Send + 'static>(drawing: D) -> Self {
font_stuff: FontStuff, Self::Draw(BoxedDrawing(Box::new(drawing)))
}
} }
impl HasFontStuff for Context { impl HasFontStuff for Context {
@ -71,6 +85,8 @@ impl Drawer {
match command { match command {
Command::Stop => {} // Already handled one level above Command::Stop => {} // Already handled one level above
Command::Rip => self.printer.rip()?, Command::Rip => self.printer.rip()?,
Command::Draw(drawing) => drawing.0.draw(&mut self.printer, &mut self.ctx)?,
Command::Test => self.on_test()?, Command::Test => self.on_test()?,
Command::Text(text) => self.on_text(text)?, Command::Text(text) => self.on_text(text)?,
Command::Image { image, bright } => self.on_image(image, bright)?, Command::Image { image, bright } => self.on_image(image, bright)?,