47 lines
1.8 KiB
C++
47 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include "lua/Lua.h"
|
|
#include "game/LocalSubgame.h"
|
|
#include "game/ServerSubgame.h"
|
|
#include "game/def/BiomeDef.h"
|
|
|
|
namespace RegisterKeybind {
|
|
namespace {
|
|
|
|
/**
|
|
* Registers a keybind from the keybinds table to the handler provided.
|
|
*
|
|
* @param keybinds - The keybinds table to index.
|
|
* @param identifier - The identifier of the keybind in the table.
|
|
* @param handler - The keybind handler to add the keybind to.
|
|
*/
|
|
|
|
static void registerKeybind(Input& input, vec<ListenerRef>& listeners, sol::table keybinds, const string& identifier) {
|
|
sol::table keybindTbl = keybinds[identifier];
|
|
u32 def = keybindTbl.get<unsigned short>("default");
|
|
|
|
auto onPress = keybindTbl.get<sol::optional<sol::function>>("on_press");
|
|
auto onRelease = keybindTbl.get<sol::optional<sol::function>>("on_release");
|
|
|
|
if (onPress) listeners.emplace_back(input.bind<Input::Event::KeyPress>(
|
|
[=](u32 key) { if (key == def) (*onPress)(); }));
|
|
if (onRelease) listeners.emplace_back(input.bind<Input::Event::KeyRelease>(
|
|
[=](u32 key) { if (key == def) (*onRelease)(); }));
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Client method to register a keybind. Calls registerKeybind with the necessary parameters.
|
|
* There is no server register method (because that wouldn't make sense), but this method is here for consistency.
|
|
* Registers a keybind to the keybind handler.
|
|
*
|
|
* @param core - The core table to index for 'registered_keybinds', i.e. `_G['zepha']`.
|
|
* @param game - The input manager to add the keybind to.
|
|
* @param identifier - The identifier of the keybind to add.
|
|
*/
|
|
|
|
static void client(sol::table& core, Input& input, vec<ListenerRef>& listeners, const string& identifier) {
|
|
registerKeybind(input, listeners, core.get<sol::table>("registered_keybinds"), identifier);
|
|
}
|
|
}; |