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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
// Widget Base Definition // // 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 opengl_graphics::GlGraphics; use piston_window::*; use crate::core::point::*; use crate::widget::config::*; /// Implementable trait that is used by every `Widget`. These are the public methods, /// and a function _may_ override them. /// /// You _must_ implement the following methods: /// /// - get_config /// - mouse_entered /// - mouse_exited /// - mouse_scrolled /// /// You _should_ override `draw`, but you are not required to. /// /// If you want a blank base widget, refer to the `BaseWidget`, which will create a /// base widget that paints the contents of its bounds with whatever color has been /// specified with `set_color`. pub trait Widget { /// Retrieves the configuration HashMap that stores the configuration list of settings /// for this widget. /// /// To implement this, the following code could be used in your object's structure: /// /// ``` /// # use pushrod::widget::widget::*; /// # use pushrod::widget::config::*; /// struct MyWidget { /// config: Configurable, /// } /// /// impl MyWidget { /// fn new() -> Self { /// Self { /// config: Configurable::new(), /// } /// } /// } /// ``` /// /// And in the overridden function for get_config in your implementation, use: /// /// ``` /// # use pushrod::widget::widget::*; /// # use pushrod::widget::config::*; /// # use pushrod::core::point::Point; /// struct MyWidget { /// config: Configurable, /// } /// /// impl Widget for MyWidget { /// fn config(&mut self) -> &mut Configurable { /// &mut self.config /// } /// /// fn mouse_entered(&mut self, widget_id: i32) {} /// fn mouse_exited(&mut self, widget_id: i32) {} /// fn mouse_scrolled(&mut self, widget_id: i32, point: Point) {} /// } /// ``` /// /// This uses a `RefCell`, since configurations require a mutable reference to the HashMap /// that stores the configs. fn config(&mut self) -> &mut Configurable; /// Indicates that a widget needs to be redrawn/refreshed. fn invalidate(&mut self) { self.config() .set(CONFIG_INVALIDATE, WidgetConfig::Invalidate {}); } /// Clears the invalidation flag. fn clear_invalidate(&mut self) { self.config().remove(CONFIG_INVALIDATE); } /// Checks to see whether or not the widget needs to be redrawn/refreshed. fn is_invalidated(&mut self) -> bool { self.config().contains_key(CONFIG_INVALIDATE) } /// Sets the `Point` of origin for this widget, given the X and Y origin points. Invalidates the widget afterward. fn set_origin(&mut self, x: i32, y: i32) { self.config().set( CONFIG_ORIGIN, WidgetConfig::Origin { point: Point { x, y }, }, ); self.invalidate(); } /// Retrieves the `Point` of origin for this object. /// Defaults to origin (0, 0) if not set. fn get_origin(&mut self) -> Point { match self.config().get(CONFIG_ORIGIN) { Some(WidgetConfig::Origin { ref point }) => point.clone(), None => make_origin_point(), _ => make_origin_point(), } } /// Sets the `Size` for this widget, given a width and height. Invalidates the widget afterward. fn set_size(&mut self, w: i32, h: i32) { self.config().set( CONFIG_SIZE, WidgetConfig::Size { size: crate::core::point::Size { w, h }, }, ); self.invalidate(); } /// Retrieves the `Size` bounds for this widget. /// Defaults to size (0, 0) if not set. fn get_size(&mut self) -> crate::core::point::Size { match self.config().get(CONFIG_SIZE) { Some(WidgetConfig::Size { ref size }) => size.clone(), None => make_unsized(), _ => make_unsized(), } } /// Sets the color for this widget. Invalidates the widget afterward. fn set_color(&mut self, color: types::Color) { self.config() .set(CONFIG_COLOR, WidgetConfig::Color { color }); self.invalidate(); } /// Retrieves the color of this widget. /// Defaults to white color `[1.0; 4]` if not set. fn get_color(&mut self) -> types::Color { if self.config().contains_key(CONFIG_COLOR) { match self.config().get(CONFIG_COLOR) { Some(WidgetConfig::Color { ref color }) => [color[0], color[1], color[2], color[3]], None => [1.0; 4], _ => [1.0; 4], } } else { [1.0; 4] } } // Events /// Called when a mouse enters the bounds of the widget. Includes the widget ID. fn mouse_entered(&mut self, widget_id: i32); /// Called when a mouse exits the bounds of the widget. Includes the widget ID. fn mouse_exited(&mut self, widget_id: i32); /// Called when a scroll event is called within the bounds of the widget. Includes the widget ID. fn mouse_scrolled(&mut self, widget_id: i32, point: Point); // Draw routines /// Draws the contents of the widget, provided a `piston2d` `Context` and `GlGraphics` object. /// /// It is **highly recommended** that you call `clear_invalidate()` after the draw completes, /// otherwise, this will continue to be redrawn continuously (unless this is the desired /// behavior.) fn draw(&mut self, context: Context, graphics: &mut GlGraphics) { let origin: Point = self.get_origin(); let size: crate::core::point::Size = self.get_size(); rectangle( self.get_color(), [ origin.x as f64, origin.y as f64, size.w as f64, size.h as f64, ], context.transform, graphics, ); self.clear_invalidate(); } } /// This is the `BaseWidget`, which contains a top-level widget for display. It does /// not contain any special logic other than being a base for a display layer. pub struct BaseWidget { config: Configurable, } /// Implementation of the constructor for the `PushrodBaseWidget`. Creates a new base widget /// that can be positioned anywhere on the screen. impl BaseWidget { pub fn new() -> Self { Self { config: Configurable::new(), } } } /// Implementation of the `BaseWidget` object with the `Widget` traits implemented. /// This function only implements `get_config`, and samples of `mouse_entered`, `mouse_exited`, /// and `mouse_scrolled`, which currently trigger messages to the screen. /// /// Example usage: /// ```no_run /// # use piston_window::*; /// # use pushrod::core::point::*; /// # use pushrod::core::window::*; /// # use pushrod::widget::widget::*; /// # fn main() { /// # let opengl = OpenGL::V3_2; /// # let mut pushrod_window: PushrodWindow = PushrodWindow::new( /// # WindowSettings::new("Pushrod Window", [640, 480]) /// # .opengl(opengl) /// # .build() /// # .unwrap_or_else(|error| panic!("Failed to build PistonWindow: {}", error)), /// # ); /// # /// let mut base_widget = BaseWidget::new(); /// /// base_widget.set_origin(100, 100); /// base_widget.set_size(200, 200); /// base_widget.set_color([0.5, 0.5, 0.5, 1.0]); /// /// // Widgets must be boxed, as they are trait objects. /// let widget_id = pushrod_window.add_widget(Box::new(base_widget)); /// /// eprintln!("Added widget: ID={}", widget_id); /// /// let mut base_widget_2 = BaseWidget::new(); /// /// base_widget_2.set_origin(125, 125); /// base_widget_2.set_size(100, 100); /// base_widget_2.set_color([0.75, 0.75, 0.75, 1.0]); /// /// // Add the second widget to the top level base widget. /// let widget_id_2 = pushrod_window.add_widget_to_parent(Box::new(base_widget_2), widget_id); /// # } /// ``` impl Widget for BaseWidget { fn config(&mut self) -> &mut Configurable { &mut self.config } fn mouse_entered(&mut self, widget_id: i32) { eprintln!("[Base] Mouse entered: id={}", widget_id); } fn mouse_exited(&mut self, widget_id: i32) { eprintln!("[Base] Mouse exited: id={}", widget_id); } fn mouse_scrolled(&mut self, widget_id: i32, point: Point) { eprintln!( "[Base] Mouse scrolled: x={} y={}: id={}", point.x, point.y, widget_id ); } }