Compare commits
3 Commits
4d790f7264
...
a0fe7bc00f
Author | SHA1 | Date | |
---|---|---|---|
a0fe7bc00f | |||
1ff9982e01 | |||
ca2f9654b3 |
11
input.go
11
input.go
@ -13,6 +13,7 @@ type TextInput struct {
|
||||
text []rune
|
||||
on struct {
|
||||
enter event.FuncBroadcaster
|
||||
edit event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,6 +48,11 @@ func (this *TextInput) OnEnter (callback func ()) event.Cookie {
|
||||
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) {
|
||||
dot := this.Dot()
|
||||
modifiers := this.Modifiers()
|
||||
@ -93,7 +99,10 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
|
||||
}
|
||||
|
||||
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) {
|
||||
|
100
numberinput.go
Normal file
100
numberinput.go
Normal 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()
|
||||
}
|
16
slider.go
16
slider.go
@ -15,7 +15,7 @@ type Slider struct {
|
||||
dragOffset image.Point
|
||||
|
||||
on struct {
|
||||
valueChange event.FuncBroadcaster
|
||||
slide event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,7 +71,6 @@ func (this *Slider) SetValue (value float64) {
|
||||
if value == this.layout.value { return }
|
||||
this.layout.value = value
|
||||
this.SetLayout(this.layout)
|
||||
this.on.valueChange.Broadcast()
|
||||
}
|
||||
|
||||
// Value returns the value of the slider between 0 and 1.
|
||||
@ -79,10 +78,10 @@ func (this *Slider) Value () float64 {
|
||||
return this.layout.value
|
||||
}
|
||||
|
||||
// OnValueChange specifies a function to be called when the slider's value
|
||||
// changes.
|
||||
func (this *Slider) OnValueChange (callback func ()) event.Cookie {
|
||||
return this.on.valueChange.Connect(callback)
|
||||
// OnValueChange specifies a function to be called when the user moves the
|
||||
// slider.
|
||||
func (this *Slider) OnSlide (callback func ()) event.Cookie {
|
||||
return this.on.slide.Connect(callback)
|
||||
}
|
||||
|
||||
func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
|
||||
@ -139,14 +138,18 @@ func (this *Slider) handleMouseDown (button input.Button) {
|
||||
case input.ButtonMiddle:
|
||||
if above {
|
||||
this.SetValue(0)
|
||||
this.on.slide.Broadcast()
|
||||
} else {
|
||||
this.SetValue(1)
|
||||
this.on.slide.Broadcast()
|
||||
}
|
||||
case input.ButtonRight:
|
||||
if above {
|
||||
this.SetValue(this.Value() - 0.05)
|
||||
this.on.slide.Broadcast()
|
||||
} else {
|
||||
this.SetValue(this.Value() + 0.05)
|
||||
this.on.slide.Broadcast()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -176,6 +179,7 @@ func (this *Slider) drag () {
|
||||
float64(pointer.X) /
|
||||
float64(gutter.Dx() - handle.Dx()))
|
||||
}
|
||||
this.on.slide.Broadcast()
|
||||
}
|
||||
|
||||
func (this *Slider) fallbackDragOffset () image.Point {
|
||||
|
Loading…
x
Reference in New Issue
Block a user