From 2d961608e2ab7776ee41cbc984eadceb486e0ddf Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Tue, 24 Dec 2024 00:03:48 +0100 Subject: [PATCH] add blinking cursor in text input field --- src/ui.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index 8c36a9c..4c80f7d 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -227,6 +227,7 @@ pub fn text_input( editable: bool, ) -> bool { let mut changed = false; + let font_size = 20; d.draw_rectangle_rec(bounds, widget_bg(*is_selected)); d.draw_rectangle_rec( Rectangle::new( @@ -237,18 +238,24 @@ pub fn text_input( ), BG_DARK, ); - let drawn_text = if *is_selected { - &format!("{text}_") - } else { - text.as_str() - }; d.draw_text( - drawn_text, + text, bounds.x as i32 + 4, bounds.y as i32 + 4, - 20, + font_size, 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) { *is_selected = !*is_selected; }