From eebc9e920d35305aa1fa8b969eb9540609955a8d Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Sat, 27 May 2023 17:19:16 -0500 Subject: [PATCH] added: mask struct --- src/cells.rs | 10 ++++-- src/cli.rs | 96 ++++++++++++++++++++++++++++++++++++++++++++++------ src/main.rs | 5 ++- 3 files changed, 97 insertions(+), 14 deletions(-) diff --git a/src/cells.rs b/src/cells.rs index e5d9b1b..ca92d48 100644 --- a/src/cells.rs +++ b/src/cells.rs @@ -16,9 +16,13 @@ pub struct Map { } impl Map { - pub fn new(rows: usize, cols: usize) -> Self { - let map = Array2::from_elem((rows, cols), Cell::new(State::Dead)); - Self { map, rows, cols } + pub fn new(x: usize, y: usize) -> Self { + let map = Array2::from_elem((y, x), Cell::new(State::Dead)); + Self { + map, + cols: x, + rows: y, + } } pub fn update(&mut self) { diff --git a/src/cli.rs b/src/cli.rs index 96bda3f..0ef0a8f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,17 +1,82 @@ use crate::{Map, State}; use crossterm::{ - cursor, execute, queue, + cursor, + event::{self, read, Event}, + execute, queue, style::{self, Stylize}, terminal, Result, }; -use std::io::{stdout, Write}; +use ndarray::Array2; +use std::{ + io::{stdout, Write}, + ops::Deref, + thread, + time::Duration, +}; + +pub type Origin = (usize, usize); + +#[derive(Clone, Debug)] +pub struct Mask { + pub cols: usize, + pub rows: usize, + pub mask: Array2>, +} + +impl Mask { + pub fn new(x: usize, y: usize) -> Self { + let mask = Array2::from_elem((y, x), None); + Self { + cols: x, + rows: y, + mask, + } + } +} -pub fn draw_frame(map: &Map, origin: (usize, usize)) -> Result<()> { - for y in 1..(map.cols - 1) { - for x in 1..(map.rows - 1) { +impl Deref for Mask { + type Target = Array2>; + fn deref(&self) -> &Self::Target { + &self.mask + } +} + +pub fn edit_mask(mask: &Mask, origin: Origin) -> Result<()> { + todo!() +} + +pub fn draw_mask(mask: &Mask, origin: Origin) -> Result<()> { + execute!(stdout(), terminal::Clear(terminal::ClearType::All))?; + for x in 1..(mask.cols - 1) { + for y in 1..(mask.rows - 1) { let x_off: u16 = (x + origin.0).try_into().unwrap(); let y_off: u16 = (y + origin.1).try_into().unwrap(); - if map.get((x, y)).unwrap().get() == State::Alive { + if mask.get((y, x)).unwrap().is_some() { + queue!( + stdout(), + cursor::Hide, + cursor::MoveTo(x_off, y_off), + style::PrintStyledContent("■".green()) + )?; + } else { + queue!( + stdout(), + cursor::Hide, + cursor::MoveTo(x_off, y_off), + style::PrintStyledContent("□".grey()) + )?; + } + } + } + stdout().flush() +} + +pub fn draw_frame(map: &Map, origin: Origin) -> Result<()> { + for x in 1..(map.cols - 1) { + for y in 1..(map.rows - 1) { + let x_off: u16 = (x + origin.0).try_into().unwrap(); + let y_off: u16 = (y + origin.1).try_into().unwrap(); + if map.get((y, x)).unwrap().get() == State::Alive { queue!( stdout(), cursor::Hide, @@ -31,15 +96,26 @@ pub fn draw_frame(map: &Map, origin: (usize, usize)) -> Result<()> { stdout().flush() } -pub fn run_map(map: &mut Map, origin: (usize, usize), time: usize) -> Result<()> { - let mut stdout = stdout(); +pub fn run_map(map: &mut Map, origin: Origin, time: Duration) -> Result<()> { loop { map.update(); - execute!(stdout, terminal::Clear(terminal::ClearType::All))?; + execute!(stdout(), terminal::Clear(terminal::ClearType::All))?; draw_frame(&map, origin)?; - std::thread::sleep(std::time::Duration::from_millis(time.try_into().unwrap())); + thread::sleep(time); + } +} + +pub fn loop_map(map: &mut Map, origin: (usize, usize), time: Duration, steps: usize) -> Result<()> { + loop { + let mut tmp = map.clone(); + for _ in 0..steps { + execute!(stdout(), terminal::Clear(terminal::ClearType::All))?; + draw_frame(&tmp, origin)?; + tmp.update(); + thread::sleep(time); + } } } diff --git a/src/main.rs b/src/main.rs index 7014fce..c5a7442 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,12 @@ use cellseq::*; use eyre::Result; fn main() -> Result<()> { + let mut mask: Mask = Mask::new(48, 24); let mut map = Map::new(48, 24); map.randomize(0.75); - run_map(&mut map, (10, 5), 300)?; + // loop_map(&mut map, (10, 5), std::time::Duration::from_millis(250), 32)?; + // draw_mask::(&mask, (10, 5))?; + edit_mask(&mask, (10, 5))?; Ok(()) } -- 2.45.2