diff --git a/petri/src/lib.rs b/petri/src/lib.rs index d206072..fdbe655 100644 --- a/petri/src/lib.rs +++ b/petri/src/lib.rs @@ -2,8 +2,11 @@ use rand::prelude::*; pub const CHUNK_SIZE: usize = 32; -#[derive(Default, Debug, PartialEq, Clone, Copy)] -pub struct Cell(pub u16); +#[derive(Debug, Default, PartialEq, Clone)] +pub struct Cell(pub u8, pub u8, pub u8); + +#[derive(Debug)] +pub struct Pos2(i8, i8); #[derive(Debug)] pub struct Dish { @@ -39,7 +42,7 @@ impl Chunk { for col in self.contents.iter_mut() { for cell in col.iter_mut() { if random::() % 4 == 0 { - *cell = Cell(1); + *cell = Cell::PINK; } } } @@ -59,42 +62,41 @@ impl Dish { pub fn new() -> Self { Self { chunk: Chunk::new().fill_random(), - rules: vec![ Rule { from: RulePattern { width: 1, height: 2, - contents: vec![Some(Cell(1)), Some(Cell(0))], + contents: vec![Some(Cell::PINK), Some(Cell::EMPTY)], }, to: RulePattern { width: 1, height: 2, - contents: vec![Some(Cell(0)), Some(Cell(1))], + contents: vec![Some(Cell::EMPTY), Some(Cell::PINK)], }, }, Rule { from: RulePattern { width: 2, height: 2, - contents: vec![Some(Cell(1)), None, Some(Cell(1)), Some(Cell(0))], + contents: vec![Some(Cell::PINK), None, Some(Cell::PINK), Some(Cell::EMPTY)], }, to: RulePattern { width: 2, height: 2, - contents: vec![Some(Cell(0)), None, Some(Cell(1)), Some(Cell(1))], + contents: vec![Some(Cell::EMPTY), None, Some(Cell::PINK), Some(Cell::PINK)], }, }, Rule { from: RulePattern { width: 2, height: 2, - contents: vec![None, Some(Cell(1)), Some(Cell(0)), Some(Cell(1))], + contents: vec![None, Some(Cell::PINK), Some(Cell::EMPTY), Some(Cell::PINK)], }, to: RulePattern { width: 2, height: 2, - contents: vec![None, Some(Cell(0)), Some(Cell(1)), Some(Cell(1))], + contents: vec![None, Some(Cell::EMPTY), Some(Cell::PINK), Some(Cell::PINK)], }, }, ], @@ -156,6 +158,12 @@ impl Dish { } } +impl Cell { + pub const EMPTY: Self = Self(0, 0, 0); + pub const WHITE: Self = Self(255, 255, 255); + pub const PINK: Self = Self(255, 147, 219); +} + #[derive(Debug)] pub struct RulePattern { width: usize, @@ -172,14 +180,6 @@ impl RulePattern { } } - pub fn get_mut(&mut self, x: usize, y: usize) -> Option<&mut Cell> { - if x >= self.width || y >= self.height { - None - } else { - self.contents[x + self.width * y].as_mut() - } - } - pub fn height(&self) -> usize { self.height } @@ -188,10 +188,3 @@ impl RulePattern { self.width } } - -impl Cell { - pub const EMPTY: Self = Cell(0); - pub fn id(&self) -> usize { - self.0 as usize - } -} diff --git a/uscope/src/main.rs b/uscope/src/main.rs index 796f3a1..ca98db5 100644 --- a/uscope/src/main.rs +++ b/uscope/src/main.rs @@ -1,5 +1,5 @@ use eframe::{ - egui::{CentralPanel, Color32, Painter, Rect, Sense, SidePanel, Ui, Vec2}, + egui::{CentralPanel, Color32, Painter, Rect, SidePanel, Ui, Vec2}, NativeOptions, }; use petri::{Chunk, Dish, Rule, CHUNK_SIZE}; @@ -16,24 +16,11 @@ fn main() { #[derive(Debug)] struct UScope { dish: Dish, - celltypes: Vec, -} - -#[derive(Default, Debug)] -pub struct CellData { - name: String, - color: Color32, } impl UScope { fn new(_cc: &eframe::CreationContext<'_>) -> Self { - Self { - dish: Dish::new(), - celltypes: vec![ - CellData::new("air", 0, 0, 0), - CellData::new("pink_sand", 255, 147, 219), - ], - } + Self { dish: Dish::new() } } } @@ -50,18 +37,18 @@ impl eframe::App for UScope { dbg!(&self.dish.rules); } for rule in &mut self.dish.rules { - rule_editor(ui, rule, &self.celltypes); + rule_editor(ui, rule); } }); CentralPanel::default().show(ctx, |ui| { let bounds = ui.available_rect_before_wrap(); let painter = ui.painter_at(bounds); - paint_chunk(painter, &self.dish.chunk, &self.celltypes); + paint_chunk(painter, &self.dish.chunk); }); } } -fn paint_chunk(painter: Painter, chunk: &Chunk, cells: &[CellData]) { +fn paint_chunk(painter: Painter, chunk: &Chunk) { let bounds = painter.clip_rect(); let size = 16.; for x in 0..CHUNK_SIZE { @@ -69,18 +56,17 @@ fn paint_chunk(painter: Painter, chunk: &Chunk, cells: &[CellData]) { let cell = &chunk.get_cell(x, y); let corner = bounds.min + (Vec2::from((x as f32, y as f32)) * size); let rect = Rect::from_min_size(corner, Vec2::splat(size)); - let color = cells[cell.id()].color; + let color = Color32::from_rgb(cell.0, cell.1, cell.2); painter.rect(rect, 0., color, (1., Color32::GRAY)); } } } -fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) { +fn rule_editor(ui: &mut Ui, rule: &mut Rule) { let patt_height = rule.from.height(); let patt_width = rule.from.height(); const CSIZE: f32 = 24.; - const OUTLINE: (f32, Color32) = (1., Color32::GRAY); let (_, bounds) = ui.allocate_space(Vec2::new( CSIZE * (patt_width * 2 + 1) as f32, @@ -92,33 +78,25 @@ fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) { bounds.min + Vec2::from((x as f32, y as f32)) * CSIZE, Vec2::splat(CSIZE), ); - if let Some(cell) = rule.from.get_mut(x, y) { - let color = cells[cell.id()].color; - ui.painter().rect(rect, 2., color, OUTLINE); - let a = ui.allocate_rect(rect, Sense::click()); - if a.clicked() { - cell.0 = (cell.0 + 1) % cells.len() as u16; - } + if let Some(cell) = rule.from.get(x, y) { + ui.painter().rect( + rect, + 2., + Color32::from_rgb(cell.0, cell.1, cell.2), + (1., Color32::GRAY), + ); } - if let Some(cell) = rule.to.get_mut(x, y) { + if let Some(cell) = rule.to.get(x, y) { let rect = rect.translate(Vec2::X * (patt_width as f32 + 1.) * CSIZE); - let color = cells[cell.id()].color; - ui.painter().rect(rect, 2., color, OUTLINE); - let a = ui.allocate_rect(rect, Sense::click()); - if a.clicked() { - cell.0 = (cell.0 + 1) % cells.len() as u16; - } + + ui.painter().rect( + rect, + 2., + Color32::from_rgb(cell.0, cell.1, cell.2), + (1., Color32::GRAY), + ); } } } } - -impl CellData { - fn new(name: &str, r: u8, g: u8, b: u8) -> Self { - Self { - name: name.to_owned(), - color: Color32::from_rgb(r, g, b), - } - } -}