0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-13 12:33:42 +00:00

feat: added context for input actions (bug: issue with context switching)

This commit is contained in:
ezveee 2025-01-16 17:18:24 +01:00
parent b6cba94323
commit a130d865dd
2 changed files with 30 additions and 6 deletions

View File

@ -100,6 +100,7 @@ public:
std::string name; std::string name;
std::vector<Key> bindings; std::vector<Key> bindings;
std::function<void()> callback; std::function<void()> callback;
std::string context;
}; };
InputManager(); InputManager();
@ -107,11 +108,15 @@ public:
void init(); // see if necessary void init(); // see if necessary
void processEvents(); void processEvents();
void registerAction(const std::string& actionName, const std::vector<Key>& keys, std::function<void()> callback); void registerAction(const std::string& actionName, const std::vector<Key>& keys, std::function<void()> callback, const std::string& context);
void setActiveContext(const std::string& context);
std::string getActiveContext() const;
private: private:
std::vector<InputAction> actions; std::vector<InputAction> actions;
std::map<Key, SDL_Scancode> keyMap; std::map<Key, SDL_Scancode> keyMap;
std::string activeContext;
void initKeyMap(); void initKeyMap();
void handleEvent(const SDL_Event& event); void handleEvent(const SDL_Event& event);

View File

@ -1,7 +1,7 @@
#include "InputManager.h" #include "InputManager.h"
#include <iostream> #include <iostream>
InputManager::InputManager() { InputManager::InputManager() : activeContext("Default") {
initKeyMap(); initKeyMap();
} }
@ -100,8 +100,17 @@ void InputManager::initKeyMap() {
}; };
} }
void InputManager::registerAction(const std::string& actionName, const std::vector<Key>& keys, std::function<void()> callback) { void InputManager::registerAction(const std::string& actionName, const std::vector<Key>& keys, std::function<void()> callback, const std::string& context) {
actions.push_back({actionName, keys, callback}); actions.push_back({actionName, keys, callback, context});
}
void InputManager::setActiveContext(const std::string& context) {
activeContext = context;
std::cout << "Active context set to: " << activeContext << std::endl;
}
std::string InputManager::getActiveContext() const {
return activeContext;
} }
void InputManager::processEvents() { void InputManager::processEvents() {
@ -116,10 +125,20 @@ void InputManager::handleEvent(const SDL_Event& event) {
if (event.type != SDL_EVENT_KEY_DOWN) return; // TODO: add other events if necessary if (event.type != SDL_EVENT_KEY_DOWN) return; // TODO: add other events if necessary
for (const auto& action : actions) { for (const auto& action : actions) {
std::cout << "Processing action " << action.name
<< " with context " << action.context
<< " (Active context: " << activeContext << ")" << std::endl;
if (action.context != activeContext) {
std::cout << "Skipping action: " << action.name << std::endl;
continue;
}
for (const auto& binding : action.bindings) { for (const auto& binding : action.bindings) {
if (event.key.scancode == keyMap[binding]) { if (event.key.scancode == keyMap[binding] && action.context == activeContext) {
std::cout << "Action triggered: " << action.name << std::endl; std::cout << "Action triggered: " << action.name << " in context: " << action.context << std::endl;
action.callback(); action.callback();
return;
} }
} }
} }