collapsable rule editors

This commit is contained in:
Crispy 2024-05-04 12:32:11 +02:00
parent 5c282a28ed
commit c345d671dd
2 changed files with 163 additions and 143 deletions

View file

@ -27,8 +27,8 @@ pub struct Rule {
variants: Vec<SubRule>, variants: Vec<SubRule>,
pub enabled: bool, pub enabled: bool,
// probability: u8 // probability: u8
pub flip_h: bool, pub flip_x: bool,
pub flip_v: bool, pub flip_y: bool,
pub rotate: bool, pub rotate: bool,
} }
@ -121,8 +121,8 @@ impl Rule {
enabled: false, enabled: false,
base: SubRule::new(), base: SubRule::new(),
variants: vec![SubRule::new()], variants: vec![SubRule::new()],
flip_h: false, flip_x: false,
flip_v: false, flip_y: false,
rotate: false, rotate: false,
} }
} }
@ -192,7 +192,7 @@ impl Rule {
variants.extend_from_slice(&new); variants.extend_from_slice(&new);
} }
if self.flip_h { if self.flip_x {
transform_variants(&mut self.variants, |b| { transform_variants(&mut self.variants, |b| {
let mut new = b.clone(); let mut new = b.clone();
for y in 0..new.height { for y in 0..new.height {
@ -204,7 +204,7 @@ impl Rule {
new new
}); });
} }
if self.flip_v { if self.flip_y {
transform_variants(&mut self.variants, |b| { transform_variants(&mut self.variants, |b| {
let mut new = b.clone(); let mut new = b.clone();
for y in 0..new.height { for y in 0..new.height {
@ -302,7 +302,7 @@ impl Dish {
(RuleCellFrom::One(Cell(0)), RuleCellTo::One(Cell(1))), (RuleCellFrom::One(Cell(0)), RuleCellTo::One(Cell(1))),
], ],
}, },
flip_h: true, flip_x: true,
..Rule::new() ..Rule::new()
}, },
]; ];

View file

@ -10,7 +10,7 @@ use eframe::{
epaint::Hsva, epaint::Hsva,
NativeOptions, NativeOptions,
}; };
use egui::PointerButton; use egui::{collapsing_header::CollapsingState, PointerButton};
use native_dialog::FileDialog; use native_dialog::FileDialog;
use rand::prelude::*; use rand::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -176,16 +176,16 @@ impl eframe::App for UScope {
let mut to_remove = None; let mut to_remove = None;
let mut to_clone = None; let mut to_clone = None;
for (i, rule) in self.dish.rules.iter_mut().enumerate() { for (i, rule) in self.dish.rules.iter_mut().enumerate() {
ui.separator(); // ui.separator();
rule_editor(ui, rule, &self.cell_types, &self.dish.cell_groups); rule_editor(
ui.horizontal(|ui| { ui,
if ui.button("delete").clicked() { rule,
to_remove = Some(i); i,
} &self.cell_types,
if ui.button("copy").clicked() { &self.dish.cell_groups,
to_clone = Some(i); &mut to_remove,
} &mut to_clone,
}); );
} }
if let Some(i) = to_remove { if let Some(i) = to_remove {
self.dish.rules.remove(i); self.dish.rules.remove(i);
@ -246,14 +246,33 @@ const CSIZE: f32 = 24.;
const RESIZE_BUTTON_WIDTH: f32 = 8.; const RESIZE_BUTTON_WIDTH: f32 = 8.;
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], groups: &[Vec<Option<Cell>>]) { fn rule_editor(
ui.checkbox(&mut rule.enabled, "enable rule"); ui: &mut Ui,
rule: &mut Rule,
index: usize,
cells: &[CellData],
groups: &[Vec<Option<Cell>>],
to_remove: &mut Option<usize>,
to_clone: &mut Option<usize>,
) {
let id = ui.make_persistent_id(format!("rule {index}"));
CollapsingState::load_with_default_open(ui.ctx(), id, true)
.show_header(ui, |ui| {
ui.checkbox(&mut rule.enabled, &rule.name);
if ui.button("delete").clicked() {
*to_remove = Some(index);
}
if ui.button("copy").clicked() {
*to_clone = Some(index);
}
})
.body(|ui| {
ui.text_edit_singleline(&mut rule.name);
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("flip"); if ui.checkbox(&mut rule.flip_x, "flip X").changed() {
if ui.checkbox(&mut rule.flip_h, "H").changed() {
rule.generate_variants(); rule.generate_variants();
} }
if ui.checkbox(&mut rule.flip_v, "V").changed() { if ui.checkbox(&mut rule.flip_y, "flip Y").changed() {
rule.generate_variants(); rule.generate_variants();
} }
}); });
@ -378,6 +397,7 @@ fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData], groups: &[Vec<O
for (a, b) in overlay_lines { for (a, b) in overlay_lines {
ui.painter().line_segment([a, b], (2., Color32::WHITE)); ui.painter().line_segment([a, b], (2., Color32::WHITE));
} }
});
} }
fn rule_cell_edit_from( fn rule_cell_edit_from(