From 2887831d771288191f7a1c9baf37bb1311b258f3 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Thu, 23 Sep 2021 06:25:01 +0200 Subject: [PATCH] SDL1 Unicode input support --- Source/DiabloUI/diabloui.cpp | 18 ++++++++++-------- Source/control.cpp | 19 +------------------ Source/control.h | 2 +- Source/diablo.cpp | 2 +- Source/miniwin/misc_msg.cpp | 15 ++++++++++++++- 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/Source/DiabloUI/diabloui.cpp b/Source/DiabloUI/diabloui.cpp index d4bfef00828d..31fc886cd338 100644 --- a/Source/DiabloUI/diabloui.cpp +++ b/Source/DiabloUI/diabloui.cpp @@ -2,6 +2,11 @@ #include #include +#ifdef USE_SDL1 +#include +#include +#include +#endif #include "DiabloUI/art_draw.h" #include "DiabloUI/button.h" @@ -223,13 +228,13 @@ void UiFocusPageDown() } } -void SelheroCatToName(char *inBuf, char *outBuf, int cnt) +void SelheroCatToName(const char *inBuf, char *outBuf, int cnt) { strncat(outBuf, inBuf, cnt - strlen(outBuf)); } #ifdef __vita__ -void selhero_SetName(char *in_buf, char *out_buf, int cnt) +void selhero_SetName(const char *in_buf, char *out_buf, int cnt) { strncpy(out_buf, in_buf, cnt); } @@ -337,12 +342,9 @@ void UiFocusNavigation(SDL_Event *event) #ifdef USE_SDL1 if ((event->key.keysym.mod & KMOD_CTRL) == 0) { Uint16 unicode = event->key.keysym.unicode; - if (unicode && (unicode & 0xFF80) == 0) { - char utf8[SDL_TEXTINPUTEVENT_TEXT_SIZE]; - utf8[0] = (char)unicode; - utf8[1] = '\0'; - SelheroCatToName(utf8, UiTextInput, UiTextInputLen); - } + std::wstring_convert, char32_t> convert; + std::string utf8 = convert.to_bytes(unicode); + SelheroCatToName(utf8.c_str(), UiTextInput, UiTextInputLen); } #endif break; diff --git a/Source/control.cpp b/Source/control.cpp index 544f747ad243..445aebfc1cd9 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -1794,7 +1794,7 @@ void control_reset_talk() force_redraw = 255; } -bool control_talk_last_key(char vkey) +bool IsTalkActive() { if (!IsChatAvailable()) return false; @@ -1802,28 +1802,11 @@ bool control_talk_last_key(char vkey) if (!talkflag) return false; -#ifdef USE_SDL1 - if (vkey >= 0 && vkey < DVL_VK_SPACE) - return false; - - std::size_t result = strlen(TalkMessage); - if (result < 78) { - TalkMessage[result] = vkey; - TalkMessage[result + 1] = '\0'; - } -#endif - return true; } void control_new_text(string_view text) { - if (!IsChatAvailable()) - return; - - if (!talkflag) - return; - strncat(TalkMessage, text.data(), sizeof(TalkMessage) - strlen(TalkMessage) - 1); } diff --git a/Source/control.h b/Source/control.h index acc434bde4d3..2d62a7bfdd8c 100644 --- a/Source/control.h +++ b/Source/control.h @@ -176,7 +176,7 @@ bool control_check_talk_btn(); void control_release_talk_btn(); void control_type_message(); void control_reset_talk(); -bool control_talk_last_key(char vkey); +bool IsTalkActive(); void control_new_text(string_view text); bool control_presskeys(int vkey); void DiabloHotkeyMsg(uint32_t dwMsg); diff --git a/Source/diablo.cpp b/Source/diablo.cpp index 8d81ca6744f7..ed8e93127fdb 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -537,7 +537,7 @@ void PressKey(int vkey) */ void PressChar(char vkey) { - if (gmenu_is_active() || control_talk_last_key(vkey) || sgnTimeoutCurs != CURSOR_NONE || MyPlayerIsDead) { + if (gmenu_is_active() || IsTalkActive() || sgnTimeoutCurs != CURSOR_NONE || MyPlayerIsDead) { return; } if (vkey == 'p' || vkey == 'P') { diff --git a/Source/miniwin/misc_msg.cpp b/Source/miniwin/misc_msg.cpp index 0ce5bf4a5e54..30d0925ea5c3 100644 --- a/Source/miniwin/misc_msg.cpp +++ b/Source/miniwin/misc_msg.cpp @@ -2,6 +2,11 @@ #include #include #include +#ifdef USE_SDL1 +#include +#include +#include +#endif #include "control.h" #include "controls/controller.h" @@ -474,6 +479,14 @@ bool FetchMessage_Real(tagMSG *lpMsg) break; case SDL_KEYDOWN: case SDL_KEYUP: { +#ifdef USE_SDL1 + if (gbRunGame && IsTalkActive()) { + Uint16 unicode = e.key.keysym.unicode; + std::wstring_convert, char32_t> convert; + std::string utf8 = convert.to_bytes(unicode); + control_new_text(utf8); + } +#endif int key = TranslateSdlKey(e.key.keysym); if (key == -1) return FalseAvail(e.type == SDL_KEYDOWN ? "SDL_KEYDOWN" : "SDL_KEYUP", e.key.keysym.sym); @@ -537,7 +550,7 @@ bool FetchMessage_Real(tagMSG *lpMsg) break; return FalseAvail("SDL_TEXTEDITING", e.edit.length); case SDL_TEXTINPUT: - if (gbRunGame) { + if (gbRunGame && IsTalkActive()) { control_new_text(e.text.text); break; }