From 045e81809ad33a8e6a2165d657f2a2a075acd5f7 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Sat, 3 Jun 2023 23:10:16 +0200 Subject: [PATCH] Codechange: remove queue_wrap / last_position from mouse movement No backend uses it anymore, so also no longer any need to support it. --- src/gfx.cpp | 40 ++++++------------------------------ src/gfx_type.h | 6 +----- src/video/allegro_v.cpp | 2 +- src/video/cocoa/cocoa_wnd.mm | 2 +- src/video/sdl2_v.cpp | 2 +- src/video/sdl_v.cpp | 2 +- src/video/win32_v.cpp | 2 +- 7 files changed, 12 insertions(+), 44 deletions(-) diff --git a/src/gfx.cpp b/src/gfx.cpp index eeafa04c06..c708a4c8e6 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -1959,50 +1959,22 @@ void CursorVars::UpdateCursorPositionRelative(int delta_x, int delta_y) * Update cursor position on mouse movement. * @param x New X position. * @param y New Y position. - * @param queued_warp True, if the OS queues mouse warps after pending mouse movement events. - * False, if the warp applies instantaneous. * @return true, if the OS cursor position should be warped back to this->pos. */ -bool CursorVars::UpdateCursorPosition(int x, int y, bool queued_warp) +bool CursorVars::UpdateCursorPosition(int x, int y) { - /* Detecting relative mouse movement is somewhat tricky. - * - There may be multiple mouse move events in the video driver queue (esp. when OpenTTD lags a bit). - * - When we request warping the mouse position (return true), a mouse move event is appended at the end of the queue. - * - * So, when this->fix_at is active, we use the following strategy: - * - The first movement triggers the warp to reset the mouse position. - * - Subsequent events have to compute movement relative to the previous event. - * - The relative movement is finished, when we receive the event matching the warp. - */ + this->delta.x = x - this->pos.x; + this->delta.y = y - this->pos.y; - if (x == this->pos.x && y == this->pos.y) { - /* Warp finished. */ - this->queued_warp = false; - } - - this->delta.x = x - (this->queued_warp ? this->last_position.x : this->pos.x); - this->delta.y = y - (this->queued_warp ? this->last_position.y : this->pos.y); - - this->last_position.x = x; - this->last_position.y = y; - - bool need_warp = false; if (this->fix_at) { - if (this->delta.x != 0 || this->delta.y != 0) { - /* Trigger warp. - * Note: We also trigger warping again, if there is already a pending warp. - * This makes it more tolerant about the OS or other software in between - * botchering the warp. */ - this->queued_warp = queued_warp; - need_warp = true; - } + return this->delta.x != 0 || this->delta.y != 0; } else if (this->pos.x != x || this->pos.y != y) { - this->queued_warp = false; // Cancel warping, we are no longer confining the position. this->dirty = true; this->pos.x = x; this->pos.y = y; } - return need_warp; + + return false; } bool ChangeResInGame(int width, int height) diff --git a/src/gfx_type.h b/src/gfx_type.h index b5a6fdbdcf..4fc96824e2 100644 --- a/src/gfx_type.h +++ b/src/gfx_type.h @@ -144,11 +144,7 @@ struct CursorVars { bool vehchain; ///< vehicle chain is dragged void UpdateCursorPositionRelative(int delta_x, int delta_y); - bool UpdateCursorPosition(int x, int y, bool queued_warp); - -private: - bool queued_warp; - Point last_position; + bool UpdateCursorPosition(int x, int y); }; /** Data about how and where to blit pixels. */ diff --git a/src/video/allegro_v.cpp b/src/video/allegro_v.cpp index 3a0a4beb01..70418d1966 100644 --- a/src/video/allegro_v.cpp +++ b/src/video/allegro_v.cpp @@ -390,7 +390,7 @@ bool VideoDriver_Allegro::PollEvent() } /* Mouse movement */ - if (_cursor.UpdateCursorPosition(mouse_x, mouse_y, false)) { + if (_cursor.UpdateCursorPosition(mouse_x, mouse_y)) { position_mouse(_cursor.pos.x, _cursor.pos.y); } if (_cursor.delta.x != 0 || _cursor.delta.y) mouse_action = true; diff --git a/src/video/cocoa/cocoa_wnd.mm b/src/video/cocoa/cocoa_wnd.mm index d4fc5b292e..03adc1cab7 100644 --- a/src/video/cocoa/cocoa_wnd.mm +++ b/src/video/cocoa/cocoa_wnd.mm @@ -659,7 +659,7 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel _cursor.UpdateCursorPositionRelative(event.deltaX * self.getContentsScale, event.deltaY * self.getContentsScale); } else { NSPoint pt = [ self mousePositionFromEvent:event ]; - _cursor.UpdateCursorPosition(pt.x, pt.y, false); + _cursor.UpdateCursorPosition(pt.x, pt.y); } HandleMouseEvents(); diff --git a/src/video/sdl2_v.cpp b/src/video/sdl2_v.cpp index 51271c2b30..de191c5373 100644 --- a/src/video/sdl2_v.cpp +++ b/src/video/sdl2_v.cpp @@ -384,7 +384,7 @@ bool VideoDriver_SDL_Base::PollEvent() } } - if (_cursor.UpdateCursorPosition(x, y, false)) { + if (_cursor.UpdateCursorPosition(x, y)) { SDL_WarpMouseInWindow(this->sdl_window, _cursor.pos.x, _cursor.pos.y); } HandleMouseEvents(); diff --git a/src/video/sdl_v.cpp b/src/video/sdl_v.cpp index d01b17e194..54ef9de27a 100644 --- a/src/video/sdl_v.cpp +++ b/src/video/sdl_v.cpp @@ -490,7 +490,7 @@ bool VideoDriver_SDL::PollEvent() } } - if (_cursor.UpdateCursorPosition(x, y, false)) { + if (_cursor.UpdateCursorPosition(x, y)) { SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y); } HandleMouseEvents(); diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp index 8d8a78a0ca..b6f278c8eb 100644 --- a/src/video/win32_v.cpp +++ b/src/video/win32_v.cpp @@ -489,7 +489,7 @@ LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) } } - if (_cursor.UpdateCursorPosition(x, y, false)) { + if (_cursor.UpdateCursorPosition(x, y)) { POINT pt; pt.x = _cursor.pos.x; pt.y = _cursor.pos.y;