Add Lua support for composition

This commit is contained in:
Tamás Bálint Misius 2021-04-01 19:45:53 +02:00
parent 3dbf6d7810
commit 1681ca77d8
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
10 changed files with 72 additions and 2 deletions

View File

@ -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);

View File

@ -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();

View File

@ -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)

View File

@ -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;

View File

@ -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();
}

View File

@ -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);

View File

@ -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),

View File

@ -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,

View File

@ -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<LuaWindow>::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");

View File

@ -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);