exit/
exit.rs

1extern crate pushrod;
2extern crate sdl2;
3
4use pushrod::render::engine::Engine;
5use pushrod::render::widget::{BaseWidget, Widget};
6use pushrod::render::widget_config::{CONFIG_BORDER_WIDTH, CONFIG_COLOR_BASE, CONFIG_COLOR_BORDER};
7use pushrod::render::{make_points, make_size};
8use sdl2::messagebox::*;
9use sdl2::pixels::Color;
10
11/*
12 * This demo just tests the rendering functionality of the `BaseWidget`.  It only tests the
13 * render portion of the library, nothing else.
14 */
15
16pub fn main() {
17    let sdl_context = sdl2::init().unwrap();
18    let video_subsystem = sdl_context.video().unwrap();
19    let window = video_subsystem
20        .window("pushrod-render exit demo", 800, 600)
21        .position_centered()
22        .opengl()
23        .build()
24        .unwrap();
25    let mut engine = Engine::new(800, 600, 30);
26    let mut new_base_widget = BaseWidget::new(make_points(100, 100), make_size(600, 400));
27
28    new_base_widget
29        .get_config()
30        .set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 0, 0));
31    new_base_widget
32        .get_config()
33        .set_numeric(CONFIG_BORDER_WIDTH, 2);
34
35    new_base_widget
36        .get_callbacks()
37        .on_mouse_entered(|x, _widgets, _layouts| {
38            x.get_config()
39                .set_color(CONFIG_COLOR_BASE, Color::RGB(255, 0, 0));
40            x.get_config().set_invalidated(true);
41            _widgets[0]
42                .widget
43                .borrow_mut()
44                .get_config()
45                .set_invalidated(true);
46            eprintln!("Mouse Entered");
47        });
48
49    new_base_widget
50        .get_callbacks()
51        .on_mouse_exited(|x, _widgets, _layouts| {
52            x.get_config()
53                .set_color(CONFIG_COLOR_BASE, Color::RGB(255, 255, 255));
54            x.get_config().set_invalidated(true);
55            _widgets[0]
56                .widget
57                .borrow_mut()
58                .get_config()
59                .set_invalidated(true);
60            eprintln!("Mouse Exited");
61        });
62
63    new_base_widget
64        .get_callbacks()
65        .on_mouse_moved(|_widget, _widgets, _layouts, points| {
66            eprintln!("Mouse Moved: {:?}", points);
67        });
68
69    new_base_widget
70        .get_callbacks()
71        .on_mouse_scrolled(|_widget, _widgets, _layouts, points| {
72            eprintln!("Mouse Scrolled: {:?}", points);
73        });
74
75    new_base_widget.get_callbacks().on_mouse_clicked(
76        |_widget, _widgets, _layouts, button, clicks, state| {
77            eprintln!(
78                "Mouse Clicked: button={} clicks={} state={}",
79                button, clicks, state
80            );
81        },
82    );
83
84    engine.add_widget(Box::new(new_base_widget), String::from("widget1"));
85
86    engine.on_exit(|engine| {
87        let buttons: Vec<_> = vec![
88            ButtonData {
89                flags: MessageBoxButtonFlag::RETURNKEY_DEFAULT,
90                button_id: 1,
91                text: "Yes",
92            },
93            ButtonData {
94                flags: MessageBoxButtonFlag::ESCAPEKEY_DEFAULT,
95                button_id: 2,
96                text: "No",
97            },
98        ];
99
100        let res = show_message_box(
101            MessageBoxFlag::WARNING,
102            buttons.as_slice(),
103            "Quit",
104            "Are you sure?",
105            None,
106            None,
107        )
108        .unwrap();
109
110        if let ClickedButton::CustomButton(x) = res {
111            if x.button_id == 1 {
112                return true;
113            }
114        }
115
116        false
117    });
118
119    engine.run(sdl_context, window);
120}