ebiten/ui/glfw/inputstate.go

71 lines
1.1 KiB
Go
Raw Normal View History

2014-12-05 18:26:02 +01:00
package glfw
import (
2014-12-05 19:10:17 +01:00
glfw "github.com/go-gl/glfw3"
2014-12-05 18:26:02 +01:00
"github.com/hajimehoshi/ebiten/ui"
)
type Keys map[ui.Key]struct{}
func newKeys() Keys {
return Keys(map[ui.Key]struct{}{})
}
func (k Keys) add(key ui.Key) {
k[key] = struct{}{}
}
func (k Keys) remove(key ui.Key) {
delete(k, key)
}
func (k Keys) Includes(key ui.Key) bool {
_, ok := k[key]
return ok
}
type InputState struct {
pressedKeys Keys
mouseX int
mouseY int
}
2014-12-05 19:10:17 +01:00
func newInputState() *InputState {
return &InputState{
pressedKeys: newKeys(),
mouseX: -1,
mouseY: -1,
}
}
2014-12-05 18:26:02 +01:00
func (i *InputState) PressedKeys() ui.Keys {
return i.pressedKeys
}
func (i *InputState) MouseX() int {
2014-12-06 07:47:48 +01:00
// TODO: Update
2014-12-05 18:26:02 +01:00
return i.mouseX
}
func (i *InputState) MouseY() int {
return i.mouseY
}
2014-12-05 19:10:17 +01:00
var glfwKeyCodeToKey = map[glfw.Key]ui.Key{
glfw.KeySpace: ui.KeySpace,
glfw.KeyLeft: ui.KeyLeft,
glfw.KeyRight: ui.KeyRight,
glfw.KeyUp: ui.KeyUp,
glfw.KeyDown: ui.KeyDown,
}
func (i *InputState) update(window *glfw.Window) {
for g, u := range glfwKeyCodeToKey {
if window.GetKey(g) == glfw.Press {
i.pressedKeys.add(u)
} else {
i.pressedKeys.remove(u)
}
}
}