objects/button.go
2023-08-08 12:46:19 -04:00

36 lines
920 B
Go

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.
func NewButton (text string) Button {
box := Button { TextBox: tomo.NewTextBox() }
theme.Apply(box, theme.R("objects", "Button"))
box.SetText(text)
box.OnMouseUp(box.handleMouseUp)
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)
}
func (this *Button) handleMouseUp (button input.Button) {
if button != input.ButtonLeft { return }
if this.MousePosition().In(this.Bounds()) {
this.on.click.Broadcast()
}
}