Add tic tac toe drawing

This commit is contained in:
Joscha 2024-03-31 13:38:04 +02:00
parent 28c44b526c
commit 9c4ee1a350
3 changed files with 82 additions and 1 deletions

View file

@ -5,6 +5,7 @@ mod egg;
mod image; mod image;
mod photo; mod photo;
mod text; mod text;
mod tictactoe;
use showbits_common::widgets::{FontStuff, HasFontStuff}; use showbits_common::widgets::{FontStuff, HasFontStuff};
use tokio::sync::mpsc; use tokio::sync::mpsc;
@ -14,6 +15,7 @@ use crate::printer::Printer;
pub use self::{ pub use self::{
calendar::CalendarDrawing, cells::CellsDrawing, chat_message::ChatMessageDrawing, calendar::CalendarDrawing, cells::CellsDrawing, chat_message::ChatMessageDrawing,
egg::EggDrawing, image::ImageDrawing, photo::PhotoDrawing, text::TextDrawing, egg::EggDrawing, image::ImageDrawing, photo::PhotoDrawing, text::TextDrawing,
tictactoe::TicTacToeDrawing,
}; };
#[derive(Default)] #[derive(Default)]

View file

@ -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::<Context>::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(())
}
}

View file

@ -14,7 +14,7 @@ use tokio::{net::TcpListener, sync::mpsc};
use crate::drawer::{ use crate::drawer::{
CalendarDrawing, CellsDrawing, ChatMessageDrawing, Command, EggDrawing, ImageDrawing, CalendarDrawing, CellsDrawing, ChatMessageDrawing, Command, EggDrawing, ImageDrawing,
PhotoDrawing, TextDrawing, PhotoDrawing, TextDrawing, TicTacToeDrawing,
}; };
use self::{r#static::get_static_file, statuscode::status_code}; use self::{r#static::get_static_file, statuscode::status_code};
@ -33,6 +33,7 @@ pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()>
.route("/image", post(post_image).fallback(get_static_file)) .route("/image", post(post_image).fallback(get_static_file))
.route("/photo", post(post_photo).fallback(get_static_file)) .route("/photo", post(post_photo).fallback(get_static_file))
.route("/text", post(post_text)) .route("/text", post(post_text))
.route("/tictactoe", post(post_tictactoe))
.fallback(get(get_static_file)) .fallback(get(get_static_file))
.layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB .layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB
.with_state(Server { tx }); .with_state(Server { tx });
@ -184,3 +185,9 @@ async fn post_text(server: State<Server>, request: Form<PostTextForm>) {
.send(Command::draw(TextDrawing(request.0.text))) .send(Command::draw(TextDrawing(request.0.text)))
.await; .await;
} }
// /tictactoe
async fn post_tictactoe(server: State<Server>) {
let _ = server.tx.send(Command::draw(TicTacToeDrawing)).await;
}