objects/numberinput.go

108 lines
2.8 KiB
Go
Raw Normal View History

2024-05-07 16:24:37 -06:00
package objects
import "math"
import "strconv"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
// NumberInput is an editable text box which accepts only numbers, and has
// controls to increment and decrement its value.
type NumberInput struct {
tomo.ContainerBox
input *TextInput
increment *Button
decrement *Button
on struct {
valueChange event.FuncBroadcaster
confirm event.FuncBroadcaster
2024-05-07 16:24:37 -06:00
}
}
// NewNumberInput creates a new number input with the specified value.
func NewNumberInput (value float64) *NumberInput {
box := &NumberInput {
ContainerBox: tomo.NewContainerBox(),
input: NewTextInput(""),
increment: NewButton(""),
decrement: NewButton(""),
}
2024-07-21 09:48:28 -06:00
box.SetRole(tomo.R("objects", "NumberInput"))
2024-05-07 16:24:37 -06:00
box.Add(box.input)
box.Add(box.decrement)
box.Add(box.increment)
2024-06-22 16:44:26 -06:00
box.SetLayout(layouts.Row { true, false, false })
2024-05-27 13:22:18 -06:00
box.increment.SetIcon(tomo.IconValueIncrement)
box.decrement.SetIcon(tomo.IconValueDecrement)
2024-05-07 16:24:37 -06:00
box.SetValue(value)
box.OnScroll(box.handleScroll)
2024-07-21 09:48:28 -06:00
box.OnKeyUp(box.handleKeyUp)
2024-05-07 16:24:37 -06:00
box.OnKeyDown(box.handleKeyDown)
box.input.OnConfirm(box.handleConfirm)
box.input.OnValueChange(box.on.valueChange.Broadcast)
2024-05-07 16:24:37 -06:00
box.increment.OnClick(func () { box.shift(1) })
box.decrement.OnClick(func () { box.shift(-1) })
return box
}
// Value returns the value of the input.
func (this *NumberInput) Value () float64 {
value, _ := strconv.ParseFloat(this.input.Text(), 64)
return value
}
2024-05-07 16:24:37 -06:00
// SetValue sets the value of the input.
func (this *NumberInput) SetValue (value float64) {
this.input.SetText(strconv.FormatFloat(value, 'g', -1, 64))
}
// OnValueChange specifies a function to be called when the user edits the input
// value.
func (this *NumberInput) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
2024-05-07 16:24:37 -06:00
}
// OnConfirm specifies a function to be called when the user presses enter within
// the number input.
func (this *NumberInput) OnConfirm (callback func ()) event.Cookie {
return this.on.confirm.Connect(callback)
2024-05-07 16:24:37 -06:00
}
func (this *NumberInput) shift (by int) {
this.SetValue(this.Value() + float64(by))
this.on.valueChange.Broadcast()
2024-05-07 16:24:37 -06:00
}
2024-07-21 09:48:28 -06:00
func (this *NumberInput) handleKeyDown (catch func (), key input.Key, numpad bool) {
switch key {
case input.KeyUp:
this.shift(1)
catch()
case input.KeyDown:
this.shift(-1)
catch()
}
}
func (this *NumberInput) handleKeyUp (catch func (), key input.Key, numpad bool) {
2024-05-07 16:24:37 -06:00
switch key {
2024-07-21 09:48:28 -06:00
case input.KeyUp: catch()
case input.KeyDown: catch()
2024-05-07 16:24:37 -06:00
}
}
2024-07-21 09:48:28 -06:00
func (this *NumberInput) handleScroll (catch func (), x, y float64) {
2024-05-07 16:24:37 -06:00
if x == 0 {
2024-07-21 09:48:28 -06:00
catch()
2024-05-07 16:24:37 -06:00
this.shift(-int(math.Round(y)))
}
}
func (this *NumberInput) handleConfirm () {
2024-05-07 16:24:37 -06:00
this.SetValue(this.Value())
this.on.confirm.Broadcast()
2024-05-07 16:24:37 -06:00
}