objects/button.go

104 lines
2.6 KiB
Go
Raw Normal View History

2023-08-08 10:46:19 -06:00
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
2023-09-07 16:13:10 -06:00
import "git.tebibyte.media/tomo/objects/layouts"
var buttonLayout = layouts.NewGrid([]bool { true }, []bool { true })
var iconButtonLayout = layouts.NewGrid([]bool { false }, []bool { true })
var bothButtonLayout = layouts.NewGrid([]bool { false, true }, []bool { true })
2023-08-08 10:46:19 -06:00
// Button is a clickable button.
type Button struct {
2023-09-07 16:13:10 -06:00
tomo.ContainerBox
2023-09-07 16:13:10 -06:00
label *Label
icon *Icon
labelActive bool
2023-08-08 10:46:19 -06:00
on struct {
click event.FuncBroadcaster
}
}
// NewButton creates a new button with the specified text.
2023-08-09 09:35:24 -06:00
func NewButton (text string) *Button {
2023-09-07 16:13:10 -06:00
box := &Button {
ContainerBox: tomo.NewContainerBox(),
label: NewLabel(text),
}
2024-06-03 19:13:18 -06:00
box.SetRole(tomo.R("objects", "Button", ""))
tomo.Apply(box)
2023-09-07 16:13:10 -06:00
box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
box.SetLayout(buttonLayout)
box.SetText(text)
2023-09-14 12:48:08 -06:00
box.CaptureDND(true)
box.CaptureMouse(true)
box.CaptureScroll(true)
box.CaptureKeyboard(true)
2023-08-08 10:46:19 -06:00
box.OnMouseUp(box.handleMouseUp)
2023-08-09 09:35:24 -06:00
box.OnKeyUp(box.handleKeyUp)
box.SetFocusable(true)
2023-08-08 10:46:19 -06:00
return box
}
2023-09-07 16:13:10 -06:00
// SetText sets the text of the button's label.
func (this *Button) SetText (text string) {
this.label.SetText(text)
if this.labelActive && text == "" {
2024-05-26 15:21:58 -06:00
this.Remove(this.label)
this.labelActive = false
}
if !this.labelActive && text != "" {
this.Add(this.label)
this.labelActive = true
}
this.applyLayout()
2023-09-07 16:13:10 -06:00
}
2024-05-06 21:23:59 -06:00
// SetIcon sets an icon for this button. Setting the icon to IconUnknown will
// remove it.
2024-05-26 15:21:58 -06:00
func (this *Button) SetIcon (id tomo.Icon) {
if this.icon != nil { this.Remove(this.icon) }
2024-05-06 21:23:59 -06:00
2024-05-26 15:21:58 -06:00
var icon *Icon; if id != tomo.IconUnknown {
icon = NewIcon(id, tomo.IconSizeSmall)
2024-05-06 21:23:59 -06:00
}
2023-09-07 16:13:10 -06:00
this.icon = icon
if this.icon != nil {
2023-09-07 16:13:10 -06:00
this.Insert(this.icon, this.label)
}
this.applyLayout()
2023-09-07 16:13:10 -06:00
}
2023-08-08 10:46:19 -06:00
// OnClick specifies a function to be called when the button is clicked.
func (this *Button) OnClick (callback func ()) event.Cookie {
return this.on.click.Connect(callback)
}
func (this *Button) applyLayout () {
if this.labelActive && this.icon == nil {
this.SetLayout(buttonLayout)
} else if !this.labelActive && this.icon != nil {
this.SetLayout(iconButtonLayout)
} else {
this.SetLayout(bothButtonLayout)
}
}
2023-08-09 09:35:24 -06:00
func (this *Button) handleKeyUp (key input.Key, numberPad bool) {
if key != input.KeyEnter && key != input.Key(' ') { return }
this.on.click.Broadcast()
}
2023-08-08 10:46:19 -06:00
func (this *Button) handleMouseUp (button input.Button) {
if button != input.ButtonLeft { return }
if this.MousePosition().In(this.Bounds()) {
this.on.click.Broadcast()
}
}