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
|
||||
}
|
||||
32
common/elements/v1/buttons/options.go
Normal file
32
common/elements/v1/buttons/options.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package buttons
|
||||
|
||||
import (
|
||||
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||
)
|
||||
|
||||
type Option func(button *Button)
|
||||
|
||||
func OnClick(f func(ms mouse.State)) Option {
|
||||
return func(button *Button) {
|
||||
button.onClick = f
|
||||
}
|
||||
}
|
||||
|
||||
func OnMouseDown(f func(ms mouse.State)) Option {
|
||||
return func(button *Button) {
|
||||
button.onMouseDown = f
|
||||
}
|
||||
}
|
||||
|
||||
func OnMouseUp(f func(ms mouse.State)) Option {
|
||||
return func(button *Button) {
|
||||
button.onMouseUp = f
|
||||
}
|
||||
}
|
||||
|
||||
func BlockOpt(o blocks.Option) Option {
|
||||
return func(s *Button) {
|
||||
o(&s.Block)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user