2020-07-22 20:26:49 +00:00
|
|
|
use egui_glium::{persistence::Persistence, RunMode, Runner};
|
2019-11-18 19:06:41 +00:00
|
|
|
|
2020-07-22 16:46:12 +00:00
|
|
|
const APP_KEY: &str = "app";
|
2019-03-12 21:59:55 +00:00
|
|
|
|
2020-07-23 10:01:48 +00:00
|
|
|
/// We dervive Deserialize/Serialize so we can persist app state on shutdown.
|
2020-07-22 16:01:27 +00:00
|
|
|
#[derive(Default, serde::Deserialize, serde::Serialize)]
|
2020-07-22 16:46:12 +00:00
|
|
|
struct MyApp {
|
2020-07-23 10:01:48 +00:00
|
|
|
counter: u64,
|
2020-07-22 16:01:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 16:46:12 +00:00
|
|
|
impl egui_glium::App for MyApp {
|
2020-07-23 10:01:48 +00:00
|
|
|
/// This function will be called whenever the Ui needs to be shown,
|
|
|
|
/// which may be many times per second.
|
|
|
|
fn ui(&mut self, ui: &mut egui::Ui, _: &mut Runner) {
|
|
|
|
if ui.button("Increment").clicked {
|
|
|
|
self.counter += 1;
|
2020-07-22 16:01:27 +00:00
|
|
|
}
|
2020-07-23 10:01:48 +00:00
|
|
|
if ui.button("Reset").clicked {
|
|
|
|
self.counter = 0;
|
2020-07-22 20:26:49 +00:00
|
|
|
}
|
2020-07-23 10:01:48 +00:00
|
|
|
ui.label(format!("Counter: {}", self.counter));
|
2020-07-22 16:01:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 16:46:12 +00:00
|
|
|
fn on_exit(&mut self, persistence: &mut Persistence) {
|
2020-07-23 10:01:48 +00:00
|
|
|
persistence.set_value(APP_KEY, self); // Save app state
|
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";
|
|
|
|
let persistence = Persistence::from_path(".egui_example_glium.json".into()); // Where to persist app state
|
|
|
|
let app: MyApp = persistence.get_value(APP_KEY).unwrap_or_default(); // Restore `MyApp` from file, or create new `MyApp`.
|
2020-07-22 20:26:49 +00:00
|
|
|
egui_glium::run(title, RunMode::Reactive, persistence, app);
|
2020-05-17 10:26:17 +00:00
|
|
|
}
|