New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CRAB: Fix wrong keymap being set in the Dialog Box #5424
Conversation
Now pressing enter key loads the next statement of the character in the dialog box.
WalkthroughWalkthroughThe update to the game's code modifies the event handling within the Changes
TipsChat with CodeRabbit Bot (
|
// Purpose: Handle input | ||
//------------------------------------------------------------------------ | ||
bool GameDialogBox::handleEvents(const Common::Event &event) { | ||
return (_button.handleEvents(event) == BUAC_LCLICK); | ||
// Switch to KBM_UI | ||
if (g_engine->_inputManager->getKeyBindingMode() != KBM_UI) | ||
g_engine->_inputManager->setKeyBindingMode(KBM_UI); | ||
|
||
bool isLeftClick = (_button.handleEvents(event) == BUAC_LCLICK); | ||
|
||
if (isLeftClick) { | ||
// Switch to KBM_GAME | ||
g_engine->_inputManager->setKeyBindingMode(KBM_GAME); | ||
} | ||
|
||
return isLeftClick; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes correctly implement the logic to switch key binding modes based on the event type. However, it's important to ensure that the key binding mode is only changed when necessary to avoid unnecessary calls which might affect performance or cause unexpected behavior in other parts of the game. Additionally, the code should ensure that the key binding mode is always correctly restored, even if an exception or an early return occurs between the mode switch and the restoration. Consider wrapping the mode switch and restoration logic in a RAII (Resource Acquisition Is Initialization) style object that automatically restores the previous mode when it goes out of scope.
class KeyBindingModeGuard {
public:
KeyBindingModeGuard(InputManager* inputManager, KeyBindingMode newMode)
: _inputManager(inputManager), _oldMode(inputManager->getKeyBindingMode()) {
_inputManager->setKeyBindingMode(newMode);
}
~KeyBindingModeGuard() {
_inputManager->setKeyBindingMode(_oldMode);
}
private:
InputManager* _inputManager;
KeyBindingMode _oldMode;
};
// Usage in the GameDialogBox::handleEvents function
bool GameDialogBox::handleEvents(const Common::Event &event) {
KeyBindingModeGuard modeGuard(g_engine->_inputManager, KBM_UI);
bool isLeftClick = (_button.handleEvents(event) == BUAC_LCLICK);
if (isLeftClick) {
// The destructor of KeyBindingModeGuard will automatically switch back to the old mode
}
return isLeftClick;
}
This ensures that the key binding mode is always restored, even if an exception is thrown or if there are multiple return paths from the function.
Thanks! |
Now pressing enter key loads the next statement of the character in the dialog box.