mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Fresh start
This commit is contained in:
@ -1,45 +0,0 @@
|
||||
package input
|
||||
|
||||
import "github.com/veandco/go-sdl2/sdl"
|
||||
|
||||
var (
|
||||
anyKeyDown bool
|
||||
anyMouseBtnDown bool
|
||||
)
|
||||
|
||||
//EventLoopStarted should be called just before processing SDL events
|
||||
func EventLoopStarted() {
|
||||
|
||||
anyKeyDown = false
|
||||
anyMouseBtnDown = false
|
||||
|
||||
//Clear XThisFrame which is needed because a repeat event needs multiple frames to happen
|
||||
for _, v := range mouseBtns {
|
||||
|
||||
v.isDoubleClick = false
|
||||
v.pressedThisFrame = false
|
||||
v.releasedThisFrame = false
|
||||
|
||||
if v.state == sdl.PRESSED {
|
||||
anyMouseBtnDown = true
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range keyboardKeys {
|
||||
|
||||
v.pressedThisFrame = false
|
||||
v.releasedThisFrame = false
|
||||
|
||||
if v.state == sdl.PRESSED {
|
||||
anyKeyDown = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func AnyKeyDown() bool {
|
||||
return anyKeyDown
|
||||
}
|
||||
|
||||
func AnyMouseBtnDown() bool {
|
||||
return anyMouseBtnDown
|
||||
}
|
||||
@ -1,67 +0,0 @@
|
||||
package input
|
||||
|
||||
import "github.com/veandco/go-sdl2/sdl"
|
||||
|
||||
type keyState struct {
|
||||
key sdl.Keycode
|
||||
state byte
|
||||
pressedThisFrame bool
|
||||
releasedThisFrame bool
|
||||
}
|
||||
|
||||
var (
|
||||
keyboardKeys = make(map[sdl.Keycode]*keyState)
|
||||
)
|
||||
|
||||
func HandleKeyboardEvent(e *sdl.KeyboardEvent) {
|
||||
|
||||
ks := keyboardKeys[e.Keysym.Sym]
|
||||
if ks == nil {
|
||||
ks = &keyState{key: e.Keysym.Sym}
|
||||
keyboardKeys[e.Keysym.Sym] = ks
|
||||
}
|
||||
|
||||
ks.state = e.State
|
||||
ks.pressedThisFrame = e.Repeat == 0 && e.State == sdl.PRESSED
|
||||
ks.releasedThisFrame = e.Repeat == 0 && e.State == sdl.RELEASED
|
||||
}
|
||||
|
||||
func KeyClicked(kc sdl.Keycode) bool {
|
||||
|
||||
key := keyboardKeys[kc]
|
||||
if key == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return key.pressedThisFrame
|
||||
}
|
||||
|
||||
func KeyReleased(kc sdl.Keycode) bool {
|
||||
|
||||
key := keyboardKeys[kc]
|
||||
if key == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return key.releasedThisFrame
|
||||
}
|
||||
|
||||
func KeyDown(kc sdl.Keycode) bool {
|
||||
|
||||
key := keyboardKeys[kc]
|
||||
if key == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return key.state == sdl.PRESSED
|
||||
}
|
||||
|
||||
func KeyUp(kc sdl.Keycode) bool {
|
||||
|
||||
key := keyboardKeys[kc]
|
||||
if key == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
return key.state == sdl.RELEASED
|
||||
}
|
||||
@ -1,79 +0,0 @@
|
||||
package input
|
||||
|
||||
import "github.com/veandco/go-sdl2/sdl"
|
||||
|
||||
type mouseBtnState struct {
|
||||
button byte
|
||||
state byte
|
||||
isDoubleClick bool
|
||||
pressedThisFrame bool
|
||||
releasedThisFrame bool
|
||||
}
|
||||
|
||||
var (
|
||||
mouseBtns = make(map[byte]*mouseBtnState)
|
||||
)
|
||||
|
||||
func HandleMouseBtnEvent(e *sdl.MouseButtonEvent) {
|
||||
|
||||
mb := mouseBtns[e.Button]
|
||||
if mb == nil {
|
||||
mb = &mouseBtnState{button: e.Button}
|
||||
mouseBtns[e.Button] = mb
|
||||
}
|
||||
|
||||
mb.state = e.State
|
||||
mb.isDoubleClick = e.Clicks > 1 && e.State == sdl.PRESSED
|
||||
mb.pressedThisFrame = e.State == sdl.PRESSED
|
||||
mb.releasedThisFrame = e.State == sdl.RELEASED
|
||||
}
|
||||
|
||||
func MouseClicked(mouseBtn byte) bool {
|
||||
|
||||
mb := mouseBtns[mouseBtn]
|
||||
if mb == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return mb.pressedThisFrame
|
||||
}
|
||||
|
||||
func MouseDoubleClicked(mouseBtn byte) bool {
|
||||
|
||||
mb := mouseBtns[mouseBtn]
|
||||
if mb == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return mb.isDoubleClick
|
||||
}
|
||||
|
||||
func MouseReleased(mouseBtn byte) bool {
|
||||
|
||||
mb := mouseBtns[mouseBtn]
|
||||
if mb == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return mb.releasedThisFrame
|
||||
}
|
||||
|
||||
func MouseDown(mouseBtn byte) bool {
|
||||
|
||||
mb := mouseBtns[mouseBtn]
|
||||
if mb == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return mb.state == sdl.PRESSED
|
||||
}
|
||||
|
||||
func MouseUp(mouseBtn byte) bool {
|
||||
|
||||
mb := mouseBtns[mouseBtn]
|
||||
if mb == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
return mb.state == sdl.RELEASED
|
||||
}
|
||||
Reference in New Issue
Block a user