Struct ProgressWidget

Source
pub struct ProgressWidget { /* private fields */ }
Expand description

This is the storage object for the ProgressWidget. It stores the config, properties, callback registry, the base widget, and progress from 0 to 100.

Implementations§

Source§

impl ProgressWidget

Creates a new ProgressWidget, which draws a progress bar inside a BaseWidget.

Source

pub fn new(points: Points, size: Size, progress: u8) -> Self

Creates a new instance of the ProgressWidget object. It draws a progress bar-style Widget given the xywh coordinates, and the percentage of fill from 0-100. The base color and border colors are set to white and black, respectively. Use the COLOR_SECONDARY setting to change the color of the fill for the progress bar.

Examples found in repository?
examples/progress.rs (line 21)
11pub fn main() {
12    let sdl_context = sdl2::init().unwrap();
13    let video_subsystem = sdl_context.video().unwrap();
14    let window = video_subsystem
15        .window("pushrod-render progress demo", 400, 180)
16        .position_centered()
17        .opengl()
18        .build()
19        .unwrap();
20    let mut engine = Engine::new(400, 180, 60);
21    let mut widget1 = ProgressWidget::new(make_points(20, 20), make_size(360, 40), 25);
22
23    widget1.set_color(CONFIG_COLOR_SECONDARY, Color::RGB(255, 0, 0));
24
25    let mut widget2 = ProgressWidget::new(make_points(20, 70), make_size(360, 40), 50);
26
27    widget2.set_color(CONFIG_COLOR_SECONDARY, Color::RGB(255, 0, 0));
28
29    let mut widget3 = ProgressWidget::new(make_points(20, 120), make_size(360, 40), 75);
30
31    widget3.set_color(CONFIG_COLOR_SECONDARY, Color::RGB(255, 0, 0));
32
33    engine.add_widget(Box::new(widget1), String::from("widget1"));
34    engine.add_widget(Box::new(widget2), String::from("widget2"));
35    engine.add_widget(Box::new(widget3), String::from("widget3"));
36
37    engine.run(sdl_context, window);
38}
More examples
Hide additional examples
examples/timer.rs (line 35)
25pub fn main() {
26    let sdl_context = sdl2::init().unwrap();
27    let video_subsystem = sdl_context.video().unwrap();
28    let window = video_subsystem
29        .window("pushrod-render timer demo", 400, 180)
30        .position_centered()
31        .opengl()
32        .build()
33        .unwrap();
34    let mut engine = Engine::new(400, 180, 30);
35    let mut widget1 = ProgressWidget::new(make_points(20, 20), make_size(360, 40), 25);
36
37    widget1.set_color(CONFIG_COLOR_SECONDARY, Color::RGB(255, 0, 0));
38    widget1.set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 0, 255));
39
40    let mut widget2 = ProgressWidget::new(make_points(20, 70), make_size(360, 40), 50);
41
42    widget2.set_color(CONFIG_COLOR_SECONDARY, Color::RGB(255, 0, 0));
43    widget2.set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 255, 0));
44
45    let mut widget3 = ProgressWidget::new(make_points(20, 120), make_size(360, 40), 75);
46
47    widget3.set_color(CONFIG_COLOR_SECONDARY, Color::RGB(255, 0, 0));
48    widget3.set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 255, 255));
49
50    let mut timer = TimerWidget::new(100, true);
51    timer.on_timeout(|_, _widgets, _layouts| {
52        let widget1_id = widget_id_for_name(_widgets, String::from("widget1"));
53        let widget2_id = widget_id_for_name(_widgets, String::from("widget2"));
54        let widget3_id = widget_id_for_name(_widgets, String::from("widget3"));
55        let progress1_value: u8 =
56            (cast!(_widgets, widget1_id, ProgressWidget).get_progress() + 1) % 100;
57        let progress2_value: u8 =
58            (cast!(_widgets, widget2_id, ProgressWidget).get_progress() + 1) % 100;
59        let progress3_value: u8 =
60            (cast!(_widgets, widget3_id, ProgressWidget).get_progress() + 1) % 100;
61
62        cast!(_widgets, widget1_id, ProgressWidget).set_progress(progress1_value);
63        cast!(_widgets, widget2_id, ProgressWidget).set_progress(progress2_value);
64        cast!(_widgets, widget3_id, ProgressWidget).set_progress(progress3_value);
65    });
66
67    engine.add_widget(Box::new(widget1), String::from("widget1"));
68    engine.add_widget(Box::new(widget2), String::from("widget2"));
69    engine.add_widget(Box::new(widget3), String::from("widget3"));
70    engine.add_widget(Box::new(timer), String::from("timer1"));
71
72    engine.run(sdl_context, window);
73}
Source

pub fn set_progress(&mut self, progress: u8)

Sets the progress for the widget. Progress value is between 0 and 100. Anything over 100 will just set the progress to 100.

Examples found in repository?
examples/timer.rs (line 62)
25pub fn main() {
26    let sdl_context = sdl2::init().unwrap();
27    let video_subsystem = sdl_context.video().unwrap();
28    let window = video_subsystem
29        .window("pushrod-render timer demo", 400, 180)
30        .position_centered()
31        .opengl()
32        .build()
33        .unwrap();
34    let mut engine = Engine::new(400, 180, 30);
35    let mut widget1 = ProgressWidget::new(make_points(20, 20), make_size(360, 40), 25);
36
37    widget1.set_color(CONFIG_COLOR_SECONDARY, Color::RGB(255, 0, 0));
38    widget1.set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 0, 255));
39
40    let mut widget2 = ProgressWidget::new(make_points(20, 70), make_size(360, 40), 50);
41
42    widget2.set_color(CONFIG_COLOR_SECONDARY, Color::RGB(255, 0, 0));
43    widget2.set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 255, 0));
44
45    let mut widget3 = ProgressWidget::new(make_points(20, 120), make_size(360, 40), 75);
46
47    widget3.set_color(CONFIG_COLOR_SECONDARY, Color::RGB(255, 0, 0));
48    widget3.set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 255, 255));
49
50    let mut timer = TimerWidget::new(100, true);
51    timer.on_timeout(|_, _widgets, _layouts| {
52        let widget1_id = widget_id_for_name(_widgets, String::from("widget1"));
53        let widget2_id = widget_id_for_name(_widgets, String::from("widget2"));
54        let widget3_id = widget_id_for_name(_widgets, String::from("widget3"));
55        let progress1_value: u8 =
56            (cast!(_widgets, widget1_id, ProgressWidget).get_progress() + 1) % 100;
57        let progress2_value: u8 =
58            (cast!(_widgets, widget2_id, ProgressWidget).get_progress() + 1) % 100;
59        let progress3_value: u8 =
60            (cast!(_widgets, widget3_id, ProgressWidget).get_progress() + 1) % 100;
61
62        cast!(_widgets, widget1_id, ProgressWidget).set_progress(progress1_value);
63        cast!(_widgets, widget2_id, ProgressWidget).set_progress(progress2_value);
64        cast!(_widgets, widget3_id, ProgressWidget).set_progress(progress3_value);
65    });
66
67    engine.add_widget(Box::new(widget1), String::from("widget1"));
68    engine.add_widget(Box::new(widget2), String::from("widget2"));
69    engine.add_widget(Box::new(widget3), String::from("widget3"));
70    engine.add_widget(Box::new(timer), String::from("timer1"));
71
72    engine.run(sdl_context, window);
73}
Source

pub fn get_progress(&mut self) -> u8

Retrieves the current progress value as a u8 value.

Examples found in repository?
examples/timer.rs (line 56)
25pub fn main() {
26    let sdl_context = sdl2::init().unwrap();
27    let video_subsystem = sdl_context.video().unwrap();
28    let window = video_subsystem
29        .window("pushrod-render timer demo", 400, 180)
30        .position_centered()
31        .opengl()
32        .build()
33        .unwrap();
34    let mut engine = Engine::new(400, 180, 30);
35    let mut widget1 = ProgressWidget::new(make_points(20, 20), make_size(360, 40), 25);
36
37    widget1.set_color(CONFIG_COLOR_SECONDARY, Color::RGB(255, 0, 0));
38    widget1.set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 0, 255));
39
40    let mut widget2 = ProgressWidget::new(make_points(20, 70), make_size(360, 40), 50);
41
42    widget2.set_color(CONFIG_COLOR_SECONDARY, Color::RGB(255, 0, 0));
43    widget2.set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 255, 0));
44
45    let mut widget3 = ProgressWidget::new(make_points(20, 120), make_size(360, 40), 75);
46
47    widget3.set_color(CONFIG_COLOR_SECONDARY, Color::RGB(255, 0, 0));
48    widget3.set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 255, 255));
49
50    let mut timer = TimerWidget::new(100, true);
51    timer.on_timeout(|_, _widgets, _layouts| {
52        let widget1_id = widget_id_for_name(_widgets, String::from("widget1"));
53        let widget2_id = widget_id_for_name(_widgets, String::from("widget2"));
54        let widget3_id = widget_id_for_name(_widgets, String::from("widget3"));
55        let progress1_value: u8 =
56            (cast!(_widgets, widget1_id, ProgressWidget).get_progress() + 1) % 100;
57        let progress2_value: u8 =
58            (cast!(_widgets, widget2_id, ProgressWidget).get_progress() + 1) % 100;
59        let progress3_value: u8 =
60            (cast!(_widgets, widget3_id, ProgressWidget).get_progress() + 1) % 100;
61
62        cast!(_widgets, widget1_id, ProgressWidget).set_progress(progress1_value);
63        cast!(_widgets, widget2_id, ProgressWidget).set_progress(progress2_value);
64        cast!(_widgets, widget3_id, ProgressWidget).set_progress(progress3_value);
65    });
66
67    engine.add_widget(Box::new(widget1), String::from("widget1"));
68    engine.add_widget(Box::new(widget2), String::from("widget2"));
69    engine.add_widget(Box::new(widget3), String::from("widget3"));
70    engine.add_widget(Box::new(timer), String::from("timer1"));
71
72    engine.run(sdl_context, window);
73}

Trait Implementations§

Source§

impl Widget for ProgressWidget

This is the Widget implementation of the ProgressWidget. It contains a BaseWidget within its bounds to draw the base background, then draws the progress fill over the top.

Source§

fn as_any(&mut self) -> &mut dyn Any

This function is a macro-created getter function that returns the Widget as an Any type. This allows the Widget trait to be downcast into a struct that implements the Widget trait.

Source§

fn get_config(&mut self) -> &mut WidgetConfig

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.

Source§

fn get_system_properties(&mut self) -> &mut HashMap<i32, String>

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.

Source§

fn get_callbacks(&mut self) -> &mut CallbackRegistry

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.

Source§

fn tick_callback( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], )

This function is a macro-created tick callback override, created by the default_widget_callbacks!() macro.

Source§

fn mouse_entered_callback( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], )

This function is a macro-created mouse entered callback override, created by the default_widget_callbacks!() macro.

Source§

fn mouse_exited_callback( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], )

This function is a macro-created mouse exited callback override, created by the default_widget_callbacks!() macro.

Source§

fn mouse_moved_callback( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _points: Points, )

This function is a macro-created mouse moved callback override, created by the default_widget_callbacks!() macro.

Source§

fn mouse_scrolled_callback( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _points: Points, )

This function is a macro-created mouse scrolled callback override, created by the default_widget_callbacks!() macro.

Source§

fn button_clicked_callback( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _button: u8, _clicks: u8, _state: bool, )

This function is a macro-created mouse scrolled callback override, created by the default_widget_callbacks!() macro.

Source§

fn draw( &mut self, c: &mut Canvas<Window>, _t: &mut TextureCache, ) -> Option<&Texture>

Draws the widget. If you wish to modify the canvas object, you must declare it as mut in your implementation (ie fn draw(&mut self, mut canvas: Canvas<Window>)). The _canvas is the currently active drawing canvas at the time this function is called. This called during the draw loop of the Engine. This returns a reference to the stored Texture object within the Widget. It is then copied to the canvas, and displayed in the display loop. In this function, you can just return a reference to the Texture if no invalidation state was set, otherwise, the draw can be re-performed, and the Texture returned. If the drawing function returns no texture, return a None, and it will not be rendered during the display loop, but it will still be called. A TextureCache is provided in case your Widget needs to cache an image or a font store. Read more
Source§

fn mouse_entered( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], )

When a mouse enters the bounds of the Widget, this function is triggered. This function implementation is optional.
Source§

fn mouse_exited( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], )

When a mouse exits the bounds of the Widget, this function is triggered. This function implementation is optional.
Source§

fn mouse_moved( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _points: Points, )

When a mouse moves within the bounds of the Widget, this function is triggered. It contains the X and Y coordinates relative to the bounds of the Widget. The points start at 0x0. This function implementation is optional.
Source§

fn mouse_scrolled( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _points: Points, )

When a mouse scroll is triggered within the bounds of the Widget, this function is triggered. Movement along the X axis indicate horizontal movement, where the Y axis indicates vertical movement. Positive movement means to the right or down, respectively. Negative movement means to the left or up, respectively. This function implementation is optional.
Source§

fn button_clicked( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _button: u8, _clicks: u8, _state: bool, )

When a mouse button is clicked within (or outside of) the bounds of the Widget, this function is called. If a mouse button is clicked, and the mouse leaves the bounds of the Widget, the mouse release event will still be triggered for the last Widget which received the mouse down state. This prevents Widgets from becoming confused. This behavior is tracked by the main loop, not by the Widget code. Therefore, when a mouse button is released outside of the bounds of this Widget, you must adjust your state accordingly, if you pay attention to the button_clicked function. This function implementation is optional.
Source§

fn tick(&mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer])

When a timer tick goes by (ie. a frame is displayed on the screen), this function is called. This function implementation is optional.
Source§

fn other_event( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _event: Event, )

When an Event is sent to the application that is not handled by the Engine::run loop, this method is called, sending the unhandled Event to the currently active Widget. This behavior is subject to change as the Engine::run loop is modified to handle more Events.
Source§

fn on_config_changed(&mut self, _k: u8, _v: Config)

This callback is called when a setter is used to configure a value. It is not called when a call to get_config() using the setter is called, so it is best to use the top-level setters and getters for the configuration values - at least, until the get_config() call can be made private.
Source§

fn set_point(&mut self, config: u8, x: i32, y: i32)

Sets a point for a configuration key.
Source§

fn set_color(&mut self, config: u8, color: Color)

Sets a color for a configuration key.
Source§

fn set_numeric(&mut self, config: u8, value: i32)

Sets a numeric value for a configuration key.
Source§

fn set_text(&mut self, config: u8, text: String)

Sets a text value for a configuration key.
Source§

fn set_toggle(&mut self, config: u8, flag: bool)

Sets a toggle for a configuration key.
Source§

fn set_compass(&mut self, config: u8, value: CompassPosition)

Sets a compass position for a configuration key.
Source§

fn get_point(&mut self, k: u8) -> Points

Retrieves a Points for a configuration key. Returns Points::default if not set.
Source§

fn get_size(&mut self, k: u8) -> Size

Retrieves a Size for a configuration key. Returns a Size::default if not set.
Source§

fn get_color(&mut self, k: u8) -> Color

Retrieves a Color for a configuration key. Returns white if not set.
Source§

fn get_numeric(&mut self, k: u8) -> i32

Retrieves a numeric value for a configuration key. Returns 0 if not set.
Source§

fn get_text(&mut self, k: u8) -> String

Retrieves text for a configuration key. Returns a blank string if not set.
Source§

fn get_toggle(&mut self, k: u8) -> bool

Retrieves a boolean toggle for a configuration key. Returns false if not set.
Source§

fn get_compass(&mut self, k: u8) -> CompassPosition

Retrieves a CompassPosition toggle for a configuration key. Returns CompassPosition::W if not set.
Source§

fn set_origin(&mut self, _origin: Points)

Sets the origin of the Widget, adjusting the X and Y coordinates. Automatically sets the invalidate flag to true when adjusted, but only if the new origin is not the same as the previous origin.
Source§

fn set_size(&mut self, _size: Vec<u32>)

Sets the size of the Widget, adjusting the width and height. Automatically sets the invalidate flag to true when adjusted, but only if the new size is not the same as the previous size.
Source§

fn get_drawing_area(&mut self) -> Rect

Returns a Rect object containing the drawing bounds of this Widget.
Source§

fn is_invalidated(&mut self) -> bool

Returns whether or not a Widget is invalidated state.
Source§

fn set_invalidated(&mut self, flag: bool)

Sets invalidation state for the current Widget.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.