diff --git a/ui/slider.go b/ui/slider.go index 6eb5051..324f917 100644 --- a/ui/slider.go +++ b/ui/slider.go @@ -11,9 +11,11 @@ type Slider struct { Minimum float32 Maximum float32 Value float32 + Integer bool - handleWidth float32 - handle sliderHandle + valueChanged func(float32) + handleWidth float32 + handle sliderHandle } func BuildSlider(o Orientation, fn func(s *Slider)) *Slider { @@ -34,11 +36,24 @@ func (s *Slider) handleDrag(pos geom.PointF32) { } else { offset = pos.Y } - s.Value = s.Minimum + ((offset-start)/length)*(s.Maximum-s.Minimum) + s.setValue(s.Minimum + ((offset-start)/length)*(s.Maximum-s.Minimum)) +} + +func (s *Slider) setValue(v float32) { if s.Minimum < s.Maximum { - s.Value = geom.Min32(s.Maximum, geom.Max32(s.Minimum, s.Value)) + v = geom.Min32(s.Maximum, geom.Max32(s.Minimum, v)) } else { - s.Value = geom.Min32(s.Minimum, geom.Max32(s.Maximum, s.Value)) + v = geom.Min32(s.Minimum, geom.Max32(s.Maximum, v)) + } + if s.Integer { + v = geom.Round32(v) + } + if s.Value != v { + s.Value = v + valueChanged := s.valueChanged + if valueChanged != nil { + valueChanged(s.Value) + } } } @@ -97,6 +112,7 @@ func (s *Slider) DesiredSize(ctx Context) geom.PointF32 { func (s *Slider) Handle(ctx Context, e Event) { s.handle.Handle(ctx, e) s.ControlBase.Handle(ctx, e) + s.setValue(s.Value) } func (s *Slider) Render(ctx Context) { @@ -111,6 +127,10 @@ func (s *Slider) Render(ctx Context) { s.handle.Render(ctx) } +func (s *Slider) OnValueChanged(fn func(float32)) { + s.valueChanged = fn +} + type sliderHandle struct { ControlBase