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
// Pushrod Widget 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.

//! `pushrod` is a GUI library for Rust.
//!
//! # Dependencies
//! Pushrod uses the following dependency:
//! ```ignore
//! [dependencies.sdl2]
//! version = "^0.32"
//! features = ["ttf", "image"]
//! ```
//!
//! To use the crate in your project, add the following dependencies:
//! ```ignore
//! [dependencies]
//! rust-pushrod = "^0.4"
//! ```
//! This will pull in the latest version.
//!
//! # Core Components
//! `pushrod::render` is the _core_ rendering components, containing the `Widget` base class, and
//! drawing loop logic.
//! `pushrod::widgets` is the extended `Widget` component library.

#[macro_use]

/// These macros are used and shared by all components in the `Pushrod` library.
mod macros {
    #[macro_export]
    /// This macro is used by `Widget` implementations, which auto-injects getter code for the
    /// `Widget`'s properties.  Since all `Widget` implementations share these functions, and must
    /// implement them locally, this `macro` serves as a quick way to implement the same reused code
    /// automatically.
    macro_rules! default_widget_properties {
        () => {
            /// This function is a macro-created getter function that returns the `Widget`'s configuration
            /// object as a borrowed mutable reference.  This code is auto-generated using the
            /// `default_widget_properties!()` macro.
            fn get_config(&mut self) -> &mut WidgetConfig {
                &mut self.config
            }

            /// This function is a macro-created getter function that returns the `Widget`'s system
            /// properties as a borrowed mutable reference.  This code is auto-generated using the
            /// `default_widget_properties!()` macro.
            fn get_system_properties(&mut self) -> &mut HashMap<i32, String> {
                &mut self.system_properties
            }

            /// This function is a macro-created getter function that returns the `Widget`'s
            /// `CallbackRegistry` object as a borrowed mutable reference.  This code is auto-generated
            /// using the `default_widget_properties!()` macro.
            fn get_callbacks(&mut self) -> &mut CallbackRegistry {
                &mut self.callback_registry
            }
        }
    }

    /// This macro is used by `Widget` implementations, which auto-injects callback functions.  It
    /// overrides standard functions so that mouse events, tick events, keyboard events, etc. all are
    /// routed through the `get_callbacks()` property function.
    macro_rules! default_widget_callbacks {
        () => {
            /// This function is a macro-created tick callback override, created by the
            /// `default_widget_callbacks!()` macro.
            fn tick_callback(&mut self, _widgets: &[WidgetContainer]) {
                if self.get_callbacks().has_on_tick() {
                    if let Some(mut cb) = self.get_callbacks().on_tick.take() {
                        cb(self, _widgets);
                        self.get_callbacks().on_tick = Some(cb);
                    }
                }
            }

            /// This function is a macro-created mouse entered callback override, created by the
            /// `default_widget_callbacks!()` macro.
            fn mouse_entered_callback(&mut self, _widgets: &[WidgetContainer]) {
                if self.get_callbacks().has_on_mouse_entered() {
                    if let Some(mut cb) = self.get_callbacks().on_mouse_entered.take() {
                        cb(self, _widgets);
                        self.get_callbacks().on_mouse_entered = Some(cb);
                    }
                }
            }

            /// This function is a macro-created mouse exited callback override, created by the
            /// `default_widget_callbacks!()` macro.
            fn mouse_exited_callback(&mut self, _widgets: &[WidgetContainer]) {
                if self.get_callbacks().has_on_mouse_exited() {
                    if let Some(mut cb) = self.get_callbacks().on_mouse_exited.take() {
                        cb(self, _widgets);
                        self.get_callbacks().on_mouse_exited = Some(cb);
                    }
                }
            }

            /// This function is a macro-created mouse moved callback override, created by the
            /// `default_widget_callbacks!()` macro.
            fn mouse_moved_callback(&mut self, _widgets: &[WidgetContainer], _points: Points) {
                if self.get_callbacks().has_on_mouse_moved() {
                    if let Some(mut cb) = self.get_callbacks().on_mouse_moved.take() {
                        cb(self, _widgets, _points);
                        self.get_callbacks().on_mouse_moved = Some(cb);
                    }
                }
            }

            /// This function is a macro-created mouse scrolled callback override, created by the
            /// `default_widget_callbacks!()` macro.
            fn mouse_scrolled_callback(&mut self, _widgets: &[WidgetContainer], _points: Points) {
                if self.get_callbacks().has_on_mouse_scrolled() {
                    if let Some(mut cb) = self.get_callbacks().on_mouse_scrolled.take() {
                        cb(self, _widgets, _points);
                        self.get_callbacks().on_mouse_scrolled = Some(cb);
                    }
                }
            }

            /// This function is a macro-created mouse scrolled callback override, created by the
            /// `default_widget_callbacks!()` macro.
            fn button_clicked_callback(&mut self, _widgets: &[WidgetContainer], _button: u8, _clicks: u8, _state: bool) {
                if self.get_callbacks().has_on_mouse_clicked() {
                    if let Some(mut cb) = self.get_callbacks().on_mouse_clicked.take() {
                        cb(self, _widgets, _button, _clicks, _state);
                        self.get_callbacks().on_mouse_clicked = Some(cb);
                    }
                }
            }
        }
    }
}

/// `widgets` is a core rendering library used by `Pushrod`, containing the default set of `Widget`s.
pub mod widgets;

/// `render` is the core rendering/event loop portion of `Pushrod`.
pub mod render;