-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_handler.go
156 lines (136 loc) · 4.57 KB
/
input_handler.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
package game_manager
import (
"github.com/veandco/go-sdl2/sdl"
)
const (
LEFT = iota
MIDDLE = iota
RIGHT = iota
)
type InputHandler struct {
mouseButtonStates [3]bool
mousePosition Vector2D
keystates []uint8
}
//*****************************************************************************
// NewInputHandler
//*****************************************************************************
func NewInputHandler() *InputHandler {
var ih InputHandler
for i := 0; i < 3; i++ {
ih.mouseButtonStates[i] = false
}
return &ih
}
//*****************************************************************************
// onMouseButtonDown
//*****************************************************************************
func (ih *InputHandler) onMouseButtonDown(event sdl.Event) {
var mouseButtonEvent = event.(*sdl.MouseButtonEvent)
if mouseButtonEvent.Button == sdl.BUTTON_LEFT {
ih.mouseButtonStates[LEFT] = true
}
if mouseButtonEvent.Button == sdl.BUTTON_MIDDLE {
ih.mouseButtonStates[MIDDLE] = true
}
if mouseButtonEvent.Button == sdl.BUTTON_RIGHT {
ih.mouseButtonStates[RIGHT] = true
}
}
//*****************************************************************************
// onMouseButtonUp
//*****************************************************************************
func (ih *InputHandler) onMouseButtonUp(event sdl.Event) {
var mouseButtonEvent = event.(*sdl.MouseButtonEvent)
if mouseButtonEvent.Button == sdl.BUTTON_LEFT {
ih.mouseButtonStates[LEFT] = false
}
if mouseButtonEvent.Button == sdl.BUTTON_MIDDLE {
ih.mouseButtonStates[MIDDLE] = false
}
if mouseButtonEvent.Button == sdl.BUTTON_RIGHT {
ih.mouseButtonStates[RIGHT] = false
}
}
//*****************************************************************************
// onMouseMove
//*****************************************************************************
func (ih *InputHandler) onMouseMove(event sdl.Event) {
var mouseMotionEvent = event.(*sdl.MouseMotionEvent)
ih.mousePosition.X = float32(mouseMotionEvent.X)
ih.mousePosition.Y = float32(mouseMotionEvent.Y)
}
//*****************************************************************************
// getMouseButtonState
//*****************************************************************************
func (ih *InputHandler) getMouseButtonState(buttonNumber int) bool {
return ih.mouseButtonStates[buttonNumber]
}
//*****************************************************************************
// getMousePosition
//*****************************************************************************
func (ih *InputHandler) getMousePosition() Vector2D {
return ih.mousePosition
}
//*****************************************************************************
// isKeyDown
//*****************************************************************************
func (ih *InputHandler) isKeyDown(key sdl.Scancode) bool {
if len(ih.keystates) != 0 {
if ih.keystates[key] == 1 {
return true
} else {
return false
}
}
return false
}
//*****************************************************************************
// onKeyDown
//*****************************************************************************
func (ih *InputHandler) onKeyDown() {
ih.keystates = sdl.GetKeyboardState()
}
//*****************************************************************************
// onKeyUp
//*****************************************************************************
func (ih *InputHandler) onKeyUp() {
ih.keystates = sdl.GetKeyboardState()
}
//*****************************************************************************
// update
//*****************************************************************************
func (ih *InputHandler) update(game *Game) {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) {
case *sdl.QuitEvent:
game.SetRunning(false)
case *sdl.MouseMotionEvent:
ih.onMouseMove(event)
case *sdl.MouseButtonEvent:
if t.State == 1 {
ih.onMouseButtonDown(event)
}
if t.State == 0 {
ih.onMouseButtonUp(event)
}
case *sdl.KeyDownEvent:
ih.onKeyDown()
case *sdl.KeyUpEvent:
ih.onKeyUp()
}
}
}
//*****************************************************************************
// clean
//*****************************************************************************
func (ih *InputHandler) clean() {
}
//*****************************************************************************
// reset
//*****************************************************************************
func (ih *InputHandler) reset() {
ih.mouseButtonStates[LEFT] = false
ih.mouseButtonStates[RIGHT] = false
ih.mouseButtonStates[MIDDLE] = false
}