basic rule editing

This commit is contained in:
Crispy 2024-05-01 00:05:20 +02:00
parent aa4cec6dbf
commit 365035a64d
2 changed files with 20 additions and 4 deletions

View file

@ -3,7 +3,7 @@ use rand::prelude::*;
pub const CHUNK_SIZE: usize = 32; pub const CHUNK_SIZE: usize = 32;
#[derive(Default, Debug, PartialEq, Clone, Copy)] #[derive(Default, Debug, PartialEq, Clone, Copy)]
pub struct Cell(u16); pub struct Cell(pub u16);
#[derive(Debug)] #[derive(Debug)]
pub struct Dish { 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 { pub fn height(&self) -> usize {
self.height self.height
} }

View file

@ -1,5 +1,5 @@
use eframe::{ use eframe::{
egui::{CentralPanel, Color32, Painter, Rect, SidePanel, Ui, Vec2}, egui::{CentralPanel, Color32, Painter, Rect, Sense, SidePanel, Ui, Vec2},
NativeOptions, NativeOptions,
}; };
use petri::{Chunk, Dish, Rule, CHUNK_SIZE}; 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, bounds.min + Vec2::from((x as f32, y as f32)) * CSIZE,
Vec2::splat(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; let color = cells[cell.id()].color;
ui.painter().rect(rect, 2., color, OUTLINE); 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 rect = rect.translate(Vec2::X * (patt_width as f32 + 1.) * CSIZE);
let color = cells[cell.id()].color; let color = cells[cell.id()].color;
ui.painter().rect(rect, 2., color, OUTLINE); 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;
}
} }
} }
} }