1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Pushrod Rendering Library
// Extensible Core Library
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use sdl2::event::Event;
//use sdl2::messagebox::*;
use sdl2::video::Window;
use sdl2::Sdl;

use crate::render::widget::{BaseWidget, Widget};
use crate::render::widget_cache::WidgetCache;
use std::time::Duration;

/// This is a storage container for the Pushrod event engine.
pub struct Engine {
    cache: WidgetCache,
    current_widget_id: i32,
}

/// This is the heart of the Pushrod event engine, and is what is used to drive the interaction
/// between the user and your application.  The suggested method of use for this code is as
/// follows:
///
/// ## Create a new object
/// Use `Engine::new()` to instantiate a new object.
///
/// ## Set up the base
/// Call `engine.setup(width, height)`, by passing the width and height of the window that is
/// being created for the main application.  This is required, as a base widget is added to the
/// render list.  This `BaseWidget` is considered the parent of the application screen.
///
/// ## Add Widgets to the Engine
/// Call `add_widget(Box::new(widget), "name".to_string())` to add your `Widget` to the managed
/// display list.
///
/// ## Call Run()
/// Calling `run(sdl, window)` will manage the application screen after all widgets have been added
/// to the display list.
///
/// That's all there is to it.  If you want to see more interactions on how the `Engine` is used in
/// an application, check out the demo test code, and look at `rust-pushrod-chassis`.
impl Engine {
    /// Creates a new `Engine` object.
    pub fn new() -> Self {
        Self {
            cache: WidgetCache::new(),
            current_widget_id: 0,
        }
    }

    /// Sets up the top-level widget so that other widgets can be added to the screen.
    pub fn setup(&mut self, window_width: u32, window_height: u32) {
        let base_widget = BaseWidget::new(0, 0, window_width, window_height);

        self.cache
            .add_widget(Box::new(base_widget), "base".to_string());
    }

    /// Adds a widget to the display list.  Widgets are rendered in the order in which they were
    /// created in the display list.
    pub fn add_widget(&mut self, widget: Box<dyn Widget>, widget_name: String) -> i32 {
        self.cache.add_widget(widget, widget_name)
    }

    /// Main application run loop, controls interaction between the user and the application.
    pub fn run(&mut self, sdl: Sdl, window: Window) {
        let mut canvas = window.into_canvas().software().build().unwrap();

        canvas.clear();
        canvas.present();

        let mut event_pump = sdl.event_pump().unwrap();

        'running: loop {
            for event in event_pump.poll_iter() {
                match event {
                    Event::MouseButtonDown {
                        mouse_btn, clicks, ..
                    } => {
                        self.cache.button_clicked(
                            self.current_widget_id,
                            mouse_btn as u8,
                            clicks,
                            true,
                        );
                    }

                    Event::MouseButtonUp {
                        mouse_btn, clicks, ..
                    } => {
                        self.cache
                            .button_clicked(-1, mouse_btn as u8, clicks, false);
                    }

                    Event::MouseMotion { x, y, .. } => {
                        let cur_widget_id = self.current_widget_id;

                        self.current_widget_id = self.cache.find_widget(x, y);

                        if cur_widget_id != self.current_widget_id {
                            self.cache.mouse_exited(cur_widget_id);
                            self.cache.mouse_entered(self.current_widget_id);
                        }

                        self.cache.mouse_moved(self.current_widget_id, vec![x, y]);
                    }

                    Event::MouseWheel { x, y, .. } => {
                        self.cache
                            .mouse_scrolled(self.current_widget_id, vec![x, y]);
                    }

                    Event::Quit { .. } => {
                        //                        let buttons: Vec<_> = vec![
                        //                            ButtonData {
                        //                                flags: MessageBoxButtonFlag::RETURNKEY_DEFAULT,
                        //                                button_id: 1,
                        //                                text: "Yes",
                        //                            },
                        //                            ButtonData {
                        //                                flags: MessageBoxButtonFlag::ESCAPEKEY_DEFAULT,
                        //                                button_id: 2,
                        //                                text: "No",
                        //                            },
                        //                        ];
                        //
                        //                        let res = show_message_box(
                        //                            MessageBoxFlag::WARNING,
                        //                            buttons.as_slice(),
                        //                            "Quit",
                        //                            "Are you sure?",
                        //                            None,
                        //                            None,
                        //                        )
                        //                        .unwrap();
                        //
                        //                        if let ClickedButton::CustomButton(x) = res {
                        //                            if x.button_id == 1 {
                        break 'running;
                        //                            }
                        //                        }
                    }

                    _ => {
                        // Send event through to widget.
                        eprintln!("Event: {:?}", event);
                    }
                }
            }

            self.cache.tick();
            self.cache.draw_loop(&mut canvas);

            ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
        }
    }
}

impl Default for Engine {
    fn default() -> Self {
        Self::new()
    }
}