spacebox/src/Input.cpp

144 lines
4.4 KiB
C++

/* /\ +--------------------------------------------------------------+
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - originally created at [http://nugget.fun] |
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
#include "Input.hpp"
Input::Input(Node *parent) : Node(parent)
{
load_key_map();
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()
{
nlohmann::json &config = configuration();
for (auto& entry : config.at("keys").items())
{
bool ctrl = false, alt = false, shift = false;
int key;
if (not entry.value().is_string())
{
for (std::string part : entry.value())
{
if (part == "CTRL")
{
ctrl = true;
}
else if (part == "SHIFT")
{
shift = true;
}
else if (part == "ALT")
{
alt = true;
}
else
{
key = get_key_code(part);
}
}
}
else
{
key = get_key_code(entry.value());
}
add_to_key_map(entry.key(), key, ctrl, shift, alt);
}
}
int Input::get_key_code(const std::string& name)
{
if (key_ids.count(name) > 0)
{
return key_ids[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))
{
post_command(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)
{
post_command(any, cancel);
}
found_command = true;
}
}
if (!found_command && !suppress_any_key)
{
post_command(any, cancel);
}
}
}
}
void Input::post_command(std::string& name, const bool& cancel)
{
SDL_Event relay;
bool* cancel_pointer = new bool(cancel);
relay.type = Delegate::command_event_type;
relay.user.data1 = &name;
relay.user.data2 = cancel_pointer;
SDL_PushEvent(&relay);
}
void Input::suppress()
{
suppressed = true;
}
void Input::unsuppress()
{
suppressed = false;
}
bool Input::is_suppressed()
{
return suppressed;
}