gui: lock mouse only when mouse axis/button is bound

This commit is contained in:
Jakub Czekański 2020-05-06 18:58:52 +02:00
parent 709c21f366
commit d35f1c612b
2 changed files with 24 additions and 5 deletions

View file

@ -46,7 +46,7 @@ void SdlInputManager::vibrationThreadFunc() {
}
void SdlInputManager::onVibrationEvent(Event::Controller::Vibration e) {
// Vibration output is choosen by DPAD_UP mapped game controller
// Vibration output is chosen by DPAD_UP mapped game controller
std::string keyName = config.controller[e.port - 1].keys["dpad_up"];
Key key(keyName);
@ -62,6 +62,18 @@ void SdlInputManager::onVibrationEvent(Event::Controller::Vibration e) {
}
}
bool SdlInputManager::isMouseBound() {
// Check if mouse axis is bound anywhere
for (auto& c : config.controller) {
for (auto& k : c.keys) {
if (k.second.rfind("mouse", 0) == 0) {
return true;
}
}
}
return false;
}
bool SdlInputManager::handleKey(Key key, AnalogValue value) {
if (waitingForKeyPress) {
waitingForKeyPress = false;
@ -104,6 +116,12 @@ void SdlInputManager::newFrame() {
mouseX = 0;
mouseY = 0;
#ifdef ANDROID
shouldCaptureMouse = false;
#else
shouldCaptureMouse = isMouseBound();
#endif
if (waitingForKeyPress) return;
Key key;
@ -126,10 +144,8 @@ bool SdlInputManager::handleEvent(SDL_Event& event) {
auto type = event.type;
if ((!mouseCaptured || waitingForKeyPress) && event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT
&& event.button.clicks == 2) {
#ifndef ANDROID
&& event.button.clicks == 2 && shouldCaptureMouse) {
mouseLocked = true;
#endif
return true;
}
@ -235,4 +251,4 @@ bool SdlInputManager::handleEvent(SDL_Event& event) {
}
return false;
}
}

View file

@ -13,6 +13,7 @@ class SdlInputManager : public InputManager {
std::unordered_map<int, SDL_GameController*> controllers;
int32_t mouseX = 0; // Track mouse movement in frame
int32_t mouseY = 0;
bool shouldCaptureMouse = false;
bool handleKey(Key key, AnalogValue value);
void fixControllerId(SDL_Event& event);
@ -32,6 +33,8 @@ class SdlInputManager : public InputManager {
void vibrationThreadFunc();
void onVibrationEvent(Event::Controller::Vibration e);
bool isMouseBound();
public:
bool mouseCaptured = false;
bool keyboardCaptured = false;