Changeset - r27487:4d1c2a7bde00
[Not reviewed]
master
0 7 0
Patric Stout - 13 months ago 2023-06-03 21:10:16
truebrain@openttd.org
Codechange: remove queue_wrap / last_position from mouse movement

No backend uses it anymore, so also no longer any need to support
it.
7 files changed with 12 insertions and 44 deletions:
0 comments (0 inline, 0 general)
src/gfx.cpp
Show inline comments
 
@@ -1894,56 +1894,28 @@ void CursorVars::UpdateCursorPositionRel
 
}
 

	
 
/**
 
 * 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.
 
	 */
 

	
 
	if (x == this->pos.x && y == this->pos.y) {
 
		/* Warp finished. */
 
		this->queued_warp = false;
 
	}
 
	this->delta.x = x - this->pos.x;
 
	this->delta.y = y - this->pos.y;
 

	
 
	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)
 
{
 
	return (_screen.width == width && _screen.height == height) || VideoDriver::GetInstance()->ChangeResolution(width, height);
 
}
src/gfx_type.h
Show inline comments
 
@@ -141,17 +141,13 @@ struct CursorVars {
 
	bool in_window;               ///< mouse inside this window, determines drawing logic
 

	
 
	/* Drag data */
 
	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. */
 
struct DrawPixelInfo {
 
	void *dst_ptr;
 
	int left, top, width, height;
src/video/allegro_v.cpp
Show inline comments
 
@@ -388,13 +388,13 @@ bool VideoDriver_Allegro::PollEvent()
 
		}
 
		prev_button_state = mouse_b;
 
		mouse_action = true;
 
	}
 

	
 
	/* 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;
 

	
 
	static int prev_mouse_z = 0;
 
	if (prev_mouse_z != mouse_z) {
src/video/cocoa/cocoa_wnd.mm
Show inline comments
 
@@ -655,13 +655,13 @@ void CocoaDialog(const char *title, cons
 
- (void)internalMouseMoveEvent:(NSEvent *)event
 
{
 
	if (_cursor.fix_at) {
 
		_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();
 
}
 

	
 
- (void)internalMouseButtonEvent
src/video/sdl2_v.cpp
Show inline comments
 
@@ -381,13 +381,13 @@ bool VideoDriver_SDL_Base::PollEvent()
 
				while (SDL_PeepEvents(&ev, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION)) {
 
					x = ev.motion.x;
 
					y = ev.motion.y;
 
				}
 
			}
 

	
 
			if (_cursor.UpdateCursorPosition(x, y, false)) {
 
			if (_cursor.UpdateCursorPosition(x, y)) {
 
				SDL_WarpMouseInWindow(this->sdl_window, _cursor.pos.x, _cursor.pos.y);
 
			}
 
			HandleMouseEvents();
 
			break;
 
		}
 

	
src/video/sdl_v.cpp
Show inline comments
 
@@ -488,13 +488,13 @@ bool VideoDriver_SDL::PollEvent()
 
				while (SDL_PeepEvents(&ev, 1, SDL_GETEVENT, SDL_MOUSEMOTION)) {
 
					x = ev.motion.x;
 
					y = ev.motion.y;
 
				}
 
			}
 

	
 
			if (_cursor.UpdateCursorPosition(x, y, false)) {
 
			if (_cursor.UpdateCursorPosition(x, y)) {
 
				SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
 
			}
 
			HandleMouseEvents();
 
			break;
 
		}
 

	
src/video/win32_v.cpp
Show inline comments
 
@@ -487,13 +487,13 @@ LRESULT CALLBACK WndProcGdi(HWND hwnd, U
 
				while (PeekMessage(&m, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE | PM_NOYIELD | PM_QS_INPUT)) {
 
					x = (int16)LOWORD(m.lParam);
 
					y = (int16)HIWORD(m.lParam);
 
				}
 
			}
 

	
 
			if (_cursor.UpdateCursorPosition(x, y, false)) {
 
			if (_cursor.UpdateCursorPosition(x, y)) {
 
				POINT pt;
 
				pt.x = _cursor.pos.x;
 
				pt.y = _cursor.pos.y;
 
				ClientToScreen(hwnd, &pt);
 
				SetCursorPos(pt.x, pt.y);
 
			}
0 comments (0 inline, 0 general)