Debian Codegraph @

src/pygame_sdl2/mouse.pyx

raw
1 # Copyright 2014 Patrick Dawson <[email protected]>
2 #
3 # This software is provided 'as-is', without any express or implied
4 # warranty. In no event will the authors be held liable for any damages
5 # arising from the use of this software.
6 #
7 # Permission is granted to anyone to use this software for any purpose,
8 # including commercial applications, and to alter it and redistribute it
9 # freely, subject to the following restrictions:
10 #
11 # 1. The origin of this software must not be misrepresented; you must not
12 # claim that you wrote the original software. If you use this software
13 # in a product, an acknowledgment in the product documentation would be
14 # appreciated but is not required.
15 # 2. Altered source versions must be plainly marked as such, and must not be
16 # misrepresented as being the original software.
17 # 3. This notice may not be removed or altered from any source distribution.
18
19 from sdl2 cimport *
20 from pygame_sdl2.display cimport main_window
21 from pygame_sdl2.surface cimport Surface
22
23 def init():
24 global active_cursor
25 active_cursor = None
26
27 def quit():
28 global active_cursor
29 active_cursor = None
30
31 def reset():
32 SDL_SetCursor(SDL_GetDefaultCursor())
33 global active_cursor
34 active_cursor = None
35
36 def get_pressed():
37 cdef Uint32 state = SDL_GetMouseState(NULL, NULL)
38 return (1 if state & SDL_BUTTON_LMASK else 0,
39 1 if state & SDL_BUTTON_MMASK else 0,
40 1 if state & SDL_BUTTON_RMASK else 0)
41
42 def get_pos():
43 cdef int x, y
44 SDL_GetMouseState(&x, &y)
45 return (x, y)
46
47 def get_rel():
48 cdef int x, y
49 SDL_GetRelativeMouseState(&x, &y)
50 return (x, y)
51
52 def set_pos(pos):
53 (x, y) = pos
54 SDL_WarpMouseInWindow(NULL, x, y)
55
56 def set_visible(visible):
57 SDL_ShowCursor(1 if visible else 0)
58
59 if SDL_GetWindowGrab(main_window.window) == SDL_ENABLE:
60 SDL_SetRelativeMouseMode(SDL_FALSE if visible else SDL_TRUE)
61
62 def get_focused():
63 return SDL_GetMouseFocus() != NULL
64
65 def set_cursor(size, hotspot, xormasks, andmasks):
66 # Does anyone use this? SDL2 has much improved custom cursor support.
67 pass
68
69 def get_cursor():
70 return None
71
72 # The ColorCursor that is currently in use.
73 active_cursor = False
74
75 cdef class ColorCursor(object):
76
77 cdef SDL_Cursor *cursor
78
79 def __init__(self, Surface surface, x, y):
80 self.cursor = SDL_CreateColorCursor(surface.surface, x, y)
81
82 def __dealloc__(self):
83 SDL_FreeCursor(self.cursor)
84
85 def activate(self):
86 global active_cursor
87 if active_cursor is not self:
88 active_cursor = self
89 SDL_SetCursor(self.cursor)
90