65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package mouse
|
|
|
|
import (
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
|
)
|
|
|
|
type NopHandler struct{}
|
|
|
|
func (n NopHandler) HandleMouseEvent(s State) bool {
|
|
return false
|
|
}
|
|
|
|
type State struct {
|
|
X, Y int
|
|
LeftDown, RightDown bool
|
|
LeftChanged, RightChanged bool
|
|
LeftClicked, RightClicked bool
|
|
Clicked bool
|
|
}
|
|
|
|
//func ClickHandler(e elements.Element) func() bool {
|
|
// newState := StateBuilder()
|
|
// return func() bool {
|
|
// _ = newState()
|
|
// if
|
|
// }
|
|
//}
|
|
|
|
func StateBuilder() func() State {
|
|
prevState := State{}
|
|
state := State{}
|
|
return func() State {
|
|
state = prevState
|
|
state.X, state.Y = ebiten.CursorPosition()
|
|
|
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
|
state.LeftDown = true
|
|
}
|
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
|
|
state.RightDown = true
|
|
}
|
|
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
|
|
state.LeftDown = false
|
|
if !state.RightDown {
|
|
state.Clicked = true
|
|
}
|
|
}
|
|
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
|
|
state.RightDown = false
|
|
if !state.LeftDown {
|
|
state.Clicked = true
|
|
}
|
|
}
|
|
|
|
state.Clicked = state.Clicked && !prevState.Clicked
|
|
|
|
state.LeftChanged = state.LeftDown != prevState.LeftDown
|
|
state.RightChanged = state.RightDown != prevState.RightDown
|
|
|
|
prevState = state
|
|
return state
|
|
}
|
|
}
|