add blinking cursor in text input field

This commit is contained in:
Crispy 2024-12-24 00:03:48 +01:00
parent 2ecb86d283
commit 2d961608e2

View file

@ -227,6 +227,7 @@ pub fn text_input(
editable: bool, editable: bool,
) -> bool { ) -> bool {
let mut changed = false; let mut changed = false;
let font_size = 20;
d.draw_rectangle_rec(bounds, widget_bg(*is_selected)); d.draw_rectangle_rec(bounds, widget_bg(*is_selected));
d.draw_rectangle_rec( d.draw_rectangle_rec(
Rectangle::new( Rectangle::new(
@ -237,18 +238,24 @@ pub fn text_input(
), ),
BG_DARK, BG_DARK,
); );
let drawn_text = if *is_selected {
&format!("{text}_")
} else {
text.as_str()
};
d.draw_text( d.draw_text(
drawn_text, text,
bounds.x as i32 + 4, bounds.x as i32 + 4,
bounds.y as i32 + 4, bounds.y as i32 + 4,
20, font_size,
Color::WHITE, Color::WHITE,
); );
// blinking cursor
if *is_selected && d.get_time().fract() < 0.5 {
let width = d.measure_text(text, font_size);
d.draw_rectangle(
bounds.x as i32 + 6 + width,
bounds.y as i32 + 4,
2,
font_size,
Color::WHITE,
);
};
if editable && mouse.left_click() && (mouse.is_over(bounds) || *is_selected) { if editable && mouse.left_click() && (mouse.is_over(bounds) || *is_selected) {
*is_selected = !*is_selected; *is_selected = !*is_selected;
} }