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.
This commit is contained in:
Daniel Gibson 2015-07-16 05:15:03 +02:00
parent b4ac413868
commit be8fb858cc
2 changed files with 13 additions and 0 deletions

View File

@ -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<wcharBufLen && wchars[i] != 0; ++i)
{
AddInputCharacter(wchars[i]);
}
}
// Math bits // Math bits
// We are keeping those static in the .cpp file so as not to leak them outside, in the case the user has implicit cast operators between ImVec2 and its own types. // We are keeping those static in the .cpp file so as not to leak them outside, in the case the user has implicit cast operators between ImVec2 and its own types.
static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x*rhs, lhs.y*rhs); } static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x*rhs, lhs.y*rhs); }

View File

@ -705,6 +705,7 @@ struct ImGuiIO
// Function // Function
IMGUI_API void AddInputCharacter(ImWchar c); // Helper to add a new character into InputCharacters[] IMGUI_API void AddInputCharacter(ImWchar c); // Helper to add a new character into InputCharacters[]
IMGUI_API void AddInputCharactersUTF8(const char* utf8chars); // Helper to add new characters into InputCharacters[] from an utf8-string
//------------------------------------------------------------------ //------------------------------------------------------------------
// Output - Retrieve after calling NewFrame(), you can use them to discard inputs or hide them from the rest of your application // Output - Retrieve after calling NewFrame(), you can use them to discard inputs or hide them from the rest of your application