0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 07:53:43 +00:00

fix: fix issue with context switching by adding action grouping by context

This commit is contained in:
ezveee 2025-01-16 17:29:16 +01:00
parent a130d865dd
commit 31e29713de
2 changed files with 7 additions and 13 deletions

View File

@ -114,7 +114,7 @@ public:
std::string getActiveContext() const;
private:
std::vector<InputAction> actions;
std::map<std::string, std::vector<InputAction>> actionsByContext;
std::map<Key, SDL_Scancode> keyMap;
std::string activeContext;

View File

@ -101,7 +101,8 @@ void InputManager::initKeyMap() {
}
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, context});
actionsByContext[context].push_back({actionName, keys, callback});
std::cout << "Registered action: " << actionName << " in context: " << context << std::endl;
}
void InputManager::setActiveContext(const std::string& context) {
@ -124,19 +125,12 @@ void InputManager::processEvents() {
void InputManager::handleEvent(const SDL_Event& event) {
if (event.type != SDL_EVENT_KEY_DOWN) return; // TODO: add other events if necessary
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;
}
auto& contextActions = actionsByContext[activeContext];
for (const auto& action : contextActions) {
for (const auto& binding : action.bindings) {
if (event.key.scancode == keyMap[binding] && action.context == activeContext) {
std::cout << "Action triggered: " << action.name << " in context: " << action.context << std::endl;
if (event.key.scancode == keyMap[binding]) {
std::cout << "Action triggered: " << action.name << " in context: " << activeContext << std::endl;
action.callback();
return;
}