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/lib.rs b/src/lib.rs index 42f802d..a42dbb2 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}; @@ -182,15 +184,31 @@ impl Universe { } } -impl fmt::Display for Universe { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - for line in self.cells.as_slice().chunks(self.width as usize) { - for &cell in line { - let symbol = if cell == Cell::Dead { ' ' } else { '◼' }; // ◻ - write!(f, "{symbol} ")?; +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, + } } - writeln!(f)?; } - Ok(()) } } + +// impl fmt::Display for Universe { +// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { +// writeln!(f, "╭{}╮\r", "─".repeat(self.width as usize * 2))?; +// for line in self.cells.as_slice().chunks(self.width as usize) { +// write!(f, "│")?; +// for &cell in line { +// let symbol = if cell == Cell::Dead { ' ' } else { '◼' }; // ◻ +// write!(f, "{symbol} ")?; +// } +// writeln!(f, "│\r")?; +// } +// writeln!(f, "╰{}╯\r", "─".repeat(self.width as usize * 2))?; +// Ok(()) +// } +// } diff --git a/src/main.rs b/src/main.rs index 7c8a1f6..e48f038 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 + 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 { diff --git a/src/ui.rs b/src/ui.rs index d767f98..7a07226 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, }; @@ -28,17 +28,25 @@ 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); - f.render_widget( - universe, - Rect::new( - 0, - 0, - main_chunks[0].height * 2 - 4, - main_chunks[0].height - 1, - ), - ); + 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, + // ), + // ); let footer = Layout::default() .direction(Direction::Horizontal)