2022-06-08 23:35:42 +00:00
|
|
|
package etk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
|
2024-01-16 21:00:20 +00:00
|
|
|
"code.rocket9labs.com/tslocum/etk/messeji"
|
2022-06-08 23:35:42 +00:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
|
|
|
|
2023-10-29 06:04:32 +00:00
|
|
|
// Button is a clickable button.
|
2022-06-08 23:35:42 +00:00
|
|
|
type Button struct {
|
|
|
|
*Box
|
|
|
|
|
2023-09-29 05:43:29 +00:00
|
|
|
Label *messeji.TextField
|
2022-07-07 21:53:14 +00:00
|
|
|
|
|
|
|
onSelected func() error
|
2022-06-08 23:35:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-29 06:04:32 +00:00
|
|
|
// NewButton returns a new Button widget.
|
2022-07-07 21:53:14 +00:00
|
|
|
func NewButton(label string, onSelected func() error) *Button {
|
2022-06-08 23:35:42 +00:00
|
|
|
textColor := Style.ButtonTextColor
|
2023-10-28 07:30:09 +00:00
|
|
|
if textColor.A == 0 {
|
2022-07-07 21:53:14 +00:00
|
|
|
textColor = Style.TextColorDark
|
2022-06-08 23:35:42 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 01:37:51 +00:00
|
|
|
l := messeji.NewTextField(Style.TextFont, Style.TextFontMutex)
|
2022-06-08 23:35:42 +00:00
|
|
|
l.SetText(label)
|
|
|
|
l.SetForegroundColor(textColor)
|
2022-07-07 21:53:14 +00:00
|
|
|
l.SetBackgroundColor(transparent)
|
2022-06-08 23:35:42 +00:00
|
|
|
l.SetHorizontal(messeji.AlignCenter)
|
|
|
|
l.SetVertical(messeji.AlignCenter)
|
2023-10-25 02:24:27 +00:00
|
|
|
l.SetScrollBarVisible(false)
|
2022-06-08 23:35:42 +00:00
|
|
|
|
|
|
|
return &Button{
|
2022-07-07 21:53:14 +00:00
|
|
|
Box: NewBox(),
|
2023-09-29 05:43:29 +00:00
|
|
|
Label: l,
|
2022-07-07 21:53:14 +00:00
|
|
|
onSelected: onSelected,
|
2022-06-08 23:35:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 06:04:32 +00:00
|
|
|
// SetRect sets the position and size of the Button.
|
2022-06-08 23:35:42 +00:00
|
|
|
func (b *Button) SetRect(r image.Rectangle) {
|
|
|
|
b.Box.rect = r
|
|
|
|
|
2023-09-29 05:43:29 +00:00
|
|
|
b.Label.SetRect(r)
|
2023-11-08 22:11:36 +00:00
|
|
|
|
|
|
|
for _, w := range b.children {
|
|
|
|
w.SetRect(r)
|
|
|
|
}
|
2022-06-08 23:35:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-29 06:04:32 +00:00
|
|
|
// HandleKeyboard is called when a keyboard event occurs.
|
|
|
|
func (b *Button) HandleKeyboard(ebiten.Key, rune) (handled bool, err error) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleMouse is called when a mouse event occurs.
|
2022-07-07 21:53:14 +00:00
|
|
|
func (b *Button) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
|
|
|
|
if !clicked {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Lock()
|
|
|
|
onSelected := b.onSelected
|
|
|
|
if onSelected == nil {
|
|
|
|
b.Unlock()
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
b.Unlock()
|
|
|
|
|
|
|
|
return true, onSelected()
|
2022-06-08 23:35:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-29 06:04:32 +00:00
|
|
|
// Draw draws the button on the screen.
|
2022-06-08 23:35:42 +00:00
|
|
|
func (b *Button) Draw(screen *ebiten.Image) error {
|
|
|
|
// Draw background.
|
|
|
|
screen.SubImage(b.rect).(*ebiten.Image).Fill(Style.ButtonBgColor)
|
|
|
|
|
|
|
|
// Draw label.
|
2023-09-29 05:43:29 +00:00
|
|
|
b.Label.Draw(screen)
|
2022-06-08 23:35:42 +00:00
|
|
|
|
|
|
|
// Draw border.
|
|
|
|
const borderSize = 4
|
|
|
|
screen.SubImage(image.Rect(b.rect.Min.X, b.rect.Min.Y, b.rect.Max.X, b.rect.Min.Y+borderSize)).(*ebiten.Image).Fill(Style.BorderColor)
|
|
|
|
screen.SubImage(image.Rect(b.rect.Min.X, b.rect.Max.Y-borderSize, b.rect.Max.X, b.rect.Max.Y)).(*ebiten.Image).Fill(Style.BorderColor)
|
|
|
|
screen.SubImage(image.Rect(b.rect.Min.X, b.rect.Min.Y, b.rect.Min.X+borderSize, b.rect.Max.Y)).(*ebiten.Image).Fill(Style.BorderColor)
|
|
|
|
screen.SubImage(image.Rect(b.rect.Max.X-borderSize, b.rect.Min.Y, b.rect.Max.X, b.rect.Max.Y)).(*ebiten.Image).Fill(Style.BorderColor)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|