From 1681ca77d89893cffbe7fe1f39128d9fbe7fb143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Thu, 1 Apr 2021 19:45:53 +0200 Subject: [PATCH] Add Lua support for composition --- src/gui/game/GameController.cpp | 6 ++++++ src/gui/game/GameController.h | 1 + src/gui/game/GameView.cpp | 6 ++++++ src/gui/game/GameView.h | 1 + src/gui/interface/Window.cpp | 3 ++- src/gui/interface/Window.h | 1 + src/lua/LuaEvents.cpp | 10 ++++++++++ src/lua/LuaEvents.h | 11 +++++++++++ src/lua/LuaScriptInterface.cpp | 31 ++++++++++++++++++++++++++++++- src/lua/LuaScriptInterface.h | 4 ++++ 10 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index 30bdc3d2c..91db38fd4 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -644,6 +644,12 @@ bool GameController::TextInput(String text) return commandInterface->HandleEvent(LuaEvents::textinput, &ev); } +bool GameController::TextEditing(String text) +{ + TextEditingEvent ev(text); + return commandInterface->HandleEvent(LuaEvents::textediting, &ev); +} + bool GameController::KeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) { KeyEvent ev(key, scan, repeat, shift, ctrl, alt); diff --git a/src/gui/game/GameController.h b/src/gui/game/GameController.h index 078650a04..05e66495d 100644 --- a/src/gui/game/GameController.h +++ b/src/gui/game/GameController.h @@ -68,6 +68,7 @@ public: bool MouseUp(int x, int y, unsigned button, char type); bool MouseWheel(int x, int y, int d); bool TextInput(String text); + bool TextEditing(String text); bool KeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt); bool KeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt); void Tick(); diff --git a/src/gui/game/GameView.cpp b/src/gui/game/GameView.cpp index 8e89276f8..2544bbb88 100644 --- a/src/gui/game/GameView.cpp +++ b/src/gui/game/GameView.cpp @@ -1703,6 +1703,12 @@ void GameView::DoTextInput(String text) Window::DoTextInput(text); } +void GameView::DoTextEditing(String text) +{ + if (c->TextEditing(text)) + Window::DoTextEditing(text); +} + void GameView::DoKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) { if (shift && !shiftBehaviour) diff --git a/src/gui/game/GameView.h b/src/gui/game/GameView.h index 27c3c1e56..c82215d21 100644 --- a/src/gui/game/GameView.h +++ b/src/gui/game/GameView.h @@ -213,6 +213,7 @@ public: void DoMouseUp(int x, int y, unsigned button) override; void DoMouseWheel(int x, int y, int d) override; void DoTextInput(String text) override; + void DoTextEditing(String text) override; void DoKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) override; void DoKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) override; diff --git a/src/gui/interface/Window.cpp b/src/gui/interface/Window.cpp index 7e53d791c..349fd98aa 100644 --- a/src/gui/interface/Window.cpp +++ b/src/gui/interface/Window.cpp @@ -13,6 +13,7 @@ Window::Window(Point _position, Point _size): Position(_position), Size(_size), AllowExclusiveDrawing(true), + DoesTextInput(false), okayButton(NULL), cancelButton(NULL), focusedComponent_(NULL), @@ -262,7 +263,7 @@ void Window::DoTick(float dt) return; #endif - if (focusedComponent_ && focusedComponent_->Visible && focusedComponent_->Enabled && focusedComponent_->DoesTextInput) + if (DoesTextInput || (focusedComponent_ && focusedComponent_->Visible && focusedComponent_->Enabled && focusedComponent_->DoesTextInput)) { ui::Engine::Ref().StartTextInput(); } diff --git a/src/gui/interface/Window.h b/src/gui/interface/Window.h index f4577dc2e..15f70cafc 100644 --- a/src/gui/interface/Window.h +++ b/src/gui/interface/Window.h @@ -35,6 +35,7 @@ namespace ui void SetCancelButton(ui::Button * button) { cancelButton = button; } bool AllowExclusiveDrawing; //false will not call draw on objects outside of bounds + bool DoesTextInput; // Add Component to window void AddComponent(Component* c); diff --git a/src/lua/LuaEvents.cpp b/src/lua/LuaEvents.cpp index 0d5955c21..2801abf05 100644 --- a/src/lua/LuaEvents.cpp +++ b/src/lua/LuaEvents.cpp @@ -37,6 +37,16 @@ int TextInputEvent::PushToStack(lua_State * l) return 1; } +TextEditingEvent::TextEditingEvent(String text): + text(text) +{} + +int TextEditingEvent::PushToStack(lua_State * l) +{ + PushString(l, text.ToUtf8()); + return 1; +} + KeyEvent::KeyEvent(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt): key(key), scan(scan), diff --git a/src/lua/LuaEvents.h b/src/lua/LuaEvents.h index 66152cdd5..9f31cfed6 100644 --- a/src/lua/LuaEvents.h +++ b/src/lua/LuaEvents.h @@ -29,6 +29,16 @@ public: int PushToStack(lua_State * l) override; }; +class TextEditingEvent : public Event +{ + String text; + +public: + TextEditingEvent(String text); + + int PushToStack(lua_State * l) override; +}; + class KeyEvent : public Event { int key; @@ -119,6 +129,7 @@ public: keypress, keyrelease, textinput, + textediting, mousedown, mouseup, mousemove, diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 040ee6bc4..a2c188aae 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -123,7 +123,8 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m): luacon_selectedreplace(""), luacon_mousedown(false), currentCommand(false), - legacy(new TPTScriptInterface(c, m)) + legacy(new TPTScriptInterface(c, m)), + textInputRefcount(0) { luacon_model = m; luacon_controller = c; @@ -500,6 +501,9 @@ void LuaScriptInterface::initInterfaceAPI() {"closeWindow", interface_closeWindow}, {"addComponent", interface_addComponent}, {"removeComponent", interface_removeComponent}, + {"grabTextInput", interface_grabTextInput}, + {"dropTextInput", interface_dropTextInput}, + {"textInputRect", interface_textInputRect}, {NULL, NULL} }; luaL_register(l, "interface", interfaceAPIMethods); @@ -582,6 +586,30 @@ int LuaScriptInterface::interface_removeComponent(lua_State * l) return 0; } +int LuaScriptInterface::interface_grabTextInput(lua_State * l) +{ + luacon_ci->textInputRefcount += 1; + luacon_controller->GetView()->DoesTextInput = luacon_ci->textInputRefcount > 0; + return 0; +} + +int LuaScriptInterface::interface_dropTextInput(lua_State * l) +{ + luacon_ci->textInputRefcount -= 1; + luacon_controller->GetView()->DoesTextInput = luacon_ci->textInputRefcount > 0; + return 0; +} + +int LuaScriptInterface::interface_textInputRect(lua_State * l) +{ + int x = luaL_checkint(l, 1); + int y = luaL_checkint(l, 2); + int w = luaL_checkint(l, 3); + int h = luaL_checkint(l, 4); + ui::Engine::Ref().TextInputRect(ui::Point{ x, y }, ui::Point{ w, h }); + return 0; +} + int LuaScriptInterface::interface_showWindow(lua_State * l) { LuaWindow * window = Luna::check(l, 1); @@ -3706,6 +3734,7 @@ void LuaScriptInterface::initEventAPI() lua_pushinteger(l, LuaEvents::keypress); lua_setfield(l, -2, "keypress"); lua_pushinteger(l, LuaEvents::keyrelease); lua_setfield(l, -2, "keyrelease"); lua_pushinteger(l, LuaEvents::textinput); lua_setfield(l, -2, "textinput"); + lua_pushinteger(l, LuaEvents::textediting); lua_setfield(l, -2, "textediting"); lua_pushinteger(l, LuaEvents::mousedown); lua_setfield(l, -2, "mousedown"); lua_pushinteger(l, LuaEvents::mouseup); lua_setfield(l, -2, "mouseup"); lua_pushinteger(l, LuaEvents::mousemove); lua_setfield(l, -2, "mousemove"); diff --git a/src/lua/LuaScriptInterface.h b/src/lua/LuaScriptInterface.h index b616b5d86..99f76421b 100644 --- a/src/lua/LuaScriptInterface.h +++ b/src/lua/LuaScriptInterface.h @@ -44,6 +44,7 @@ class LuaScriptInterface: public CommandInterface bool luacon_mousedown; bool currentCommand; TPTScriptInterface * legacy; + int textInputRefcount; // signs static int simulation_signIndex(lua_State *l); @@ -139,6 +140,9 @@ class LuaScriptInterface: public CommandInterface static int interface_closeWindow(lua_State * l); static int interface_addComponent(lua_State * l); static int interface_removeComponent(lua_State * l); + static int interface_grabTextInput(lua_State * l); + static int interface_dropTextInput(lua_State * l); + static int interface_textInputRect(lua_State * l); void initGraphicsAPI(); static int graphics_textSize(lua_State * l);