2020-08-31 05:55:07 +00:00
|
|
|
//! Source code example of how to create your own widget.
|
2020-09-11 15:17:43 +00:00
|
|
|
//! This is meant to be read as a tutorial, hence the plethora of comments.
|
2020-08-31 05:55:07 +00:00
|
|
|
|
2020-09-11 15:17:43 +00:00
|
|
|
/// iOS-style toggle switch:
|
|
|
|
///
|
|
|
|
/// ``` text
|
|
|
|
/// _____________
|
|
|
|
/// / /.....\
|
|
|
|
/// | |.......|
|
|
|
|
/// \_______\_____/
|
|
|
|
/// ```
|
2020-12-29 12:40:11 +00:00
|
|
|
pub fn toggle(ui: &mut egui::Ui, on: &mut bool) -> egui::Response {
|
2020-09-11 15:17:43 +00:00
|
|
|
// Widget code can be broken up in four steps:
|
|
|
|
// 1. Decide a size for the widget
|
|
|
|
// 2. Allocate space for it
|
|
|
|
// 3. Handle interactions with the widget (if any)
|
|
|
|
// 4. Paint the widget
|
|
|
|
|
|
|
|
// 1. Deciding widget size:
|
|
|
|
// You can query the `ui` how much space is available,
|
2020-09-18 22:58:36 +00:00
|
|
|
// but in this example we have a fixed size widget of the default size for a button:
|
2021-02-01 15:55:40 +00:00
|
|
|
let desired_size = ui.spacing().interact_size;
|
2020-09-11 15:17:43 +00:00
|
|
|
|
|
|
|
// 2. Allocating space:
|
2020-12-26 18:14:13 +00:00
|
|
|
// This is where we get a region of the screen assigned.
|
|
|
|
// We also tell the Ui to sense clicks in the allocated region.
|
2021-01-06 10:03:29 +00:00
|
|
|
let (rect, response) = ui.allocate_exact_size(desired_size, egui::Sense::click());
|
2020-08-31 05:55:07 +00:00
|
|
|
|
2020-12-26 00:38:26 +00:00
|
|
|
// 3. Interact: Time to check for clicks!.
|
2021-01-25 17:50:19 +00:00
|
|
|
if response.clicked() {
|
2020-08-31 05:55:07 +00:00
|
|
|
*on = !*on;
|
|
|
|
}
|
|
|
|
|
2020-09-11 15:17:43 +00:00
|
|
|
// 4. Paint!
|
2021-01-17 13:48:59 +00:00
|
|
|
// First let's ask for a simple animation from egui.
|
|
|
|
// egui keeps track of changes in the boolean associated with the id and
|
2020-09-11 15:17:43 +00:00
|
|
|
// returns an animated value in the 0-1 range for how much "on" we are.
|
2020-12-26 18:14:13 +00:00
|
|
|
let how_on = ui.ctx().animate_bool(response.id, *on);
|
2020-09-11 15:17:43 +00:00
|
|
|
// We will follow the current style by asking
|
2020-08-31 05:55:07 +00:00
|
|
|
// "how should something that is being interacted with be painted?".
|
2020-09-11 15:17:43 +00:00
|
|
|
// This will, for instance, give us different colors when the widget is hovered or clicked.
|
2020-09-05 11:30:04 +00:00
|
|
|
let visuals = ui.style().interact(&response);
|
2021-01-15 21:20:26 +00:00
|
|
|
let off_bg_fill = egui::Rgba::from(visuals.bg_fill);
|
|
|
|
let on_bg_fill = egui::Rgba::from_rgb(0.0, 0.5, 0.0);
|
2020-12-29 12:40:11 +00:00
|
|
|
let bg_fill = egui::lerp(off_bg_fill..=on_bg_fill, how_on);
|
2020-09-11 15:17:43 +00:00
|
|
|
// All coordinates are in absolute screen coordinates so we use `rect` to place the elements.
|
2021-01-12 19:50:54 +00:00
|
|
|
let rect = rect.expand(visuals.expansion);
|
2020-08-31 05:55:07 +00:00
|
|
|
let radius = 0.5 * rect.height();
|
2020-10-10 04:52:33 +00:00
|
|
|
ui.painter().rect(rect, radius, bg_fill, visuals.bg_stroke);
|
2020-09-11 15:17:43 +00:00
|
|
|
// Paint the circle, animating it from left to right with `how_on`:
|
2020-12-29 12:40:11 +00:00
|
|
|
let circle_x = egui::lerp((rect.left() + radius)..=(rect.right() - radius), how_on);
|
|
|
|
let center = egui::pos2(circle_x, rect.center().y);
|
2020-10-10 04:52:33 +00:00
|
|
|
ui.painter()
|
2021-01-15 21:20:26 +00:00
|
|
|
.circle(center, 0.75 * radius, visuals.bg_fill, visuals.fg_stroke);
|
2020-08-31 05:55:07 +00:00
|
|
|
|
2020-09-11 15:17:43 +00:00
|
|
|
// All done! Return the interaction response so the user can check what happened
|
|
|
|
// (hovered, clicked, ...) and maybe show a tooltip:
|
2020-08-31 05:55:07 +00:00
|
|
|
response
|
|
|
|
}
|
|
|
|
|
2020-10-10 10:33:48 +00:00
|
|
|
/// Here is the same code again, but a bit more compact:
|
|
|
|
#[allow(dead_code)]
|
2020-12-29 12:40:11 +00:00
|
|
|
fn toggle_compact(ui: &mut egui::Ui, on: &mut bool) -> egui::Response {
|
2021-02-01 15:55:40 +00:00
|
|
|
let desired_size = ui.spacing().interact_size;
|
2021-01-06 10:03:29 +00:00
|
|
|
let (rect, response) = ui.allocate_exact_size(desired_size, egui::Sense::click());
|
2021-01-25 17:50:19 +00:00
|
|
|
*on ^= response.clicked(); // toggle if clicked
|
2020-10-10 10:33:48 +00:00
|
|
|
|
2020-12-26 18:14:13 +00:00
|
|
|
let how_on = ui.ctx().animate_bool(response.id, *on);
|
2020-10-10 10:33:48 +00:00
|
|
|
let visuals = ui.style().interact(&response);
|
2021-01-15 21:20:26 +00:00
|
|
|
let off_bg_fill = egui::Rgba::from(visuals.bg_fill);
|
|
|
|
let on_bg_fill = egui::Rgba::from_rgb(0.0, 0.5, 0.0);
|
2020-12-29 12:40:11 +00:00
|
|
|
let bg_fill = egui::lerp(off_bg_fill..=on_bg_fill, how_on);
|
2021-01-12 19:50:54 +00:00
|
|
|
let rect = rect.expand(visuals.expansion);
|
2020-10-10 10:33:48 +00:00
|
|
|
let radius = 0.5 * rect.height();
|
|
|
|
ui.painter().rect(rect, radius, bg_fill, visuals.bg_stroke);
|
2020-12-29 12:40:11 +00:00
|
|
|
let circle_x = egui::lerp((rect.left() + radius)..=(rect.right() - radius), how_on);
|
|
|
|
let center = egui::pos2(circle_x, rect.center().y);
|
2020-10-10 10:33:48 +00:00
|
|
|
ui.painter()
|
2021-01-15 21:20:26 +00:00
|
|
|
.circle(center, 0.75 * radius, visuals.bg_fill, visuals.fg_stroke);
|
2020-10-10 10:33:48 +00:00
|
|
|
|
|
|
|
response
|
|
|
|
}
|
|
|
|
|
2020-12-29 12:40:11 +00:00
|
|
|
pub fn demo(ui: &mut egui::Ui, on: &mut bool) {
|
|
|
|
ui.horizontal_wrapped_for_text(egui::TextStyle::Button, |ui| {
|
2020-10-01 20:53:11 +00:00
|
|
|
toggle(ui, on).on_hover_text("Click to toggle");
|
2020-12-29 12:40:11 +00:00
|
|
|
ui.add(crate::__egui_github_link_file!());
|
2021-02-07 09:55:45 +00:00
|
|
|
})
|
2021-02-07 13:09:44 +00:00
|
|
|
.response
|
2021-02-07 09:55:45 +00:00
|
|
|
.on_hover_text(
|
|
|
|
"It's easy to create your own widgets!\n\
|
|
|
|
This toggle switch is just one function and 20 lines of code.",
|
|
|
|
);
|
2020-08-31 05:55:07 +00:00
|
|
|
}
|