[−][src]Trait pushrod::widget::widget::Widget
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
.
Required methods
fn get_config(&mut self) -> &RefCell<HashMap<ConfigKey, WidgetConfig>>
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:
struct MyWidget { config: RefCell<HashMap<ConfigKey, WidgetConfig>> } impl MyWidget { fn new() -> Self { Self { config: RefCell::new(HashMap::new()), } } }
And in the overridden function for get_config in your implementation, use:
struct MyWidget { config: RefCell<HashMap<u8, WidgetConfig>> } impl Widget for MyWidget { fn get_config(&mut self) -> &RefCell<HashMap<u8, WidgetConfig>> { &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 mouse_entered(&mut self, widget_id: i32)
Called when a mouse enters the bounds of the widget. Includes the widget ID.
fn mouse_exited(&mut self, widget_id: i32)
Called when a mouse exits the bounds of the widget. Includes the widget ID.
fn mouse_scrolled(&mut self, widget_id: i32, point: Point)
Called when a scroll event is called within the bounds of the widget. Includes the widget ID.
Provided methods
fn set_config(&mut self, key: u8, value: WidgetConfig)
Sets a configuration object by its key.
fn invalidate(&mut self)
Indicates that a widget needs to be redrawn/refreshed.
fn clear_invalidate(&mut self)
Clears the invalidation flag.
fn is_invalidated(&mut self) -> bool
Checks to see whether or not the widget needs to be redrawn/refreshed.
fn set_origin(&mut self, point: Point)
Sets the Point
of origin for this widget. Invalidates the widget afterward.
fn get_origin(&mut self) -> Point
Retrieves the Point
of origin for this object.
Defaults to origin (0, 0) if not set.
fn set_size(&mut self, size: Size)
Sets the Size
for this widget. Invalidates the widget afterward.
fn get_size(&mut self) -> Size
Retrieves the Size
bounds for this widget.
Defaults to size (0, 0) if not set.
fn set_color(&mut self, color: Color)
Sets the color for this widget. Invalidates the widget afterward.
fn get_color(&mut self) -> Color
Retrieves the color of this widget.
Defaults to white color [1.0; 4]
if not set.
fn draw(&mut self, context: Context, graphics: &mut GlGraphics)
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.)
Implementors
impl Widget for BoxWidget
[src]
Implementation of the BoxWidget
object with the Widget
traits implemented.
This implementation is similar to the BaseWidget
, but incorporates a drawable box inside
the widget. Base widget is the BaseWidget
.
This is basically just a box with a fill color. Use this to draw other things like buttons, text widgets, and so on, if you need anything with a drawable border.
Example usage:
let mut box_widget = BoxWidget::new(); box_widget.set_origin(Point { x: 100, y: 100 }); box_widget.set_size(pushrod::core::point::Size { w: 200, h: 200 }); box_widget.set_color([0.5, 0.5, 0.5, 1.0]); box_widget.set_border_color([0.0, 0.0, 0.0, 1.0]); box_widget.set_border_thickness(3);
fn get_config(&mut self) -> &RefCell<HashMap<ConfigKey, WidgetConfig>>
[src]
fn set_origin(&mut self, point: Point)
[src]
Sets the Point
of origin for this widget and the base widget. Invalidates the widget afterward.
fn set_size(&mut self, size: Size)
[src]
Sets the Size
for this widget and the base widget. Invalidates the widget afterward.
fn set_color(&mut self, color: Color)
[src]
Sets the color for this widget. Invalidates the widget afterward.
fn get_color(&mut self) -> Color
[src]
Retrieves the color of this widget.
Defaults to white color [1.0; 4]
if not set.
fn mouse_entered(&mut self, widget_id: i32)
[src]
fn mouse_exited(&mut self, widget_id: i32)
[src]
fn mouse_scrolled(&mut self, widget_id: i32, point: Point)
[src]
fn draw(&mut self, context: Context, graphics: &mut GlGraphics)
[src]
Draws the contents of the widget in this order:
- Base widget first
- Box graphic for the specified width
fn set_config(&mut self, key: u8, value: WidgetConfig)
[src]
fn invalidate(&mut self)
[src]
fn clear_invalidate(&mut self)
[src]
fn is_invalidated(&mut self) -> bool
[src]
fn get_origin(&mut self) -> Point
[src]
fn get_size(&mut self) -> Size
[src]
impl Widget for BaseWidget
[src]
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:
let mut base_widget = BaseWidget::new(); base_widget.set_origin(Point { x: 100, y: 100 }); base_widget.set_size(pushrod::core::point::Size { w: 200, h: 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(Point { x: 125, y: 125 }); base_widget_2.set_size(pushrod::core::poiNt::Size { w: 100, h: 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);