objects/button.go

44 lines
1.2 KiB
Go
Raw Permalink 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/theme"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
// Button is a clickable button.
type Button struct {
tomo.TextBox
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 {
box := &Button { TextBox: tomo.NewTextBox() }
2023-08-10 15:52:01 -06:00
theme.Apply(box, theme.R("objects", "Button", ""))
2023-08-08 10:46:19 -06:00
box.SetText(text)
2023-08-09 09:35:24 -06:00
box.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
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
}
// 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)
}
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()
}
}