Replies: 1 comment
-
I think the Strip from egui_extras might be a better fit for what you are doing. Example code
struct MyApp {
rows: usize,
columns: usize,
}
impl Default for MyApp {
fn default() -> Self {
Self {
rows: 4,
columns: 2,
}
}
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
let Self { rows, columns } = self;
egui::CentralPanel::default().show(ctx, |ui| {
ui.horizontal(|ui| {
ui.label("Rows");
ui.add(egui::DragValue::new(rows).clamp_range(1..=10));
ui.label("Columns");
ui.add(egui::DragValue::new(columns).clamp_range(1..=10));
});
ui.spacing_mut().item_spacing = [0.0; 2].into();
let (rows, columns) = (*rows, *columns);
egui_extras::StripBuilder::new(ui)
.sizes(egui_extras::Size::relative((rows as f32).recip()), rows)
.vertical(|mut strip| {
for r in 0..rows {
strip.cell(|ui| {
egui_extras::StripBuilder::new(ui)
.sizes(
egui_extras::Size::relative((columns as f32).recip()),
columns,
)
.horizontal(|mut strip| {
for c in 0..columns {
strip.cell(|ui| {
let i = r * columns + c;
ui.ctx().debug_painter().debug_rect(
ui.max_rect(),
Color32::GREEN,
format!("{i} ({r}, {c})"),
);
})
}
});
});
}
});
});
}
} egui_strip_example.webmNote that I had to set the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Every time I fight with this I think, "I should post and see if there's a better way to do this" and don't, but not this time!!
I've been using egui for a while now, but struggle with complex layouts. Here's an example I just did:
It took me quite a lot of manual work to get that to all layout that way, and I'm wondering if there's an easier way. In particular, the way I had to do it means if I want to add a few more plots to the grid, I have to go tweak some constants and whatnot, and I'd love a way to just be able to chuck an arbitrary number of these in a grid and have it just divide it all up nicely.
The way I ended up having to do it was:
and so on, and then
draw_named_plot
does this:What I'd ideally love is to just be able to say something like:
I understand that this can't literally work, because a Grid doesn't automatically consume the entire remaining UI space if the elements you put in don't fill it.
But I am at least wondering if there is a better way than the way I did it, because my approach has a lot of problems:
outer_margin(Margin::ZERO)
,inner_margin(Margin::ZERO)
,ui.spacing_mut().item_spacing = Vec2::ZERO
all over the placeI really love using egui, I should add - and Plot itself is a pretty fantastic tool for diagnostic stuff and visualization.
I'm really just wondering if I'm totally misunderstanding the way I'm supposed to use the layout system. I'd love if I could quickly knock out apps like this and when I want to plot something new, just add another Plot into the Grid's show() having provided some hints like "fill the whole UI and there will be this many rows and cols" to the Grid when I made it, so I don't have to go tweak a bunch of constants and fiddle with it to get it looking right.
Thank you!!
Beta Was this translation helpful? Give feedback.
All reactions