render/
render.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::pixels::Color;
9
10/*
11 * This demo just tests the rendering functionality of the `BaseWidget`.  It only tests the
12 * render portion of the library, nothing else.
13 */
14
15pub fn main() {
16    let sdl_context = sdl2::init().unwrap();
17    let video_subsystem = sdl_context.video().unwrap();
18    let window = video_subsystem
19        .window("pushrod-render demo", 800, 600)
20        .position_centered()
21        .opengl()
22        .build()
23        .unwrap();
24    let mut engine = Engine::new(800, 600, 30);
25    let mut new_base_widget = BaseWidget::new(make_points(100, 100), make_size(600, 400));
26
27    new_base_widget
28        .get_config()
29        .set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 0, 0));
30    new_base_widget
31        .get_config()
32        .set_numeric(CONFIG_BORDER_WIDTH, 2);
33
34    new_base_widget
35        .get_callbacks()
36        .on_mouse_entered(|x, _widgets, _layouts| {
37            x.get_config()
38                .set_color(CONFIG_COLOR_BASE, Color::RGB(255, 0, 0));
39            x.get_config().set_invalidated(true);
40            _widgets[0]
41                .widget
42                .borrow_mut()
43                .get_config()
44                .set_invalidated(true);
45            eprintln!("Mouse Entered");
46        });
47
48    new_base_widget
49        .get_callbacks()
50        .on_mouse_exited(|x, _widgets, _layouts| {
51            x.get_config()
52                .set_color(CONFIG_COLOR_BASE, Color::RGB(255, 255, 255));
53            x.get_config().set_invalidated(true);
54            _widgets[0]
55                .widget
56                .borrow_mut()
57                .get_config()
58                .set_invalidated(true);
59            eprintln!("Mouse Exited");
60        });
61
62    new_base_widget
63        .get_callbacks()
64        .on_mouse_moved(|_widget, _widgets, _layouts, points| {
65            eprintln!("Mouse Moved: {:?}", points);
66        });
67
68    new_base_widget
69        .get_callbacks()
70        .on_mouse_scrolled(|_widget, _widgets, _layouts, points| {
71            eprintln!("Mouse Scrolled: {:?}", points);
72        });
73
74    new_base_widget.get_callbacks().on_mouse_clicked(
75        |_widget, _widgets, _layouts, button, clicks, state| {
76            eprintln!(
77                "Mouse Clicked: button={} clicks={} state={}",
78                button, clicks, state
79            );
80        },
81    );
82
83    engine.add_widget(Box::new(new_base_widget), String::from("widget1"));
84
85    engine.on_exit(|_| {
86        eprintln!("Application exiting.");
87        true
88    });
89
90    engine.run(sdl_context, window);
91}