-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmouse.go
134 lines (107 loc) · 2.4 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
package sdl
import (
"errors"
"unsafe"
)
// #include <SDL.h>
//
// #include "mouse.h"
import "C"
type Cursor struct {
c *C.SDL_Cursor
}
func GetMouseFocus() *Window {
return &Window{C.SDL_GetMouseFocus()}
}
func GetMouseState() (state uint8, x, y int) {
var cx, cy C.int
cstate := C.SDL_GetMouseState(&cx, &cy)
return uint8(cstate), int(cx), int(cy)
}
func GetRelativeMouseState() (state uint8, x, y int) {
var cx, cy C.int
cstate := C.SDL_GetRelativeMouseState(&cx, &cy)
return uint8(cstate), int(cx), int(cy)
}
func WarpMouse(x, y int) {
C.SDL_WarpMouseInWindow(nil, C.int(x), C.int(y))
}
func (w *Window) WarpMouse(x, y int) {
C.SDL_WarpMouseInWindow(w.c, C.int(x), C.int(y))
}
func SetRelativeMouseMode(e bool) error {
ce := C.SDL_bool(C.SDL_FALSE)
if e {
ce = C.SDL_TRUE
}
if C.SDL_SetRelativeMouseMode(ce) != 0 {
return getError()
}
return nil
}
func GetRelativeMouseMode() bool {
return C.SDL_GetRelativeMouseMode() == C.SDL_TRUE
}
func CreateCursor(data []uint8, mask []uint8, w, h, hx, hy int) (*Cursor, error) {
wh := w * h
if len(data) < wh {
return nil, errors.New("len(data) < w * h")
}
if (len(mask) < wh) && (len(mask) > 0) {
return nil, errors.New("len(mask) < w * h and > 0")
}
var mp *C.Uint8
if len(mask) > 0 {
mp = (*C.Uint8)(unsafe.Pointer(&mask[0]))
}
c := C.SDL_CreateCursor(
(*C.Uint8)(unsafe.Pointer(&data[0])),
mp,
C.int(w),
C.int(h),
C.int(hx),
C.int(hy),
)
if c == nil {
return nil, getError()
}
return &Cursor{c}, nil
}
func (s *Surface) CreateColorCursor(hx, hy int) (*Cursor, error) {
c := C.SDL_CreateColorCursor(s.c(), C.int(hx), C.int(hy))
if c == nil {
return nil, getError()
}
return &Cursor{c}, nil
}
func (c *Cursor) Set() {
C.SDL_SetCursor(c.c)
}
func GetCursor() (*Cursor, error) {
c := C.SDL_GetCursor()
if c == nil {
return nil, getError()
}
return &Cursor{c}, nil
}
func (c *Cursor) Free() {
C.SDL_FreeCursor(c.c)
}
func ShowCursor(t int) bool {
return C.SDL_ShowCursor(C.int(t)) != 0
}
func BUTTON(x uint8) uint8 {
return uint8(C.BUTTON(C.Uint8(x)))
}
const (
BUTTON_LEFT = C.SDL_BUTTON_LEFT
BUTTON_MIDDLE = C.SDL_BUTTON_MIDDLE
BUTTON_RIGHT = C.SDL_BUTTON_RIGHT
BUTTON_X1 = C.SDL_BUTTON_X1
BUTTON_X2 = C.SDL_BUTTON_X2
BUTTON_LMASK = C.SDL_BUTTON_LMASK
BUTTON_MMASK = C.SDL_BUTTON_MMASK
BUTTON_RMASK = C.SDL_BUTTON_RMASK
BUTTON_X1MASK = C.SDL_BUTTON_X1MASK
BUTTON_X2MASK = C.SDL_BUTTON_X2MASK
)