From be8fb858cc810ff2125669e4e7e1254405f1ebc8 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Thu, 16 Jul 2015 05:15:03 +0200 Subject: [PATCH 1/2] Add ImGuiIO::AddInputCharactersUTF8(char* utf8str) It'll convert the utf8 string to ImWchar's and passes each of them to AddInputCharacter(). Very handy for SDL2 SDL_TEXTINPUT events, which provide a buffer with an UTF-8 string. --- imgui.cpp | 12 ++++++++++++ imgui.h | 1 + 2 files changed, 13 insertions(+) diff --git a/imgui.cpp b/imgui.cpp index 915c581ac..6083fa4d4 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -778,6 +778,18 @@ void ImGuiIO::AddInputCharacter(ImWchar c) } } +void ImGuiIO::AddInputCharactersUTF8(const char* utf8chars) +{ + // we can't pass more wchars than ImGuiIO::InputCharacters[] can hold so don't convert more + static const int wcharBufLen = sizeof(ImGuiIO::InputCharacters)/sizeof(ImWchar); + ImWchar wchars[wcharBufLen]; + ImTextStrFromUtf8(wchars, wcharBufLen, utf8chars, NULL); + for(int i=0; i Date: Thu, 16 Jul 2015 05:16:22 +0200 Subject: [PATCH 2/2] Use ImGuiIO::AddInputCharactersUTF8() in SDL2 example Now Unicode text input works in this example. --- examples/sdl_opengl_example/imgui_impl_sdl.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/sdl_opengl_example/imgui_impl_sdl.cpp b/examples/sdl_opengl_example/imgui_impl_sdl.cpp index 10260d2d9..980b11dcc 100644 --- a/examples/sdl_opengl_example/imgui_impl_sdl.cpp +++ b/examples/sdl_opengl_example/imgui_impl_sdl.cpp @@ -118,9 +118,7 @@ bool ImGui_ImplSdl_ProcessEvent(SDL_Event* event) case SDL_TEXTINPUT: { ImGuiIO& io = ImGui::GetIO(); - unsigned int c = event->text.text[0]; - if (c > 0 && c < 0x10000) - io.AddInputCharacter((unsigned short)c); + io.AddInputCharactersUTF8(event->text.text); return true; } case SDL_KEYDOWN: