2020-08-21 16:53:43 +00:00
|
|
|
//! Example of how to use Egui
|
|
|
|
|
2020-08-05 17:45:39 +00:00
|
|
|
#![deny(warnings)]
|
|
|
|
#![warn(clippy::all)]
|
|
|
|
|
|
|
|
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
|
2020-10-24 17:45:27 +00:00
|
|
|
#[derive(serde::Deserialize, serde::Serialize)]
|
2020-07-22 16:46:12 +00:00
|
|
|
struct MyApp {
|
2020-10-24 17:45:27 +00:00
|
|
|
name: String,
|
|
|
|
age: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for MyApp {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
name: "Arthur".to_owned(),
|
|
|
|
age: 42,
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 16:01:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
impl egui::app::App for MyApp {
|
2020-10-24 08:56:23 +00:00
|
|
|
/// Called each time the UI needs repainting, which may be many times per second.
|
2020-10-24 16:37:20 +00:00
|
|
|
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
|
2020-10-17 10:33:30 +00:00
|
|
|
fn ui(
|
|
|
|
&mut self,
|
2020-10-24 08:56:23 +00:00
|
|
|
ctx: &std::sync::Arc<egui::Context>,
|
2020-10-24 17:45:27 +00:00
|
|
|
integration_context: &mut egui::app::IntegrationContext,
|
2020-10-24 17:23:16 +00:00
|
|
|
) {
|
2020-10-24 17:45:27 +00:00
|
|
|
let MyApp { name, age } = self;
|
2020-08-21 16:53:43 +00:00
|
|
|
|
|
|
|
// Example used in `README.md`.
|
2020-10-24 17:45:27 +00:00
|
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
|
|
|
ui.heading("My Egui Application");
|
|
|
|
|
|
|
|
ui.horizontal(|ui| {
|
|
|
|
ui.label("Your name: ");
|
|
|
|
ui.text_edit(name);
|
|
|
|
});
|
|
|
|
|
|
|
|
ui.add(egui::Slider::u32(age, 0..=120).text("age"));
|
|
|
|
if ui.button("Click each year").clicked {
|
|
|
|
*age += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.label(format!("Hello '{}', age {}", name, age));
|
|
|
|
|
|
|
|
ui.advance_cursor(16.0);
|
|
|
|
if ui.button("Quit").clicked {
|
|
|
|
integration_context.output.quit = true;
|
2020-08-21 16:53:43 +00:00
|
|
|
}
|
|
|
|
});
|
2020-10-24 17:45:27 +00:00
|
|
|
|
|
|
|
integration_context.output.window_size = Some(ctx.used_size()); // resize the window to be just the size we need it to be
|
2020-07-22 16:01:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
fn on_exit(&mut self, storage: &mut dyn egui::app::Storage) {
|
|
|
|
egui::app::set_value(storage, egui::app::APP_KEY, self);
|
2020-07-22 16:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 21:59:55 +00:00
|
|
|
fn main() {
|
2020-07-23 10:01:48 +00:00
|
|
|
let title = "My Egui Window";
|
2020-10-19 18:25:05 +00:00
|
|
|
|
|
|
|
// Persist app state to file:
|
|
|
|
let storage = egui_glium::storage::FileStorage::from_path(".egui_example_glium.json".into());
|
|
|
|
|
|
|
|
// Alternative: store nowhere
|
|
|
|
// let storage = egui::app::DummyStorage::default();
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
let app: MyApp = egui::app::get_value(&storage, egui::app::APP_KEY).unwrap_or_default(); // Restore `MyApp` from file, or create new `MyApp`.
|
2020-10-19 18:25:05 +00:00
|
|
|
egui_glium::run(title, Box::new(storage), app);
|
2020-05-17 10:26:17 +00:00
|
|
|
}
|