2023-02-10 19:55:59 -07:00
|
|
|
package basicElements
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/config"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
|
|
|
|
2023-02-11 15:04:50 -07:00
|
|
|
// Slider is a slider control with a floating point value between zero and one.
|
2023-02-10 19:55:59 -07:00
|
|
|
type Slider struct {
|
|
|
|
*core.Core
|
|
|
|
*core.FocusableCore
|
|
|
|
core core.CoreControl
|
|
|
|
focusableControl core.FocusableCoreControl
|
|
|
|
|
|
|
|
value float64
|
|
|
|
vertical bool
|
|
|
|
dragging bool
|
|
|
|
dragOffset int
|
|
|
|
track image.Rectangle
|
|
|
|
bar image.Rectangle
|
|
|
|
|
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
|
|
|
|
|
|
|
onSlide func ()
|
|
|
|
onRelease func ()
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:04:50 -07:00
|
|
|
// NewSlider creates a new slider with the specified value. If vertical is set
|
|
|
|
// to true,
|
2023-02-10 19:55:59 -07:00
|
|
|
func NewSlider (value float64, vertical bool) (element *Slider) {
|
|
|
|
element = &Slider {
|
|
|
|
value: value,
|
|
|
|
vertical: vertical,
|
|
|
|
}
|
|
|
|
if vertical {
|
|
|
|
element.theme.Case = theme.C("basic", "sliderVertical")
|
|
|
|
} else {
|
|
|
|
element.theme.Case = theme.C("basic", "sliderHorizontal")
|
|
|
|
}
|
|
|
|
element.Core, element.core = core.NewCore(element.draw)
|
|
|
|
element.FocusableCore,
|
|
|
|
element.focusableControl = core.NewFocusableCore(element.redo)
|
|
|
|
element.updateMinimumSize()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Slider) HandleMouseDown (x, y int, button input.Button) {
|
|
|
|
if !element.Enabled() { return }
|
|
|
|
element.Focus()
|
|
|
|
if button == input.ButtonLeft {
|
|
|
|
element.dragging = true
|
|
|
|
element.value = element.valueFor(x, y)
|
|
|
|
if element.onSlide != nil {
|
|
|
|
element.onSlide()
|
|
|
|
}
|
|
|
|
element.redo()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Slider) HandleMouseUp (x, y int, button input.Button) {
|
|
|
|
if button != input.ButtonLeft || !element.dragging { return }
|
|
|
|
element.dragging = false
|
|
|
|
if element.onRelease != nil {
|
|
|
|
element.onRelease()
|
|
|
|
}
|
|
|
|
element.redo()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Slider) HandleMouseMove (x, y int) {
|
|
|
|
if element.dragging {
|
|
|
|
element.dragging = true
|
|
|
|
element.value = element.valueFor(x, y)
|
|
|
|
if element.onSlide != nil {
|
|
|
|
element.onSlide()
|
|
|
|
}
|
|
|
|
element.redo()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Slider) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }
|
|
|
|
|
|
|
|
func (element *Slider) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
|
2023-03-07 17:13:08 -07:00
|
|
|
switch key {
|
|
|
|
case input.KeyUp:
|
|
|
|
element.changeValue(0.1)
|
|
|
|
case input.KeyDown:
|
|
|
|
element.changeValue(-0.1)
|
|
|
|
case input.KeyRight:
|
|
|
|
if element.vertical {
|
|
|
|
element.changeValue(-0.1)
|
|
|
|
} else {
|
|
|
|
element.changeValue(0.1)
|
|
|
|
}
|
|
|
|
case input.KeyLeft:
|
|
|
|
if element.vertical {
|
|
|
|
element.changeValue(0.1)
|
|
|
|
} else {
|
|
|
|
element.changeValue(-0.1)
|
|
|
|
}
|
|
|
|
}
|
2023-02-10 19:55:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Slider) HandleKeyUp (key input.Key, modifiers input.Modifiers) { }
|
|
|
|
|
2023-02-11 15:04:50 -07:00
|
|
|
// Value returns the slider's value.
|
2023-02-10 19:55:59 -07:00
|
|
|
func (element *Slider) Value () (value float64) {
|
|
|
|
return element.value
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:04:50 -07:00
|
|
|
// SetEnabled sets whether or not the slider can be interacted with.
|
2023-02-10 19:55:59 -07:00
|
|
|
func (element *Slider) SetEnabled (enabled bool) {
|
|
|
|
element.focusableControl.SetEnabled(enabled)
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:04:50 -07:00
|
|
|
// SetValue sets the slider's value.
|
2023-02-10 19:55:59 -07:00
|
|
|
func (element *Slider) SetValue (value float64) {
|
|
|
|
if value < 0 { value = 0 }
|
|
|
|
if value > 1 { value = 1 }
|
|
|
|
|
|
|
|
if element.value == value { return }
|
|
|
|
|
|
|
|
element.value = value
|
2023-03-08 18:24:43 -07:00
|
|
|
if element.onRelease != nil {
|
|
|
|
element.onRelease()
|
|
|
|
}
|
2023-02-10 19:55:59 -07:00
|
|
|
element.redo()
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:04:50 -07:00
|
|
|
// OnSlide sets a function to be called every time the slider handle changes
|
|
|
|
// position while being dragged.
|
2023-02-10 19:55:59 -07:00
|
|
|
func (element *Slider) OnSlide (callback func ()) {
|
|
|
|
element.onSlide = callback
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:04:50 -07:00
|
|
|
// OnRelease sets a function to be called when the handle stops being dragged.
|
2023-02-10 19:55:59 -07:00
|
|
|
func (element *Slider) OnRelease (callback func ()) {
|
|
|
|
element.onRelease = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTheme sets the element's theme.
|
|
|
|
func (element *Slider) SetTheme (new theme.Theme) {
|
|
|
|
if new == element.theme.Theme { return }
|
|
|
|
element.theme.Theme = new
|
|
|
|
element.redo()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the element's configuration.
|
|
|
|
func (element *Slider) SetConfig (new config.Config) {
|
|
|
|
if new == element.config.Config { return }
|
|
|
|
element.config.Config = new
|
|
|
|
element.updateMinimumSize()
|
|
|
|
element.redo()
|
|
|
|
}
|
|
|
|
|
2023-03-07 17:13:08 -07:00
|
|
|
func (element *Slider) changeValue (delta float64) {
|
|
|
|
element.value += delta
|
|
|
|
if element.value < 0 {
|
|
|
|
element.value = 0
|
|
|
|
}
|
|
|
|
if element.value > 1 {
|
|
|
|
element.value = 1
|
|
|
|
}
|
|
|
|
if element.onRelease != nil {
|
|
|
|
element.onRelease()
|
|
|
|
}
|
|
|
|
element.redo()
|
|
|
|
}
|
|
|
|
|
2023-02-10 19:55:59 -07:00
|
|
|
func (element *Slider) valueFor (x, y int) (value float64) {
|
|
|
|
if element.vertical {
|
|
|
|
value =
|
|
|
|
float64(y - element.track.Min.Y - element.bar.Dy() / 2) /
|
|
|
|
float64(element.track.Dy() - element.bar.Dy())
|
2023-02-10 23:46:12 -07:00
|
|
|
value = 1 - value
|
2023-02-10 19:55:59 -07:00
|
|
|
} else {
|
|
|
|
value =
|
|
|
|
float64(x - element.track.Min.X - element.bar.Dx() / 2) /
|
|
|
|
float64(element.track.Dx() - element.bar.Dx())
|
|
|
|
}
|
|
|
|
|
|
|
|
if value < 0 { value = 0 }
|
|
|
|
if value > 1 { value = 1 }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Slider) updateMinimumSize () {
|
|
|
|
if element.vertical {
|
|
|
|
element.core.SetMinimumSize (
|
|
|
|
element.config.HandleWidth(),
|
|
|
|
element.config.HandleWidth() * 2)
|
|
|
|
} else {
|
|
|
|
element.core.SetMinimumSize (
|
|
|
|
element.config.HandleWidth() * 2,
|
|
|
|
element.config.HandleWidth())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Slider) redo () {
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Slider) draw () {
|
|
|
|
bounds := element.Bounds()
|
2023-02-26 20:20:17 -07:00
|
|
|
element.track = element.theme.Padding(theme.PatternGutter).Apply(bounds)
|
2023-02-10 19:55:59 -07:00
|
|
|
if element.vertical {
|
|
|
|
barSize := element.track.Dx()
|
|
|
|
element.bar = image.Rect(0, 0, barSize, barSize).Add(bounds.Min)
|
|
|
|
barOffset :=
|
|
|
|
float64(element.track.Dy() - barSize) *
|
|
|
|
(1 - element.value)
|
|
|
|
element.bar = element.bar.Add(image.Pt(0, int(barOffset)))
|
|
|
|
} else {
|
|
|
|
barSize := element.track.Dy()
|
|
|
|
element.bar = image.Rect(0, 0, barSize, barSize).Add(bounds.Min)
|
|
|
|
barOffset :=
|
|
|
|
float64(element.track.Dx() - barSize) *
|
|
|
|
element.value
|
|
|
|
element.bar = element.bar.Add(image.Pt(int(barOffset), 0))
|
|
|
|
}
|
|
|
|
|
2023-02-26 20:20:17 -07:00
|
|
|
state := theme.State {
|
2023-02-10 19:55:59 -07:00
|
|
|
Focused: element.Focused(),
|
|
|
|
Disabled: !element.Enabled(),
|
|
|
|
Pressed: element.dragging,
|
|
|
|
}
|
2023-02-26 20:20:17 -07:00
|
|
|
artist.DrawBounds (
|
2023-02-12 23:49:33 -07:00
|
|
|
element.core,
|
2023-02-10 19:55:59 -07:00
|
|
|
element.theme.Pattern(theme.PatternGutter, state),
|
|
|
|
bounds)
|
2023-02-26 20:20:17 -07:00
|
|
|
artist.DrawBounds (
|
2023-02-12 23:49:33 -07:00
|
|
|
element.core,
|
2023-02-10 19:55:59 -07:00
|
|
|
element.theme.Pattern(theme.PatternHandle, state),
|
|
|
|
element.bar)
|
|
|
|
}
|