Skip to content

Commit

Permalink
SDL1 Unicode input support
Browse files Browse the repository at this point in the history
  • Loading branch information
AJenbo committed Sep 23, 2021
1 parent 7aaf58d commit 2887831
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
18 changes: 10 additions & 8 deletions Source/DiabloUI/diabloui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

#include <algorithm>
#include <string>
#ifdef USE_SDL1
#include <codecvt>
#include <locale>
#include <cassert>
#endif

#include "DiabloUI/art_draw.h"
#include "DiabloUI/button.h"
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<std::codecvt_utf8<char32_t>, char32_t> convert;
std::string utf8 = convert.to_bytes(unicode);
SelheroCatToName(utf8.c_str(), UiTextInput, UiTextInputLen);
}
#endif
break;
Expand Down
19 changes: 1 addition & 18 deletions Source/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1794,36 +1794,19 @@ void control_reset_talk()
force_redraw = 255;
}

bool control_talk_last_key(char vkey)
bool IsTalkActive()
{
if (!IsChatAvailable())
return false;

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);
}

Expand Down
2 changes: 1 addition & 1 deletion Source/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Source/diablo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
15 changes: 14 additions & 1 deletion Source/miniwin/misc_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#include <cstdint>
#include <deque>
#include <string>
#ifdef USE_SDL1
#include <codecvt>
#include <locale>
#include <cassert>
#endif

#include "control.h"
#include "controls/controller.h"
Expand Down Expand Up @@ -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<std::codecvt_utf8<char32_t>, 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);
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 2887831

Please sign in to comment.