Browse Source

Implement backspace for UTF-8 strings

pull/2933/head
Anders Jenbo 5 years ago
parent
commit
70cff81547
  1. 6
      Source/DiabloUI/diabloui.cpp
  2. 5
      Source/control.cpp
  3. 18
      Source/utils/utf8.h

6
Source/DiabloUI/diabloui.cpp

@ -27,6 +27,7 @@
#include "utils/sdl_wrap.h"
#include "utils/stubs.h"
#include "utils/language.h"
#include "utils/utf8.h"
#ifdef __SWITCH__
// for virtual keyboard on Switch
@ -330,10 +331,7 @@ void UiFocusNavigation(SDL_Event *event)
#endif
case SDLK_BACKSPACE:
case SDLK_LEFT: {
int nameLen = strlen(UiTextInput);
if (nameLen > 0) {
UiTextInput[nameLen - 1] = '\0';
}
UiTextInput[FindLastUtf8Symbols(UiTextInput)] = '\0';
return;
}
default:

5
Source/control.cpp

@ -38,6 +38,7 @@
#include "utils/language.h"
#include "utils/sdl_geometry.h"
#include "utils/stdcompat/optional.hpp"
#include "utils/utf8.h"
#include "options.h"
#ifdef _DEBUG
@ -1802,9 +1803,7 @@ bool control_presskeys(int vkey)
} else if (vkey == DVL_VK_RETURN) {
ControlPressEnter();
} else if (vkey == DVL_VK_BACK) {
std::size_t len = strlen(TalkMessage);
if (len > 0)
TalkMessage[len - 1] = '\0';
TalkMessage[FindLastUtf8Symbols(TalkMessage)] = '\0';
} else if (vkey == DVL_VK_DOWN) {
ControlUpDown(1);
} else if (vkey == DVL_VK_UP) {

18
Source/utils/utf8.h

@ -65,3 +65,21 @@ inline const char *utf8_decode(const char *buf, uint32_t *c, int *e)
return reinterpret_cast<const char *>(next);
}
inline int FindLastUtf8Symbols(const char *text)
{
std::string textBuffer(text);
textBuffer.resize(textBuffer.size() + 4); // Buffer must be padded before calling utf8_decode()
const char *textData = textBuffer.data();
const char *previousPosition = textData;
uint32_t next;
int error;
for (; *textData != '\0'; previousPosition = textData) {
textData = utf8_decode(textData, &next, &error);
if (*textData == '\0')
return previousPosition - textBuffer.data();
}
return 0;
}

Loading…
Cancel
Save