Emscripten: text input with HTML <input> element

This commit is contained in:
Sergii Pylypenko
2021-06-24 01:00:17 +03:00
parent 4eed59213e
commit 8ffb519ec6
2 changed files with 70 additions and 2 deletions

View File

@@ -38,6 +38,9 @@
#ifdef __ANDROID__
#include <SDL_screenkeyboard.h>
#endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
/** Method to open the OSK. */
enum OskActivation {
@@ -47,8 +50,9 @@ enum OskActivation {
OSKA_IMMEDIATELY, ///< Focusing click already opens OSK.
};
#ifdef __ANDROID__
static char _android_text_input[512];
#endif
static const NWidgetPart _nested_land_info_widgets[] = {
NWidget(NWID_HORIZONTAL),
@@ -832,6 +836,25 @@ void QueryString::HandleEditBox(Window *w, int wid)
}
}
#endif
#ifdef __EMSCRIPTEN__
// TODO: move this code to SDL2 Emscripten backend
if (w->IsWidgetFocused(wid)) {
char buf[512] = "";
int status = EM_ASM_INT({
var value = Module.getTextInput();
if (value == null) {
return 0;
}
var lengthBytes = Math.min(512, lengthBytesUTF8(value) + 1);
stringToUTF8(value, $0, lengthBytes);
return 1;
}, buf);
if (status) {
this->text.Assign(buf);
w->OnEditboxChanged(wid);
}
}
#endif
}
void QueryString::DrawEditBox(const Window *w, int wid) const
@@ -1020,6 +1043,13 @@ void QueryString::ClickEditBox(Window *w, Point pt, int wid, int click_count, bo
this->text.DeleteAll();
SDL_ANDROID_GetScreenKeyboardTextInputAsync(_android_text_input, sizeof(_android_text_input));
#endif
#ifdef __EMSCRIPTEN__
// TODO: move this code to SDL2 Emscripten backend
EM_ASM({
Module.startTextInput(UTF8ToString($0));
}, this->text.buf);
this->text.DeleteAll();
#endif
}
/** Class for the string query window. */