Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(win32mouse): stop edge scrolling when the pointer leaves the window

Olcay Seygan968d61c1e983
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bb658da79..45c50f36e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -514,7 +514,7 @@ found and fixed — EA's own, not port damage.**
- The far edge of the view opens with the height, instead of stopping at a distance fixed for the stock zoom - which is what put black beyond the terrain when you zoom out past it.
- The box on the radar follows the camera when you pan. It only ever redrew itself when the zoom or the angle changed, so scrolling left it behind.
- The camera turns in whole 45-degree steps, instantly, while you hold the key.
-- Edge scrolling works windowed, and scroll speed no longer follows your frame rate.
+- Edge scrolling works windowed, and scroll speed no longer follows your frame rate. It also stops when you take the pointer off the window. Windows says nothing about a cursor that has walked out onto the desktop, so the game kept reading the last edge it crossed and the map slid on its own until you brought the mouse back. A game that has only just opened asks Windows where the pointer actually is instead of assuming the top left corner, which is an edge like any other and used to drag the camera there before you had touched anything.
- Taking over another base no longer squeezes the picture into the top four fifths of the screen.
- A skirmish opens zoomed all the way out (`StartAtMaxZoom = No` restores the old opening).
- Hold Ctrl and roll the wheel to turn a building before placing it.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/Mouse.h b/GeneralsMD/Code/GameEngine/Include/GameClient/Mouse.h
index ed857a9c9..cdb33363b 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/Mouse.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/Mouse.h
@@ -272,6 +272,12 @@ public:
virtual RedrawMode getRedrawMode(void) { return m_currentRedrawMode; } //get cursor drawing method
virtual void setVisibility(Bool visible) { m_visible = visible; } // set visibility for load screens, etc
inline Bool getVisibility(void) { return m_visible; } // get visibility state
+
+ /** Is the pointer over the game window at all? A device that cannot tell says yes, which is
+ * the fullscreen answer anyway; the windowed devices override this. Edge scrolling is the
+ * caller that needs it: the last reported position sits on an edge for as long as the cursor
+ * is away, and reading it as a live position scrolls the map with nobody at the mouse. */
+ virtual Bool isCursorInWindow( void ) const { return TRUE; }
void drawTooltip( void ); ///< draw the tooltip text
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp
index ded5d5044..6f0dcac3a 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp
@@ -387,22 +387,27 @@ GameMessageDisposition LookAtTranslator::translateGameMessage(const GameMessage
}
// retail disables edge scrolling entirely in a window; EdgeScrollInWindowedMode in
- // Options.ini turns it back on for people who play windowed or borderless.
- if (!TheGlobalData->m_windowed || TheGlobalData->m_edgeScrollInWindowedMode)
+ // Options.ini turns it back on for people who play windowed or borderless. Either way
+ // the pointer has to be over the window: the position below is the last one the mouse
+ // device was told about, and a pointer that has left a windowed game left through an
+ // edge, so believing it would scroll the map for as long as the mouse sat on the desktop.
+ const Bool edgeScrollAllowed = (!TheGlobalData->m_windowed || TheGlobalData->m_edgeScrollInWindowedMode)
+ && TheMouse->isCursorInWindow();
+
+ if (m_isScrolling)
{
- if (m_isScrolling)
+ if ( m_scrollType == SCROLL_SCREENEDGE
+ && (!edgeScrollAllowed
+ || (m_currentPos.x >= edgeScrollSize && m_currentPos.y >= edgeScrollSize && m_currentPos.y < height-edgeScrollSize && m_currentPos.x < width-edgeScrollSize)) )
{
- if ( m_scrollType == SCROLL_SCREENEDGE && (m_currentPos.x >= edgeScrollSize && m_currentPos.y >= edgeScrollSize && m_currentPos.y < height-edgeScrollSize && m_currentPos.x < width-edgeScrollSize) )
- {
- stopScrolling();
- }
+ stopScrolling();
}
- else
+ }
+ else if (edgeScrollAllowed)
+ {
+ if ( m_currentPos.x < edgeScrollSize || m_currentPos.y < edgeScrollSize || m_currentPos.y >= height-edgeScrollSize || m_currentPos.x >= width-edgeScrollSize )
{
- if ( m_currentPos.x < edgeScrollSize || m_currentPos.y < edgeScrollSize || m_currentPos.y >= height-edgeScrollSize || m_currentPos.x >= width-edgeScrollSize )
- {
- setScrolling(SCROLL_SCREENEDGE);
- }
+ setScrolling(SCROLL_SCREENEDGE);
}
}
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/Win32Device/GameClient/Win32Mouse.h b/GeneralsMD/Code/GameEngineDevice/Include/Win32Device/GameClient/Win32Mouse.h
index 3e8498d99..d67863c00 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/Win32Device/GameClient/Win32Mouse.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/Win32Device/GameClient/Win32Mouse.h
@@ -79,6 +79,8 @@ public:
virtual void setVisibility(Bool visible);
+ virtual Bool isCursorInWindow( void ) const { return m_cursorInWindow; }
+
/// add an event from a win32 window procedure
void addWin32Event( UINT msg, WPARAM wParam, LPARAM lParam, DWORD time );
void lostFocus (Bool state) { m_lostFocus = state;}
@@ -106,7 +108,9 @@ protected:
incremented to the next index */
MouseCursor m_currentWin32Cursor; ///< keep track of last cursor image sent to D3D.
Int m_directionFrame; ///< current frame of directional cursor (frome 0 points up).
- Bool m_lostFocus; ///< flag if window has lost focues and mouse should stop being updated.
+ Bool m_lostFocus; ///< flag if window has lost focues and mouse should stop being updated.
+ Bool m_cursorInWindow; ///< is the pointer over our client area right now, asked of Windows each frame
+ Bool m_positionReported; ///< has a window message ever told us where the pointer is
}; // end Win32Mouse
// INLINING ///////////////////////////////////////////////////////////////////
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/Win32Device/GameClient/Win32Mouse.cpp b/GeneralsMD/Code/GameEngineDevice/Source/Win32Device/GameClient/Win32Mouse.cpp
index 7ab4a9570..2dd22b14d 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/Win32Device/GameClient/Win32Mouse.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/Win32Device/GameClient/Win32Mouse.cpp
@@ -281,6 +281,8 @@ Win32Mouse::Win32Mouse( void )
cursorResources[i][j]=NULL;
m_directionFrame=0; //points up.
m_lostFocus = FALSE;
+ m_cursorInWindow = TRUE;
+ m_positionReported = FALSE;
} // end Win32Mouse
//-------------------------------------------------------------------------------------------------
@@ -328,7 +330,34 @@ void Win32Mouse::reset( void )
void Win32Mouse::update( void )
{
- // extend
+ //
+ // Windows reports the pointer to us only while it is over our client area, and only when it
+ // moves. Two silences come out of that and both of them scroll the camera by themselves. A
+ // pointer that walks out of a windowed game leaves m_currMouse.pos frozen on the edge it left
+ // by, and the screen-edge scroll reads that position every frame afterwards. A game that has
+ // just started has been parked at (0,0) by GameClient::init and nobody has moved the mouse yet,
+ // so the pointer is believed to be in the top left corner, which is also an edge. Ask Windows
+ // where the thing actually is instead of trusting the last thing it said.
+ //
+ POINT screenPos;
+ POINT clientPos;
+ RECT client;
+
+ ::GetCursorPos( &screenPos );
+ clientPos = screenPos;
+ ::ScreenToClient( ApplicationHWnd, &clientPos );
+ ::GetClientRect( ApplicationHWnd, &client );
+
+ // WindowFromPoint, not the rectangle alone: another application's window sitting over ours owns
+ // the pointer that is inside our rectangle, and the map should not scroll under it.
+ m_cursorInWindow = ::PtInRect( &client, clientPos ) && ::WindowFromPoint( screenPos ) == ApplicationHWnd;
+
+ // until the first window message tells us where the pointer is, Windows is the only one who
+ // knows, and the (0,0) we were given at init is a corner nobody put the mouse in.
+ if( m_positionReported == FALSE && m_cursorInWindow )
+ setPosition( clientPos.x, clientPos.y );
+
+ // extend
Mouse::update();
} // end update
@@ -348,6 +377,10 @@ void Win32Mouse::addWin32Event( UINT msg, WPARAM wParam, LPARAM lParam, DWORD ti
if( m_eventBuffer[ m_nextFreeIndex ].msg != 0 )
return;
+ // every one of these carries a position, so from here on the event stream owns where the
+ // pointer is and update() stops seeding it from the OS
+ m_positionReported = TRUE;
+
// add to this index
m_eventBuffer[ m_nextFreeIndex ].msg = msg;
m_eventBuffer[ m_nextFreeIndex ].wParam = wParam;