pushrod/
lib.rs

1// Pushrod Widget Library
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! `pushrod` is a GUI library for Rust.
16//!
17//! # Dependencies
18//! Pushrod uses the following dependency:
19//! ```ignore
20//! [dependencies.sdl2]
21//! version = "^0.32"
22//! features = ["ttf", "image"]
23//! ```
24//!
25//! To use the crate in your project, add the following dependencies:
26//! ```ignore
27//! [dependencies]
28//! rust-pushrod = "^0.4"
29//! ```
30//! This will pull in the latest version.
31//!
32//! # Core Components
33//! `pushrod::render` is the _core_ rendering components, containing the `Widget` base class, and
34//! drawing loop logic.
35//! `pushrod::widgets` is the extended `Widget` component library.
36
37#[macro_use]
38
39/// These macros are used and shared by all components in the `Pushrod` library.
40mod macros {
41    #[macro_export]
42
43    /// This macro is used by `Widget` implementations, which auto-injects getter code for the
44    /// `Widget`'s properties.  Since all `Widget` implementations share these functions, and must
45    /// implement them locally, this `macro` serves as a quick way to implement the same reused code
46    /// automatically.
47    macro_rules! default_widget_properties {
48        () => {
49            /// This function is a macro-created getter function that returns the `Widget`'s configuration
50            /// object as a borrowed mutable reference.  This code is auto-generated using the
51            /// `default_widget_properties!()` macro.
52            fn get_config(&mut self) -> &mut WidgetConfig {
53                &mut self.config
54            }
55
56            /// This function is a macro-created getter function that returns the `Widget`'s system
57            /// properties as a borrowed mutable reference.  This code is auto-generated using the
58            /// `default_widget_properties!()` macro.
59            fn get_system_properties(&mut self) -> &mut HashMap<i32, String> {
60                &mut self.system_properties
61            }
62
63            /// This function is a macro-created getter function that returns the `Widget`'s
64            /// `CallbackRegistry` object as a borrowed mutable reference.  This code is auto-generated
65            /// using the `default_widget_properties!()` macro.
66            fn get_callbacks(&mut self) -> &mut CallbackRegistry {
67                &mut self.callback_registry
68            }
69        }
70    }
71
72    /// This macro is used by `Widget` implementations, which auto-injects callback functions.  It
73    /// overrides standard functions so that mouse events, tick events, keyboard events, etc. all are
74    /// routed through the `get_callbacks()` property function.
75    macro_rules! default_widget_callbacks {
76        () => {
77            /// This function is a macro-created tick callback override, created by the
78            /// `default_widget_callbacks!()` macro.
79            fn tick_callback(&mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer]) {
80                if self.get_callbacks().has_on_tick() {
81                    if let Some(mut cb) = self.get_callbacks().on_tick.take() {
82                        cb(self, _widgets, _layouts);
83                        self.get_callbacks().on_tick = Some(cb);
84                    }
85                }
86            }
87
88            /// This function is a macro-created mouse entered callback override, created by the
89            /// `default_widget_callbacks!()` macro.
90            fn mouse_entered_callback(&mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer]) {
91                if self.get_callbacks().has_on_mouse_entered() {
92                    if let Some(mut cb) = self.get_callbacks().on_mouse_entered.take() {
93                        cb(self, _widgets, _layouts);
94                        self.get_callbacks().on_mouse_entered = Some(cb);
95                    }
96                }
97            }
98
99            /// This function is a macro-created mouse exited callback override, created by the
100            /// `default_widget_callbacks!()` macro.
101            fn mouse_exited_callback(&mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer]) {
102                if self.get_callbacks().has_on_mouse_exited() {
103                    if let Some(mut cb) = self.get_callbacks().on_mouse_exited.take() {
104                        cb(self, _widgets, _layouts);
105                        self.get_callbacks().on_mouse_exited = Some(cb);
106                    }
107                }
108            }
109
110            /// This function is a macro-created mouse moved callback override, created by the
111            /// `default_widget_callbacks!()` macro.
112            fn mouse_moved_callback(&mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _points: Points) {
113                if self.get_callbacks().has_on_mouse_moved() {
114                    if let Some(mut cb) = self.get_callbacks().on_mouse_moved.take() {
115                        cb(self, _widgets, _layouts, _points);
116                        self.get_callbacks().on_mouse_moved = Some(cb);
117                    }
118                }
119            }
120
121            /// This function is a macro-created mouse scrolled callback override, created by the
122            /// `default_widget_callbacks!()` macro.
123            fn mouse_scrolled_callback(&mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _points: Points) {
124                if self.get_callbacks().has_on_mouse_scrolled() {
125                    if let Some(mut cb) = self.get_callbacks().on_mouse_scrolled.take() {
126                        cb(self, _widgets, _layouts, _points);
127                        self.get_callbacks().on_mouse_scrolled = Some(cb);
128                    }
129                }
130            }
131
132            /// This function is a macro-created mouse scrolled callback override, created by the
133            /// `default_widget_callbacks!()` macro.
134            fn button_clicked_callback(&mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _button: u8, _clicks: u8, _state: bool) {
135                if self.get_callbacks().has_on_mouse_clicked() {
136                    if let Some(mut cb) = self.get_callbacks().on_mouse_clicked.take() {
137                        cb(self, _widgets, _layouts, _button, _clicks, _state);
138                        self.get_callbacks().on_mouse_clicked = Some(cb);
139                    }
140                }
141            }
142        }
143    }
144
145    /// This macro implements extra functions for the `Widget` automatically.  This is a list of functions
146    /// that are not optional, and must be implemented in some form or fashion.  If you choose not to
147    /// implement your own version of the functions, use this macro to apply the functions automatically.
148    macro_rules! default_widget_functions {
149        () => {
150            /// This function is a macro-created getter function that returns the `Widget` as an `Any`
151            /// type.  This allows the `Widget` trait to be downcast into a `struct` that implements
152            /// the `Widget` trait.
153            fn as_any(&mut self) -> &mut dyn Any {
154                self
155            }
156        }
157    }
158}
159
160/// `widgets` is a core rendering library used by `Pushrod`, containing the default set of `Widget`s.
161pub mod widgets;
162
163/// `render` is the core rendering/event loop portion of `Pushrod`.
164pub mod render;
165
166/// `layouts` is the core layout managers included with `Pushrod`.
167pub mod layouts;