forked from veandco/go-sdl2
/
mouse.go
209 lines (176 loc) · 6.86 KB
/
mouse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package sdl
/*
#include "sdl_wrapper.h"
#if defined(__WIN32)
#include <SDL2/SDL_syswm.h>
#else
#include <SDL_syswm.h>
#endif
#if !(SDL_VERSION_ATLEAST(2,0,4))
#pragma message("SDL_CaptureMouse is not supported before SDL 2.0.4")
static int SDL_CaptureMouse(SDL_bool enabled)
{
return -1;
}
#pragma message("SDL_MOUSEWHEEL_NORMAL is not supported before SDL 2.0.4")
#define SDL_MOUSEWHEEL_NORMAL (0)
#pragma message("SDL_MOUSEWHEEL_FLIPPED is not supported before SDL 2.0.4")
#define SDL_MOUSEWHEEL_FLIPPED (0)
#endif
*/
import "C"
import "unsafe"
// Cursor types for CreateSystemCursor()
const (
SYSTEM_CURSOR_ARROW = C.SDL_SYSTEM_CURSOR_ARROW // arrow
SYSTEM_CURSOR_IBEAM = C.SDL_SYSTEM_CURSOR_IBEAM // i-beam
SYSTEM_CURSOR_WAIT = C.SDL_SYSTEM_CURSOR_WAIT // wait
SYSTEM_CURSOR_CROSSHAIR = C.SDL_SYSTEM_CURSOR_CROSSHAIR // crosshair
SYSTEM_CURSOR_WAITARROW = C.SDL_SYSTEM_CURSOR_WAITARROW // small wait cursor (or wait if not available)
SYSTEM_CURSOR_SIZENWSE = C.SDL_SYSTEM_CURSOR_SIZENWSE // double arrow pointing northwest and southeast
SYSTEM_CURSOR_SIZENESW = C.SDL_SYSTEM_CURSOR_SIZENESW // double arrow pointing northeast and southwest
SYSTEM_CURSOR_SIZEWE = C.SDL_SYSTEM_CURSOR_SIZEWE // double arrow pointing west and east
SYSTEM_CURSOR_SIZENS = C.SDL_SYSTEM_CURSOR_SIZENS // double arrow pointing north and south
SYSTEM_CURSOR_SIZEALL = C.SDL_SYSTEM_CURSOR_SIZEALL // four pointed arrow pointing north, south, east, and west
SYSTEM_CURSOR_NO = C.SDL_SYSTEM_CURSOR_NO // slashed circle or crossbones
SYSTEM_CURSOR_HAND = C.SDL_SYSTEM_CURSOR_HAND // hand
NUM_SYSTEM_CURSORS = C.SDL_NUM_SYSTEM_CURSORS // (only for bounding internal arrays)
)
// Scroll direction types for the Scroll event
const (
MOUSEWHEEL_NORMAL = C.SDL_MOUSEWHEEL_NORMAL // the scroll direction is normal
MOUSEWHEEL_FLIPPED = C.SDL_MOUSEWHEEL_FLIPPED // the scroll direction is flipped / natural
)
// Used as a mask when testing buttons in buttonstate.
const (
BUTTON_LEFT = C.SDL_BUTTON_LEFT // left mouse button
BUTTON_MIDDLE = C.SDL_BUTTON_MIDDLE // middle mouse button
BUTTON_RIGHT = C.SDL_BUTTON_RIGHT // right mouse button
BUTTON_X1 = C.SDL_BUTTON_X1 // x1 mouse button
BUTTON_X2 = C.SDL_BUTTON_X2 // x2 mouse button
)
// Cursor is a custom cursor created by CreateCursor() or CreateColorCursor().
type Cursor C.SDL_Cursor
// SystemCursor is a system cursor created by CreateSystemCursor().
type SystemCursor C.SDL_SystemCursor
func (c *Cursor) cptr() *C.SDL_Cursor {
return (*C.SDL_Cursor)(unsafe.Pointer(c))
}
func (c SystemCursor) c() C.SDL_SystemCursor {
return C.SDL_SystemCursor(c)
}
// GetMouseFocus returns the window which currently has mouse focus.
// (https://wiki.libsdl.org/SDL_GetMouseFocus)
func GetMouseFocus() *Window {
return (*Window)(unsafe.Pointer(C.SDL_GetMouseFocus()))
}
// GetMouseState returns the current state of the mouse.
// (https://wiki.libsdl.org/SDL_GetMouseState)
func GetMouseState() (x, y int32, state uint32) {
var _x, _y C.int
_state := uint32(C.SDL_GetMouseState(&_x, &_y))
return int32(_x), int32(_y), _state
}
// GetRelativeMouseState returns the relative state of the mouse.
// (https://wiki.libsdl.org/SDL_GetRelativeMouseState)
func GetRelativeMouseState() (x, y int32, state uint32) {
var _x, _y C.int
_state := uint32(C.SDL_GetRelativeMouseState(&_x, &_y))
return int32(_x), int32(_y), _state
}
// WarpMouseInWindow moves the mouse to the given position within the window.
// (https://wiki.libsdl.org/SDL_WarpMouseInWindow)
func (window *Window) WarpMouseInWindow(x, y int32) {
C.SDL_WarpMouseInWindow(window.cptr(), C.int(x), C.int(y))
}
// SetRelativeMouseMode sets relative mouse mode.
// (https://wiki.libsdl.org/SDL_SetRelativeMouseMode)
func SetRelativeMouseMode(enabled bool) int {
return int(C.SDL_SetRelativeMouseMode(C.SDL_bool(Btoi(enabled))))
}
// GetRelativeMouseMode reports where relative mouse mode is enabled.
// (https://wiki.libsdl.org/SDL_GetRelativeMouseMode)
func GetRelativeMouseMode() bool {
return C.SDL_GetRelativeMouseMode() > 0
}
// CreateCursor creates a cursor using the specified bitmap data and mask (in MSB format).
// (https://wiki.libsdl.org/SDL_CreateCursor)
func CreateCursor(data, mask *uint8, w, h, hotX, hotY int32) *Cursor {
_data := (*C.Uint8)(unsafe.Pointer(data))
_mask := (*C.Uint8)(unsafe.Pointer(mask))
return (*Cursor)(C.SDL_CreateCursor(_data, _mask, C.int(w), C.int(h), C.int(hotX), C.int(hotY)))
}
// CreateColorCursor creates a color cursor.
// (https://wiki.libsdl.org/SDL_CreateColorCursor)
func CreateColorCursor(surface *Surface, hotX, hotY int32) *Cursor {
return (*Cursor)(C.SDL_CreateColorCursor(surface.cptr(), C.int(hotX), C.int(hotY)))
}
// CreateSystemCursor creates a system cursor.
// (https://wiki.libsdl.org/SDL_CreateSystemCursor)
func CreateSystemCursor(id SystemCursor) *Cursor {
return (*Cursor)(C.SDL_CreateSystemCursor(id.c()))
}
// SetCursor sets the active cursor.
// (https://wiki.libsdl.org/SDL_SetCursor)
func SetCursor(cursor *Cursor) {
C.SDL_SetCursor(cursor.cptr())
}
// GetCursor returns the active cursor.
// (https://wiki.libsdl.org/SDL_GetCursor)
func GetCursor() *Cursor {
return (*Cursor)(C.SDL_GetCursor())
}
// GetDefaultCursor returns the default cursor.
// (https://wiki.libsdl.org/SDL_GetDefaultCursor)
func GetDefaultCursor() *Cursor {
return (*Cursor)(C.SDL_GetDefaultCursor())
}
// FreeCursor frees a cursor created with CreateCursor(), CreateColorCursor() or CreateSystemCursor().
// (https://wiki.libsdl.org/SDL_FreeCursor)
func FreeCursor(cursor *Cursor) {
C.SDL_FreeCursor(cursor.cptr())
}
// ShowCursor toggles whether or not the cursor is shown.
// (https://wiki.libsdl.org/SDL_ShowCursor)
func ShowCursor(toggle int) (int, error) {
i := int(C.SDL_ShowCursor(C.int(toggle)))
return i, errorFromInt(i)
}
// CaptureMouse captures the mouse and tracks input outside an SDL window.
// (https://wiki.libsdl.org/SDL_CaptureMouse)
func CaptureMouse(toggle bool) error {
var ierr C.int
if toggle {
ierr = C.SDL_CaptureMouse(C.SDL_TRUE)
} else {
ierr = C.SDL_CaptureMouse(C.SDL_FALSE)
}
if ierr != 0 {
return GetError()
}
return nil
}
// Button is used as a mask when testing buttons in buttonstate.
func Button(flag uint32) uint32 {
return 1 << (flag - 1)
}
// ButtonLMask is used as a mask when testing buttons in buttonstate.
func ButtonLMask() uint32 {
return Button(BUTTON_LEFT)
}
// ButtonMMask is used as a mask when testing buttons in buttonstate.
func ButtonMMask() uint32 {
return Button(BUTTON_MIDDLE)
}
// ButtonRMask is used as a mask when testing buttons in buttonstate.
func ButtonRMask() uint32 {
return Button(BUTTON_RIGHT)
}
// ButtonX1Mask is used as a mask when testing buttons in buttonstate.
func ButtonX1Mask() uint32 {
return Button(BUTTON_X1)
}
// ButtonX2Mask is used as a mask when testing buttons in buttonstate.
func ButtonX2Mask() uint32 {
return Button(BUTTON_X2)
}