From 8ffb519ec6d1f154ae0e9c9630b076159b4b4a95 Mon Sep 17 00:00:00 2001 From: Sergii Pylypenko Date: Thu, 24 Jun 2021 01:00:17 +0300 Subject: [PATCH] Emscripten: text input with HTML element --- os/emscripten/shell.html | 40 +++++++++++++++++++++++++++++++++++++++- src/misc_gui.cpp | 32 +++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/os/emscripten/shell.html b/os/emscripten/shell.html index 838e350a3e..fc1fc19774 100644 --- a/os/emscripten/shell.html +++ b/os/emscripten/shell.html @@ -81,6 +81,13 @@ padding: 4px 4px; } + div.textinput { + position: absolute; + width: 100%; + z-index: 4; + display: "none"; + } + canvas.emscripten { border: 0px none; height: 100%; @@ -108,6 +115,10 @@ Warning: savegames are stored in the Indexed DB of your browser.
Your browser can delete savegames without notice! +
+ + +
@@ -200,7 +211,34 @@ document.getElementById("filesystem").style.display = "none"; }, 300); }, 10000); - } + }, + + textInputValue: null, + + startTextInput: function(text) { + Module.print("Start text input!"); + Module.textInputValue = null; + document.getElementById("textinput").style.display = "block"; + document.getElementById("textinputbox").value = text; + document.getElementById("textinputbox").focus(); + }, + + getTextInput: function() { + if (Module.textInputValue !== null) { + var value = Module.textInputValue; + Module.textInputValue = null; + return value; + } + return null; + }, + + finishTextInput: function() { + Module.print("Text input finished!"); + Module.textInputValue = document.getElementById("textinputbox").value; + Module.print(Module.textInputValue); + document.getElementById("textinput").style.display = "none"; + }, + }; window.onerror = function() { diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index c04af22c60..c4156f3cef 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -38,6 +38,9 @@ #ifdef __ANDROID__ #include #endif +#ifdef __EMSCRIPTEN__ +#include +#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. */