mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-13 14:53:41 +00:00
fix: fix issue with context switching by adding action grouping by context
This commit is contained in:
parent
a130d865dd
commit
31e29713de
@ -114,7 +114,7 @@ public:
|
|||||||
std::string getActiveContext() const;
|
std::string getActiveContext() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<InputAction> actions;
|
std::map<std::string, std::vector<InputAction>> actionsByContext;
|
||||||
std::map<Key, SDL_Scancode> keyMap;
|
std::map<Key, SDL_Scancode> keyMap;
|
||||||
std::string activeContext;
|
std::string activeContext;
|
||||||
|
|
||||||
|
|||||||
@ -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) {
|
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) {
|
void InputManager::setActiveContext(const std::string& context) {
|
||||||
@ -124,19 +125,12 @@ void InputManager::processEvents() {
|
|||||||
void InputManager::handleEvent(const SDL_Event& event) {
|
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) {
|
auto& contextActions = actionsByContext[activeContext];
|
||||||
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& action : contextActions) {
|
||||||
for (const auto& binding : action.bindings) {
|
for (const auto& binding : action.bindings) {
|
||||||
if (event.key.scancode == keyMap[binding] && action.context == activeContext) {
|
if (event.key.scancode == keyMap[binding]) {
|
||||||
std::cout << "Action triggered: " << action.name << " in context: " << action.context << std::endl;
|
std::cout << "Action triggered: " << action.name << " in context: " << activeContext << std::endl;
|
||||||
action.callback();
|
action.callback();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user