Extras button

This commit is contained in:
djmil 2024-08-31 21:49:57 +02:00
parent 92ceea208f
commit 67854d9189

View File

@ -16,14 +16,17 @@ pub struct Simulation {
circles: Vec<Circle>, circles: Vec<Circle>,
#[serde(skip)] #[serde(skip)]
colors: Vec<egui::Color32>, colors: Vec<egui::Color32>,
show_extras: bool,
} }
impl Default for Simulation { impl Default for Simulation {
fn default() -> Self { fn default() -> Self {
Self { Self {
circles_count: 2, circles_count: 3,
circles: Vec::new(), circles: Vec::new(),
colors: Vec::new(), colors: Vec::new(),
show_extras: true,
} }
} }
} }
@ -73,43 +76,38 @@ impl eframe::App for Simulation {
ui.add_space(16.0); ui.add_space(16.0);
} }
egui::widgets::global_dark_light_mode_buttons(ui); ui.toggle_value(&mut self.show_extras, "♘ Extras");
ui.with_layout(egui::Layout::top_down(egui::Align::RIGHT), |ui| { egui::widgets::global_dark_light_mode_buttons(ui);
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 4.0;
ui.hyperlink_to(
"source code",
"https://gitea.djmil.dev/djmil/egui-circles",
);
ui.label("rust");
});
});
}); });
}); });
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
// The central panel the region left after adding TopPanel's and SidePanel's // The central panel the region left after adding TopPanel's and SidePanel's
ui.horizontal(|ui| { if self.show_extras {
ui.label("Click on circles or press"); ui.horizontal(|ui| {
if ui.button("push").clicked() { ui.label("Click on circles or press");
self.circles.iter_mut().for_each(|c| (*c).v = Vec2{
x: rand::thread_rng().gen_range(-4.0..4.0),
y: rand::thread_rng().gen_range(-4.0..4.0)});
};
ui.label("or"); if ui.button("push").clicked() {
self.circles.iter_mut().for_each(|c| (*c).v = Vec2{
x: rand::thread_rng().gen_range(-4.0..4.0),
y: rand::thread_rng().gen_range(-4.0..4.0)});
};
if ui.button("halt").clicked() { ui.label("or");
self.circles.iter_mut().for_each(|c| (*c).v = Vec2{x: 0.0, y: 0.0});
};
ui.label("buttons"); if ui.button("halt").clicked() {
}); self.circles.iter_mut().for_each(|c| (*c).v = Vec2{x: 0.0, y: 0.0});
};
ui.add(egui::Slider::new(&mut self.circles_count, 0..=25).text("circles count")); ui.label("buttons");
});
ui.add(egui::Slider::new(&mut self.circles_count, 0..=25).text("circles count"));
ui.separator();
}
let painter = ui.painter(); let painter = ui.painter();
@ -146,12 +144,13 @@ impl eframe::App for Simulation {
}); });
} }
ui.separator(); if self.show_extras {
ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| { powered_by_egui_and_eframe(ui);
powered_by_egui_and_eframe(ui); written_by_djmil(ui);
egui::warn_if_debug_build(ui); egui::warn_if_debug_build(ui);
}); });
}
}); });
@ -212,6 +211,17 @@ fn powered_by_egui_and_eframe(ui: &mut egui::Ui) {
}); });
} }
fn written_by_djmil(ui: &mut egui::Ui) {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 4.0;
ui.label("Writen by Djmil in");
ui.hyperlink_to(
"rust",
"https://gitea.djmil.dev/djmil/egui-circles",
);
});
}
fn collision(c1: &Circle, c2: &Circle) -> (Vec2, Vec2) { fn collision(c1: &Circle, c2: &Circle) -> (Vec2, Vec2) {
let m1 = c1.r; let m1 = c1.r;
let m2 = c2.r; let m2 = c2.r;