Skip to content

Commit

Permalink
Merge pull request #212 from AJ-Ferguson/focus-fix
Browse files Browse the repository at this point in the history
Improve window focus tracking
  • Loading branch information
HumanGamer authored Mar 28, 2024
2 parents 34b89f8 + 3b374c2 commit d923b90
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
1 change: 1 addition & 0 deletions engine/source/platformWin32/platformWin32.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct Win32PlatState
S32 desktopWidth;
S32 desktopHeight;
U32 currentTime;
bool focused;

Win32WinMgr* windowManager;
GFXVideoMode* videoMode;
Expand Down
2 changes: 1 addition & 1 deletion engine/source/platformWin32/winInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ bool Input::isActive()
//------------------------------------------------------------------------------
void Input::process()
{
if (smManager && smManager->isEnabled() && smActive)
if (smManager && smManager->isEnabled() && smActive && winState.focused)
smManager->process();
}

Expand Down
71 changes: 48 additions & 23 deletions engine/source/platformWin32/winWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ static bool sgQueueEvents;
extern U16 DIK_to_Key(U8 dikCode);

// static helper variables
static bool windowActive = true;
static bool windowLocked = false;
static bool capsLockDown = false;

Expand Down Expand Up @@ -90,6 +89,7 @@ Win32PlatState::Win32PlatState()
desktopWidth = NULL;
desktopHeight = NULL;
currentTime = NULL;
focused = false;

windowManager = NULL;

Expand Down Expand Up @@ -211,7 +211,7 @@ static bool cursorInWindow()
if (Win32Window == NULL)
return false;

if (!windowActive)
if (!winState.focused)
return false;

if (Win32Window->mBorderless)
Expand Down Expand Up @@ -279,7 +279,7 @@ static void updateCursorVisibility()
static void setMouseClipping()
{
ClipCursor(NULL);
if (windowActive)
if (winState.focused)
{
setCursorVisible(false);

Expand Down Expand Up @@ -485,7 +485,7 @@ static S32 mouseY = 0xFFFFFFFF;
//--------------------------------------
static void CheckCursorPos()
{
if (windowLocked && windowActive)
if (windowLocked && winState.focused)
{
POINT mousePos;
GetCursorPos(&mousePos);
Expand Down Expand Up @@ -592,6 +592,39 @@ static void mouseWheelEvent(S32 delta)
}
}

//--------------------------------------
static void updateWindowFocus(HWND hWnd, bool newFocus, bool setMouseClip)
{
// Ensure the window is actually focused right now
bool currentFocus = GetForegroundWindow() == hWnd;
if (winState.focused == newFocus || currentFocus != newFocus)
return;

winState.focused = newFocus;
Con::printf("Window focused: %d", winState.focused);

if (winState.focused)
{
Input::activate();
}
else
{
DInputManager* mgr = dynamic_cast<DInputManager*>(Input::getManager());
if (!mgr || !mgr->isMouseActive())
{
// Deactivate all the mouse triggers:
for (U32 i = 0; i < 3; i++)
{
if (mouseButtonState[i])
mouseButtonEvent(SI_BREAK, KEY_BUTTON0 + i);
}
}
Input::deactivate();
}

if (windowLocked && setMouseClip)
setMouseClipping();
}

struct WinMessage
{
Expand Down Expand Up @@ -635,11 +668,9 @@ static LRESULT PASCAL WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM
break;
case WM_ACTIVATE:
setCursorVisible(false);
windowActive = LOWORD(wParam) != WA_INACTIVE;
if (windowActive)
if (LOWORD(wParam) != WA_INACTIVE)
{
Game->refreshWindow();
Input::activate();

if (Win32Window)
{
Expand All @@ -650,22 +681,16 @@ static LRESULT PASCAL WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM
}
}
}
else
{
DInputManager* mgr = dynamic_cast<DInputManager*>(Input::getManager());
if (!mgr || !mgr->isMouseActive())
{
// Deactivate all the mouse triggers:
for (U32 i = 0; i < 3; i++)
{
if (mouseButtonState[i])
mouseButtonEvent(SI_BREAK, KEY_BUTTON0 + i);
}
}
Input::deactivate();
}
if (windowLocked)
setMouseClipping();
updateWindowFocus(hWnd, LOWORD(wParam) != WA_INACTIVE, true);
break;
case WM_SETFOCUS:
updateWindowFocus(hWnd, true, true);
break;
case WM_KILLFOCUS:
updateWindowFocus(hWnd, false, true);
break;
case WM_NCACTIVATE:
updateWindowFocus(hWnd, wParam, false);
break;

case WM_MOVE:
Expand Down

0 comments on commit d923b90

Please sign in to comment.