From b673b3339c94b876764b485bd38be04e11725551 Mon Sep 17 00:00:00 2001 From: Jeromos Kovacs Date: Sun, 24 Mar 2024 17:32:51 +0100 Subject: [PATCH 1/3] fix: canvas usage --- src/lib.rs | 15 +++++++++++++++ src/main.rs | 4 ++-- src/ui.rs | 43 ++++++++++--------------------------------- 3 files changed, 27 insertions(+), 35 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4006869..20e2bde 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +use ratatui::{style::Color, widgets::canvas::Shape}; + use crate::shapes::HandleError; use std::{fmt, time::Duration}; @@ -185,6 +187,19 @@ impl Universe { } } +impl Shape for Universe { + fn draw(&self, painter: &mut ratatui::widgets::canvas::Painter) { + for y in 0..self.height { + for x in 0..self.width { + match self.cells.get(self.get_index(x, y)).unwrap() { + Cell::Alive => painter.paint(y.into(), x.into(), Color::White), + Cell::Dead => continue, + } + } + } + } +} + // impl fmt::Display for Universe { // fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // writeln!(f, "╭{}╮\r", "─".repeat(self.width as usize * 2))?; diff --git a/src/main.rs b/src/main.rs index 7c8a1f6..c5fc6b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ fn main() -> Result<(), Box> { // create app and run it with width and height from terminal size let wh = size()?; - let mut app = App::new(wh.1 - 4); + let mut app = App::new((wh.1 + 10) * 3); let res = run_app(&mut terminal, &mut app); @@ -52,7 +52,7 @@ fn run_app(terminal: &mut Terminal, app: &mut App) -> io::Result< loop { let wh = size()?; - app.set_wh(wh.1 + 1 - 5); + app.set_wh((wh.1 + 1 - 5) * 2); terminal.draw(|f| ui::ui(f, app))?; diff --git a/src/ui.rs b/src/ui.rs index bb056e4..9822421 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -3,7 +3,7 @@ use ratatui::{ layout::{Constraint, Direction, Layout, Rect}, style::{Color, Style}, text::{Line, Span}, - widgets::{Block, BorderType, Borders, Paragraph}, + widgets::{canvas::Canvas, Block, BorderType, Borders, Paragraph}, Frame, }; @@ -19,8 +19,6 @@ pub fn ui(f: &mut Frame, app: &App) { .constraints([Constraint::Min(0), Constraint::Length(1)]) .split(f.size()); - // let current_shape = shapes::get(app.wh, app.i()).unwrap(); - let main_chunks = Layout::default() .direction(Direction::Horizontal) .constraints([Constraint::Percentage(50)]) @@ -30,24 +28,15 @@ pub fn ui(f: &mut Frame, app: &App) { .borders(Borders::ALL) .border_type(BorderType::Rounded) .title("Conway's Game of Life"); - let universe = Paragraph::new(app.universe.to_string()).block(cgol); + // let universe = Paragraph::new(app.universe.to_string()).block(cgol); + // let universe = Canvas::new().block(cgol); + let universe = Canvas::default() + // .x_bounds([0., main_chunks[0].height as f64 * 2. - 4.]) + // .y_bounds([0., main_chunks[0].height as f64 * 2. - 4.]) + .paint(|ctx| ctx.draw(&app.universe)) + .block(cgol); - // let shape = Paragraph::new(current_shape.to_string()).block( - // Block::default() - // .borders(Borders::ALL) - // .border_type(BorderType::Rounded), - // ); - // f.render_widget(universe, Rect::new(0, 0, app.wh() * 2 + 2, app.wh() + 2)); - f.render_widget( - universe, - Rect::new( - 0, - 0, - main_chunks[0].height * 2 - 4, - main_chunks[0].height - 1, - ), - ); - // f.render_widget(shape, main_chunks[1]); + f.render_widget(universe, main_chunks[0]); let footer = Layout::default() .direction(Direction::Horizontal) @@ -58,7 +47,6 @@ pub fn ui(f: &mut Frame, app: &App) { "[q]uit, [r]estart, [R]eset, [n]ext, [p]revious, play[ ]pause, 'k': faster, 'j': slower", Style::default().fg(Color::Yellow), ); - // let key_notes_footer = Paragraph::new(current_keys_hint); let stat_style = Style::default().fg(Color::LightBlue); let poll_t = Span::styled( @@ -72,21 +60,10 @@ pub fn ui(f: &mut Frame, app: &App) { ), stat_style, ); - // let area_size = Span::styled(format!("Area size: {}", f.size()), stat_style); - // let wh = Span::styled(format!("wh: {:?}", app.wh()), stat_style); let div = Span::styled(" | ", Style::default().fg(Color::White)); - let current_stats = vec![ - current_keys_hint, - div.clone(), - poll_t, - // div.clone(), - // area_size, - // div.clone(), - // wh, - ]; + let current_stats = vec![current_keys_hint, div.clone(), poll_t]; let footer_data = Line::from(current_stats); - // f.render_widget(key_notes_footer, footer[0]); f.render_widget(footer_data, footer[0]); } From 61dbda9762fc11f0bc88fdf32f61059cdd30026a Mon Sep 17 00:00:00 2001 From: Jeromos Kovacs Date: Sun, 24 Mar 2024 17:53:56 +0100 Subject: [PATCH 2/3] fix: canvas size is quite correct --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index c5fc6b9..e48f038 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,7 +52,7 @@ fn run_app(terminal: &mut Terminal, app: &mut App) -> io::Result< loop { let wh = size()?; - app.set_wh((wh.1 + 1 - 5) * 2); + app.set_wh((wh.1 + 10) * 3); terminal.draw(|f| ui::ui(f, app))?; @@ -92,7 +92,7 @@ fn run_app(terminal: &mut Terminal, app: &mut App) -> io::Result< } else { // resize and restart let wh = size()?; - app.set_wh(wh.1 + 1 - 5); + app.set_wh((wh.1 + 10) * 3); app.restart(); } } else { From 246d02e9c1c8c923c43d41a795e8e755efca5a7f Mon Sep 17 00:00:00 2001 From: Jeromos Kovacs Date: Sun, 24 Mar 2024 18:09:59 +0100 Subject: [PATCH 3/3] fix: works --- src/app.rs | 6 +++--- src/ui.rs | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/app.rs b/src/app.rs index bd0fe24..d28990d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -41,9 +41,9 @@ impl App { pub fn i(&self) -> usize { self.i } - pub fn render_universe(&self) { - println!("{}", self.universe); - } + // pub fn render_universe(&self) { + // println!("{}", self.universe); + // } pub fn wh(&self) -> u16 { self.wh } diff --git a/src/ui.rs b/src/ui.rs index aba4740..7a07226 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -38,15 +38,15 @@ pub fn ui(f: &mut Frame, app: &App) { f.render_widget(universe, main_chunks[0]); - f.render_widget( - universe, - Rect::new( - 0, - 0, - main_chunks[0].height * 2 - 4, - main_chunks[0].height - 1, - ), - ); + // f.render_widget( + // universe, + // Rect::new( + // 0, + // 0, + // main_chunks[0].height * 2 - 4, + // main_chunks[0].height - 1, + // ), + // ); let footer = Layout::default() .direction(Direction::Horizontal)