Move focus handling into the input code

This commit is contained in:
Cameron Gutman
2020-04-25 15:37:33 -07:00
parent 65d53e800b
commit 68acf969f2
3 changed files with 52 additions and 35 deletions

View File

@@ -1350,6 +1350,51 @@ void SdlInputHandler::raiseAllKeys()
m_KeysDown.clear();
}
void SdlInputHandler::notifyFocusGained(SDL_Window* window)
{
// Capture mouse cursor when user actives the window by clicking on
// window's client area (borders and title bar excluded).
// Without this you would have to click the window twice (once to
// activate it, second time to enable capture). With this you need to
// click it only once.
//
// On Linux, the button press event is delivered after the focus gain
// so this is not neccessary (and leads to a click sent to the host
// when focusing the window by clicking).
//
// By excluding window's borders and title bar out, lets user still
// interact with them without mouse capture kicking in.
#if defined(Q_OS_WIN32) || defined(Q_OS_DARWIN)
int mouseX, mouseY;
Uint32 mouseState = SDL_GetGlobalMouseState(&mouseX, &mouseY);
if (mouseState & SDL_BUTTON(SDL_BUTTON_LEFT)) {
int x, y, width, height;
SDL_GetWindowPosition(window, &x, &y);
SDL_GetWindowSize(window, &width, &height);
if (mouseX > x && mouseX < x+width && mouseY > y && mouseY < y+height) {
setCaptureActive(true);
}
}
#else
Q_UNUSED(window);
#endif
}
void SdlInputHandler::notifyFocusLost(SDL_Window* window)
{
// Release mouse cursor when another window is activated (e.g. by using ALT+TAB).
// This lets user to interact with our window's title bar and with the buttons in it.
// Doing this while the window is full-screen breaks the transition out of FS
// (desktop and exclusive), so we must check for that before releasing mouse capture.
if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN)) {
setCaptureActive(false);
}
// Raise all keys that are currently pressed. If we don't do this, certain keys
// used in shortcuts that cause focus loss (such as Alt+Tab) may get stuck down.
raiseAllKeys();
}
bool SdlInputHandler::isCaptureActive()
{
if (SDL_GetRelativeMouseMode()) {

View File

@@ -67,6 +67,10 @@ public:
void raiseAllKeys();
void notifyFocusGained(SDL_Window* window);
void notifyFocusLost(SDL_Window* window);
bool isCaptureActive();
void setCaptureActive(bool active);

View File

@@ -1204,43 +1204,11 @@ void Session::exec(int displayOriginX, int displayOriginY)
break;
case SDL_WINDOWEVENT:
// Capture mouse cursor when user actives the window by clicking on
// window's client area (borders and title bar excluded).
// Without this you would have to click the window twice (once to
// activate it, second time to enable capture). With this you need to
// click it only once.
// On Linux, the button press event is delivered after the focus gain
// so this is not neccessary (and leads to a click sent to the host
// when focusing the window by clicking).
// By excluding window's borders and title bar out, lets user still
// interact with them without mouse capture kicking in.
#if defined(Q_OS_WIN32) || defined(Q_OS_DARWIN)
if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
int mouseX, mouseY;
Uint32 mouseState = SDL_GetGlobalMouseState(&mouseX, &mouseY);
if (mouseState & SDL_BUTTON(SDL_BUTTON_LEFT)) {
int x, y, width, height;
SDL_GetWindowPosition(m_Window, &x, &y);
SDL_GetWindowSize(m_Window, &width, &height);
if (mouseX > x && mouseX < x+width && mouseY > y && mouseY < y+height) {
m_InputHandler->setCaptureActive(true);
}
}
m_InputHandler->notifyFocusGained(m_Window);
}
#endif
if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
// Release mouse cursor when another window is activated (e.g. by using ALT+TAB).
// This lets user to interact with our window's title bar and with the buttons in it.
// Doing this while the window is full-screen breaks the transition out of FS
// (desktop and exclusive), so we must check for that before releasing mouse capture.
if (!(SDL_GetWindowFlags(m_Window) & SDL_WINDOW_FULLSCREEN)) {
m_InputHandler->setCaptureActive(false);
}
// Raise all keys that are currently pressed. If we don't do this, certain keys
// used in shortcuts that cause focus loss (such as Alt+Tab) may get stuck down.
m_InputHandler->raiseAllKeys();
else if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
m_InputHandler->notifyFocusLost(m_Window);
}
// Capture the mouse on SDL_WINDOWEVENT_ENTER if needed