scripting.h fix + input library

This commit is contained in:
MihailRis
2024-05-05 00:28:30 +03:00
parent f9dd865bee
commit f02edcf15a
12 changed files with 121 additions and 34 deletions
+13 -1
View File
@@ -127,13 +127,14 @@ void lua::LuaState::createLibs() {
openlib("core", corelib, 0);
openlib("file", filelib, 0);
openlib("gui", guilib, 0);
openlib("input", inputlib, 0);
openlib("inventory", inventorylib, 0);
openlib("item", itemlib, 0);
openlib("json", jsonlib, 0);
openlib("pack", packlib, 0);
openlib("player", playerlib, 0);
openlib("time", timelib, 0);
openlib("world", worldlib, 0);
openlib("json", jsonlib, 0);
addfunc("print", lua_wrap_errors<l_print>);
}
@@ -426,6 +427,17 @@ void lua::LuaState::removeEnvironment(int id) {
logger.debug() << "removed environment " << envName(id);
}
bool lua::LuaState::emit_event(const std::string &name, std::function<int(lua::LuaState *)> args) {
getglobal("events");
getfield("emit");
pushstring(name);
callNoThrow(args(this) + 1);
bool result = toboolean(-1);
pop(2);
return result;
}
void lua::LuaState::dumpStack() {
int top = gettop();
for (int i = 1; i <= top; i++) {
+1
View File
@@ -67,6 +67,7 @@ namespace lua {
int createEnvironment(int parent);
void removeEnvironment(int id);
const std::string storeAnonymous();
bool emit_event(const std::string& name, std::function<int(lua::LuaState*)> args=[](auto*){return 0;});
void dumpStack();
};
+1 -1
View File
@@ -18,7 +18,7 @@ extern const luaL_Reg playerlib [];
extern const luaL_Reg timelib [];
extern const luaL_Reg worldlib [];
extern const luaL_Reg jsonlib [];
extern const luaL_Reg inputlib [];
// Lua Overrides
extern int l_print(lua_State* L);
+1
View File
@@ -1,5 +1,6 @@
#include "lua_commons.h"
#include "api_lua.h"
#include "LuaState.h"
#include "../../../engine.h"
#include "../../../files/settings_io.hpp"
+26
View File
@@ -0,0 +1,26 @@
#include "api_lua.h"
#include "lua_commons.h"
#include "../scripting.h"
#include "../../../window/input.h"
#include "../../../window/Events.h"
#include "../../../frontend/screens/Screen.hpp"
#include "../../../engine.h"
#include "LuaState.h"
namespace scripting {
extern lua::LuaState* state;
}
static int l_keycode(lua_State* L) {
const char* name = lua_tostring(L, 1);
lua_pushinteger(L, static_cast<int>(input_util::keycode_from(name)));
return 1;
}
const luaL_Reg inputlib [] = {
{"keycode", lua_wrap_errors<l_keycode>},
{NULL, NULL}
};