exit_dialog/
exit_dialog.rs

1use macroquad::prelude::*;
2use macroquad::ui::{hash, root_ui, widgets::Window};
3
4#[macroquad::main("Exit dialog")]
5async fn main() {
6    prevent_quit();
7
8    let mut show_exit_dialog = false;
9    let mut user_decided_to_exit = false;
10
11    loop {
12        clear_background(GRAY);
13
14        if is_quit_requested() {
15            show_exit_dialog = true;
16        }
17
18        if show_exit_dialog {
19            let dialog_size = vec2(200., 70.);
20            let screen_size = vec2(screen_width(), screen_height());
21            let dialog_position = screen_size / 2. - dialog_size / 2.;
22            Window::new(hash!(), dialog_position, dialog_size).ui(&mut *root_ui(), |ui| {
23                ui.label(None, "Do you really want to quit?");
24                ui.separator();
25                ui.same_line(60.);
26                if ui.button(None, "Yes") {
27                    user_decided_to_exit = true;
28                }
29                ui.same_line(120.);
30                if ui.button(None, "No") {
31                    show_exit_dialog = false;
32                }
33            });
34        }
35
36        if user_decided_to_exit {
37            break;
38        }
39
40        next_frame().await
41    }
42}