Forgot about sliders lol

This commit is contained in:
Sasha Koshka 2023-04-20 01:04:03 -04:00
parent 17fda82bbe
commit ff3802ca5e
2 changed files with 47 additions and 40 deletions

View File

@ -1,5 +1,7 @@
package elements package elements
import "git.tebibyte.media/sashakoshka/tomo"
// Numeric is a type constraint representing a number. // Numeric is a type constraint representing a number.
type Numeric interface { type Numeric interface {
~float32 | ~float64 | ~float32 | ~float64 |
@ -10,7 +12,7 @@ type Numeric interface {
// LerpSlider is a slider that has a minimum and maximum value, and who's value // LerpSlider is a slider that has a minimum and maximum value, and who's value
// can be any numeric type. // can be any numeric type.
type LerpSlider[T Numeric] struct { type LerpSlider[T Numeric] struct {
*Slider slider
min T min T
max T max T
} }
@ -22,16 +24,14 @@ func NewLerpSlider[T Numeric] (
) ( ) (
element *LerpSlider[T], element *LerpSlider[T],
) { ) {
if min > max { if min > max { min, max = max, min }
temp := max
max = min
min = temp
}
element = &LerpSlider[T] { element = &LerpSlider[T] {
Slider: NewSlider(0, orientation),
min: min, min: min,
max: max, max: max,
} }
element.vertical = bool(orientation)
element.entity = tomo.NewEntity(element).(tomo.FocusableEntity)
element.construct()
element.SetValue(value) element.SetValue(value)
return return
} }
@ -39,13 +39,13 @@ func NewLerpSlider[T Numeric] (
// SetValue sets the slider's value. // SetValue sets the slider's value.
func (element *LerpSlider[T]) SetValue (value T) { func (element *LerpSlider[T]) SetValue (value T) {
value -= element.min value -= element.min
element.Slider.SetValue(float64(value) / float64(element.Range())) element.slider.SetValue(float64(value) / float64(element.Range()))
} }
// Value returns the slider's value. // Value returns the slider's value.
func (element *LerpSlider[T]) Value () (value T) { func (element *LerpSlider[T]) Value () (value T) {
return T ( return T (
float64(element.Slider.Value()) * float64(element.Range())) + float64(element.slider.Value()) * float64(element.Range())) +
element.min element.min
} }

View File

@ -9,6 +9,20 @@ import "git.tebibyte.media/sashakoshka/tomo/default/config"
// Slider is a slider control with a floating point value between zero and one. // Slider is a slider control with a floating point value between zero and one.
type Slider struct { type Slider struct {
slider
}
// NewSlider creates a new slider with the specified value.
func NewSlider (value float64, orientation Orientation) (element *Slider) {
element = &Slider { }
element.value = value
element.vertical = bool(orientation)
element.entity = tomo.NewEntity(element).(tomo.FocusableEntity)
element.construct()
return
}
type slider struct {
entity tomo.FocusableEntity entity tomo.FocusableEntity
value float64 value float64
@ -26,30 +40,23 @@ type Slider struct {
onRelease func () onRelease func ()
} }
// NewSlider creates a new slider with the specified value. func (element *slider) construct () {
func NewSlider (value float64, orientation Orientation) (element *Slider) { element.enabled = true
element = &Slider { if element.vertical {
value: value,
vertical: bool(orientation),
enabled: true,
}
if orientation == Vertical {
element.theme.Case = tomo.C("tomo", "sliderVertical") element.theme.Case = tomo.C("tomo", "sliderVertical")
} else { } else {
element.theme.Case = tomo.C("tomo", "sliderHorizontal") element.theme.Case = tomo.C("tomo", "sliderHorizontal")
} }
element.entity = tomo.NewEntity(element).(tomo.FocusableEntity)
element.updateMinimumSize() element.updateMinimumSize()
return
} }
// Entity returns this element's entity. // Entity returns this element's entity.
func (element *Slider) Entity () tomo.Entity { func (element *slider) Entity () tomo.Entity {
return element.entity return element.entity
} }
// Draw causes the element to draw to the specified destination canvas. // Draw causes the element to draw to the specified destination canvas.
func (element *Slider) Draw (destination canvas.Canvas) { func (element *slider) Draw (destination canvas.Canvas) {
bounds := element.entity.Bounds() bounds := element.entity.Bounds()
element.track = element.theme.Padding(tomo.PatternGutter).Apply(bounds) element.track = element.theme.Padding(tomo.PatternGutter).Apply(bounds)
if element.vertical { if element.vertical {
@ -78,27 +85,27 @@ func (element *Slider) Draw (destination canvas.Canvas) {
} }
// Focus gives this element input focus. // Focus gives this element input focus.
func (element *Slider) Focus () { func (element *slider) Focus () {
if !element.entity.Focused() { element.entity.Focus() } if !element.entity.Focused() { element.entity.Focus() }
} }
// Enabled returns whether this slider can be dragged or not. // Enabled returns whether this slider can be dragged or not.
func (element *Slider) Enabled () bool { func (element *slider) Enabled () bool {
return element.enabled return element.enabled
} }
// SetEnabled sets whether this slider can be dragged or not. // SetEnabled sets whether this slider can be dragged or not.
func (element *Slider) SetEnabled (enabled bool) { func (element *slider) SetEnabled (enabled bool) {
if element.enabled == enabled { return } if element.enabled == enabled { return }
element.enabled = enabled element.enabled = enabled
element.entity.Invalidate() element.entity.Invalidate()
} }
func (element *Slider) HandleFocusChange () { func (element *slider) HandleFocusChange () {
element.entity.Invalidate() element.entity.Invalidate()
} }
func (element *Slider) HandleMouseDown (x, y int, button input.Button) { func (element *slider) HandleMouseDown (x, y int, button input.Button) {
if !element.Enabled() { return } if !element.Enabled() { return }
element.Focus() element.Focus()
if button == input.ButtonLeft { if button == input.ButtonLeft {
@ -111,7 +118,7 @@ func (element *Slider) HandleMouseDown (x, y int, button input.Button) {
} }
} }
func (element *Slider) HandleMouseUp (x, y int, button input.Button) { func (element *slider) HandleMouseUp (x, y int, button input.Button) {
if button != input.ButtonLeft || !element.dragging { return } if button != input.ButtonLeft || !element.dragging { return }
element.dragging = false element.dragging = false
if element.onRelease != nil { if element.onRelease != nil {
@ -120,7 +127,7 @@ func (element *Slider) HandleMouseUp (x, y int, button input.Button) {
element.entity.Invalidate() element.entity.Invalidate()
} }
func (element *Slider) HandleMotion (x, y int) { func (element *slider) HandleMotion (x, y int) {
if element.dragging { if element.dragging {
element.dragging = true element.dragging = true
element.value = element.valueFor(x, y) element.value = element.valueFor(x, y)
@ -131,9 +138,9 @@ func (element *Slider) HandleMotion (x, y int) {
} }
} }
func (element *Slider) HandleScroll (x, y int, deltaX, deltaY float64) { } func (element *slider) HandleScroll (x, y int, deltaX, deltaY float64) { }
func (element *Slider) HandleKeyDown (key input.Key, modifiers input.Modifiers) { func (element *slider) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
switch key { switch key {
case input.KeyUp: case input.KeyUp:
element.changeValue(0.1) element.changeValue(0.1)
@ -154,15 +161,15 @@ func (element *Slider) HandleKeyDown (key input.Key, modifiers input.Modifiers)
} }
} }
func (element *Slider) HandleKeyUp (key input.Key, modifiers input.Modifiers) { } func (element *slider) HandleKeyUp (key input.Key, modifiers input.Modifiers) { }
// Value returns the slider's value. // Value returns the slider's value.
func (element *Slider) Value () (value float64) { func (element *slider) Value () (value float64) {
return element.value return element.value
} }
// SetValue sets the slider's value. // SetValue sets the slider's value.
func (element *Slider) SetValue (value float64) { func (element *slider) SetValue (value float64) {
if value < 0 { value = 0 } if value < 0 { value = 0 }
if value > 1 { value = 1 } if value > 1 { value = 1 }
@ -177,31 +184,31 @@ func (element *Slider) SetValue (value float64) {
// OnSlide sets a function to be called every time the slider handle changes // OnSlide sets a function to be called every time the slider handle changes
// position while being dragged. // position while being dragged.
func (element *Slider) OnSlide (callback func ()) { func (element *slider) OnSlide (callback func ()) {
element.onSlide = callback element.onSlide = callback
} }
// OnRelease sets a function to be called when the handle stops being dragged. // OnRelease sets a function to be called when the handle stops being dragged.
func (element *Slider) OnRelease (callback func ()) { func (element *slider) OnRelease (callback func ()) {
element.onRelease = callback element.onRelease = callback
} }
// SetTheme sets the element's theme. // SetTheme sets the element's theme.
func (element *Slider) SetTheme (new tomo.Theme) { func (element *slider) SetTheme (new tomo.Theme) {
if new == element.theme.Theme { return } if new == element.theme.Theme { return }
element.theme.Theme = new element.theme.Theme = new
element.entity.Invalidate() element.entity.Invalidate()
} }
// SetConfig sets the element's configuration. // SetConfig sets the element's configuration.
func (element *Slider) SetConfig (new tomo.Config) { func (element *slider) SetConfig (new tomo.Config) {
if new == element.config.Config { return } if new == element.config.Config { return }
element.config.Config = new element.config.Config = new
element.updateMinimumSize() element.updateMinimumSize()
element.entity.Invalidate() element.entity.Invalidate()
} }
func (element *Slider) changeValue (delta float64) { func (element *slider) changeValue (delta float64) {
element.value += delta element.value += delta
if element.value < 0 { if element.value < 0 {
element.value = 0 element.value = 0
@ -215,7 +222,7 @@ func (element *Slider) changeValue (delta float64) {
element.entity.Invalidate() element.entity.Invalidate()
} }
func (element *Slider) valueFor (x, y int) (value float64) { func (element *slider) valueFor (x, y int) (value float64) {
if element.vertical { if element.vertical {
value = value =
float64(y - element.track.Min.Y - element.bar.Dy() / 2) / float64(y - element.track.Min.Y - element.bar.Dy() / 2) /
@ -232,7 +239,7 @@ func (element *Slider) valueFor (x, y int) (value float64) {
return return
} }
func (element *Slider) updateMinimumSize () { func (element *slider) updateMinimumSize () {
gutterPadding := element.theme.Padding(tomo.PatternGutter) gutterPadding := element.theme.Padding(tomo.PatternGutter)
handlePadding := element.theme.Padding(tomo.PatternHandle) handlePadding := element.theme.Padding(tomo.PatternHandle)
if element.vertical { if element.vertical {