2024-01-01 16:19:40 -06:00
|
|
|
/*
|
|
|
|
A slider that goes between a High and Low time
|
|
|
|
*/
|
|
|
|
|
|
|
|
package gadgets
|
|
|
|
|
2024-01-18 05:04:18 -06:00
|
|
|
import (
|
2024-01-01 16:19:40 -06:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2024-01-18 05:04:18 -06:00
|
|
|
"go.wit.com/gui"
|
2024-01-05 20:29:57 -06:00
|
|
|
"go.wit.com/log"
|
2024-01-01 16:19:40 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type Duration struct {
|
2024-01-18 05:04:18 -06:00
|
|
|
p *gui.Node // parent widget
|
|
|
|
l *gui.Node // label widget
|
|
|
|
s *gui.Node // slider widget
|
2024-01-01 16:19:40 -06:00
|
|
|
|
2024-01-18 05:04:18 -06:00
|
|
|
Label string
|
|
|
|
Low time.Duration
|
|
|
|
High time.Duration
|
|
|
|
Duration time.Duration
|
2024-01-01 16:19:40 -06:00
|
|
|
|
2024-01-18 05:04:18 -06:00
|
|
|
Custom func()
|
2024-01-01 16:19:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Duration) Set(d time.Duration) {
|
|
|
|
var timeRange, step, offset time.Duration
|
|
|
|
|
2024-01-18 05:04:18 -06:00
|
|
|
if d > n.High {
|
2024-01-01 16:19:40 -06:00
|
|
|
d = n.High
|
|
|
|
}
|
2024-01-18 05:04:18 -06:00
|
|
|
if d < n.Low {
|
2024-01-01 16:19:40 -06:00
|
|
|
d = n.Low
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the duration
|
|
|
|
n.Duration = d
|
|
|
|
|
|
|
|
// figure out the integer offset for the Slider GUI Widget
|
|
|
|
timeRange = n.High - n.Low
|
|
|
|
step = timeRange / 1000
|
2024-01-18 05:04:18 -06:00
|
|
|
if step == 0 {
|
2024-01-21 11:42:31 -06:00
|
|
|
log.Log(GADGETS, "duration.Set() division by step == 0", n.Low, n.High, timeRange, step)
|
2024-01-31 14:02:20 -06:00
|
|
|
n.s.SetInt(0)
|
2024-01-01 16:19:40 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
offset = d - n.Low
|
|
|
|
i := int(offset / step)
|
2024-01-21 11:42:31 -06:00
|
|
|
log.Log(GADGETS, "duration.Set() =", n.Low, n.High, d, "i =", i)
|
2024-01-11 20:37:39 -06:00
|
|
|
// n.s.I = i
|
2024-01-31 14:02:20 -06:00
|
|
|
n.s.SetInt(i)
|
2024-01-01 16:19:40 -06:00
|
|
|
n.s.Custom()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDurationSlider(n *gui.Node, label string, low time.Duration, high time.Duration) *Duration {
|
2024-01-18 05:04:18 -06:00
|
|
|
d := Duration{
|
|
|
|
p: n,
|
2024-01-01 16:19:40 -06:00
|
|
|
Label: label,
|
2024-01-18 05:04:18 -06:00
|
|
|
High: high,
|
|
|
|
Low: low,
|
2024-01-01 16:19:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// various timeout settings
|
|
|
|
d.l = n.NewLabel(label)
|
|
|
|
d.s = n.NewSlider(label, 0, 1000)
|
2024-01-18 05:04:18 -06:00
|
|
|
d.s.Custom = func() {
|
|
|
|
d.Duration = low + (high-low)*time.Duration(d.s.Int())/1000
|
2024-01-01 16:19:40 -06:00
|
|
|
log.Println("d.Duration =", d.Duration)
|
|
|
|
s := fmt.Sprintf("%s (%v)", d.Label, d.Duration)
|
|
|
|
d.l.SetText(s)
|
2024-01-18 05:04:18 -06:00
|
|
|
if d.Custom != nil {
|
2024-01-01 16:19:40 -06:00
|
|
|
d.Custom()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &d
|
|
|
|
}
|