diff --git a/petri/src/lib.rs b/petri/src/lib.rs index 66d85a8..d206072 100644 --- a/petri/src/lib.rs +++ b/petri/src/lib.rs @@ -3,7 +3,7 @@ use rand::prelude::*; pub const CHUNK_SIZE: usize = 32; #[derive(Default, Debug, PartialEq, Clone, Copy)] -pub struct Cell(u16); +pub struct Cell(pub u16); #[derive(Debug)] pub struct Dish { @@ -172,6 +172,14 @@ 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 } diff --git a/uscope/src/main.rs b/uscope/src/main.rs index b9c2a80..796f3a1 100644 --- a/uscope/src/main.rs +++ b/uscope/src/main.rs @@ -1,5 +1,5 @@ use eframe::{ - egui::{CentralPanel, Color32, Painter, Rect, SidePanel, Ui, Vec2}, + egui::{CentralPanel, Color32, Painter, Rect, Sense, SidePanel, Ui, Vec2}, NativeOptions, }; use petri::{Chunk, Dish, Rule, CHUNK_SIZE}; @@ -92,15 +92,23 @@ 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(x, y) { + 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.to.get(x, y) { + if let Some(cell) = rule.to.get_mut(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; + } } } }