diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 4a97a0502..328d8ba4f 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -158,6 +158,50 @@ void Lua::GetVector(lua_State *state, std::vector &pvec) } } +void Lua::PushInterfaceKeys(lua_State *L, + const std::set &keys) { + lua_createtable(L, 0, keys.size() + 5); + + for (auto &key : keys) + { + if (auto name = enum_item_raw_key(key)) + lua_pushstring(L, name); + else + lua_pushinteger(L, key); + + lua_pushboolean(L, true); + lua_rawset(L, -3); + + int charval = Screen::keyToChar(key); + if (charval >= 0) + { + lua_pushinteger(L, charval); + lua_setfield(L, -2, "_STRING"); + } + } + + if (df::global::enabler) { + if (df::global::enabler->mouse_lbut_down) { + lua_pushboolean(L, true); + lua_setfield(L, -2, "_MOUSE_L"); + } + if (df::global::enabler->mouse_rbut_down) { + lua_pushboolean(L, true); + lua_setfield(L, -2, "_MOUSE_R"); + } + if (df::global::enabler->mouse_lbut) { + lua_pushboolean(L, true); + lua_setfield(L, -2, "_MOUSE_L_DOWN"); + df::global::enabler->mouse_lbut = 0; + } + if (df::global::enabler->mouse_rbut) { + lua_pushboolean(L, true); + lua_setfield(L, -2, "_MOUSE_R_DOWN"); + df::global::enabler->mouse_rbut = 0; + } + } +} + int Lua::PushPosXYZ(lua_State *state, df::coord pos) { if (!pos.isValid()) diff --git a/library/include/LuaTools.h b/library/include/LuaTools.h index 6dc5ae0bd..1b3c5d595 100644 --- a/library/include/LuaTools.h +++ b/library/include/LuaTools.h @@ -30,6 +30,8 @@ distribution. #include #include +#include "df/interfacest.h" + #include "ColorText.h" #include "DataDefs.h" @@ -321,6 +323,8 @@ namespace DFHack {namespace Lua { Push(L, val); lua_setfield(L, idx, name); } + DFHACK_EXPORT void PushInterfaceKeys(lua_State *L, const std::set &keys); + template void PushVector(lua_State *state, const T &pvec, bool addn = false) { diff --git a/library/modules/Screen.cpp b/library/modules/Screen.cpp index bff14a380..ebcc3f229 100644 --- a/library/modules/Screen.cpp +++ b/library/modules/Screen.cpp @@ -749,50 +749,7 @@ int dfhack_lua_viewscreen::do_input(lua_State *L) } lua_pushvalue(L, -2); - - lua_createtable(L, 0, keys->size()+3); - - for (auto it = keys->begin(); it != keys->end(); ++it) - { - auto key = *it; - - if (auto name = enum_item_raw_key(key)) - lua_pushstring(L, name); - else - lua_pushinteger(L, key); - - lua_pushboolean(L, true); - lua_rawset(L, -3); - - int charval = Screen::keyToChar(key); - if (charval >= 0) - { - lua_pushinteger(L, charval); - lua_setfield(L, -2, "_STRING"); - } - } - - if (enabler) - { - if (enabler->mouse_lbut_down) { - lua_pushboolean(L, true); - lua_setfield(L, -2, "_MOUSE_L"); - } - if (enabler->mouse_rbut_down) { - lua_pushboolean(L, true); - lua_setfield(L, -2, "_MOUSE_R"); - } - if (enabler->mouse_lbut) { - lua_pushboolean(L, true); - lua_setfield(L, -2, "_MOUSE_L_DOWN"); - enabler->mouse_lbut = 0; - } - if (enabler->mouse_rbut) { - lua_pushboolean(L, true); - lua_setfield(L, -2, "_MOUSE_R_DOWN"); - enabler->mouse_rbut = 0; - } - } + Lua::PushInterfaceKeys(L, *keys); lua_call(L, 2, 0); self->update_focus(L, -1);