Added documentation for the sliders

This commit is contained in:
Sasha Koshka 2023-02-11 17:04:50 -05:00
parent d7a6193c04
commit 7f1c3ae870
2 changed files with 16 additions and 0 deletions

View File

@ -7,12 +7,16 @@ type Numeric interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
// LerpSlider is a slider that has a minimum and maximum value, and who's value
// can be any numeric type.
type LerpSlider[T Numeric] struct {
*Slider
min T
max T
}
// NewLerpSlider creates a new LerpSlider with a minimum and maximum value. If
// vertical is set to true, the slider will be vertical instead of horizontal.
func NewLerpSlider[T Numeric] (min, max T, value T, vertical bool) (element *LerpSlider[T]) {
if min > max {
temp := max
@ -28,17 +32,20 @@ func NewLerpSlider[T Numeric] (min, max T, value T, vertical bool) (element *Ler
return
}
// SetValue sets the slider's value.
func (element *LerpSlider[T]) SetValue (value T) {
value -= element.min
element.Slider.SetValue(float64(value) / float64(element.Range()))
}
// Value returns the slider's value.
func (element *LerpSlider[T]) Value () (value T) {
return T (
float64(element.Slider.Value()) * float64(element.Range())) +
element.min
}
// Range returns the difference between the slider's maximum and minimum values.
func (element *LerpSlider[T]) Range () T {
return element.max - element.min
}

View File

@ -7,6 +7,7 @@ import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
// Slider is a slider control with a floating point value between zero and one.
type Slider struct {
*core.Core
*core.FocusableCore
@ -27,6 +28,8 @@ type Slider struct {
onRelease func ()
}
// NewSlider creates a new slider with the specified value. If vertical is set
// to true,
func NewSlider (value float64, vertical bool) (element *Slider) {
element = &Slider {
value: value,
@ -85,14 +88,17 @@ func (element *Slider) HandleKeyDown (key input.Key, modifiers input.Modifiers)
func (element *Slider) HandleKeyUp (key input.Key, modifiers input.Modifiers) { }
// Value returns the slider's value.
func (element *Slider) Value () (value float64) {
return element.value
}
// SetEnabled sets whether or not the slider can be interacted with.
func (element *Slider) SetEnabled (enabled bool) {
element.focusableControl.SetEnabled(enabled)
}
// SetValue sets the slider's value.
func (element *Slider) SetValue (value float64) {
if value < 0 { value = 0 }
if value > 1 { value = 1 }
@ -103,10 +109,13 @@ func (element *Slider) SetValue (value float64) {
element.redo()
}
// OnSlide sets a function to be called every time the slider handle changes
// position while being dragged.
func (element *Slider) OnSlide (callback func ()) {
element.onSlide = callback
}
// OnRelease sets a function to be called when the handle stops being dragged.
func (element *Slider) OnRelease (callback func ()) {
element.onRelease = callback
}