2023-03-30 21:19:04 -06:00
|
|
|
package elements
|
2023-02-10 19:55:59 -07:00
|
|
|
|
2023-04-19 23:04:03 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
|
|
|
|
2023-02-10 19:55:59 -07:00
|
|
|
// Numeric is a type constraint representing a number.
|
|
|
|
type Numeric interface {
|
|
|
|
~float32 | ~float64 |
|
|
|
|
~int | ~int8 | ~int16 | ~int32 | ~int64 |
|
|
|
|
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:04:50 -07:00
|
|
|
// LerpSlider is a slider that has a minimum and maximum value, and who's value
|
|
|
|
// can be any numeric type.
|
2023-02-10 19:55:59 -07:00
|
|
|
type LerpSlider[T Numeric] struct {
|
2023-04-19 23:04:03 -06:00
|
|
|
slider
|
2023-02-10 19:55:59 -07:00
|
|
|
min T
|
|
|
|
max T
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:10:47 -06:00
|
|
|
// NewVLerpSlider creates a new horizontal LerpSlider with a minimum and maximum
|
|
|
|
// value.
|
|
|
|
func NewVLerpSlider[T Numeric] (min, max T, value T) (element *LerpSlider[T]) {
|
|
|
|
element = NewHLerpSlider(min, max, value)
|
|
|
|
element.vertical = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHLerpSlider creates a new horizontal LerpSlider with a minimum and maximum
|
|
|
|
// value.
|
|
|
|
func NewHLerpSlider[T Numeric] (min, max T, value T) (element *LerpSlider[T]) {
|
2023-04-19 23:04:03 -06:00
|
|
|
if min > max { min, max = max, min }
|
2023-02-10 19:55:59 -07:00
|
|
|
element = &LerpSlider[T] {
|
|
|
|
min: min,
|
|
|
|
max: max,
|
|
|
|
}
|
2023-04-19 23:04:03 -06:00
|
|
|
element.entity = tomo.NewEntity(element).(tomo.FocusableEntity)
|
|
|
|
element.construct()
|
2023-02-10 19:55:59 -07:00
|
|
|
element.SetValue(value)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:04:50 -07:00
|
|
|
// SetValue sets the slider's value.
|
2023-02-10 19:55:59 -07:00
|
|
|
func (element *LerpSlider[T]) SetValue (value T) {
|
|
|
|
value -= element.min
|
2023-04-19 23:04:03 -06:00
|
|
|
element.slider.SetValue(float64(value) / float64(element.Range()))
|
2023-02-10 19:55:59 -07:00
|
|
|
}
|
|
|
|
|
2023-02-11 15:04:50 -07:00
|
|
|
// Value returns the slider's value.
|
2023-02-10 19:55:59 -07:00
|
|
|
func (element *LerpSlider[T]) Value () (value T) {
|
|
|
|
return T (
|
2023-04-19 23:04:03 -06:00
|
|
|
float64(element.slider.Value()) * float64(element.Range())) +
|
2023-02-10 19:55:59 -07:00
|
|
|
element.min
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:04:50 -07:00
|
|
|
// Range returns the difference between the slider's maximum and minimum values.
|
2023-02-10 19:55:59 -07:00
|
|
|
func (element *LerpSlider[T]) Range () T {
|
|
|
|
return element.max - element.min
|
|
|
|
}
|