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"
|
|
|
|
|
2024-08-24 13:11:57 -06:00
|
|
|
var _ tomo.Object = new(Button)
|
|
|
|
|
2024-06-12 01:15:38 -06:00
|
|
|
var buttonLayout = layouts.Row { true }
|
|
|
|
var iconButtonLayout = layouts.Row { true }
|
|
|
|
var bothButtonLayout = layouts.Row { false, true }
|
2023-08-08 10:46:19 -06:00
|
|
|
|
|
|
|
// Button is a clickable button.
|
|
|
|
type Button struct {
|
2024-08-24 13:11:57 -06:00
|
|
|
box tomo.ContainerBox
|
2024-05-07 18:15:49 -06:00
|
|
|
|
2023-09-07 16:13:10 -06:00
|
|
|
label *Label
|
|
|
|
icon *Icon
|
2024-05-07 18:15:49 -06:00
|
|
|
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 {
|
2024-08-24 13:11:57 -06:00
|
|
|
button := &Button {
|
|
|
|
box: tomo.NewContainerBox(),
|
|
|
|
label: NewLabel(text),
|
2023-09-07 16:13:10 -06:00
|
|
|
}
|
2024-08-24 13:11:57 -06:00
|
|
|
button.box.SetRole(tomo.R("objects", "Button"))
|
|
|
|
button.label.SetAttr(tomo.AAlign(tomo.AlignMiddle, tomo.AlignMiddle))
|
|
|
|
button.box.SetAttr(tomo.ALayout(buttonLayout))
|
|
|
|
button.SetText(text)
|
2023-09-14 12:48:08 -06:00
|
|
|
|
2024-08-24 13:11:57 -06:00
|
|
|
button.box.SetInputMask(true)
|
|
|
|
button.box.OnButtonDown(button.handleButtonDown)
|
|
|
|
button.box.OnButtonUp(button.handleButtonUp)
|
|
|
|
button.box.OnKeyDown(button.handleKeyDown)
|
|
|
|
button.box.OnKeyUp(button.handleKeyUp)
|
|
|
|
button.box.SetFocusable(true)
|
|
|
|
return button
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBox returns the underlying box.
|
|
|
|
func (this *Button) GetBox () tomo.Box {
|
|
|
|
return this.box
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFocused sets whether or not this button has keyboard focus. If set to
|
|
|
|
// true, this method will steal focus away from whichever object currently has
|
|
|
|
// focus.
|
|
|
|
func (this *Button) SetFocused (focused bool) {
|
|
|
|
this.box.SetFocused(focused)
|
2023-08-08 10:46:19 -06:00
|
|
|
}
|
|
|
|
|
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)
|
2024-05-07 18:15:49 -06:00
|
|
|
if this.labelActive && text == "" {
|
2024-08-24 13:11:57 -06:00
|
|
|
this.box.Remove(this.label)
|
2024-05-07 18:15:49 -06:00
|
|
|
this.labelActive = false
|
|
|
|
}
|
|
|
|
if !this.labelActive && text != "" {
|
2024-08-24 13:11:57 -06:00
|
|
|
this.box.Add(this.label)
|
2024-05-07 18:15:49 -06:00
|
|
|
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) {
|
2024-08-24 13:11:57 -06:00
|
|
|
if this.icon != nil { this.box.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
|
|
|
|
|
2024-05-07 18:15:49 -06:00
|
|
|
if this.icon != nil {
|
2024-08-24 13:11:57 -06:00
|
|
|
this.box.Insert(this.icon, this.label)
|
2023-09-07 16:13:10 -06:00
|
|
|
}
|
2024-08-24 13:11:57 -06:00
|
|
|
this.box.SetTag("icon", this.icon != nil)
|
2024-05-07 18:15:49 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-05-07 18:15:49 -06:00
|
|
|
func (this *Button) applyLayout () {
|
|
|
|
if this.labelActive && this.icon == nil {
|
2024-08-24 13:11:57 -06:00
|
|
|
this.box.SetAttr(tomo.ALayout(buttonLayout))
|
2024-05-07 18:15:49 -06:00
|
|
|
} else if !this.labelActive && this.icon != nil {
|
2024-08-24 13:11:57 -06:00
|
|
|
this.box.SetAttr(tomo.ALayout(iconButtonLayout))
|
2024-05-07 18:15:49 -06:00
|
|
|
} else {
|
2024-08-24 13:11:57 -06:00
|
|
|
this.box.SetAttr(tomo.ALayout(bothButtonLayout))
|
2024-05-07 18:15:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *Button) handleKeyDown (key input.Key, numberPad bool) bool {
|
2024-08-16 14:15:52 -06:00
|
|
|
if !isClickingKey(key) { return false }
|
2024-08-16 13:31:48 -06:00
|
|
|
this.on.click.Broadcast()
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Button) handleKeyUp (key input.Key, numberPad bool) bool {
|
2024-08-16 14:15:52 -06:00
|
|
|
if !isClickingKey(key) { return false }
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2023-08-09 09:35:24 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *Button) handleButtonDown (button input.Button) bool {
|
2024-08-16 14:15:52 -06:00
|
|
|
if !isClickingButton(button) { return false }
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2024-07-21 09:48:28 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *Button) handleButtonUp (button input.Button) bool {
|
2024-08-16 14:15:52 -06:00
|
|
|
if !isClickingButton(button) { return false }
|
2024-08-24 13:11:57 -06:00
|
|
|
if this.box.Window().MousePosition().In(this.box.Bounds()) {
|
2023-08-08 10:46:19 -06:00
|
|
|
this.on.click.Broadcast()
|
|
|
|
}
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2023-08-08 10:46:19 -06:00
|
|
|
}
|