diff --git a/photon/mouse.py b/photon/mouse.py new file mode 100644 index 0000000..46c6643 --- /dev/null +++ b/photon/mouse.py @@ -0,0 +1,46 @@ +import ctypes +from .internal import _SDL, errcheck +from .enum import CEnum + +def get_mouse_state(): + x = ctypes.c_int() + y = ctypes.c_int() + buttons = _SDL.SDL_GetMouseState(ctypes.byref(x), ctypes.byref(y)) + return x.value, y.value, buttons + +def get_relative_mouse_state(): + x = ctypes.c_int() + y = ctypes.c_int() + buttons = _SDL.SDL_GetRelativeMouseState(ctypes.byref(x), ctypes.byref(y)) + return x.value, y.value, buttons + +def get_relative_mouse_mode(): + return _SDL.SDL_GetRelativeMouseMode() == 1 + +def set_relative_mouse_mode(enabled): + errcheck(_SDL.SDL_SetRelativeMouseMode(enabled)) + +def set_cursor_visibility(visible): + errcheck(_SDL.SDL_ShowCursor(visible)) + +def is_cursor_visible(): + return errcheck(_SDL.SDL_ShowCursor(-1)) == 1 + +def warp_mouse_in_window(x, y, window=0): + if window: + window = window._handle + _SDL.SDL_WarpMouseInWindow(window, x, y) + +class Button(CEnum): + LEFT = 1 + MIDDLE = 2 + RIGHT = 3 + X1 = 4 + X2 = 5 + + # masks for comparison against get_mouse_state return values + LMASK = 1 + MMASK = 2 + RMASK = 4 + X1MASK = 8 + X2MASK = 16 diff --git a/photon/tests/test_mouse.py b/photon/tests/test_mouse.py new file mode 100644 index 0000000..2cc3278 --- /dev/null +++ b/photon/tests/test_mouse.py @@ -0,0 +1,27 @@ +from .. import init, InitFlags +from ..internal import SDLError +from ..mouse import (get_relative_mouse_mode, set_relative_mouse_mode, + set_cursor_visibility, is_cursor_visible) +from nose.tools import with_setup + +def init_everything(): + init(InitFlags.EVERYTHING) + +@with_setup(init_everything) +def test_relative_mouse_mode(): + try: + set_relative_mouse_mode(True) + assert get_relative_mouse_mode() == True + set_relative_mouse_mode(False) + assert get_relative_mouse_mode() == False + except SDLError: + pass # sometimes this method isn't supported + +@with_setup(init_everything) +def test_cursor_visiblity(): + assert is_cursor_visible() == True + set_cursor_visibility(False) + assert is_cursor_visible() == False + set_cursor_visibility(True) + assert is_cursor_visible() == True +