Skip to content

Instantly share code, notes, and snippets.

@LordSmurf
Created January 20, 2023 18:15
Show Gist options
  • Save LordSmurf/1a36649346512d2193d005337d17f1a7 to your computer and use it in GitHub Desktop.
Save LordSmurf/1a36649346512d2193d005337d17f1a7 to your computer and use it in GitHub Desktop.
Example of implementing defaults
use eframe::{egui, epi};
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "persistence", serde(default))] // if we add new fields, give them default values when deserializing old state
pub struct TemplateApp {
// Example stuff:
label: String,
// this how you opt-out of serialization of a member
#[cfg_attr(feature = "persistence", serde(skip))]
value: f32,
}
impl Default for TemplateApp {
fn default() -> Self {
Self {
// Example stuff:
label: "Hello World!".to_owned(),
value: 2.7,
}
}
}
impl epi::App for TemplateApp {
fn name(&self) -> &str {
"Simply Aliases"
}
/// Called once before the first frame.
fn setup(
&mut self,
_ctx: &egui::CtxRef,
_frame: &mut epi::Frame<'_>,
_storage: Option<&dyn epi::Storage>,
) {
// Load previous app state (if any).
// Note that you must enable the `persistence` feature for this to work.
#[cfg(feature = "persistence")]
if let Some(storage) = _storage {
*self = epi::get_value(storage, epi::APP_KEY).unwrap_or_default()
}
}
/// Called by the frame work to save state before shutdown.
/// Note that you must enable the `persistence` feature for this to work.
#[cfg(feature = "persistence")]
fn save(&mut self, storage: &mut dyn epi::Storage) {
epi::set_value(storage, epi::APP_KEY, self);
}
/// Called each time the UI needs repainting, which may be many times per second.
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
fn update(&mut self, ctx: &egui::CtxRef, frame: &mut epi::Frame<'_>) {
let Self { label, value } = self;
// Examples of how to create different panels and windows.
// Pick whichever suits you.
// Tip: a good default choice is to just keep the `CentralPanel`.
// For inspiration and more examples, go to https://emilk.github.io/egui
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
// The top panel is often a good place for a menu bar:
egui::menu::bar(ui, |ui| {
egui::menu::menu(ui, "File", |ui| {
if ui.button("Quit").clicked() {
frame.quit();
}
});
egui::menu::menu(ui, "Alias", |ui| {
if ui.button("User Alias").clicked() {}
if ui.button("Zm Level Weapons").clicked() {}
});
});
});
egui::SidePanel::left("side_panel").show(ctx, |ui| {
ui.heading("Type");
ui.horizontal(|ui| {
ui.label("Write something: ");
ui.text_edit_singleline(label);
});
ui.add(egui::Slider::new(value, 0.0..=10.0).text("value"));
if ui.button("Increment").clicked() {
*value += 1.0;
}
ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.label("powered by ");
ui.hyperlink_to("egui", "https://github.com/emilk/egui");
ui.label(" and ");
ui.hyperlink_to("eframe", "https://github.com/emilk/egui/tree/master/eframe");
});
});
});
egui::CentralPanel::default().show(ctx, |ui| {
// The central panel the region left after adding TopPanel's and SidePanel's
ui.heading("eframe template");
ui.hyperlink("https://github.com/emilk/eframe_template");
ui.add(egui::github_link_file!(
"https://github.com/emilk/eframe_template/blob/master/",
"Source code."
));
egui::warn_if_debug_build(ui);
});
if false {
egui::Window::new("Window").show(ctx, |ui| {
ui.label("Windows can be moved by dragging them.");
ui.label("They are automatically sized based on contents.");
ui.label("You can turn on resizing and scrolling if you like.");
ui.label("You would normally chose either panels OR windows.");
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment