holy crap click handling works too
This commit was merged in pull request #5.
This commit is contained in:
50
common/elements/v1/buttons/button.go
Normal file
50
common/elements/v1/buttons/button.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package buttons
|
||||
|
||||
import (
|
||||
"git.vezzani.net/ben/games/common/elements/v1"
|
||||
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||
)
|
||||
|
||||
func New(ops ...Option) elements.InitFunc {
|
||||
return func(bounds elements.Bounds) elements.Element {
|
||||
b := Button{
|
||||
Block: blocks.Block{ContainerBounds: bounds},
|
||||
}
|
||||
for op := range ops {
|
||||
ops[op](&b)
|
||||
}
|
||||
|
||||
return &b
|
||||
}
|
||||
}
|
||||
|
||||
type Button struct {
|
||||
blocks.Block
|
||||
|
||||
onClick func(ms mouse.State)
|
||||
onMouseDown func(ms mouse.State)
|
||||
onMouseUp func(ms mouse.State)
|
||||
}
|
||||
|
||||
func (b *Button) HandleMouseEvent(ms mouse.State) bool {
|
||||
if !b.Block.ContainerBounds.Contains(elements.Point{
|
||||
X: float64(ms.X),
|
||||
Y: float64(ms.Y),
|
||||
}) {
|
||||
return false
|
||||
}
|
||||
switch {
|
||||
case b.onClick != nil && (ms.RightClicked || ms.LeftClicked):
|
||||
b.onClick(ms)
|
||||
return true
|
||||
case b.onMouseUp != nil && (ms.RightChanged && !ms.RightDown || ms.LeftChanged && !ms.LeftDown):
|
||||
b.onMouseUp(ms)
|
||||
return true
|
||||
case b.onMouseDown != nil && (ms.RightChanged && ms.RightDown || ms.LeftChanged && ms.LeftDown):
|
||||
b.onMouseDown(ms)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user