51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
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
|
|
}
|