2024-05-07 16:24:37 -06:00
|
|
|
package objects
|
|
|
|
|
|
|
|
import "math"
|
|
|
|
import "strconv"
|
|
|
|
import "git.tebibyte.media/tomo/tomo"
|
2024-08-24 18:19:01 -06:00
|
|
|
import "git.tebibyte.media/tomo/tomo/text"
|
2024-05-07 16:24:37 -06:00
|
|
|
import "git.tebibyte.media/tomo/tomo/input"
|
|
|
|
import "git.tebibyte.media/tomo/tomo/event"
|
|
|
|
import "git.tebibyte.media/tomo/objects/layouts"
|
|
|
|
|
2024-08-24 18:19:01 -06:00
|
|
|
var _ tomo.Object = new(NumberInput)
|
|
|
|
|
2024-05-07 16:24:37 -06:00
|
|
|
// NumberInput is an editable text box which accepts only numbers, and has
|
|
|
|
// controls to increment and decrement its value.
|
|
|
|
type NumberInput struct {
|
2024-08-24 18:19:01 -06:00
|
|
|
box tomo.ContainerBox
|
2024-05-07 16:24:37 -06:00
|
|
|
input *TextInput
|
|
|
|
increment *Button
|
|
|
|
decrement *Button
|
|
|
|
on struct {
|
2024-06-27 12:01:14 -06:00
|
|
|
valueChange event.FuncBroadcaster
|
2024-06-27 12:09:58 -06:00
|
|
|
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 {
|
2024-08-24 18:19:01 -06:00
|
|
|
numberInput := &NumberInput {
|
|
|
|
box: tomo.NewContainerBox(),
|
|
|
|
input: NewTextInput(""),
|
|
|
|
increment: NewButton(""),
|
|
|
|
decrement: NewButton(""),
|
2024-05-07 16:24:37 -06:00
|
|
|
}
|
2024-08-24 18:19:01 -06:00
|
|
|
numberInput.box.SetRole(tomo.R("objects", "NumberInput"))
|
|
|
|
numberInput.box.Add(numberInput.input)
|
|
|
|
numberInput.box.Add(numberInput.decrement)
|
|
|
|
numberInput.box.Add(numberInput.increment)
|
|
|
|
numberInput.box.SetAttr(tomo.ALayout(layouts.Row { true, false, false }))
|
|
|
|
numberInput.increment.SetIcon(tomo.IconValueIncrement)
|
|
|
|
numberInput.decrement.SetIcon(tomo.IconValueDecrement)
|
2024-05-07 16:24:37 -06:00
|
|
|
|
2024-08-24 18:19:01 -06:00
|
|
|
numberInput.SetValue(value)
|
2024-05-07 16:24:37 -06:00
|
|
|
|
2024-08-24 18:19:01 -06:00
|
|
|
numberInput.box.OnScroll(numberInput.handleScroll)
|
|
|
|
numberInput.box.OnKeyDown(numberInput.handleKeyDown)
|
|
|
|
numberInput.box.OnKeyUp(numberInput.handleKeyUp)
|
|
|
|
numberInput.input.OnConfirm(numberInput.handleConfirm)
|
|
|
|
numberInput.input.OnValueChange(numberInput.on.valueChange.Broadcast)
|
|
|
|
numberInput.increment.OnClick(func () { numberInput.shift( 1) })
|
|
|
|
numberInput.decrement.OnClick(func () { numberInput.shift(-1) })
|
|
|
|
return numberInput
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBox returns the underlying box.
|
|
|
|
func (this *NumberInput) GetBox () tomo.Box {
|
|
|
|
return this.box
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFocused sets whether or not this number input has keyboard focus. If set
|
|
|
|
// to true, this method will steal focus away from whichever object currently
|
|
|
|
// has focus.
|
|
|
|
func (this *NumberInput) SetFocused (focused bool) {
|
|
|
|
this.input.SetFocused(focused)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select sets the text cursor or selection.
|
|
|
|
func (this *NumberInput) Select (dot text.Dot) {
|
|
|
|
this.input.Select(dot)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dot returns the text cursor or selection.
|
|
|
|
func (this *NumberInput) Dot () text.Dot {
|
|
|
|
return this.input.Dot()
|
2024-05-07 16:24:37 -06:00
|
|
|
}
|
|
|
|
|
2024-06-27 12:01:14 -06:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:01:14 -06:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2024-06-27 12:09:58 -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))
|
2024-06-27 12:01:14 -06:00
|
|
|
this.on.valueChange.Broadcast()
|
2024-05-07 16:24:37 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *NumberInput) handleKeyDown (key input.Key, numpad bool) bool {
|
2024-07-21 09:48:28 -06:00
|
|
|
switch key {
|
|
|
|
case input.KeyUp:
|
|
|
|
this.shift(1)
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2024-07-21 09:48:28 -06:00
|
|
|
case input.KeyDown:
|
|
|
|
this.shift(-1)
|
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
|
|
|
return false
|
2024-07-21 09:48:28 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *NumberInput) handleKeyUp (key input.Key, numpad bool) bool {
|
2024-05-07 16:24:37 -06:00
|
|
|
switch key {
|
2024-07-25 10:58:38 -06:00
|
|
|
case input.KeyUp: return true
|
|
|
|
case input.KeyDown: return true
|
2024-05-07 16:24:37 -06:00
|
|
|
}
|
2024-07-25 10:58:38 -06:00
|
|
|
return false
|
2024-05-07 16:24:37 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *NumberInput) handleScroll (x, y float64) bool {
|
2024-05-07 16:24:37 -06:00
|
|
|
if x == 0 {
|
|
|
|
this.shift(-int(math.Round(y)))
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2024-05-07 16:24:37 -06:00
|
|
|
}
|
2024-07-25 10:58:38 -06:00
|
|
|
return false
|
2024-05-07 16:24:37 -06:00
|
|
|
}
|
|
|
|
|
2024-06-27 12:09:58 -06:00
|
|
|
func (this *NumberInput) handleConfirm () {
|
2024-05-07 16:24:37 -06:00
|
|
|
this.SetValue(this.Value())
|
2024-06-27 12:09:58 -06:00
|
|
|
this.on.confirm.Broadcast()
|
2024-05-07 16:24:37 -06:00
|
|
|
}
|