Struct ToggleButtonWidget

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

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

Implementations§

Source§

impl ToggleButtonWidget

This is the implementation of the ToggleButtonWidget that draws a button on the screen that can be toggled on or off.

Source

pub fn new( points: Points, size: Size, text: String, font_size: i32, selected: bool, ) -> Self

Creates a new ToggleButtonWidget given the x, y, w, h coordinates, the text to display inside the button, font_size of the font to display, and the initial selected state: true being selected, false otherwise.

Examples found in repository?
examples/toggle_button.rs (lines 26-32)
16pub fn main() {
17    let sdl_context = sdl2::init().unwrap();
18    let video_subsystem = sdl_context.video().unwrap();
19    let window = video_subsystem
20        .window("pushrod-render toggle button demo", 400, 100)
21        .position_centered()
22        .opengl()
23        .build()
24        .unwrap();
25    let mut engine = Engine::new(400, 100, 60);
26    let mut button1 = ToggleButtonWidget::new(
27        make_points(20, 20),
28        make_size(170, 60),
29        String::from("1"),
30        40,
31        false,
32    );
33
34    button1.set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 0, 0));
35    button1.set_numeric(CONFIG_BORDER_WIDTH, 2);
36    button1.on_toggle(|_, _widgets, _layouts, _state| {
37        eprintln!("1 Toggled: {}", _state);
38    });
39
40    let mut button2 = ToggleButtonWidget::new(
41        make_points(210, 20),
42        make_size(170, 60),
43        String::from("2"),
44        40,
45        true,
46    );
47
48    button2.set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 0, 0));
49    button2.set_numeric(CONFIG_BORDER_WIDTH, 2);
50    button2.on_toggle(|_, _widgets, _layouts, _state| {
51        eprintln!("2 Toggled: {}", _state);
52    });
53
54    engine.add_widget(Box::new(button1), String::from("button1"));
55    engine.add_widget(Box::new(button2), String::from("button2"));
56
57    engine.run(sdl_context, window);
58}
Source

pub fn set_selected(&mut self, selected: bool)

Sets the selected state of this Widget.

Source

pub fn is_selected(&self) -> bool

Returns the selected state of this Widget: true indicates selected, false otherwise.

Source

pub fn on_toggle<F>(&mut self, callback: F)
where F: FnMut(&mut ToggleButtonWidget, &[WidgetContainer], &[LayoutContainer], bool) + 'static,

Assigns the callback closure that will be used when the Widget toggles state.

Examples found in repository?
examples/toggle_button.rs (lines 36-38)
16pub fn main() {
17    let sdl_context = sdl2::init().unwrap();
18    let video_subsystem = sdl_context.video().unwrap();
19    let window = video_subsystem
20        .window("pushrod-render toggle button demo", 400, 100)
21        .position_centered()
22        .opengl()
23        .build()
24        .unwrap();
25    let mut engine = Engine::new(400, 100, 60);
26    let mut button1 = ToggleButtonWidget::new(
27        make_points(20, 20),
28        make_size(170, 60),
29        String::from("1"),
30        40,
31        false,
32    );
33
34    button1.set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 0, 0));
35    button1.set_numeric(CONFIG_BORDER_WIDTH, 2);
36    button1.on_toggle(|_, _widgets, _layouts, _state| {
37        eprintln!("1 Toggled: {}", _state);
38    });
39
40    let mut button2 = ToggleButtonWidget::new(
41        make_points(210, 20),
42        make_size(170, 60),
43        String::from("2"),
44        40,
45        true,
46    );
47
48    button2.set_color(CONFIG_COLOR_BORDER, Color::RGB(0, 0, 0));
49    button2.set_numeric(CONFIG_BORDER_WIDTH, 2);
50    button2.on_toggle(|_, _widgets, _layouts, _state| {
51        eprintln!("2 Toggled: {}", _state);
52    });
53
54    engine.add_widget(Box::new(button1), String::from("button1"));
55    engine.add_widget(Box::new(button2), String::from("button2"));
56
57    engine.run(sdl_context, window);
58}

Trait Implementations§

Source§

impl Widget for ToggleButtonWidget

This is the Widget implementation of the ToggleButtonWidget.

Source§

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

Draws the ToggleButtonWidget 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 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 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 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.