From 2158faadabdc12a7991aec809f8cafb8950d52e1 Mon Sep 17 00:00:00 2001 From: obligaron Date: Tue, 26 Oct 2021 20:11:37 +0200 Subject: [PATCH] Use SDL_StartTextInput for gold drop dialog --- Source/control.cpp | 32 ++++++++++++++++++-------------- Source/control.h | 1 + Source/inv.cpp | 11 ++++++++--- Source/miniwin/misc_msg.cpp | 11 +++++++++-- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/Source/control.cpp b/Source/control.cpp index dde44c4bf..3a9f09ad1 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -1684,9 +1684,6 @@ void control_drop_gold(char vkey) return; } - char input[6]; - memset(input, 0, sizeof(input)); - snprintf(input, sizeof(input), "%i", dropGoldValue); if (vkey == DVL_VK_RETURN) { if (dropGoldValue > 0) RemoveGold(myPlayer, initialDropGoldIndex); @@ -1695,17 +1692,7 @@ void control_drop_gold(char vkey) CloseGoldDrop(); dropGoldValue = 0; } else if (vkey == DVL_VK_BACK) { - input[strlen(input) - 1] = '\0'; - dropGoldValue = atoi(input); - } else if (vkey - '0' >= 0 && vkey - '0' <= 9) { - if (dropGoldValue != 0 || atoi(input) <= initialDropGoldValue) { - input[strlen(input)] = vkey; - if (atoi(input) > initialDropGoldValue) - return; - } else { - input[0] = vkey; - } - dropGoldValue = atoi(input); + dropGoldValue = dropGoldValue / 10; } } @@ -1895,7 +1882,24 @@ void DiabloHotkeyMsg(uint32_t dwMsg) void CloseGoldDrop() { + if (!dropGoldFlag) + return; dropGoldFlag = false; + SDL_StopTextInput(); +} + +void GoldDropNewText(string_view text) +{ + for (char vkey : text) { + int digit = vkey - '0'; + if (digit >= 0 && digit <= 9) { + int newGoldValue = dropGoldValue * 10; + newGoldValue += digit; + if (newGoldValue <= initialDropGoldValue) { + dropGoldValue = newGoldValue; + } + } + } } } // namespace devilution diff --git a/Source/control.h b/Source/control.h index cadb2ead6..bbf9b84d4 100644 --- a/Source/control.h +++ b/Source/control.h @@ -181,6 +181,7 @@ void control_new_text(string_view text); bool control_presskeys(int vkey); void DiabloHotkeyMsg(uint32_t dwMsg); void CloseGoldDrop(); +void GoldDropNewText(string_view text); extern Rectangle ChrBtnsRect[4]; } // namespace devilution diff --git a/Source/inv.cpp b/Source/inv.cpp index 0f8837c5c..618525ae5 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -1166,11 +1166,16 @@ void StartGoldDrop() else initialDropGoldValue = myPlayer.SpdList[pcursinvitem - INVITEM_BELT_FIRST]._ivalue; - dropGoldFlag = true; - dropGoldValue = 0; - if (talkflag) control_reset_talk(); + + Point start = GetPanelPosition(UiPanels::Inventory, { 67, 128 }); + SDL_Rect rect = { start.x, start.y, 180, 20 }; + SDL_SetTextInputRect(&rect); + + dropGoldFlag = true; + dropGoldValue = 0; + SDL_StartTextInput(); } } // namespace diff --git a/Source/miniwin/misc_msg.cpp b/Source/miniwin/misc_msg.cpp index 6518513e6..00b3f6b9d 100644 --- a/Source/miniwin/misc_msg.cpp +++ b/Source/miniwin/misc_msg.cpp @@ -481,12 +481,15 @@ bool FetchMessage_Real(tagMSG *lpMsg) case SDL_KEYDOWN: case SDL_KEYUP: { #ifdef USE_SDL1 - if (gbRunGame && IsTalkActive()) { + if (gbRunGame && (IsTalkActive() || dropGoldFlag)) { Uint16 unicode = e.key.keysym.unicode; if (unicode >= ' ') { std::wstring_convert, char32_t> convert; std::string utf8 = convert.to_bytes(unicode); - control_new_text(utf8); + if (IsTalkActive()) + control_new_text(utf8); + if (dropGoldFlag) + GoldDropNewText(utf8); } } #endif @@ -559,6 +562,10 @@ bool FetchMessage_Real(tagMSG *lpMsg) control_new_text(e.text.text); break; } + if (gbRunGame && dropGoldFlag) { + GoldDropNewText(e.text.text); + break; + } return FalseAvail("SDL_TEXTINPUT", e.text.windowID); case SDL_WINDOWEVENT: switch (e.window.event) {