2022-06-08 23:35:42 +00:00
|
|
|
package etk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
2024-01-23 08:53:47 +00:00
|
|
|
"image/color"
|
2022-06-08 23:35:42 +00:00
|
|
|
|
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"
|
2024-08-22 06:43:05 +00:00
|
|
|
"golang.org/x/image/font"
|
2024-04-23 20:05:39 +00:00
|
|
|
"golang.org/x/image/font/sfnt"
|
2022-06-08 23:35:42 +00:00
|
|
|
)
|
|
|
|
|
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
|
2024-01-23 20:39:08 +00:00
|
|
|
field *messeji.TextField
|
2024-08-22 06:43:05 +00:00
|
|
|
textFont *sfnt.Font
|
|
|
|
textSize int
|
|
|
|
textAutoSize int
|
2024-01-23 20:39:08 +00:00
|
|
|
borderSize int
|
2024-01-23 08:53:47 +00:00
|
|
|
borderTop color.RGBA
|
|
|
|
borderRight color.RGBA
|
|
|
|
borderBottom color.RGBA
|
|
|
|
borderLeft color.RGBA
|
|
|
|
onSelected func() error
|
2024-01-29 04:44:19 +00:00
|
|
|
pressed bool
|
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
|
|
|
}
|
2024-01-23 20:39:08 +00:00
|
|
|
f := newText()
|
|
|
|
f.SetText(label)
|
|
|
|
f.SetForegroundColor(textColor)
|
|
|
|
f.SetHorizontal(messeji.AlignCenter)
|
|
|
|
f.SetVertical(messeji.AlignCenter)
|
|
|
|
f.SetScrollBarVisible(false)
|
2022-06-08 23:35:42 +00:00
|
|
|
|
2024-01-29 04:44:19 +00:00
|
|
|
b := &Button{
|
2024-01-23 08:53:47 +00:00
|
|
|
Box: NewBox(),
|
2024-01-23 20:39:08 +00:00
|
|
|
field: f,
|
2024-08-22 06:43:05 +00:00
|
|
|
textFont: Style.TextFont,
|
|
|
|
textSize: Scale(Style.TextSize),
|
2024-01-23 08:53:47 +00:00
|
|
|
onSelected: onSelected,
|
2024-01-23 20:39:08 +00:00
|
|
|
borderSize: Scale(Style.BorderSize),
|
2024-01-23 08:53:47 +00:00
|
|
|
borderTop: Style.BorderColorTop,
|
|
|
|
borderRight: Style.BorderColorRight,
|
|
|
|
borderBottom: Style.BorderColorBottom,
|
|
|
|
borderLeft: Style.BorderColorLeft,
|
2022-06-08 23:35:42 +00:00
|
|
|
}
|
2024-01-29 04:44:19 +00:00
|
|
|
b.SetBackground(Style.ButtonBgColor)
|
2024-08-22 06:43:05 +00:00
|
|
|
b.resizeFont()
|
2024-01-29 04:44:19 +00:00
|
|
|
return b
|
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
|
|
|
|
|
2024-01-23 20:39:08 +00:00
|
|
|
b.field.SetRect(r)
|
2024-08-22 06:43:05 +00:00
|
|
|
b.resizeFont()
|
2023-11-08 22:11:36 +00:00
|
|
|
|
|
|
|
for _, w := range b.children {
|
|
|
|
w.SetRect(r)
|
|
|
|
}
|
2022-06-08 23:35:42 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 07:56:05 +00:00
|
|
|
// SetBorderSize sets the size of the border around the button.
|
|
|
|
func (b *Button) SetBorderSize(size int) {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
|
|
|
|
b.borderSize = size
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBorderColors sets the color of the top, right, bottom and left border.
|
|
|
|
func (b *Button) SetBorderColors(top color.RGBA, right color.RGBA, bottom color.RGBA, left color.RGBA) {
|
2024-01-23 08:53:47 +00:00
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
|
|
|
|
b.borderTop = top
|
|
|
|
b.borderRight = right
|
|
|
|
b.borderBottom = bottom
|
|
|
|
b.borderLeft = left
|
|
|
|
}
|
|
|
|
|
2024-01-23 20:39:08 +00:00
|
|
|
// Text returns the content of the text buffer.
|
|
|
|
func (b *Button) Text() string {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
|
|
|
|
return b.field.Text()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetText sets the text in the field.
|
|
|
|
func (b *Button) SetText(text string) {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
|
|
|
|
b.field.SetText(text)
|
2024-08-22 06:43:05 +00:00
|
|
|
b.resizeFont()
|
2024-01-23 20:39:08 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 20:05:39 +00:00
|
|
|
// SetFont sets the font and text size of button label. Scaling is not applied.
|
|
|
|
func (b *Button) SetFont(fnt *sfnt.Font, size int) {
|
2024-01-23 20:39:08 +00:00
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
|
2024-08-22 06:43:05 +00:00
|
|
|
b.textFont, b.textSize = fnt, size
|
|
|
|
b.resizeFont()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Button) resizeFont() {
|
2024-09-02 04:32:09 +00:00
|
|
|
w, h := b.rect.Dx()-b.field.Padding()*2, b.rect.Dy()-b.field.Padding()*2
|
|
|
|
if w == 0 || h == 0 {
|
|
|
|
if b.textAutoSize == b.textSize {
|
|
|
|
return
|
|
|
|
}
|
2024-08-22 06:43:05 +00:00
|
|
|
b.textAutoSize = b.textSize
|
2024-09-02 04:32:09 +00:00
|
|
|
ff := FontFace(b.textFont, b.textSize)
|
|
|
|
b.field.SetFont(ff, fontMutex)
|
2024-08-22 06:43:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var autoSize int
|
|
|
|
var ff font.Face
|
|
|
|
for autoSize = b.textSize; autoSize > 0; autoSize-- {
|
|
|
|
ff = FontFace(b.textFont, autoSize)
|
2024-09-02 04:32:09 +00:00
|
|
|
bounds := BoundString(ff, b.field.Text())
|
|
|
|
if bounds.Dx() <= w && bounds.Dy() <= h {
|
2024-08-22 06:43:05 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if b.textAutoSize == autoSize {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.field.SetFont(ff, fontMutex)
|
|
|
|
b.textAutoSize = autoSize
|
2024-01-23 20:39:08 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 21:41:47 +00:00
|
|
|
// SetHorizontal sets the horizontal alignment of the button label.
|
|
|
|
func (b *Button) SetHorizontal(h Alignment) {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
|
|
|
|
b.field.SetHorizontal(messeji.Alignment(h))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVertical sets the vertical alignment of the button label.
|
2024-09-19 02:41:36 +00:00
|
|
|
func (b *Button) SetVertical(v Alignment) {
|
2024-07-22 21:41:47 +00:00
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
|
2024-09-19 02:41:36 +00:00
|
|
|
b.field.SetVertical(messeji.Alignment(v))
|
2024-07-22 21:41:47 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 05:05:01 +00:00
|
|
|
// Cursor returns the cursor shape shown when a mouse cursor hovers over the
|
|
|
|
// widget, or -1 to let widgets beneath determine the cursor shape.
|
|
|
|
func (b *Button) Cursor() ebiten.CursorShapeType {
|
|
|
|
return ebiten.CursorShapePointer
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-01-29 04:44:19 +00:00
|
|
|
if b.pressed && !pressed {
|
2024-02-10 20:38:13 +00:00
|
|
|
b.Lock()
|
2024-01-29 04:44:19 +00:00
|
|
|
b.pressed = false
|
2024-02-10 20:38:13 +00:00
|
|
|
b.background = Style.ButtonBgColor
|
|
|
|
b.Unlock()
|
2024-01-29 04:44:19 +00:00
|
|
|
}
|
2022-07-07 21:53:14 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Lock()
|
2024-01-29 04:44:19 +00:00
|
|
|
b.pressed = true
|
|
|
|
b.background = color.RGBA{uint8(float64(Style.ButtonBgColor.R) * 0.95), uint8(float64(Style.ButtonBgColor.G) * 0.95), uint8(float64(Style.ButtonBgColor.B) * 0.95), 255}
|
2022-07-07 21:53:14 +00:00
|
|
|
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 {
|
2024-01-23 08:53:47 +00:00
|
|
|
r := b.rect
|
|
|
|
|
2022-06-08 23:35:42 +00:00
|
|
|
// Draw label.
|
2024-01-23 20:39:08 +00:00
|
|
|
b.field.Draw(screen)
|
2022-06-08 23:35:42 +00:00
|
|
|
|
|
|
|
// Draw border.
|
2024-01-24 07:56:05 +00:00
|
|
|
if b.borderSize != 0 {
|
2024-01-29 04:44:19 +00:00
|
|
|
if !b.pressed {
|
|
|
|
screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Min.X+b.borderSize, r.Max.Y)).(*ebiten.Image).Fill(b.borderLeft)
|
|
|
|
screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Max.X, r.Min.Y+b.borderSize)).(*ebiten.Image).Fill(b.borderTop)
|
|
|
|
screen.SubImage(image.Rect(r.Max.X-b.borderSize, r.Min.Y, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(b.borderRight)
|
|
|
|
screen.SubImage(image.Rect(r.Min.X, r.Max.Y-b.borderSize, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(b.borderBottom)
|
|
|
|
} else {
|
|
|
|
screen.SubImage(image.Rect(r.Max.X-b.borderSize, r.Min.Y, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(b.borderLeft)
|
|
|
|
screen.SubImage(image.Rect(r.Min.X, r.Max.Y-b.borderSize, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(b.borderTop)
|
|
|
|
screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Min.X+b.borderSize, r.Max.Y)).(*ebiten.Image).Fill(b.borderRight)
|
|
|
|
screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Max.X, r.Min.Y+b.borderSize)).(*ebiten.Image).Fill(b.borderBottom)
|
|
|
|
}
|
2024-01-24 07:56:05 +00:00
|
|
|
}
|
2022-06-08 23:35:42 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|