Compare commits

...

3 Commits

Author SHA1 Message Date
a0fe7bc00f Slider has an OnSlide method instead of OnValueChange 2024-05-07 18:24:44 -04:00
1ff9982e01 Add NumberInput 2024-05-07 18:24:37 -04:00
ca2f9654b3 Add OnEdit to TextInput 2024-05-07 18:24:25 -04:00
3 changed files with 120 additions and 7 deletions

View File

@ -13,6 +13,7 @@ type TextInput struct {
text []rune text []rune
on struct { on struct {
enter event.FuncBroadcaster enter event.FuncBroadcaster
edit event.FuncBroadcaster
} }
} }
@ -47,6 +48,11 @@ func (this *TextInput) OnEnter (callback func ()) event.Cookie {
return this.on.enter.Connect(callback) return this.on.enter.Connect(callback)
} }
// OnEdit specifies a function to be called when the user edits the input text.
func (this *TextInput) OnEdit (callback func ()) event.Cookie {
return this.on.edit.Connect(callback)
}
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) { func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
dot := this.Dot() dot := this.Dot()
modifiers := this.Modifiers() modifiers := this.Modifiers()
@ -93,7 +99,10 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
} }
this.Select(dot) this.Select(dot)
if changed { this.SetText(string(this.text)) } if changed {
this.SetText(string(this.text))
this.on.edit.Broadcast()
}
} }
func (this *TextInput) handleScroll (x, y float64) { func (this *TextInput) handleScroll (x, y float64) {

100
numberinput.go Normal file
View File

@ -0,0 +1,100 @@
package objects
import "math"
import "strconv"
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"
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 {
enter event.FuncBroadcaster
edit event.FuncBroadcaster
}
}
// 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(""),
}
theme.Apply(box, theme.R("objects", "NumberInput", ""))
box.Add(box.input)
box.Add(box.increment)
box.Add(box.decrement)
box.SetLayout(layouts.NewGrid([]bool { true, false, false }, []bool { true }))
box.increment.SetIcon(theme.IconActionIncrement)
box.decrement.SetIcon(theme.IconActionDecrement)
box.SetValue(value)
box.CaptureScroll(true)
box.CaptureKeyboard(true)
box.OnScroll(box.handleScroll)
box.OnKeyDown(box.handleKeyDown)
box.input.OnEnter(box.handleEnter)
box.input.OnEdit(box.on.edit.Broadcast)
box.increment.OnClick(func () { box.shift(1) })
box.decrement.OnClick(func () { box.shift(-1) })
return box
}
// SetValue sets the value of the input.
func (this *NumberInput) SetValue (value float64) {
this.input.SetText(strconv.FormatFloat(value, 'g', -1, 64))
}
// Value returns the value of the input.
func (this *NumberInput) Value () float64 {
value, _ := strconv.ParseFloat(this.input.Text(), 64)
return value
}
// OnEnter specifies a function to be called when the user presses enter within
// the text input.
func (this *NumberInput) OnEnter (callback func ()) event.Cookie {
return this.on.enter.Connect(callback)
}
// OnEdit specifies a function to be called when the user edits the input value.
func (this *NumberInput) OnEdit (callback func ()) event.Cookie {
return this.on.edit.Connect(callback)
}
func (this *NumberInput) shift (by int) {
this.SetValue(this.Value() + float64(by))
this.on.edit.Broadcast()
}
func (this *NumberInput) handleKeyDown (key input.Key, numpad bool) {
switch key {
case input.KeyUp: this.shift(1)
case input.KeyDown: this.shift(-1)
default: this.input.handleKeyDown(key, numpad)
}
}
func (this *NumberInput) handleScroll (x, y float64) {
if x == 0 {
this.shift(-int(math.Round(y)))
} else {
this.input.handleScroll(x, y)
}
}
func (this *NumberInput) handleEnter () {
this.SetValue(this.Value())
this.on.enter.Broadcast()
}

View File

@ -15,7 +15,7 @@ type Slider struct {
dragOffset image.Point dragOffset image.Point
on struct { on struct {
valueChange event.FuncBroadcaster slide event.FuncBroadcaster
} }
} }
@ -71,7 +71,6 @@ func (this *Slider) SetValue (value float64) {
if value == this.layout.value { return } if value == this.layout.value { return }
this.layout.value = value this.layout.value = value
this.SetLayout(this.layout) this.SetLayout(this.layout)
this.on.valueChange.Broadcast()
} }
// Value returns the value of the slider between 0 and 1. // Value returns the value of the slider between 0 and 1.
@ -79,10 +78,10 @@ func (this *Slider) Value () float64 {
return this.layout.value return this.layout.value
} }
// OnValueChange specifies a function to be called when the slider's value // OnValueChange specifies a function to be called when the user moves the
// changes. // slider.
func (this *Slider) OnValueChange (callback func ()) event.Cookie { func (this *Slider) OnSlide (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback) return this.on.slide.Connect(callback)
} }
func (this *Slider) handleKeyDown (key input.Key, numpad bool) { func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
@ -139,14 +138,18 @@ func (this *Slider) handleMouseDown (button input.Button) {
case input.ButtonMiddle: case input.ButtonMiddle:
if above { if above {
this.SetValue(0) this.SetValue(0)
this.on.slide.Broadcast()
} else { } else {
this.SetValue(1) this.SetValue(1)
this.on.slide.Broadcast()
} }
case input.ButtonRight: case input.ButtonRight:
if above { if above {
this.SetValue(this.Value() - 0.05) this.SetValue(this.Value() - 0.05)
this.on.slide.Broadcast()
} else { } else {
this.SetValue(this.Value() + 0.05) this.SetValue(this.Value() + 0.05)
this.on.slide.Broadcast()
} }
} }
} }
@ -176,6 +179,7 @@ func (this *Slider) drag () {
float64(pointer.X) / float64(pointer.X) /
float64(gutter.Dx() - handle.Dx())) float64(gutter.Dx() - handle.Dx()))
} }
this.on.slide.Broadcast()
} }
func (this *Slider) fallbackDragOffset () image.Point { func (this *Slider) fallbackDragOffset () image.Point {