mirror of
https://github.com/eliasstepanik/imgui-rs.git
synced 2026-01-12 05:58:35 +00:00
Fix input character passing
escape_default is actually not the right function here, because it is meant for escaping strings with C-like rules. For example, character " becomes the string \" Since Strings are UTF-8, we can just directly allocate a String as an intermediate buffer.
This commit is contained in:
parent
20bf8cd7f0
commit
8ec34e0701
19
src/lib.rs
19
src/lib.rs
@ -231,17 +231,14 @@ impl ImGui {
|
||||
io.key_map[key as usize] = mapping as i32;
|
||||
}
|
||||
pub fn add_input_character(&mut self, character: char) {
|
||||
if !character.is_control() {
|
||||
// TODO: This is slightly better. We should use char::encode_utf8 when it stabilizes
|
||||
// to allow us to skip the string intermediate since we can then go directly
|
||||
// to bytes
|
||||
let utf8_str: String = character.escape_default().collect();
|
||||
let mut bytes = utf8_str.into_bytes();
|
||||
// into_bytes does not produce a c-string, we must append the null terminator
|
||||
bytes.push(0);
|
||||
unsafe {
|
||||
imgui_sys::ImGuiIO_AddInputCharactersUTF8(bytes.as_ptr() as *const i8);
|
||||
}
|
||||
// TODO: This is slightly better. We should use char::encode_utf8 when it stabilizes
|
||||
// to allow us to skip the string intermediate since we can then go directly
|
||||
// to bytes
|
||||
let mut string = String::new();
|
||||
string.push(character);
|
||||
string.push('\0');
|
||||
unsafe {
|
||||
imgui_sys::ImGuiIO_AddInputCharactersUTF8(string.as_ptr() as *const i8);
|
||||
}
|
||||
}
|
||||
pub fn get_time(&self) -> f32 { unsafe { imgui_sys::igGetTime() } }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user