spacebox/src/Input.cpp

148 lines
4.5 KiB
C++

/* +------------------------------------------------------+
____/ \____ /| - Open source game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
| ~~~~~~~~~~~~ | +------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
#include "Input.hpp"
Input::Input(Node *parent) : Node(parent)
{
get_delegate().subscribe(&Input::respond, this, SDL_KEYDOWN);
get_delegate().subscribe(&Input::respond, this, SDL_KEYUP);
for (KeyCombination& combination : key_map)
{
print_key_combination(combination);
}
}
void Input::print_key_combination(const KeyCombination &combination) const
{
std::cout << "<KeyCombination " << combination.command << ", " <<
combination.key << ", ctrl " << combination.ctrl << ", shift " <<
combination.shift << ", alt " << combination.alt << ">" << std::endl;
}
void Input::load_key_map()
{
const nlohmann::json& config = configuration()();
for (const auto& [command, input] : config.at("keys").items())
{
if (input.is_array())
{
if (input[0].is_array())
{
for (nlohmann::json version : input)
{
key_map.push_back(parse_key(command, version));
}
}
else
{
key_map.push_back(parse_key(command, input));
}
}
else
{
key_map.push_back((KeyCombination){command, get_key_code(input.get<std::string>()), false, false, false});
}
}
}
KeyCombination Input::parse_key(const std::string& command, const nlohmann::json& json) const
{
SDL_Keycode key_code;
bool ctrl = false, alt = false, shift = false;
for (const std::string& component : json)
{
if (component == "CTRL")
{
ctrl = true;
}
else if (component == "SHIFT")
{
shift = true;
}
else if (component == "ALT")
{
alt = true;
}
else
{
key_code = get_key_code(component);
}
}
return (KeyCombination){command, key_code, ctrl, shift, alt};
}
int Input::get_key_code(const std::string& name) const
{
if (key_ids.count(name) > 0)
{
return key_ids.at(name);
}
else
{
return (SDL_Keycode) name[0];
}
}
void Input::add_to_key_map(std::string command, SDL_Keycode key_code, bool ctrl, bool shift, bool alt)
{
key_map.push_back((KeyCombination){command, key_code, ctrl, shift, alt});
}
void Input::respond(SDL_Event &event)
{
if (!is_suppressed())
{
SDL_Keymod mod = SDL_GetModState();
SDL_Keycode sym = event.key.keysym.sym;
bool found_command = false, cancel = event.type != SDL_KEYDOWN, ctrl = mod & KMOD_CTRL,
shift = mod & KMOD_SHIFT, alt = mod & KMOD_ALT,
suppress_any_key = configuration()["input"]["suppress any key on mods"] && (ctrl || alt);
const std::vector<std::string>& system_any_key_ignore = configuration()["input"]["system any key ignore commands"];
const std::vector<std::string>& any_key_ignore = configuration()["input"]["any key ignore commands"];
bool ignore_repeat = configuration()["input"]["ignore repeat keypress"];
if (event.key.repeat == 0 || !ignore_repeat)
{
for (KeyCombination& combination : key_map)
{
if (sym == combination.key && (!combination.ctrl || ctrl) && (!combination.shift || shift) && (!combination.alt || alt))
{
sb::Delegate::post(combination.command, cancel);
if (!sb::is_in_container(system_any_key_ignore, combination.command) && !found_command &&
!sb::is_in_container(any_key_ignore, combination.command) && !suppress_any_key)
{
sb::Delegate::post(any, cancel);
}
found_command = true;
}
}
if (!found_command && !suppress_any_key)
{
sb::Delegate::post(any, cancel);
}
}
}
}
void Input::suppress()
{
suppressed = true;
}
void Input::unsuppress()
{
suppressed = false;
}
bool Input::is_suppressed()
{
return suppressed;
}