2024-05-07 11:45:06 -06:00
|
|
|
package objects
|
|
|
|
|
|
|
|
import "git.tebibyte.media/tomo/tomo"
|
|
|
|
import "git.tebibyte.media/tomo/tomo/input"
|
|
|
|
import "git.tebibyte.media/tomo/tomo/event"
|
|
|
|
|
|
|
|
// Checkbox is a control that can be toggled.
|
|
|
|
type Checkbox struct {
|
|
|
|
tomo.Box
|
|
|
|
value bool
|
|
|
|
on struct {
|
|
|
|
valueChange event.FuncBroadcaster
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCheckbox creates a new checkbox with the specified value.
|
|
|
|
func NewCheckbox (value bool) *Checkbox {
|
|
|
|
box := &Checkbox {
|
|
|
|
Box: tomo.NewBox(),
|
|
|
|
}
|
2024-06-03 19:13:18 -06:00
|
|
|
box.SetRole(tomo.R("objects", "Checkbox", ""))
|
2024-06-22 13:37:52 -06:00
|
|
|
box.SetValue(value)
|
2024-05-07 11:45:06 -06:00
|
|
|
|
|
|
|
box.OnMouseUp(box.handleMouseUp)
|
|
|
|
box.OnKeyUp(box.handleKeyUp)
|
|
|
|
box.SetFocusable(true)
|
|
|
|
return box
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetValue sets the value of the checkbox.
|
|
|
|
func (this *Checkbox) SetValue (value bool) {
|
|
|
|
this.value = value
|
|
|
|
if this.value {
|
2024-05-27 13:22:18 -06:00
|
|
|
this.SetTextureCenter(tomo.IconCheckboxChecked.Texture(tomo.IconSizeSmall))
|
2024-05-07 11:45:06 -06:00
|
|
|
} else {
|
2024-05-27 13:59:24 -06:00
|
|
|
this.SetTextureCenter(tomo.IconCheckboxUnchecked.Texture(tomo.IconSizeSmall))
|
2024-05-07 11:45:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Toggle toggles the value of the checkbox between true and false.
|
|
|
|
func (this *Checkbox) Toggle () {
|
|
|
|
this.SetValue(!this.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the value of the checkbox.
|
|
|
|
func (this *Checkbox) Value () bool {
|
|
|
|
return this.value
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnValueChange specifies a function to be called when the checkbox's value
|
|
|
|
// changes.
|
|
|
|
func (this *Checkbox) OnValueChange (callback func ()) event.Cookie {
|
|
|
|
return this.on.valueChange.Connect(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Checkbox) handleKeyUp (key input.Key, numberPad bool) {
|
|
|
|
if key != input.KeyEnter && key != input.Key(' ') { return }
|
|
|
|
this.Toggle()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Checkbox) handleMouseUp (button input.Button) {
|
|
|
|
if button != input.ButtonLeft { return }
|
|
|
|
if this.MousePosition().In(this.Bounds()) {
|
|
|
|
this.Toggle()
|
|
|
|
}
|
|
|
|
}
|