make grid optional

This commit is contained in:
Crispy 2024-05-03 14:51:12 +02:00
parent bb8b272ed4
commit 69b8ec8bb6

View file

@ -1,5 +1,7 @@
use eframe::{ use eframe::{
egui::{CentralPanel, Color32, Painter, Pos2, Rect, Sense, SidePanel, Slider, Ui, Vec2}, egui::{
CentralPanel, Color32, Painter, Pos2, Rect, ScrollArea, Sense, SidePanel, Slider, Ui, Vec2,
},
epaint::Hsva, epaint::Hsva,
NativeOptions, NativeOptions,
}; };
@ -20,6 +22,7 @@ struct UScope {
dish: Dish, dish: Dish,
brush: Cell, brush: Cell,
speed: usize, speed: usize,
show_grid: bool,
celltypes: Vec<CellData>, celltypes: Vec<CellData>,
} }
@ -34,6 +37,7 @@ impl UScope {
Self { Self {
dish: Dish::new(), dish: Dish::new(),
speed: 250, speed: 250,
show_grid: false,
brush: Cell(1), brush: Cell(1),
celltypes: vec![ celltypes: vec![
CellData::new("air", 0, 0, 0), CellData::new("air", 0, 0, 0),
@ -53,6 +57,7 @@ impl eframe::App for UScope {
ui.heading("Simulation"); ui.heading("Simulation");
ui.label("speed"); ui.label("speed");
ui.add(Slider::new(&mut self.speed, 0..=5000)); ui.add(Slider::new(&mut self.speed, 0..=5000));
ui.checkbox(&mut self.show_grid, "show grid");
ui.separator(); ui.separator();
ui.heading("Cells"); ui.heading("Cells");
@ -76,25 +81,28 @@ impl eframe::App for UScope {
ui.separator(); ui.separator();
ui.heading("Rules"); ui.heading("Rules");
let mut to_remove = None; ScrollArea::vertical().show(ui, |ui| {
for (i, rule) in self.dish.rules.iter_mut().enumerate() { let mut to_remove = None;
rule_editor(ui, rule, &self.celltypes); for (i, rule) in self.dish.rules.iter_mut().enumerate() {
if ui.button("delete").clicked() { ui.separator();
to_remove = Some(i); rule_editor(ui, rule, &self.celltypes);
if ui.button("delete").clicked() {
to_remove = Some(i);
}
}
if let Some(i) = to_remove {
self.dish.rules.remove(i);
} }
ui.separator(); ui.separator();
} if ui.button("add rule").clicked() {
if let Some(i) = to_remove { self.dish.rules.push(Rule::new());
self.dish.rules.remove(i); }
} });
if ui.button("add rule").clicked() {
self.dish.rules.push(Rule::new());
}
}); });
CentralPanel::default().show(ctx, |ui| { CentralPanel::default().show(ctx, |ui| {
let bounds = ui.available_rect_before_wrap(); let bounds = ui.available_rect_before_wrap();
let painter = ui.painter_at(bounds); let painter = ui.painter_at(bounds);
paint_chunk(painter, &self.dish.chunk, &self.celltypes); paint_chunk(painter, &self.dish.chunk, &self.celltypes, self.show_grid);
let rect = ui.allocate_rect(bounds, Sense::click_and_drag()); let rect = ui.allocate_rect(bounds, Sense::click_and_drag());
if let Some(pos) = rect.interact_pointer_pos() { if let Some(pos) = rect.interact_pointer_pos() {
@ -108,7 +116,7 @@ impl eframe::App for UScope {
} }
const GRID_SIZE: f32 = 16.; const GRID_SIZE: f32 = 16.;
fn paint_chunk(painter: Painter, chunk: &Chunk, cells: &[CellData]) { fn paint_chunk(painter: Painter, chunk: &Chunk, cells: &[CellData], grid: bool) {
let bounds = painter.clip_rect(); let bounds = painter.clip_rect();
for x in 0..CHUNK_SIZE { for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE { for y in 0..CHUNK_SIZE {
@ -116,7 +124,11 @@ fn paint_chunk(painter: Painter, chunk: &Chunk, cells: &[CellData]) {
let corner = bounds.min + (Vec2::from((x as f32, y as f32)) * GRID_SIZE); let corner = bounds.min + (Vec2::from((x as f32, y as f32)) * GRID_SIZE);
let rect = Rect::from_min_size(corner, Vec2::splat(GRID_SIZE)); let rect = Rect::from_min_size(corner, Vec2::splat(GRID_SIZE));
let color = cells[cell.id()].color; let color = cells[cell.id()].color;
painter.rect(rect, 0., color, (1., Color32::GRAY)); if grid {
painter.rect(rect, 0., color, (1., Color32::GRAY));
} else {
painter.rect_filled(rect, 0., color);
}
} }
} }
} }
@ -125,12 +137,15 @@ const CSIZE: f32 = 24.;
const OUTLINE: (f32, Color32) = (2., Color32::GRAY); const OUTLINE: (f32, Color32) = (2., Color32::GRAY);
fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) { fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) {
ui.checkbox(&mut rule.enabled, "enable rule"); ui.checkbox(&mut rule.enabled, "enable rule");
if ui.checkbox(&mut rule.flip_h, "flip H").changed() { ui.horizontal(|ui| {
rule.generate_variants(); ui.label("flip");
} if ui.checkbox(&mut rule.flip_h, "H").changed() {
if ui.checkbox(&mut rule.flip_v, "flip V").changed() { rule.generate_variants();
rule.generate_variants(); }
} if ui.checkbox(&mut rule.flip_v, "V").changed() {
rule.generate_variants();
}
});
if ui.checkbox(&mut rule.rotate, "rotate").changed() { if ui.checkbox(&mut rule.rotate, "rotate").changed() {
rule.generate_variants(); rule.generate_variants();
} }