mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 07:53:43 +00:00
mod: changed InputManager to account for multiple bindings for one action
This commit is contained in:
parent
9c3185c6d8
commit
b6cba94323
@ -96,27 +96,23 @@ public:
|
||||
GRAVE
|
||||
};
|
||||
|
||||
struct InputAction {
|
||||
std::string name;
|
||||
std::vector<Key> bindings;
|
||||
std::function<void()> callback;
|
||||
};
|
||||
|
||||
InputManager();
|
||||
~InputManager();
|
||||
|
||||
void init(); // see if necessary
|
||||
void processEvents();
|
||||
void registerAction(const std::string& actionName, EventType eventType, Key key, std::function<void()> callback);
|
||||
bool isKeyPressed(Key key);
|
||||
void registerAction(const std::string& actionName, const std::vector<Key>& keys, std::function<void()> callback);
|
||||
|
||||
private:
|
||||
std::vector<InputAction> actions;
|
||||
std::map<Key, SDL_Scancode> keyMap;
|
||||
std::map<EventType, SDL_EventType> eventMap;
|
||||
|
||||
struct ActionBinding {
|
||||
std::string actionName; // not strictly necessary, but good for debugging purposes
|
||||
EventType eventType;
|
||||
Key key;
|
||||
std::function<void()> callback;
|
||||
};
|
||||
std::vector<ActionBinding> actionBindings;
|
||||
|
||||
void initKeyMap();
|
||||
void initEventMap();
|
||||
void handleEvent(const SDL_Event& event);
|
||||
};
|
||||
@ -3,7 +3,6 @@
|
||||
|
||||
InputManager::InputManager() {
|
||||
initKeyMap();
|
||||
initEventMap();
|
||||
}
|
||||
|
||||
InputManager::~InputManager() {
|
||||
@ -101,34 +100,27 @@ void InputManager::initKeyMap() {
|
||||
};
|
||||
}
|
||||
|
||||
void InputManager::initEventMap() {
|
||||
eventMap = {
|
||||
{EventType::KeyDown, SDL_EVENT_KEY_DOWN},
|
||||
{EventType::KeyUp, SDL_EVENT_KEY_UP},
|
||||
};
|
||||
}
|
||||
|
||||
void InputManager::registerAction(const std::string& actionName, EventType eventType, Key key, std::function<void()> callback) {
|
||||
actionBindings.push_back({actionName, eventType, key, callback});
|
||||
}
|
||||
|
||||
bool InputManager::isKeyPressed(Key key) {
|
||||
const bool* state = SDL_GetKeyboardState(nullptr);
|
||||
auto sdlKey = keyMap[key];
|
||||
return state[sdlKey];
|
||||
void InputManager::registerAction(const std::string& actionName, const std::vector<Key>& keys, std::function<void()> callback) {
|
||||
actions.push_back({actionName, keys, callback});
|
||||
}
|
||||
|
||||
void InputManager::processEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
std::cout << "Event detected: " << event.type << std::endl;
|
||||
handleEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
void InputManager::handleEvent(const SDL_Event& event) {
|
||||
for (const auto& binding : actionBindings) {
|
||||
if (event.type == eventMap[binding.eventType] && event.key.scancode == keyMap[binding.key]) {
|
||||
binding.callback();
|
||||
if (event.type != SDL_EVENT_KEY_DOWN) return; // TODO: add other events if necessary
|
||||
|
||||
for (const auto& action : actions) {
|
||||
for (const auto& binding : action.bindings) {
|
||||
if (event.key.scancode == keyMap[binding]) {
|
||||
std::cout << "Action triggered: " << action.name << std::endl;
|
||||
action.callback();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -27,7 +27,7 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
|
||||
return SDL_APP_SUCCESS;
|
||||
}
|
||||
|
||||
vego::game->handleEvents(); // bad
|
||||
//vego::game->handleEvents(); // bad
|
||||
|
||||
Uint64 frameStart = SDL_GetTicks();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user