-
Notifications
You must be signed in to change notification settings - Fork 11
/
input.go
83 lines (65 loc) · 1.74 KB
/
input.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
package strife
import "github.com/veandco/go-sdl2/sdl"
// MOUSE INPUT
// MouseButtonState contains the state of
// the mouse button, i.e. if its left/right is down
// no buttons are down, or the scroll wheel is clicked
type MouseButtonState int
// The types of mouse button state.
const (
NoMouseButtonsDown MouseButtonState = iota
LeftMouseButton
RightMouseButton
ScrollWheel
)
// MouseHandler is a wrapper to store the X, Y location
// of the mouse + the current state
type MouseHandler struct {
X, Y int
ButtonState MouseButtonState
}
// FIXME
var mouseInstance = &MouseHandler{}
// MouseButtonsState returns the current state of the mouse
func MouseButtonsState() MouseButtonState {
return mouseInstance.ButtonState
}
// MouseCoords returns the coords of the mouse
func MouseCoords() (int, int) {
return mouseInstance.X, mouseInstance.Y
}
// KEY INPUT
// KeyboardHandler is a wrapper to handle key press
type KeyboardHandler struct {
keys map[int]bool
buff []int
}
// KeyState wraps over SDL GetKeyboardState
func KeyState() []uint8 {
return sdl.GetKeyboardState()
}
// KeyPressed will query if a key is pressed in the KeyboardHandler
func KeyPressed(keyCode int) bool {
if val, ok := keyboardInstance.keys[keyCode]; ok {
return val
}
return false
}
var keyboardInstance = &KeyboardHandler{
keys: map[int]bool{},
buff: []int{},
}
// PollKeys returns if there are in key presses
// in the buffer
func PollKeys() bool {
return len(keyboardInstance.buff) > 0
}
// PopKey pops a key from the key press queue.
func PopKey() int {
keyBuffSize := len(keyboardInstance.buff)
// get the key
keyPressed := keyboardInstance.buff[keyBuffSize-1]
// apply pop
keyboardInstance.buff = keyboardInstance.buff[:keyBuffSize-1]
return keyPressed
}