From 487c2f76a9879fbcef1dd8484c4016c6a0ae76f4 Mon Sep 17 00:00:00 2001 From: Sergii Pylypenko Date: Wed, 7 Jul 2021 23:46:07 +0300 Subject: [PATCH] Emscripten: console text input --- os/emscripten/shell.html | 8 ++++++-- src/console_gui.cpp | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/os/emscripten/shell.html b/os/emscripten/shell.html index c59dbe0e98..d6c96a6c5e 100644 --- a/os/emscripten/shell.html +++ b/os/emscripten/shell.html @@ -89,12 +89,12 @@ } #textinputbox { - width: 95%; + width: 100%; font-size: xx-large; } #textinputbutton { - width: 95%; + width: 100%; } canvas.emscripten { @@ -226,6 +226,10 @@ textInputValue: null, startTextInput: function(text) { + if (document.getElementById("textinput").style.display != "none") { + //Module.print("Text input already started"); + return; + } //Module.print("Start text input!"); Module.textInputValue = null; document.getElementById("textinput").style.display = "block"; diff --git a/src/console_gui.cpp b/src/console_gui.cpp index 0dab2279df..34873e9ddc 100644 --- a/src/console_gui.cpp +++ b/src/console_gui.cpp @@ -32,6 +32,9 @@ #ifdef __ANDROID__ #include #endif +#ifdef __EMSCRIPTEN__ +#include +#endif static const uint ICON_HISTORY_SIZE = 20; static const uint ICON_LINE_SPACING = 2; @@ -189,6 +192,13 @@ struct IConsoleWindow : Window this->truncate_timer.SetInterval(3000); ResizeWindow(this, _screen.width - GetMinButtonSize() * 2, _screen.height / 3); this->left = GetMinButtonSize(); + +#ifdef __EMSCRIPTEN__ + // TODO: move this code to SDL2 Emscripten backend + EM_ASM({ + Module.startTextInput(UTF8ToString($0)); + }, ""); +#endif // __EMSCRIPTEN__ } ~IConsoleWindow() @@ -234,6 +244,23 @@ struct IConsoleWindow : Window if (_focused_window == this && _iconsole_cmdline.caret) { DrawString(this->line_offset + delta + _iconsole_cmdline.caretxoffs, right, this->height - this->line_height, "_", TC_WHITE, SA_LEFT | SA_FORCE); } + +#ifdef __EMSCRIPTEN__ + // TODO: move this code to SDL2 Emscripten backend + 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->OnQueryTextFinished(buf); + } +#endif } void OnQueryTextFinished(char *str) override @@ -262,6 +289,16 @@ struct IConsoleWindow : Window if (_iconsole_cmdline.HandleCaret()) this->SetDirty(); } + void OnClick(Point pt, int widget, int click_count) override + { +#ifdef __EMSCRIPTEN__ + // TODO: move this code to SDL2 Emscripten backend + EM_ASM({ + Module.startTextInput(UTF8ToString($0)); + }, ""); +#endif // __EMSCRIPTEN__ + } + EventState OnKeyPress(WChar key, uint16 keycode) override { if (_focused_window != this) return ES_NOT_HANDLED;