Changeset - r13472:90d554b485e4
[Not reviewed]
master
0 1 0
peter1138 - 15 years ago 2009-11-07 21:41:41
peter1138@openttd.org
(svn r18001) -Codechange: [SDL] When the mouse cursor is locked into position when scrolling a viewport, warp the mouse pointer to the centre of the window. This gives maximum freedom of movement. The pointer position is restored when the lock is removed. Visually the mouse cursor stays where it was.
1 file changed with 17 insertions and 3 deletions:
0 comments (0 inline, 0 general)
src/video/sdl_v.cpp
Show inline comments
 
@@ -20,24 +20,26 @@
 
#include "../blitter/factory.hpp"
 
#include "../network/network.h"
 
#include "../functions.h"
 
#include "../thread/thread.h"
 
#include "../genworld.h"
 
#include "sdl_v.h"
 
#include <SDL.h>
 

	
 
static FVideoDriver_SDL iFVideoDriver_SDL;
 

	
 
static SDL_Surface *_sdl_screen;
 
static bool _all_modes;
 
/** Flag used to determine if _cursor.fix_at has changed. */
 
static bool _last_fix_at;
 

	
 
/** Whether the drawing is/may be done in a separate thread. */
 
static bool _draw_threaded;
 
/** Thread used to 'draw' to the screen, i.e. push data to the screen. */
 
static ThreadObject *_draw_thread = NULL;
 
/** Mutex to keep the access to the shared memory controlled. */
 
static ThreadMutex *_draw_mutex = NULL;
 
/** Should we keep continue drawing? */
 
static volatile bool _draw_continue;
 

	
 
#define MAX_DIRTY_RECTS 100
 
static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS];
 
@@ -354,35 +356,47 @@ static uint32 ConvertSdlKeyIntoMy(SDL_ke
 
	if (sym->mod & KMOD_META)  key |= WKC_META;
 
	if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
 
	if (sym->mod & KMOD_CTRL)  key |= WKC_CTRL;
 
	if (sym->mod & KMOD_ALT)   key |= WKC_ALT;
 

	
 
	return (key << 16) + sym->unicode;
 
}
 

	
 
static int PollEvent()
 
{
 
	SDL_Event ev;
 

	
 
	if (_cursor.fix_at != _last_fix_at) {
 
		_last_fix_at = _cursor.fix_at;
 
		if (_last_fix_at) {
 
			/* Move to centre of window */
 
			SDL_CALL SDL_WarpMouse(_screen.width / 2, _screen.height / 2);
 

	
 
		} else {
 
			/* Restore position */
 
			SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
 
		}
 
	}
 

	
 
	if (!SDL_CALL SDL_PollEvent(&ev)) return -2;
 

	
 
	switch (ev.type) {
 
		case SDL_MOUSEMOTION:
 
			if (_cursor.fix_at) {
 
				int dx = ev.motion.x - _cursor.pos.x;
 
				int dy = ev.motion.y - _cursor.pos.y;
 
				int dx = ev.motion.x - _screen.width / 2;
 
				int dy = ev.motion.y - _screen.height / 2;
 
				if (dx != 0 || dy != 0) {
 
					_cursor.delta.x += dx;
 
					_cursor.delta.y += dy;
 
					SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
 
					SDL_CALL SDL_WarpMouse(_screen.width / 2, _screen.height / 2);
 
				}
 
			} else {
 
				_cursor.delta.x = ev.motion.x - _cursor.pos.x;
 
				_cursor.delta.y = ev.motion.y - _cursor.pos.y;
 
				_cursor.pos.x = ev.motion.x;
 
				_cursor.pos.y = ev.motion.y;
 
				_cursor.dirty = true;
 
			}
 
			HandleMouseEvents();
 
			break;
 

	
 
		case SDL_MOUSEBUTTONDOWN:
0 comments (0 inline, 0 general)