Skip to main content

SliderWidget

Struct SliderWidget 

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

This is the storage object for the SliderWidget. It stores the config, properties, callback registry.

Implementations§

Source§

impl SliderWidget

This is the implementation of the SliderWidget, a control that draws a bounds line indicator, and a draggable slider.

Source

pub fn new( points: Points, size: Size, min: u32, max: u32, current: u32, orientation: SliderOrientation, ) -> Self

Creates a new SliderWidget given the x, y, w, h coordinates, sets the min and max values, the current value, and the orientation of the slider as drawn.

Examples found in repository?
examples/grid.rs (lines 55-62)
32pub fn main() {
33    let sdl_context = sdl2::init().unwrap();
34    let video_subsystem = sdl_context.video().unwrap();
35    let window = video_subsystem
36        .window("pushrod-render grid demo", 600, 340)
37        .position_centered()
38        .opengl()
39        .build()
40        .unwrap();
41    let mut engine = Engine::new(600, 340, 60);
42    let mut grid1 = GridWidget::new(make_points(20, 20), make_size(280, 280), 10, false);
43    let mut grid2 = GridWidget::new(make_points(310, 20), make_size(280, 280), 10, true);
44
45    grid1
46        .get_config()
47        .set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 0, 0));
48    grid1.get_config().set_numeric(CONFIG_BORDER_WIDTH, 1);
49
50    grid2
51        .get_config()
52        .set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 0, 0));
53    grid2.get_config().set_numeric(CONFIG_BORDER_WIDTH, 1);
54
55    let mut slider1 = SliderWidget::new(
56        make_points(20, 310),
57        make_size(320, 20),
58        1,
59        20,
60        10,
61        SliderHorizontal,
62    );
63
64    slider1.on_value_changed(|_slider, _widgets, _layouts, pos| {
65        let text1_id = widget_id_for_name(_widgets, String::from("text1"));
66        let grid1_id = widget_id_for_name(_widgets, String::from("grid1"));
67        let grid2_id = widget_id_for_name(_widgets, String::from("grid2"));
68
69        cast!(_widgets, text1_id, TextWidget).set_text(format!("{}", pos));
70        cast!(_widgets, grid1_id, GridWidget).set_grid_size(pos);
71        cast!(_widgets, grid2_id, GridWidget).set_grid_size(pos);
72    });
73
74    let mut text_widget1 = TextWidget::new(
75        String::from("assets/OpenSans-Regular.ttf"),
76        sdl2::ttf::FontStyle::NORMAL,
77        16,
78        TextJustify::Left,
79        String::from("10"),
80        make_points(360, 310),
81        make_size(40, 20),
82    );
83
84    text_widget1
85        .get_config()
86        .set_color(CONFIG_COLOR_TEXT, Color::RGB(0, 0, 0));
87
88    engine.add_widget(Box::new(grid1), String::from("grid1"));
89    engine.add_widget(Box::new(grid2), String::from("grid2"));
90    engine.add_widget(Box::new(slider1), String::from("slider1"));
91    engine.add_widget(Box::new(text_widget1), String::from("text1"));
92
93    engine.run(sdl_context, window);
94}
More examples
Hide additional examples
examples/slider.rs (lines 41-48)
31pub fn main() {
32    let sdl_context = sdl2::init().unwrap();
33    let video_subsystem = sdl_context.video().unwrap();
34    let window = video_subsystem
35        .window("pushrod-render slider demo", 400, 300)
36        .position_centered()
37        .opengl()
38        .build()
39        .unwrap();
40    let mut engine = Engine::new(400, 300, 60);
41    let mut slider1 = SliderWidget::new(
42        make_points(20, 20),
43        make_size(300, 20),
44        0,
45        100,
46        20,
47        SliderHorizontal,
48    );
49
50    slider1.on_value_changed(|_slider, _widgets, _layouts, pos| {
51        let text1_id = widget_id_for_name(_widgets, String::from("text1"));
52
53        cast!(_widgets, text1_id, TextWidget).set_text(format!("{}", pos));
54    });
55
56    let mut text_widget1 = TextWidget::new(
57        String::from("assets/OpenSans-Regular.ttf"),
58        sdl2::ttf::FontStyle::NORMAL,
59        16,
60        TextJustify::Left,
61        String::from("20"),
62        make_points(330, 20),
63        make_size(50, 20),
64    );
65
66    text_widget1
67        .get_config()
68        .set_color(CONFIG_COLOR_TEXT, Color::RGB(0, 0, 0));
69
70    let mut slider2 = SliderWidget::new(
71        make_points(20, 50),
72        make_size(300, 20),
73        20,
74        80,
75        40,
76        SliderHorizontal,
77    );
78
79    slider2.on_value_changed(|_slider, _widgets, _layouts, pos| {
80        let text2_id = widget_id_for_name(_widgets, String::from("text2"));
81
82        cast!(_widgets, text2_id, TextWidget).set_text(format!("{}", pos));
83    });
84
85    let mut text_widget2 = TextWidget::new(
86        String::from("assets/OpenSans-Regular.ttf"),
87        sdl2::ttf::FontStyle::NORMAL,
88        16,
89        TextJustify::Left,
90        String::from("40"),
91        make_points(330, 50),
92        make_size(50, 20),
93    );
94
95    text_widget2
96        .get_config()
97        .set_color(CONFIG_COLOR_TEXT, Color::RGB(0, 0, 0));
98
99    let mut slider3 = SliderWidget::new(
100        make_points(30, 80),
101        make_size(20, 170),
102        0,
103        100,
104        0,
105        SliderVertical,
106    );
107
108    slider3.on_value_changed(|_slider, _widgets, _layouts, pos| {
109        let text3_id = widget_id_for_name(_widgets, String::from("text3"));
110
111        cast!(_widgets, text3_id, TextWidget).set_text(format!("{}", pos));
112    });
113
114    let mut text_widget3 = TextWidget::new(
115        String::from("assets/OpenSans-Regular.ttf"),
116        sdl2::ttf::FontStyle::NORMAL,
117        16,
118        TextJustify::Center,
119        String::from("0"),
120        make_points(16, 270),
121        make_size(50, 20),
122    );
123
124    text_widget3
125        .get_config()
126        .set_color(CONFIG_COLOR_TEXT, Color::RGB(0, 0, 0));
127
128    let mut slider4 = SliderWidget::new(
129        make_points(60, 80),
130        make_size(20, 170),
131        20,
132        80,
133        40,
134        SliderVertical,
135    );
136
137    slider4.on_value_changed(|_slider, _widgets, _layouts, pos| {
138        let text4_id = widget_id_for_name(_widgets, String::from("text4"));
139
140        cast!(_widgets, text4_id, TextWidget).set_text(format!("{}", pos));
141    });
142
143    let mut text_widget4 = TextWidget::new(
144        String::from("assets/OpenSans-Regular.ttf"),
145        sdl2::ttf::FontStyle::NORMAL,
146        16,
147        TextJustify::Center,
148        String::from("40"),
149        make_points(56, 270),
150        make_size(50, 20),
151    );
152
153    text_widget4
154        .get_config()
155        .set_color(CONFIG_COLOR_TEXT, Color::RGB(0, 0, 0));
156
157    engine.add_widget(Box::new(slider1), String::from("slider1"));
158    engine.add_widget(Box::new(text_widget1), String::from("text1"));
159    engine.add_widget(Box::new(slider2), String::from("slider2"));
160    engine.add_widget(Box::new(text_widget2), String::from("text2"));
161    engine.add_widget(Box::new(slider3), String::from("slider3"));
162    engine.add_widget(Box::new(text_widget3), String::from("text3"));
163    engine.add_widget(Box::new(slider4), String::from("slider4"));
164    engine.add_widget(Box::new(text_widget4), String::from("text4"));
165
166    engine.run(sdl_context, window);
167}
Source

pub fn on_value_changed<F>(&mut self, callback: F)
where F: FnMut(&mut SliderWidget, &[WidgetContainer], &[LayoutContainer], u32) + 'static,

Assigns the callback closure that will be used when the Widget changes value.

Examples found in repository?
examples/grid.rs (lines 64-72)
32pub fn main() {
33    let sdl_context = sdl2::init().unwrap();
34    let video_subsystem = sdl_context.video().unwrap();
35    let window = video_subsystem
36        .window("pushrod-render grid demo", 600, 340)
37        .position_centered()
38        .opengl()
39        .build()
40        .unwrap();
41    let mut engine = Engine::new(600, 340, 60);
42    let mut grid1 = GridWidget::new(make_points(20, 20), make_size(280, 280), 10, false);
43    let mut grid2 = GridWidget::new(make_points(310, 20), make_size(280, 280), 10, true);
44
45    grid1
46        .get_config()
47        .set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 0, 0));
48    grid1.get_config().set_numeric(CONFIG_BORDER_WIDTH, 1);
49
50    grid2
51        .get_config()
52        .set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 0, 0));
53    grid2.get_config().set_numeric(CONFIG_BORDER_WIDTH, 1);
54
55    let mut slider1 = SliderWidget::new(
56        make_points(20, 310),
57        make_size(320, 20),
58        1,
59        20,
60        10,
61        SliderHorizontal,
62    );
63
64    slider1.on_value_changed(|_slider, _widgets, _layouts, pos| {
65        let text1_id = widget_id_for_name(_widgets, String::from("text1"));
66        let grid1_id = widget_id_for_name(_widgets, String::from("grid1"));
67        let grid2_id = widget_id_for_name(_widgets, String::from("grid2"));
68
69        cast!(_widgets, text1_id, TextWidget).set_text(format!("{}", pos));
70        cast!(_widgets, grid1_id, GridWidget).set_grid_size(pos);
71        cast!(_widgets, grid2_id, GridWidget).set_grid_size(pos);
72    });
73
74    let mut text_widget1 = TextWidget::new(
75        String::from("assets/OpenSans-Regular.ttf"),
76        sdl2::ttf::FontStyle::NORMAL,
77        16,
78        TextJustify::Left,
79        String::from("10"),
80        make_points(360, 310),
81        make_size(40, 20),
82    );
83
84    text_widget1
85        .get_config()
86        .set_color(CONFIG_COLOR_TEXT, Color::RGB(0, 0, 0));
87
88    engine.add_widget(Box::new(grid1), String::from("grid1"));
89    engine.add_widget(Box::new(grid2), String::from("grid2"));
90    engine.add_widget(Box::new(slider1), String::from("slider1"));
91    engine.add_widget(Box::new(text_widget1), String::from("text1"));
92
93    engine.run(sdl_context, window);
94}
More examples
Hide additional examples
examples/slider.rs (lines 50-54)
31pub fn main() {
32    let sdl_context = sdl2::init().unwrap();
33    let video_subsystem = sdl_context.video().unwrap();
34    let window = video_subsystem
35        .window("pushrod-render slider demo", 400, 300)
36        .position_centered()
37        .opengl()
38        .build()
39        .unwrap();
40    let mut engine = Engine::new(400, 300, 60);
41    let mut slider1 = SliderWidget::new(
42        make_points(20, 20),
43        make_size(300, 20),
44        0,
45        100,
46        20,
47        SliderHorizontal,
48    );
49
50    slider1.on_value_changed(|_slider, _widgets, _layouts, pos| {
51        let text1_id = widget_id_for_name(_widgets, String::from("text1"));
52
53        cast!(_widgets, text1_id, TextWidget).set_text(format!("{}", pos));
54    });
55
56    let mut text_widget1 = TextWidget::new(
57        String::from("assets/OpenSans-Regular.ttf"),
58        sdl2::ttf::FontStyle::NORMAL,
59        16,
60        TextJustify::Left,
61        String::from("20"),
62        make_points(330, 20),
63        make_size(50, 20),
64    );
65
66    text_widget1
67        .get_config()
68        .set_color(CONFIG_COLOR_TEXT, Color::RGB(0, 0, 0));
69
70    let mut slider2 = SliderWidget::new(
71        make_points(20, 50),
72        make_size(300, 20),
73        20,
74        80,
75        40,
76        SliderHorizontal,
77    );
78
79    slider2.on_value_changed(|_slider, _widgets, _layouts, pos| {
80        let text2_id = widget_id_for_name(_widgets, String::from("text2"));
81
82        cast!(_widgets, text2_id, TextWidget).set_text(format!("{}", pos));
83    });
84
85    let mut text_widget2 = TextWidget::new(
86        String::from("assets/OpenSans-Regular.ttf"),
87        sdl2::ttf::FontStyle::NORMAL,
88        16,
89        TextJustify::Left,
90        String::from("40"),
91        make_points(330, 50),
92        make_size(50, 20),
93    );
94
95    text_widget2
96        .get_config()
97        .set_color(CONFIG_COLOR_TEXT, Color::RGB(0, 0, 0));
98
99    let mut slider3 = SliderWidget::new(
100        make_points(30, 80),
101        make_size(20, 170),
102        0,
103        100,
104        0,
105        SliderVertical,
106    );
107
108    slider3.on_value_changed(|_slider, _widgets, _layouts, pos| {
109        let text3_id = widget_id_for_name(_widgets, String::from("text3"));
110
111        cast!(_widgets, text3_id, TextWidget).set_text(format!("{}", pos));
112    });
113
114    let mut text_widget3 = TextWidget::new(
115        String::from("assets/OpenSans-Regular.ttf"),
116        sdl2::ttf::FontStyle::NORMAL,
117        16,
118        TextJustify::Center,
119        String::from("0"),
120        make_points(16, 270),
121        make_size(50, 20),
122    );
123
124    text_widget3
125        .get_config()
126        .set_color(CONFIG_COLOR_TEXT, Color::RGB(0, 0, 0));
127
128    let mut slider4 = SliderWidget::new(
129        make_points(60, 80),
130        make_size(20, 170),
131        20,
132        80,
133        40,
134        SliderVertical,
135    );
136
137    slider4.on_value_changed(|_slider, _widgets, _layouts, pos| {
138        let text4_id = widget_id_for_name(_widgets, String::from("text4"));
139
140        cast!(_widgets, text4_id, TextWidget).set_text(format!("{}", pos));
141    });
142
143    let mut text_widget4 = TextWidget::new(
144        String::from("assets/OpenSans-Regular.ttf"),
145        sdl2::ttf::FontStyle::NORMAL,
146        16,
147        TextJustify::Center,
148        String::from("40"),
149        make_points(56, 270),
150        make_size(50, 20),
151    );
152
153    text_widget4
154        .get_config()
155        .set_color(CONFIG_COLOR_TEXT, Color::RGB(0, 0, 0));
156
157    engine.add_widget(Box::new(slider1), String::from("slider1"));
158    engine.add_widget(Box::new(text_widget1), String::from("text1"));
159    engine.add_widget(Box::new(slider2), String::from("slider2"));
160    engine.add_widget(Box::new(text_widget2), String::from("text2"));
161    engine.add_widget(Box::new(slider3), String::from("slider3"));
162    engine.add_widget(Box::new(text_widget3), String::from("text3"));
163    engine.add_widget(Box::new(slider4), String::from("slider4"));
164    engine.add_widget(Box::new(text_widget4), String::from("text4"));
165
166    engine.run(sdl_context, window);
167}

Trait Implementations§

Source§

impl CanvasHelper for SliderWidget

Source§

fn draw_point(&mut self, c: &mut Canvas<Window>, x: i32, y: i32)

Draws a point in the Canvas.
Source§

fn draw_bounding_box(&mut self, c: &mut Canvas<Window>)

Draws a box around the bounding area of the Widget.
Source§

fn get_rect_dest(&mut self) -> Rect

Returns a Rect destination object
Source§

impl Widget for SliderWidget

This is the Widget implementation of the SliderWidget.

Source§

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

Draws the SliderWidget contents.

Source§

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

When a mouse enters the bounds of the Widget, this function is triggered.

Source§

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

When a mouse exits the bounds of the Widget, this function is triggered.

Source§

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

When a mouse is moved in the bounds of this Widget, this function is triggered.

Source§

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

Handles the scrolling functionality.

Source§

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

Overrides the button_clicked callback to handle toggling.

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 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.