holy crap click handling works too
This commit was merged in pull request #5.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package mouse
|
||||
|
||||
import (
|
||||
"git.vezzani.net/ben/games/common/elements/v1"
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
)
|
||||
@@ -11,6 +12,14 @@ func (n NopHandler) HandleMouseEvent(s State) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type ChildrenProvider interface {
|
||||
GetChildren() []elements.Element
|
||||
}
|
||||
|
||||
type EventHandler interface {
|
||||
HandleMouseEvent(s State) bool
|
||||
}
|
||||
|
||||
type State struct {
|
||||
X, Y int
|
||||
LeftDown, RightDown bool
|
||||
@@ -19,13 +28,30 @@ type State struct {
|
||||
Clicked bool
|
||||
}
|
||||
|
||||
//func ClickHandler(e elements.Element) func() bool {
|
||||
// newState := StateBuilder()
|
||||
// return func() bool {
|
||||
// _ = newState()
|
||||
// if
|
||||
// }
|
||||
//}
|
||||
func Handler(e elements.Element) func() bool {
|
||||
newState := StateBuilder()
|
||||
ms := newState()
|
||||
return func() bool {
|
||||
ms = newState()
|
||||
return propagateMouse(e, ms)
|
||||
}
|
||||
}
|
||||
|
||||
func propagateMouse(e elements.Element, ms State) bool {
|
||||
if p, ok := e.(ChildrenProvider); ok {
|
||||
for _, c := range p.GetChildren() {
|
||||
if propagateMouse(c, ms) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if e, ok := e.(EventHandler); ok {
|
||||
return e.HandleMouseEvent(ms)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func StateBuilder() func() State {
|
||||
prevState := State{}
|
||||
@@ -57,6 +83,8 @@ func StateBuilder() func() State {
|
||||
|
||||
state.LeftChanged = state.LeftDown != prevState.LeftDown
|
||||
state.RightChanged = state.RightDown != prevState.RightDown
|
||||
state.LeftClicked = state.LeftChanged && state.LeftDown
|
||||
state.RightClicked = state.RightChanged && state.RightDown
|
||||
|
||||
prevState = state
|
||||
return state
|
||||
|
||||
Reference in New Issue
Block a user