Added value changed event for slider.

Added flag for snapping to integer values.
This commit is contained in:
Sander Schobers 2019-07-09 19:08:40 +02:00
parent 757758d8e9
commit 52d22e7a18

View File

@ -11,7 +11,9 @@ type Slider struct {
Minimum float32
Maximum float32
Value float32
Integer bool
valueChanged func(float32)
handleWidth float32
handle sliderHandle
}
@ -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