input.add_callback
This commit is contained in:
@@ -31,8 +31,6 @@ public:
|
|||||||
virtual std::string toString() const = 0;
|
virtual std::string toString() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
using observer_handler = std::shared_ptr<int>;
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
class ObservableSetting : public Setting {
|
class ObservableSetting : public Setting {
|
||||||
int nextid = 1;
|
int nextid = 1;
|
||||||
|
|||||||
+2
-1
@@ -2,6 +2,7 @@
|
|||||||
#define FRONTEND_HUD_H_
|
#define FRONTEND_HUD_H_
|
||||||
|
|
||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
|
#include "../util/ObjectsKeeper.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -63,7 +64,7 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Hud {
|
class Hud : public util::ObjectsKeeper {
|
||||||
Assets* assets;
|
Assets* assets;
|
||||||
std::unique_ptr<Camera> uicamera;
|
std::unique_ptr<Camera> uicamera;
|
||||||
gui::GUI* gui;
|
gui::GUI* gui;
|
||||||
|
|||||||
@@ -5,22 +5,43 @@
|
|||||||
#include "../../../window/input.h"
|
#include "../../../window/input.h"
|
||||||
#include "../../../window/Events.h"
|
#include "../../../window/Events.h"
|
||||||
#include "../../../frontend/screens/Screen.hpp"
|
#include "../../../frontend/screens/Screen.hpp"
|
||||||
|
#include "../../../frontend/hud.h"
|
||||||
#include "../../../engine.h"
|
#include "../../../engine.h"
|
||||||
|
|
||||||
#include "LuaState.h"
|
#include "LuaState.h"
|
||||||
|
|
||||||
namespace scripting {
|
namespace scripting {
|
||||||
extern lua::LuaState* state;
|
extern lua::LuaState* state;
|
||||||
|
extern Hud* hud;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
using namespace scripting;
|
||||||
|
|
||||||
static int l_keycode(lua_State* L) {
|
static int l_keycode(lua_State* L) {
|
||||||
const char* name = lua_tostring(L, 1);
|
const char* name = lua_tostring(L, 1);
|
||||||
lua_pushinteger(L, static_cast<int>(input_util::keycode_from(name)));
|
lua_pushinteger(L, static_cast<int>(input_util::keycode_from(name)));
|
||||||
return 1;
|
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 [] = {
|
const luaL_Reg inputlib [] = {
|
||||||
{"keycode", lua_wrap_errors<l_keycode>},
|
{"keycode", lua_wrap_errors<l_keycode>},
|
||||||
|
{"add_callback", lua_wrap_errors<l_add_callback>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include <variant>
|
#include <variant>
|
||||||
|
|
||||||
using scriptenv = std::shared_ptr<int>;
|
using scriptenv = std::shared_ptr<int>;
|
||||||
|
using observer_handler = std::shared_ptr<int>;
|
||||||
|
|
||||||
/// @brief dynamic integer type (64 bit signed integer)
|
/// @brief dynamic integer type (64 bit signed integer)
|
||||||
using integer_t = int64_t;
|
using integer_t = int64_t;
|
||||||
|
|||||||
@@ -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_
|
||||||
@@ -85,6 +85,7 @@ void Events::pollEvents() {
|
|||||||
if (!binding.state) {
|
if (!binding.state) {
|
||||||
binding.state = true;
|
binding.state = true;
|
||||||
binding.justChange = true;
|
binding.justChange = true;
|
||||||
|
binding.onactived.notify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
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) {
|
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) {
|
bool Events::active(std::string name) {
|
||||||
|
|||||||
+8
-2
@@ -1,6 +1,8 @@
|
|||||||
#ifndef WINDOW_INPUT_H_
|
#ifndef WINDOW_INPUT_H_
|
||||||
#define WINDOW_INPUT_H_
|
#define WINDOW_INPUT_H_
|
||||||
|
|
||||||
|
#include "../util/RunnablesList.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
/// @brief Represents glfw3 keycode values.
|
/// @brief Represents glfw3 keycode values.
|
||||||
@@ -86,8 +88,6 @@ enum class keycode : int {
|
|||||||
UNKNOWN = -1
|
UNKNOWN = -1
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// @brief Represents glfw3 mouse button IDs.
|
/// @brief Represents glfw3 mouse button IDs.
|
||||||
/// @details There is a subset of glfw3 mouse button IDs.
|
/// @details There is a subset of glfw3 mouse button IDs.
|
||||||
enum class mousecode : int {
|
enum class mousecode : int {
|
||||||
@@ -104,6 +104,7 @@ inline mousecode MOUSECODES_ALL[] {
|
|||||||
|
|
||||||
namespace input_util {
|
namespace input_util {
|
||||||
void initialize();
|
void initialize();
|
||||||
|
|
||||||
keycode keycode_from(const std::string& name);
|
keycode keycode_from(const std::string& name);
|
||||||
/// @return Key label by keycode
|
/// @return Key label by keycode
|
||||||
std::string to_string(keycode code);
|
std::string to_string(keycode code);
|
||||||
@@ -117,11 +118,16 @@ enum class inputtype {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct Binding {
|
struct Binding {
|
||||||
|
util::RunnablesList onactived;
|
||||||
|
|
||||||
inputtype type;
|
inputtype type;
|
||||||
int code;
|
int code;
|
||||||
bool state = false;
|
bool state = false;
|
||||||
bool justChange = false;
|
bool justChange = false;
|
||||||
|
|
||||||
|
Binding(){}
|
||||||
|
Binding(inputtype type, int code) : type(type), code(code) {}
|
||||||
|
|
||||||
bool active() const {
|
bool active() const {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user