spacebox/src/Input.hpp
Cocktail Frank efa08e7c93 Use SDL_GetKeyFromName to read key names in config
Log a warning if SDL does not understand the key name.
2025-02-04 13:28:42 -05:00

81 lines
2.3 KiB
C++

/* +=======================================================+
____/ \____ /: Open source game framework licensed to freely use, :
\ / / : copy, and modify - created for dank.game :
+==\ ^__^ /==+ : :
: ~/ \~ : : Download at https://open.shampoo.ooo/shampoo/spacebox :
: ~~~~~~~~~~~~ : +=======================================================+
: SPACE ~~~~~ : /
: ~~~~~~~ BOX :/
+=============*/
#pragma once
#include <string>
#include <map>
#include <functional>
#include <algorithm>
#include "SDL.h"
#include "Node.hpp"
#include "Animation.hpp"
struct KeyCombination
{
std::string command;
int key;
bool ctrl;
bool shift;
bool alt;
};
class Input : public Node
{
private:
std::vector<KeyCombination> key_map;
bool suppressed = false;
KeyCombination parse_key(const std::string& command, const nlohmann::json& json) const;
public:
inline static std::string any = "any";
sb::Animation unsuppress_animation { std::bind(&Input::unsuppress, this) };
Input(Node*);
void respond(SDL_Event&);
/*!
* Read the "keys" section of the config into this object and store it as a key map. The "keys" section describes
* how commands should be mapped to keys. For example, if "action" is mapped to "Space", then the spacebar will
* trigger an "action" command.
*
* Each command in the "keys" section can be mapped to one or more keys, and each of the mappings can include
* modifiers like "CTRL" or "SHIFT".
*
* If a key name is not understood by SDL, a warning will be printed to the log and program execution will continue
* with the command mapped to "SDLK_UNKNOWN".
*/
void load_key_map();
void add_to_key_map(std::string, SDL_Keycode, bool = false, bool = false, bool = false);
void print_key_combination(const KeyCombination&) const;
void suppress();
void unsuppress();
bool is_suppressed();
virtual std::string class_name() const { return "Input"; }
};
/* Add Input class to the sb namespace. This should be the default location, but Input is left in the global namespace
* for backward compatibility.
*/
namespace sb
{
using ::Input;
}
#include "extension.hpp"
#include "Configuration.hpp"
#include "Delegate.hpp"