factor out keys -> lua onInput code to LuaTools

develop
myk002 2022-10-31 11:43:37 -07:00 committed by Myk
parent 3bfc360c1d
commit b82a604c8d
3 changed files with 49 additions and 44 deletions

@ -158,6 +158,50 @@ void Lua::GetVector(lua_State *state, std::vector<std::string> &pvec)
} }
} }
void Lua::PushInterfaceKeys(lua_State *L,
const std::set<df::interface_key> &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) int Lua::PushPosXYZ(lua_State *state, df::coord pos)
{ {
if (!pos.isValid()) if (!pos.isValid())

@ -30,6 +30,8 @@ distribution.
#include <map> #include <map>
#include <type_traits> #include <type_traits>
#include "df/interfacest.h"
#include "ColorText.h" #include "ColorText.h"
#include "DataDefs.h" #include "DataDefs.h"
@ -321,6 +323,8 @@ namespace DFHack {namespace Lua {
Push(L, val); lua_setfield(L, idx, name); Push(L, val); lua_setfield(L, idx, name);
} }
DFHACK_EXPORT void PushInterfaceKeys(lua_State *L, const std::set<df::interface_key> &keys);
template<class T> template<class T>
void PushVector(lua_State *state, const T &pvec, bool addn = false) void PushVector(lua_State *state, const T &pvec, bool addn = false)
{ {

@ -749,50 +749,7 @@ int dfhack_lua_viewscreen::do_input(lua_State *L)
} }
lua_pushvalue(L, -2); lua_pushvalue(L, -2);
Lua::PushInterfaceKeys(L, *keys);
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_call(L, 2, 0); lua_call(L, 2, 0);
self->update_focus(L, -1); self->update_focus(L, -1);