move more ui to the scroll area, clippy fixes

This commit is contained in:
Crispy 2024-05-03 23:25:56 +02:00
parent 4189c5188e
commit 33f706928c
2 changed files with 101 additions and 98 deletions

View file

@ -18,7 +18,7 @@ pub struct Chunk {
pub contents: Box<[[Cell; CHUNK_SIZE]; CHUNK_SIZE]>, pub contents: Box<[[Cell; CHUNK_SIZE]; CHUNK_SIZE]>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Default, Serialize, Deserialize)]
pub struct Rule { pub struct Rule {
base: SubRule, base: SubRule,
#[serde(skip)] #[serde(skip)]
@ -61,6 +61,12 @@ pub enum RuleCellTo {
Copy(usize), Copy(usize),
} }
impl std::default::Default for SubRule {
fn default() -> Self {
Self::new()
}
}
impl SubRule { impl SubRule {
fn new() -> Self { fn new() -> Self {
Self { Self {
@ -113,17 +119,6 @@ impl Rule {
pub const SHRINK_UP: ResizeParam = (0, -1, 0, 1); pub const SHRINK_UP: ResizeParam = (0, -1, 0, 1);
pub const SHRINK_DOWN: ResizeParam = (0, -1, 0, 0); pub const SHRINK_DOWN: ResizeParam = (0, -1, 0, 0);
pub fn new() -> Self {
Self {
enabled: false,
base: SubRule::new(),
variants: Vec::new(),
flip_h: false,
flip_v: false,
rotate: false,
}
}
pub fn get(&self, x: usize, y: usize) -> (RuleCellFrom, RuleCellTo) { pub fn get(&self, x: usize, y: usize) -> (RuleCellFrom, RuleCellTo) {
self.base.get(x, y) self.base.get(x, y)
} }
@ -264,7 +259,7 @@ impl Chunk {
} }
pub fn get_cell(&self, x: usize, y: usize) -> Cell { pub fn get_cell(&self, x: usize, y: usize) -> Cell {
self.contents[x][y].clone() self.contents[x][y]
} }
fn set_cell(&mut self, x: usize, y: usize, cell: Cell) { fn set_cell(&mut self, x: usize, y: usize, cell: Cell) {
@ -285,7 +280,7 @@ impl Dish {
(RuleCellFrom::One(Cell(0)), RuleCellTo::One(Cell(1))), (RuleCellFrom::One(Cell(0)), RuleCellTo::One(Cell(1))),
], ],
}, },
..Rule::new() ..Rule::default()
}, },
Rule { Rule {
enabled: true, enabled: true,
@ -300,7 +295,7 @@ impl Dish {
], ],
}, },
flip_h: true, flip_h: true,
..Rule::new() ..Rule::default()
}, },
]; ];
@ -371,7 +366,7 @@ impl Dish {
let py = y.wrapping_add_unsigned(dy) as usize; let py = y.wrapping_add_unsigned(dy) as usize;
match variant.get(dx, dy).1 { match variant.get(dx, dy).1 {
RuleCellTo::One(rule_cell) => { RuleCellTo::One(rule_cell) => {
self.set_cell(px, py, rule_cell.clone()); self.set_cell(px, py, rule_cell);
} }
RuleCellTo::GroupRandom(group_id) => { RuleCellTo::GroupRandom(group_id) => {
let group = &self.cell_groups[group_id]; let group = &self.cell_groups[group_id];

View file

@ -100,7 +100,9 @@ impl eframe::App for UScope {
for _ in 0..self.speed { for _ in 0..self.speed {
self.dish.fire_blindly(); self.dish.fire_blindly();
} }
SidePanel::left("left_panel").show(ctx, |ui| { SidePanel::left("left_panel")
.min_width(100.)
.show(ctx, |ui| {
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));
@ -115,6 +117,7 @@ impl eframe::App for UScope {
}); });
ui.separator(); ui.separator();
ScrollArea::vertical().show(ui, |ui| {
ui.heading("Cells"); ui.heading("Cells");
for (i, cell) in self.cell_types.iter_mut().enumerate() { for (i, cell) in self.cell_types.iter_mut().enumerate() {
ui.horizontal(|ui| { ui.horizontal(|ui| {
@ -140,7 +143,8 @@ impl eframe::App for UScope {
ui.heading("Groups"); ui.heading("Groups");
for group in &mut self.dish.cell_groups { for group in &mut self.dish.cell_groups {
let (rect, _response) = ui.allocate_exact_size(Vec2::splat(CSIZE), Sense::click()); let (rect, _response) =
ui.allocate_exact_size(Vec2::splat(CSIZE), Sense::click());
draw_group(ui, rect, group, &self.cell_types); draw_group(ui, rect, group, &self.cell_types);
ui.menu_button("edit", |ui| { ui.menu_button("edit", |ui| {
let mut void = group.contains(&None); let mut void = group.contains(&None);
@ -168,7 +172,7 @@ impl eframe::App for UScope {
} }
ui.heading("Rules"); ui.heading("Rules");
ScrollArea::vertical().show(ui, |ui| {
let mut to_remove = None; let mut to_remove = 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();
@ -182,7 +186,7 @@ impl eframe::App for UScope {
} }
ui.separator(); ui.separator();
if ui.button("add rule").clicked() { if ui.button("add rule").clicked() {
self.dish.rules.push(Rule::new()); self.dish.rules.push(Rule::default());
} }
}); });
}); });
@ -505,7 +509,11 @@ fn rule_cell_edit_to(
} }
fn draw_group(ui: &mut Ui, rect: Rect, group: &[Option<Cell>], cells: &[CellData]) { fn draw_group(ui: &mut Ui, rect: Rect, group: &[Option<Cell>], cells: &[CellData]) {
let group_size = group.len(); let mut group_size = group.len();
let has_void = group.contains(&None);
if has_void {
group_size -= 1;
}
let radius_per_color = (CSIZE * 0.7) / (group_size as f32); let radius_per_color = (CSIZE * 0.7) / (group_size as f32);
for (i, cell) in group.iter().flatten().enumerate() { for (i, cell) in group.iter().flatten().enumerate() {
let color = cells[cell.id()].color; let color = cells[cell.id()].color;
@ -513,7 +521,7 @@ fn draw_group(ui: &mut Ui, rect: Rect, group: &[Option<Cell>], cells: &[CellData
ui.painter_at(rect) ui.painter_at(rect)
.circle_filled(rect.center(), radius, color); .circle_filled(rect.center(), radius, color);
} }
if group.contains(&None) { if has_void {
ui.painter_at(rect) ui.painter_at(rect)
.line_segment([rect.min, rect.max], (1., Color32::WHITE)); .line_segment([rect.min, rect.max], (1., Color32::WHITE));
} }