2020-11-08 18:14:41 -08:00
|
|
|
#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.
|
|
|
|
*/
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-08-29 20:38:22 -07:00
|
|
|
static void registerKeybind(Input& input, vec<CallbackRef>& refs, sol::table keybinds, const string& identifier) {
|
2020-11-08 18:14:41 -08:00
|
|
|
sol::table keybindTbl = keybinds[identifier];
|
2021-08-29 20:38:22 -07:00
|
|
|
u32 def = keybindTbl.get<unsigned short>("default");
|
2020-11-08 18:14:41 -08:00
|
|
|
|
|
|
|
auto onPress = keybindTbl.get<sol::optional<sol::function>>("on_press");
|
|
|
|
auto onRelease = keybindTbl.get<sol::optional<sol::function>>("on_release");
|
|
|
|
|
2021-08-29 20:38:22 -07:00
|
|
|
if (onPress) refs.emplace_back(input.events.bind(Input::CBType::KEY_PRESS, [=](u32 key, i32) {
|
|
|
|
if (key == def) (*onPress)(); }));
|
|
|
|
if (onRelease) refs.emplace_back(input.events.bind(Input::CBType::KEY_RELEASE, [=](u32 key, i32) {
|
|
|
|
if (key == def) (*onRelease)(); }));
|
2020-11-08 18:14:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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']`.
|
2021-08-29 20:38:22 -07:00
|
|
|
* @param game - The input manager to add the keybind to.
|
2020-11-08 18:14:41 -08:00
|
|
|
* @param identifier - The identifier of the keybind to add.
|
|
|
|
*/
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-08-29 20:38:22 -07:00
|
|
|
static void client(sol::table& core, Input& input, vec<CallbackRef>& refs, const string& identifier) {
|
|
|
|
registerKeybind(input, refs, core.get<sol::table>("registered_keybinds"), identifier);
|
2020-11-08 22:57:34 -08:00
|
|
|
}
|
2020-11-08 18:14:41 -08:00
|
|
|
};
|