Emscripten: console text input

This commit is contained in:
Sergii Pylypenko
2021-07-07 23:46:07 +03:00
parent a3b87d43a3
commit 487c2f76a9
2 changed files with 43 additions and 2 deletions

View File

@@ -89,12 +89,12 @@
} }
#textinputbox { #textinputbox {
width: 95%; width: 100%;
font-size: xx-large; font-size: xx-large;
} }
#textinputbutton { #textinputbutton {
width: 95%; width: 100%;
} }
canvas.emscripten { canvas.emscripten {
@@ -226,6 +226,10 @@
textInputValue: null, textInputValue: null,
startTextInput: function(text) { startTextInput: function(text) {
if (document.getElementById("textinput").style.display != "none") {
//Module.print("Text input already started");
return;
}
//Module.print("Start text input!"); //Module.print("Start text input!");
Module.textInputValue = null; Module.textInputValue = null;
document.getElementById("textinput").style.display = "block"; document.getElementById("textinput").style.display = "block";

View File

@@ -32,6 +32,9 @@
#ifdef __ANDROID__ #ifdef __ANDROID__
#include <SDL_screenkeyboard.h> #include <SDL_screenkeyboard.h>
#endif #endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
static const uint ICON_HISTORY_SIZE = 20; static const uint ICON_HISTORY_SIZE = 20;
static const uint ICON_LINE_SPACING = 2; static const uint ICON_LINE_SPACING = 2;
@@ -189,6 +192,13 @@ struct IConsoleWindow : Window
this->truncate_timer.SetInterval(3000); this->truncate_timer.SetInterval(3000);
ResizeWindow(this, _screen.width - GetMinButtonSize() * 2, _screen.height / 3); ResizeWindow(this, _screen.width - GetMinButtonSize() * 2, _screen.height / 3);
this->left = GetMinButtonSize(); this->left = GetMinButtonSize();
#ifdef __EMSCRIPTEN__
// TODO: move this code to SDL2 Emscripten backend
EM_ASM({
Module.startTextInput(UTF8ToString($0));
}, "");
#endif // __EMSCRIPTEN__
} }
~IConsoleWindow() ~IConsoleWindow()
@@ -234,6 +244,23 @@ struct IConsoleWindow : Window
if (_focused_window == this && _iconsole_cmdline.caret) { 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); 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 void OnQueryTextFinished(char *str) override
@@ -262,6 +289,16 @@ struct IConsoleWindow : Window
if (_iconsole_cmdline.HandleCaret()) this->SetDirty(); 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 EventState OnKeyPress(WChar key, uint16 keycode) override
{ {
if (_focused_window != this) return ES_NOT_HANDLED; if (_focused_window != this) return ES_NOT_HANDLED;