Struct Button

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

A Button widget that can be clicked to perform an action.

Implementations§

Source§

impl Button

Source

pub fn new( width: f32, height: f32, text: String, bg: Color, fg: Color, font: Option<Font>, ) -> Self

Creates a new Button widget.

Examples found in repository?
examples/helloworld.rs (line 8)
5async fn main() { 
6    let poppins = load_ttf_font("examples/poppins.ttf").await.unwrap();
7    let label = Label::new("Hello, world!".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.5, 1.0, 1.0), Some(poppins.clone()), 36.0);
8    let mut button = Button::new(500.0, 80.0, "Clickity Clickity Click".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.75, 0.5, 1.0), Some(poppins.clone()));
9    let mut toggle = Toggle::new(150.0, 50.0, "Toggle Me".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(1.0, 1.0, 0.5, 1.0), Some(poppins.clone()));
10
11    loop {
12        clear_background(Color::new(0.05, 0.05, 0.1, 1.0));
13        
14        label.render(screen_width() / 2.0 - label.width() / 2.0, screen_height() / 2.0 - label.height() / 2.0 - 100.0);
15        button.update(screen_width() / 2.0 - button.width() / 2.0, screen_height() / 2.0 - button.height() / 2.0 + 100.0);
16        button.render(screen_width() / 2.0 - button.width() / 2.0, screen_height() / 2.0 - button.height() / 2.0 + 100.0);
17        toggle.update(screen_width() - toggle.width() - 10.0, 10.0);
18        toggle.render(screen_width() - toggle.width() - 10.0, 10.0);
19
20        if button.is_clicked() {
21            println!("Button clicked!");
22        }
23
24        next_frame().await;
25    }
26}
More examples
Hide additional examples
examples/cont.rs (line 17)
14async fn main() {
15    let poppins = load_ttf_font("examples/poppins.ttf").await.unwrap();
16    let label = Label::new("Inside a Container!".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.5, 1.0, 1.0), Some(poppins.clone()), 32.0);
17    let button = Button::new(400.0, 80.0, "eeeeeeee".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.75, 0.5, 1.0), Some(poppins.clone()));
18
19    let sublabel = Label::new("Inside a SUB Container!".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.5, 1.0, 1.0), Some(poppins.clone()), 32.0);
20    let subbutton = Toggle::new(300.0, 60.0, "hhhhhhhh".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(1.0, 1.0, 0.5, 1.0), Some(poppins.clone()));
21
22    let mut subcontainer = Container::new(
23        flowquad::widgets::container::Direction::Vertical,
24        flowquad::widgets::container::Align::Center,
25        20.0, Color::new(0.05, 0.05, 0.1, 1.0),
26        Some((20.0, 20.0, 20.0, 20.0)), Some((5.0, Color::new(1.0, 0.5, 0.5, 1.0)))
27    );
28    subcontainer.add_child(Box::new(sublabel));
29    subcontainer.add_child(Box::new(subbutton));
30
31    let mut container = Container::new(
32        flowquad::widgets::container::Direction::Horizontal,
33        flowquad::widgets::container::Align::Center,
34        20.0, Color::new(0.05, 0.05, 0.1, 1.0),
35        Some((20.0, 20.0, 20.0, 20.0)), Some((5.0, Color::new(1.0, 0.5, 0.5, 1.0)))
36    );
37
38    container.add_child(Box::new(label));
39    container.add_child(Box::new(button));
40    container.add_child(Box::new(subcontainer)); // This is completely legal, as Container implements Widget trait!
41
42    loop {
43        clear_background(Color::new(0.05, 0.05, 0.1, 1.0));
44
45        container.update(screen_width() / 2.0 - container.width() / 2.0, screen_height() / 2.0 - container.height() / 2.0);
46        container.render(screen_width() / 2.0 - container.width() / 2.0, screen_height() / 2.0 - container.height() / 2.0);
47
48        next_frame().await;
49    }
50}

Trait Implementations§

Source§

impl Action for Button

Source§

fn is_clicked(&self) -> bool

Returns if the widget is clicked.
Source§

fn is_hovered(&self) -> bool

Returns if the widget is hovered.
Source§

impl Widget for Button

Source§

fn as_any(&self) -> &dyn Any

Returns the type of the widget as an Any type.
Source§

fn width(&self) -> f32

Returns the width of the widget.
Source§

fn height(&self) -> f32

Returns the height of the widget.
Source§

fn bg(&self) -> Color

Returns the background color of the widget.
Source§

fn update(&mut self, x: f32, y: f32)

Updates the widget’s state based on its position.
Source§

fn render(&self, x: f32, y: f32)

Renders the widget at the specified position.

Auto Trait Implementations§

§

impl Freeze for Button

§

impl RefUnwindSafe for Button

§

impl Send for Button

§

impl Sync for Button

§

impl Unpin for Button

§

impl UnwindSafe for Button

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.