input.add_callback

This commit is contained in:
MihailRis 2024-05-05 06:49:17 +03:00
parent 92f6fd2226
commit ba885e4e08
7 changed files with 66 additions and 6 deletions

View File

@ -31,8 +31,6 @@ public:
virtual std::string toString() const = 0;
};
using observer_handler = std::shared_ptr<int>;
template<class T>
class ObservableSetting : public Setting {
int nextid = 1;

View File

@ -2,6 +2,7 @@
#define FRONTEND_HUD_H_
#include "../typedefs.h"
#include "../util/ObjectsKeeper.hpp"
#include <string>
#include <memory>
@ -63,7 +64,7 @@ public:
}
};
class Hud {
class Hud : public util::ObjectsKeeper {
Assets* assets;
std::unique_ptr<Camera> uicamera;
gui::GUI* gui;

View File

@ -5,22 +5,43 @@
#include "../../../window/input.h"
#include "../../../window/Events.h"
#include "../../../frontend/screens/Screen.hpp"
#include "../../../frontend/hud.h"
#include "../../../engine.h"
#include "LuaState.h"
namespace scripting {
extern lua::LuaState* state;
extern Hud* hud;
}
using namespace scripting;
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;
}
static int l_add_callback(lua_State* L) {
auto bindname = lua_tostring(L, 1);
const auto& bind = Events::bindings.find(bindname);
if (bind == Events::bindings.end()) {
luaL_error(L, "unknown binding %q", bindname);
}
state->pushvalue(2);
runnable callback = state->createRunnable();
if (hud) {
hud->keepAlive(bind->second.onactived.add(callback));
} else {
engine->keepAlive(bind->second.onactived.add(callback));
}
return 0;
}
const luaL_Reg inputlib [] = {
{"keycode", lua_wrap_errors<l_keycode>},
{"add_callback", lua_wrap_errors<l_add_callback>},
{NULL, NULL}
};

View File

@ -7,6 +7,7 @@
#include <variant>
using scriptenv = std::shared_ptr<int>;
using observer_handler = std::shared_ptr<int>;
/// @brief dynamic integer type (64 bit signed integer)
using integer_t = int64_t;

View File

@ -0,0 +1,32 @@
#ifndef UTIL_RUNNABLES_LIST_HPP_
#define UTIL_RUNNABLES_LIST_HPP_
#include "../typedefs.h"
#include "../delegates.h"
#include <memory>
#include <unordered_map>
namespace util {
class RunnablesList {
int nextid = 1;
std::unordered_map<int, runnable> runnables;
public:
observer_handler add(runnable callback) {
int id = nextid++;
runnables[id] = callback;
return observer_handler(new int(id), [this](int* id) {
runnables.erase(*id);
delete id;
});
}
void notify() {
for (auto& entry : runnables) {
entry.second();
}
}
};
}
#endif // UTIL_RUNNABLES_LIST_HPP_

View File

@ -85,6 +85,7 @@ void Events::pollEvents() {
if (!binding.state) {
binding.state = true;
binding.justChange = true;
binding.onactived.notify();
}
}
else {
@ -105,7 +106,7 @@ void Events::bind(std::string name, inputtype type, mousecode code) {
}
void Events::bind(std::string name, inputtype type, int code) {
bindings[name] = { type, code, false, false };
bindings[name] = Binding(type, code);
}
bool Events::active(std::string name) {

View File

@ -1,6 +1,8 @@
#ifndef WINDOW_INPUT_H_
#define WINDOW_INPUT_H_
#include "../util/RunnablesList.hpp"
#include <string>
/// @brief Represents glfw3 keycode values.
@ -86,8 +88,6 @@ enum class keycode : int {
UNKNOWN = -1
};
/// @brief Represents glfw3 mouse button IDs.
/// @details There is a subset of glfw3 mouse button IDs.
enum class mousecode : int {
@ -104,6 +104,7 @@ 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);
@ -117,10 +118,15 @@ enum class inputtype {
};
struct Binding {
util::RunnablesList onactived;
inputtype type;
int code;
bool state = false;
bool justChange = false;
Binding(){}
Binding(inputtype type, int code) : type(type), code(code) {}
bool active() const {
return state;