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:
Joonas Javanainen 2015-10-18 12:34:00 +03:00
parent 20bf8cd7f0
commit 8ec34e0701

View File

@ -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() } }