scripting.h fix + input library
This commit is contained in:
parent
f9dd865bee
commit
f02edcf15a
@ -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++) {
|
||||
|
||||
@ -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();
|
||||
};
|
||||
|
||||
@ -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,5 +1,6 @@
|
||||
#include "lua_commons.h"
|
||||
#include "api_lua.h"
|
||||
#include "LuaState.h"
|
||||
|
||||
#include "../../../engine.h"
|
||||
#include "../../../files/settings_io.hpp"
|
||||
|
||||
26
src/logic/scripting/lua/libinput.cpp
Normal file
26
src/logic/scripting/lua/libinput.cpp
Normal 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}
|
||||
};
|
||||
|
||||
@ -110,25 +110,25 @@ void scripting::on_world_load(LevelController* controller) {
|
||||
load_script("world.lua");
|
||||
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
emit_event(pack.id + ".worldopen");
|
||||
state->emit_event(pack.id + ".worldopen");
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_world_tick() {
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
emit_event(pack.id + ".worldtick");
|
||||
state->emit_event(pack.id + ".worldtick");
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_world_save() {
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
emit_event(pack.id + ".worldsave");
|
||||
state->emit_event(pack.id + ".worldsave");
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_world_quit() {
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
emit_event(pack.id + ".worldquit");
|
||||
state->emit_event(pack.id + ".worldquit");
|
||||
}
|
||||
|
||||
state->getglobal("pack");
|
||||
@ -151,7 +151,7 @@ void scripting::on_world_quit() {
|
||||
|
||||
void scripting::on_blocks_tick(const Block* block, int tps) {
|
||||
std::string name = block->name + ".blockstick";
|
||||
emit_event(name, [tps] (lua::LuaState* state) {
|
||||
state->emit_event(name, [tps] (lua::LuaState* state) {
|
||||
state->pushinteger(tps);
|
||||
return 1;
|
||||
});
|
||||
@ -159,7 +159,7 @@ void scripting::on_blocks_tick(const Block* block, int tps) {
|
||||
|
||||
void scripting::update_block(const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name + ".update";
|
||||
emit_event(name, [x, y, z] (lua::LuaState* state) {
|
||||
state->emit_event(name, [x, y, z] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
return 3;
|
||||
});
|
||||
@ -167,7 +167,7 @@ void scripting::update_block(const Block* block, int x, int y, int z) {
|
||||
|
||||
void scripting::random_update_block(const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name + ".randupdate";
|
||||
emit_event(name, [x, y, z] (lua::LuaState* state) {
|
||||
state->emit_event(name, [x, y, z] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
return 3;
|
||||
});
|
||||
@ -175,7 +175,7 @@ void scripting::random_update_block(const Block* block, int x, int y, int z) {
|
||||
|
||||
void scripting::on_block_placed(Player* player, const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name + ".placed";
|
||||
emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
return 4;
|
||||
@ -184,7 +184,7 @@ void scripting::on_block_placed(Player* player, const Block* block, int x, int y
|
||||
|
||||
void scripting::on_block_broken(Player* player, const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name + ".broken";
|
||||
emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
return 4;
|
||||
@ -193,7 +193,7 @@ void scripting::on_block_broken(Player* player, const Block* block, int x, int y
|
||||
|
||||
bool scripting::on_block_interact(Player* player, const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name + ".interact";
|
||||
return emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
return state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
return 4;
|
||||
@ -202,7 +202,7 @@ bool scripting::on_block_interact(Player* player, const Block* block, int x, int
|
||||
|
||||
bool scripting::on_item_use(Player* player, const ItemDef* item) {
|
||||
std::string name = item->name + ".use";
|
||||
return emit_event(name, [player] (lua::LuaState* state) {
|
||||
return state->emit_event(name, [player] (lua::LuaState* state) {
|
||||
state->pushinteger(player->getId());
|
||||
return 1;
|
||||
});
|
||||
@ -210,7 +210,7 @@ bool scripting::on_item_use(Player* player, const ItemDef* item) {
|
||||
|
||||
bool scripting::on_item_use_on_block(Player* player, const ItemDef* item, int x, int y, int z) {
|
||||
std::string name = item->name + ".useon";
|
||||
return emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
return state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
return 4;
|
||||
@ -219,7 +219,7 @@ bool scripting::on_item_use_on_block(Player* player, const ItemDef* item, int x,
|
||||
|
||||
bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z) {
|
||||
std::string name = item->name + ".blockbreakby";
|
||||
return emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
return state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
return 4;
|
||||
@ -232,7 +232,7 @@ void scripting::on_ui_open(
|
||||
) {
|
||||
auto argsptr = std::make_shared<std::vector<std::unique_ptr<dynamic::Value>>>(std::move(args));
|
||||
std::string name = layout->getId() + ".open";
|
||||
emit_event(name, [=] (lua::LuaState* state) {
|
||||
state->emit_event(name, [=] (lua::LuaState* state) {
|
||||
for (const auto& value : *argsptr) {
|
||||
state->pushvalue(*value);
|
||||
}
|
||||
@ -242,7 +242,7 @@ void scripting::on_ui_open(
|
||||
|
||||
void scripting::on_ui_progress(UiDocument* layout, int workDone, int workTotal) {
|
||||
std::string name = layout->getId() + ".progress";
|
||||
emit_event(name, [=] (lua::LuaState* state) {
|
||||
state->emit_event(name, [=] (lua::LuaState* state) {
|
||||
state->pushinteger(workDone);
|
||||
state->pushinteger(workTotal);
|
||||
return 2;
|
||||
@ -251,7 +251,7 @@ void scripting::on_ui_progress(UiDocument* layout, int workDone, int workTotal)
|
||||
|
||||
void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) {
|
||||
std::string name = layout->getId() + ".close";
|
||||
emit_event(name, [inventory] (lua::LuaState* state) {
|
||||
state->emit_event(name, [inventory] (lua::LuaState* state) {
|
||||
state->pushinteger(inventory == nullptr ? 0 : inventory->getId());
|
||||
return 1;
|
||||
});
|
||||
@ -278,16 +278,6 @@ bool scripting::register_event(int env, const std::string& name, const std::stri
|
||||
return false;
|
||||
}
|
||||
|
||||
bool scripting::emit_event(const std::string &name, std::function<int(lua::LuaState *)> args) {
|
||||
state->getglobal("events");
|
||||
state->getfield("emit");
|
||||
state->pushstring(name);
|
||||
state->callNoThrow(args(state) + 1);
|
||||
bool result = state->toboolean(-1);
|
||||
state->pop(2);
|
||||
return result;
|
||||
}
|
||||
|
||||
void scripting::load_block_script(scriptenv senv, std::string prefix, fs::path file, block_funcs_set& funcsset) {
|
||||
int env = *senv;
|
||||
std::string src = files::read_string(file);
|
||||
|
||||
@ -2,7 +2,6 @@
|
||||
#include "../../typedefs.h"
|
||||
#include "../../delegates.h"
|
||||
|
||||
#include "lua/LuaState.h"
|
||||
#include "scripting_functional.h"
|
||||
|
||||
#include <vector>
|
||||
@ -41,9 +40,6 @@ namespace scripting {
|
||||
|
||||
extern bool register_event(int env, const std::string& name, const std::string& id);
|
||||
|
||||
static inline int noargs(lua::LuaState *) { return 0; }
|
||||
extern bool emit_event(const std::string& name, std::function<int(lua::LuaState* state)> args = noargs);
|
||||
|
||||
scriptenv get_root_environment();
|
||||
scriptenv create_pack_environment(const ContentPack& pack);
|
||||
scriptenv create_doc_environment(scriptenv parent, const std::string& name);
|
||||
|
||||
@ -25,7 +25,7 @@ void scripting::on_frontend_init(Hud* hud) {
|
||||
scripting::state->openlib("hud", hudlib, 0);
|
||||
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
emit_event(pack.id + ".hudopen", [&] (lua::LuaState* state) {
|
||||
state->emit_event(pack.id + ".hudopen", [&] (lua::LuaState* state) {
|
||||
state->pushinteger(hud->getPlayer()->getId());
|
||||
return 1;
|
||||
});
|
||||
@ -34,7 +34,7 @@ void scripting::on_frontend_init(Hud* hud) {
|
||||
|
||||
void scripting::on_frontend_close() {
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
emit_event(pack.id + ".hudclose", [&] (lua::LuaState* state) {
|
||||
state->emit_event(pack.id + ".hudclose", [&] (lua::LuaState* state) {
|
||||
state->pushinteger(hud->getPlayer()->getId());
|
||||
return 1;
|
||||
});
|
||||
|
||||
@ -182,6 +182,8 @@ int Window::initialize(DisplaySettings* settings){
|
||||
logger.info() << "GL Vendor: " << (char*)vendor;
|
||||
logger.info() << "GL Renderer: " << (char*)renderer;
|
||||
logger.info() << "GLFW: " << glfwGetVersionString();
|
||||
|
||||
input_util::initialize();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -1,10 +1,37 @@
|
||||
#include "input.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <unordered_map>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif // _WIN32
|
||||
|
||||
static std::unordered_map<std::string, int> keycodes {
|
||||
{"enter", GLFW_KEY_ENTER},
|
||||
{"space", GLFW_KEY_SPACE},
|
||||
{"backspace", GLFW_KEY_BACKSPACE},
|
||||
{"caps-lock", GLFW_KEY_CAPS_LOCK},
|
||||
{"escape", GLFW_KEY_ESCAPE},
|
||||
{"delete", GLFW_KEY_DELETE},
|
||||
{"home", GLFW_KEY_HOME},
|
||||
{"end", GLFW_KEY_END},
|
||||
{"insert", GLFW_KEY_INSERT},
|
||||
{"page-down", GLFW_KEY_PAGE_DOWN},
|
||||
{"page-up", GLFW_KEY_PAGE_UP},
|
||||
{"left-shift", GLFW_KEY_LEFT_SHIFT},
|
||||
{"right-shift", GLFW_KEY_RIGHT_SHIFT},
|
||||
{"left-ctrl", GLFW_KEY_LEFT_CONTROL},
|
||||
{"right-ctrl", GLFW_KEY_RIGHT_CONTROL},
|
||||
{"left-alt", GLFW_KEY_LEFT_ALT},
|
||||
{"right-alt", GLFW_KEY_RIGHT_ALT},
|
||||
{"left-super", GLFW_KEY_LEFT_SUPER},
|
||||
{"right-super", GLFW_KEY_RIGHT_SUPER},
|
||||
{"left", GLFW_KEY_LEFT},
|
||||
{"right", GLFW_KEY_RIGHT},
|
||||
{"down", GLFW_KEY_DOWN},
|
||||
{"up", GLFW_KEY_UP},
|
||||
};
|
||||
|
||||
void Binding::reset(inputtype type, int code) {
|
||||
this->type = type;
|
||||
this->code = code;
|
||||
@ -18,6 +45,26 @@ void Binding::reset(mousecode code) {
|
||||
reset(inputtype::mouse, static_cast<int>(code));
|
||||
}
|
||||
|
||||
void input_util::initialize() {
|
||||
for (int i = 0; i <= 9; i++) {
|
||||
keycodes[std::to_string(i)] = GLFW_KEY_0+i;
|
||||
}
|
||||
for (int i = 0; i < 25; i++) {
|
||||
keycodes["f"+std::to_string(i)] = GLFW_KEY_F1+i;
|
||||
}
|
||||
for (char i = 'a'; i <= 'z'; i++) {
|
||||
keycodes[std::to_string(i)] = GLFW_KEY_A-'a'+i;
|
||||
}
|
||||
}
|
||||
|
||||
keycode input_util::keycode_from(const std::string& name) {
|
||||
const auto& found = keycodes.find(name);
|
||||
if (found == keycodes.end()) {
|
||||
return keycode::UNKNOWN;
|
||||
}
|
||||
return static_cast<keycode>(found->second);
|
||||
}
|
||||
|
||||
std::string input_util::to_string(keycode code) {
|
||||
int icode_repr = static_cast<int>(code);
|
||||
#ifdef _WIN32
|
||||
|
||||
@ -83,6 +83,7 @@ enum class keycode : int {
|
||||
NUM_LOCK = 282,
|
||||
LEFT_BRACKET = 91,
|
||||
RIGHT_BRACKET = 93,
|
||||
UNKNOWN = -1
|
||||
};
|
||||
|
||||
|
||||
@ -102,6 +103,8 @@ inline mousecode MOUSECODES_ALL[] {
|
||||
};
|
||||
|
||||
namespace input_util {
|
||||
void initialize();
|
||||
keycode keycode_from(const std::string& name);
|
||||
/// @return Key label by keycode
|
||||
std::string to_string(keycode code);
|
||||
/// @return Mouse button label by keycode
|
||||
|
||||
9
src/window/input.md
Normal file
9
src/window/input.md
Normal file
@ -0,0 +1,9 @@
|
||||
## Key Names
|
||||
- space, backspace, tab, enter, caps-lock, escape
|
||||
- left-ctrl, left-shift, left-alt, left-super
|
||||
- right-ctrl, right-shift, right-alt, right-alt
|
||||
- delete, home, end, insert, page-up, page-down
|
||||
- left, right, down, up
|
||||
- a..z
|
||||
- 0..9
|
||||
- f1..f25
|
||||
Loading…
x
Reference in New Issue
Block a user