egui: an easy-to-use GUI in pure Rust!
Try the live web demo: https://emilk.github.io/egui/index.html. Read more about egui at https://github.com/emilk/egui.
To quickly get started with egui, you can take a look at egui_template
which uses eframe.
To create a GUI using egui you first need a [CtxRef] (by convention referred to by ctx).
Then you add a [Window] or a [SidePanel] to get a [Ui], which is what you'll be using to add all the buttons and labels that you need.
Using egui
To see what is possible to build with egui you can check out the online demo at https://emilk.github.io/egui/#demo.
If you like the "learning by doing" approach, clone https://github.com/emilk/egui_template and get started using egui right away.
A simple example
Here is a simple counter that can be incremented and decremented using two buttons:
In some GUI frameworks this would require defining multiple types and functions with callbacks or message handlers,
but thanks to egui being immediate mode everything is one self-contained function!
Getting a [Ui]
Use one of [SidePanel], [TopPanel], [CentralPanel], [Window] or [Area] to
get access to an [Ui] where you can put widgets. For example:
# let mut ctx = default;
# ctx.begin_frame;
default.show;
Quick start
# let ui = &mut __test;
# let mut my_string = Stringnew;
# let mut my_boolean = true;
# let mut my_f32 = 42.0;
ui.label;
ui.hyperlink;
ui.text_edit_singleline;
if ui.button.clicked
ui.add;
ui.add;
ui.checkbox;
let mut my_enum = First;
ui.horizontal;
ui.separator;
# let my_image = default;
ui.image;
ui.collapsing;
Conventions
Conventions unless otherwise specified:
- angles are in radians
Vec2::Xis right andVec2::Yis down.Pos2::ZEROis left top.- Positions and sizes are measured in points. Each point may consist of many physical pixels.
Integrating with egui
Most likely you are using an existing egui backend/integration such as eframe or bevy_egui,
but if you want to integrate egui into a new game engine, this is the section for you.
To write your own integration for egui you need to do this:
# fn handle_output(_: egui::Output) {}
# fn paint(_: Vec<egui::ClippedMesh>) {}
# fn gather_input() -> egui::RawInput { egui::RawInput::default() }
let mut ctx = egui::CtxRef::default();
// Game loop:
loop {
let raw_input: egui::RawInput = gather_input();
ctx.begin_frame(raw_input);
egui::CentralPanel::default().show(&ctx, |ui| {
ui.label("Hello world!");
if ui.button("Click me").clicked() {
/* take some action here */
}
});
let (output, shapes) = ctx.end_frame();
let clipped_meshes = ctx.tessellate(shapes); // create triangles to paint
handle_output(output);
paint(clipped_meshes);
}
Understanding immediate mode
egui is an immediate mode GUI library. It is useful to fully grok what "immediate mode" implies.
Here is an example to illustrate it:
# let ui = &mut __test;
if ui.button.clicked
#
This code is being executed each frame at maybe 60 frames per second. Each frame egui does these things:
- lays out the letters
click mein order to figure out the size of the button - decides where on screen to place the button
- check if the mouse is hovering or clicking that location
- chose button colors based on if it is being hovered or clicked
- add a [
Shape::Rect] and [Shape::Text] to the list of shapes to be painted later this frame - return a [
Response] with theclickedmember so the user can check for interactions
There is no button being created and stored somewhere.
The only output of this call is some colored shapes, and a [Response].
Read more about the pros and cons of immediate mode at https://github.com/emilk/egui#why-immediate-mode.
How widgets works
# let ui = &mut __test;
if ui.button.clicked
#
is short for
# let ui = &mut __test;
let button = new;
if ui.add.clicked
#
which is short for
# use Widget;
# let ui = &mut __test;
let button = new;
let response = button.ui;
if response.clicked
#
[Button] uses the builder pattern to create the data required to show it. The [Button] is then discarded.
[Button] implements trait [Widget], which looks like this:
# use *;
Code snippets
# let ui = &mut __test;
# let mut some_bool = true;
// Miscellaneous tips and tricks
ui.horizontal_wrapped;
// Change test color on subsequent widgets:
ui.visuals_mut.override_text_color = Some;
// Turn off text wrapping on subsequent widgets:
ui.style_mut.wrap = Some;