Struct TextInput

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

The TextInput widget that allows the user to enter text.

Implementations§

Source§

impl TextInput

Source

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

Creates a new TextInput widget.

Examples found in repository?
examples/textinp.rs (line 8)
5async fn main() {
6    let poppins = load_ttf_font("examples/poppins.ttf").await.unwrap();
7    let label = Label::new("Text Inputs!".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()), 48.0);
8    let textinp = TextInput::new(512.0, 64.0, Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.5, 1.0, 1.0), Some(poppins.clone()));
9    let textinp2 = TextInput::new(512.0, 64.0, Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.5, 1.0, 1.0), Some(poppins.clone()));
10    let mut container = Container::new(Direction::Vertical, Align::Center, 20.0, Color::new(0.05, 0.05, 0.1, 1.0), None, None);
11    container.add_child(Box::new(label));
12    container.add_child(Box::new(textinp));
13    container.add_child(Box::new(textinp2));
14
15    let mut previous_text = String::new();
16    let mut previous_text2 = String::new();
17
18    loop {
19        clear_background(Color::new(0.05, 0.05, 0.1, 1.0));
20        
21        container.update(screen_width() / 2.0 - container.width() / 2.0, screen_height() / 2.0 - container.height() / 2.0);
22        container.render(screen_width() / 2.0 - container.width() / 2.0, screen_height() / 2.0 - container.height() / 2.0);
23        // println!("Text: {}", container.get_child_as::<TextInput>(1).unwrap().get_text());
24        // println!("Text2: {}", container.get_child_as::<TextInput>(2).unwrap().get_text());
25        let textinp = container.get_child_as::<TextInput>(1).unwrap();
26        let textinp2 = container.get_child_as::<TextInput>(2).unwrap();
27        if textinp.get_text() != previous_text {
28            println!("Text: {}", textinp.get_text());
29            previous_text = textinp.get_text();
30        }
31        if textinp2.get_text() != previous_text2 {
32            println!("Text2: {}", textinp2.get_text());
33            previous_text2 = textinp2.get_text();
34        }
35
36        next_frame().await;
37    }
38}
Source

pub fn get_text(&self) -> String

Returns the text entered in the TextInput widget.

Examples found in repository?
examples/textinp.rs (line 27)
5async fn main() {
6    let poppins = load_ttf_font("examples/poppins.ttf").await.unwrap();
7    let label = Label::new("Text Inputs!".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()), 48.0);
8    let textinp = TextInput::new(512.0, 64.0, Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.5, 1.0, 1.0), Some(poppins.clone()));
9    let textinp2 = TextInput::new(512.0, 64.0, Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.5, 1.0, 1.0), Some(poppins.clone()));
10    let mut container = Container::new(Direction::Vertical, Align::Center, 20.0, Color::new(0.05, 0.05, 0.1, 1.0), None, None);
11    container.add_child(Box::new(label));
12    container.add_child(Box::new(textinp));
13    container.add_child(Box::new(textinp2));
14
15    let mut previous_text = String::new();
16    let mut previous_text2 = String::new();
17
18    loop {
19        clear_background(Color::new(0.05, 0.05, 0.1, 1.0));
20        
21        container.update(screen_width() / 2.0 - container.width() / 2.0, screen_height() / 2.0 - container.height() / 2.0);
22        container.render(screen_width() / 2.0 - container.width() / 2.0, screen_height() / 2.0 - container.height() / 2.0);
23        // println!("Text: {}", container.get_child_as::<TextInput>(1).unwrap().get_text());
24        // println!("Text2: {}", container.get_child_as::<TextInput>(2).unwrap().get_text());
25        let textinp = container.get_child_as::<TextInput>(1).unwrap();
26        let textinp2 = container.get_child_as::<TextInput>(2).unwrap();
27        if textinp.get_text() != previous_text {
28            println!("Text: {}", textinp.get_text());
29            previous_text = textinp.get_text();
30        }
31        if textinp2.get_text() != previous_text2 {
32            println!("Text2: {}", textinp2.get_text());
33            previous_text2 = textinp2.get_text();
34        }
35
36        next_frame().await;
37    }
38}

Trait Implementations§

Source§

impl Action for TextInput

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 TextInput

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§

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.