mirror of
https://github.com/eliasstepanik/imgui-rs.git
synced 2026-01-25 12:28: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;
|
io.key_map[key as usize] = mapping as i32;
|
||||||
}
|
}
|
||||||
pub fn add_input_character(&mut self, character: char) {
|
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
|
||||||
// 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 allow us to skip the string intermediate since we can then go directly
|
// to bytes
|
||||||
// to bytes
|
let mut string = String::new();
|
||||||
let utf8_str: String = character.escape_default().collect();
|
string.push(character);
|
||||||
let mut bytes = utf8_str.into_bytes();
|
string.push('\0');
|
||||||
// into_bytes does not produce a c-string, we must append the null terminator
|
unsafe {
|
||||||
bytes.push(0);
|
imgui_sys::ImGuiIO_AddInputCharactersUTF8(string.as_ptr() as *const i8);
|
||||||
unsafe {
|
|
||||||
imgui_sys::ImGuiIO_AddInputCharactersUTF8(bytes.as_ptr() as *const i8);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn get_time(&self) -> f32 { unsafe { imgui_sys::igGetTime() } }
|
pub fn get_time(&self) -> f32 { unsafe { imgui_sys::igGetTime() } }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user