58 lines
1.2 KiB
Rust
58 lines
1.2 KiB
Rust
|
use raylib::prelude::*;
|
||
|
|
||
|
pub fn text_input(
|
||
|
d: &mut RaylibDrawHandle,
|
||
|
bounds: Rectangle,
|
||
|
text: &mut String,
|
||
|
is_selected: &mut bool,
|
||
|
) -> bool {
|
||
|
let mut changed = false;
|
||
|
let (bg, border) = if *is_selected {
|
||
|
(Color::DARKCYAN, Color::CYAN)
|
||
|
} else {
|
||
|
(Color::GRAY, Color::DIMGRAY)
|
||
|
};
|
||
|
d.draw_rectangle_rec(bounds, border);
|
||
|
d.draw_rectangle_rec(shrink_rec(bounds, 2.), bg);
|
||
|
d.draw_text(
|
||
|
text,
|
||
|
bounds.x as i32 + 4,
|
||
|
bounds.y as i32 + 4,
|
||
|
10,
|
||
|
Color::WHITE,
|
||
|
);
|
||
|
let mouse_pos = d.get_mouse_position();
|
||
|
if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT)
|
||
|
&& (bounds.check_collision_point_rec(mouse_pos) || *is_selected)
|
||
|
{
|
||
|
*is_selected = !*is_selected;
|
||
|
}
|
||
|
|
||
|
if *is_selected {
|
||
|
if d.is_key_pressed(KeyboardKey::KEY_BACKSPACE) {
|
||
|
changed = true;
|
||
|
text.pop();
|
||
|
}
|
||
|
let char_code = unsafe { ffi::GetCharPressed() };
|
||
|
let c = if char_code > 0 {
|
||
|
char::from_u32(char_code as u32)
|
||
|
} else {
|
||
|
None
|
||
|
};
|
||
|
if let Some(c) = c {
|
||
|
changed = true;
|
||
|
text.push(c);
|
||
|
}
|
||
|
}
|
||
|
changed
|
||
|
}
|
||
|
|
||
|
pub fn shrink_rec(rec: Rectangle, a: f32) -> Rectangle {
|
||
|
Rectangle {
|
||
|
x: rec.x + a,
|
||
|
y: rec.y + a,
|
||
|
width: rec.width - a * 2.,
|
||
|
height: rec.height - a * 2.,
|
||
|
}
|
||
|
}
|