diff --git a/showbits-thermal-printer/src/documents.rs b/showbits-thermal-printer/src/documents.rs index 19a9d6d..36c445a 100644 --- a/showbits-thermal-printer/src/documents.rs +++ b/showbits-thermal-printer/src/documents.rs @@ -5,6 +5,7 @@ pub mod cells; pub mod egg; pub mod image; pub mod text; +pub mod tictactoe; fn typst_with_lib() -> Typst { Typst::new() diff --git a/showbits-thermal-printer/src/documents/tictactoe/data.json b/showbits-thermal-printer/src/documents/tictactoe/data.json new file mode 100644 index 0000000..20bfab5 --- /dev/null +++ b/showbits-thermal-printer/src/documents/tictactoe/data.json @@ -0,0 +1,3 @@ +{ + "feed": false +} diff --git a/showbits-thermal-printer/src/documents/tictactoe/lib b/showbits-thermal-printer/src/documents/tictactoe/lib new file mode 120000 index 0000000..dc598c5 --- /dev/null +++ b/showbits-thermal-printer/src/documents/tictactoe/lib @@ -0,0 +1 @@ +../lib \ No newline at end of file diff --git a/showbits-thermal-printer/src/documents/tictactoe/main.typ b/showbits-thermal-printer/src/documents/tictactoe/main.typ new file mode 100644 index 0000000..256cb75 --- /dev/null +++ b/showbits-thermal-printer/src/documents/tictactoe/main.typ @@ -0,0 +1,20 @@ +#import "@preview/cetz:0.3.2" +#import "lib/main.typ" as lib; +#show: it => lib.init(it) + +#let data = json("data.json") + +#cetz.canvas( + length: lib.width, + { + import cetz.draw: * + line((1 / 3, 0), (1 / 3, 1), stroke: 4pt) + line((2 / 3, 0), (2 / 3, 1), stroke: 4pt) + line((0, 1 / 3), (1, 1 / 3), stroke: 4pt) + line((0, 2 / 3), (1, 2 / 3), stroke: 4pt) + }, +) + +#if data.feed { + lib.feed +} diff --git a/showbits-thermal-printer/src/documents/tictactoe/mod.rs b/showbits-thermal-printer/src/documents/tictactoe/mod.rs new file mode 100644 index 0000000..47ae8a0 --- /dev/null +++ b/showbits-thermal-printer/src/documents/tictactoe/mod.rs @@ -0,0 +1,34 @@ +use axum::{Form, extract::State}; +use serde::{Deserialize, Serialize}; + +use crate::{ + drawer::{Command, NewTypstDrawing}, + server::{Server, somehow}, +}; + +#[derive(Serialize)] +struct Data { + feed: bool, +} + +#[derive(Deserialize)] +pub struct FormData { + pub feed: Option, +} + +pub async fn post(server: State, Form(form): Form) -> somehow::Result<()> { + let data = Data { + feed: form.feed.unwrap_or(true), + }; + + let typst = super::typst_with_lib() + .with_json("/data.json", &data) + .with_main_file(include_str!("main.typ")); + + let _ = server + .tx + .send(Command::draw(NewTypstDrawing::new(typst))) + .await; + + Ok(()) +} diff --git a/showbits-thermal-printer/src/drawer.rs b/showbits-thermal-printer/src/drawer.rs index 7e92d89..3fa4798 100644 --- a/showbits-thermal-printer/src/drawer.rs +++ b/showbits-thermal-printer/src/drawer.rs @@ -1,7 +1,6 @@ mod backlog; mod chat_message; mod new_typst; -mod tictactoe; use showbits_common::widgets::{FontStuff, HasFontStuff}; use tokio::sync::mpsc; @@ -10,11 +9,8 @@ use crate::persistent_printer::PersistentPrinter; pub use self::{ backlog::BacklogDrawing, chat_message::ChatMessageDrawing, new_typst::NewTypstDrawing, - tictactoe::TicTacToeDrawing, }; -pub const FEED: f32 = 96.0; - #[derive(Default)] pub struct Context { font_stuff: FontStuff, diff --git a/showbits-thermal-printer/src/drawer/tictactoe.rs b/showbits-thermal-printer/src/drawer/tictactoe.rs deleted file mode 100644 index 3d167b6..0000000 --- a/showbits-thermal-printer/src/drawer/tictactoe.rs +++ /dev/null @@ -1,72 +0,0 @@ -use showbits_common::{ - Node, Tree, WidgetExt, - color::{BLACK, WHITE}, - widgets::{Block, Text}, -}; -use taffy::{ - AlignItems, Display, FlexDirection, - style_helpers::{length, percent, repeat}, -}; - -use crate::persistent_printer::PersistentPrinter; - -use super::{Context, Drawing, FEED}; - -pub struct TicTacToeDrawing; - -impl Drawing for TicTacToeDrawing { - fn draw(&self, printer: &mut PersistentPrinter, 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_padding_bottom(length(FEED)) - .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)?; - Ok(()) - } -} diff --git a/showbits-thermal-printer/src/server.rs b/showbits-thermal-printer/src/server.rs index b2b7dda..c06bea7 100644 --- a/showbits-thermal-printer/src/server.rs +++ b/showbits-thermal-printer/src/server.rs @@ -12,7 +12,7 @@ use tokio::{net::TcpListener, sync::mpsc}; use crate::{ documents, - drawer::{ChatMessageDrawing, Command, TicTacToeDrawing}, + drawer::{ChatMessageDrawing, Command}, }; use self::r#static::get_static_file; @@ -42,7 +42,10 @@ pub async fn run(tx: mpsc::Sender, addr: String) -> anyhow::Result<()> "/text", post(documents::text::post).fallback(get_static_file), ) - .route("/tictactoe", post(post_tictactoe)) + .route( + "/tictactoe", + post(documents::tictactoe::post).fallback(get_static_file), + ) .fallback(get(get_static_file)) .layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB .with_state(Server { tx }); @@ -69,9 +72,3 @@ async fn post_chat_message(server: State, request: Form) { - let _ = server.tx.send(Command::draw(TicTacToeDrawing)).await; -}