diff --git a/showbits-thermal-printer/src/drawer.rs b/showbits-thermal-printer/src/drawer.rs index 378c0ba..0f3ca49 100644 --- a/showbits-thermal-printer/src/drawer.rs +++ b/showbits-thermal-printer/src/drawer.rs @@ -5,6 +5,7 @@ mod egg; mod image; mod photo; mod text; +mod tictactoe; use showbits_common::widgets::{FontStuff, HasFontStuff}; use tokio::sync::mpsc; @@ -14,6 +15,7 @@ use crate::printer::Printer; pub use self::{ calendar::CalendarDrawing, cells::CellsDrawing, chat_message::ChatMessageDrawing, egg::EggDrawing, image::ImageDrawing, photo::PhotoDrawing, text::TextDrawing, + tictactoe::TicTacToeDrawing, }; #[derive(Default)] diff --git a/showbits-thermal-printer/src/drawer/tictactoe.rs b/showbits-thermal-printer/src/drawer/tictactoe.rs new file mode 100644 index 0000000..62da12a --- /dev/null +++ b/showbits-thermal-printer/src/drawer/tictactoe.rs @@ -0,0 +1,72 @@ +use showbits_common::{ + color::{BLACK, WHITE}, + widgets::{Block, Text}, + Node, Tree, WidgetExt, +}; +use taffy::{ + style_helpers::{length, percent, repeat}, + AlignItems, Display, FlexDirection, +}; + +use crate::printer::Printer; + +use super::{Context, Drawing}; + +pub struct TicTacToeDrawing; + +impl Drawing for TicTacToeDrawing { + fn draw(&self, printer: &mut Printer, ctx: &mut Context) -> anyhow::Result<()> { + let block_size = length(128.0); + let width = length(2.0); + + let mut tree = Tree::::new(WHITE); + + let mut grid = Node::empty() + .with_display(Display::Grid) + .with_grid_template_columns(vec![repeat(3, vec![block_size])]) + .with_grid_auto_rows(vec![block_size]); + + for y in 0..3 { + for x in 0..3 { + let mut block = Block::new().with_border(BLACK).node(); + + if x >= 1 { + block = block.with_border_left(width); + } + if x <= 1 { + block = block.with_border_right(width); + } + + if y >= 1 { + block = block.with_border_top(width); + } + if y <= 1 { + block = block.with_border_bottom(width); + } + + grid = grid.and_child(block.register(&mut tree)?); + } + } + + let title = Text::new() + .with_metrics(Text::default_metrics().scale(2.0)) + .and_plain("Tick-Tack-Zeh") + .widget(&mut ctx.font_stuff) + .node() + .register(&mut tree)?; + + let root = Node::empty() + .with_size_width(percent(1.0)) + .with_display(Display::Flex) + .with_flex_direction(FlexDirection::Column) + .with_align_items(Some(AlignItems::Center)) + .with_gap(length(16.0)) + .and_child(title) + .and_child(grid.register(&mut tree)?) + .register(&mut tree)?; + + printer.print_tree(&mut tree, ctx, root)?; + printer.feed()?; + Ok(()) + } +} diff --git a/showbits-thermal-printer/src/server.rs b/showbits-thermal-printer/src/server.rs index 12973be..a2268a6 100644 --- a/showbits-thermal-printer/src/server.rs +++ b/showbits-thermal-printer/src/server.rs @@ -14,7 +14,7 @@ use tokio::{net::TcpListener, sync::mpsc}; use crate::drawer::{ CalendarDrawing, CellsDrawing, ChatMessageDrawing, Command, EggDrawing, ImageDrawing, - PhotoDrawing, TextDrawing, + PhotoDrawing, TextDrawing, TicTacToeDrawing, }; use self::{r#static::get_static_file, statuscode::status_code}; @@ -33,6 +33,7 @@ pub async fn run(tx: mpsc::Sender, addr: String) -> anyhow::Result<()> .route("/image", post(post_image).fallback(get_static_file)) .route("/photo", post(post_photo).fallback(get_static_file)) .route("/text", post(post_text)) + .route("/tictactoe", post(post_tictactoe)) .fallback(get(get_static_file)) .layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB .with_state(Server { tx }); @@ -184,3 +185,9 @@ async fn post_text(server: State, request: Form) { .send(Command::draw(TextDrawing(request.0.text))) .await; } + +// /tictactoe + +async fn post_tictactoe(server: State) { + let _ = server.tx.send(Command::draw(TicTacToeDrawing)).await; +}