2023-03-30 21:19:04 -06:00
|
|
|
package elements
|
2023-02-10 19:55:59 -07:00
|
|
|
|
|
|
|
import "image"
|
2023-03-30 23:06:29 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
2023-02-10 19:55:59 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
2023-05-02 20:19:29 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/ability"
|
2023-02-10 19:55:59 -07:00
|
|
|
|
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 {
|
2023-04-19 23:04:03 -06:00
|
|
|
slider
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:10:47 -06:00
|
|
|
// NewVSlider creates a new horizontal slider with the specified value.
|
|
|
|
func NewVSlider (value float64) (element *Slider) {
|
|
|
|
element = NewHSlider(value)
|
|
|
|
element.vertical = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHSlider creates a new horizontal slider with the specified value.
|
|
|
|
func NewHSlider (value float64) (element *Slider) {
|
2023-04-19 23:04:03 -06:00
|
|
|
element = &Slider { }
|
|
|
|
element.value = value
|
2023-05-02 20:19:29 -06:00
|
|
|
element.entity = tomo.NewEntity(element)
|
2023-04-19 23:04:03 -06:00
|
|
|
element.construct()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type slider struct {
|
2023-04-14 23:45:11 -06:00
|
|
|
value float64
|
|
|
|
vertical bool
|
|
|
|
dragging bool
|
|
|
|
enabled bool
|
2023-02-10 19:55:59 -07:00
|
|
|
dragOffset int
|
2023-04-14 23:45:11 -06:00
|
|
|
track image.Rectangle
|
|
|
|
bar image.Rectangle
|
2023-02-10 19:55:59 -07:00
|
|
|
|
|
|
|
onSlide func ()
|
|
|
|
onRelease func ()
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) construct () {
|
|
|
|
element.enabled = true
|
|
|
|
if element.vertical {
|
2023-03-30 23:06:29 -06:00
|
|
|
element.theme.Case = tomo.C("tomo", "sliderVertical")
|
2023-02-10 19:55:59 -07:00
|
|
|
} else {
|
2023-03-30 23:06:29 -06:00
|
|
|
element.theme.Case = tomo.C("tomo", "sliderHorizontal")
|
2023-02-10 19:55:59 -07:00
|
|
|
}
|
|
|
|
element.updateMinimumSize()
|
|
|
|
}
|
|
|
|
|
2023-04-14 23:45:11 -06:00
|
|
|
// Entity returns this element's entity.
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) Entity () tomo.Entity {
|
2023-04-14 23:45:11 -06:00
|
|
|
return element.entity
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw causes the element to draw to the specified destination canvas.
|
2023-05-02 20:19:29 -06:00
|
|
|
func (element *slider) Draw (destination artist.Canvas) {
|
2023-04-14 23:45:11 -06:00
|
|
|
bounds := element.entity.Bounds()
|
|
|
|
element.track = element.theme.Padding(tomo.PatternGutter).Apply(bounds)
|
|
|
|
if element.vertical {
|
|
|
|
barSize := element.track.Dx()
|
2023-04-21 19:45:20 -06:00
|
|
|
element.bar = image.Rect(0, 0, barSize, barSize).Add(element.track.Min)
|
2023-04-14 23:45:11 -06:00
|
|
|
barOffset :=
|
|
|
|
float64(element.track.Dy() - barSize) *
|
|
|
|
(1 - element.value)
|
|
|
|
element.bar = element.bar.Add(image.Pt(0, int(barOffset)))
|
|
|
|
} else {
|
|
|
|
barSize := element.track.Dy()
|
2023-04-21 19:45:20 -06:00
|
|
|
element.bar = image.Rect(0, 0, barSize, barSize).Add(element.track.Min)
|
2023-04-14 23:45:11 -06:00
|
|
|
barOffset :=
|
|
|
|
float64(element.track.Dx() - barSize) *
|
|
|
|
element.value
|
|
|
|
element.bar = element.bar.Add(image.Pt(int(barOffset), 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
state := tomo.State {
|
|
|
|
Disabled: !element.Enabled(),
|
|
|
|
Focused: element.entity.Focused(),
|
|
|
|
Pressed: element.dragging,
|
|
|
|
}
|
|
|
|
element.theme.Pattern(tomo.PatternGutter, state).Draw(destination, bounds)
|
2023-04-21 19:45:20 -06:00
|
|
|
element.theme.Pattern(tomo.PatternHandle, state).Draw(destination, element.bar)
|
2023-04-14 23:45:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Focus gives this element input focus.
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) Focus () {
|
2023-04-14 23:45:11 -06:00
|
|
|
if !element.entity.Focused() { element.entity.Focus() }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enabled returns whether this slider can be dragged or not.
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) Enabled () bool {
|
2023-04-14 23:45:11 -06:00
|
|
|
return element.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEnabled sets whether this slider can be dragged or not.
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) SetEnabled (enabled bool) {
|
2023-04-14 23:45:11 -06:00
|
|
|
if element.enabled == enabled { return }
|
|
|
|
element.enabled = enabled
|
|
|
|
element.entity.Invalidate()
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) HandleFocusChange () {
|
2023-04-15 16:51:42 -06:00
|
|
|
element.entity.Invalidate()
|
|
|
|
}
|
|
|
|
|
2023-04-20 12:44:54 -06:00
|
|
|
func (element *slider) HandleMouseDown (
|
|
|
|
position image.Point,
|
|
|
|
button input.Button,
|
|
|
|
modifiers input.Modifiers,
|
|
|
|
) {
|
2023-02-10 19:55:59 -07:00
|
|
|
if !element.Enabled() { return }
|
|
|
|
element.Focus()
|
|
|
|
if button == input.ButtonLeft {
|
|
|
|
element.dragging = true
|
2023-04-20 12:44:54 -06:00
|
|
|
element.value = element.valueFor(position.X, position.Y)
|
2023-02-10 19:55:59 -07:00
|
|
|
if element.onSlide != nil {
|
|
|
|
element.onSlide()
|
|
|
|
}
|
2023-04-14 23:45:11 -06:00
|
|
|
element.entity.Invalidate()
|
2023-02-10 19:55:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 12:44:54 -06:00
|
|
|
func (element *slider) HandleMouseUp (
|
|
|
|
position image.Point,
|
|
|
|
button input.Button,
|
|
|
|
modifiers input.Modifiers,
|
|
|
|
) {
|
2023-02-10 19:55:59 -07:00
|
|
|
if button != input.ButtonLeft || !element.dragging { return }
|
|
|
|
element.dragging = false
|
|
|
|
if element.onRelease != nil {
|
|
|
|
element.onRelease()
|
|
|
|
}
|
2023-04-14 23:45:11 -06:00
|
|
|
element.entity.Invalidate()
|
2023-02-10 19:55:59 -07:00
|
|
|
}
|
|
|
|
|
2023-04-20 12:44:54 -06:00
|
|
|
func (element *slider) HandleMotion (position image.Point) {
|
2023-02-10 19:55:59 -07:00
|
|
|
if element.dragging {
|
|
|
|
element.dragging = true
|
2023-04-20 12:44:54 -06:00
|
|
|
element.value = element.valueFor(position.X, position.Y)
|
2023-02-10 19:55:59 -07:00
|
|
|
if element.onSlide != nil {
|
|
|
|
element.onSlide()
|
|
|
|
}
|
2023-04-14 23:45:11 -06:00
|
|
|
element.entity.Invalidate()
|
2023-02-10 19:55:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 12:44:54 -06:00
|
|
|
func (element *slider) HandleScroll (
|
|
|
|
position image.Point,
|
|
|
|
deltaX, deltaY float64,
|
|
|
|
modifiers input.Modifiers,
|
|
|
|
) { }
|
2023-02-10 19:55:59 -07:00
|
|
|
|
2023-04-19 23:04:03 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) HandleKeyUp (key input.Key, modifiers input.Modifiers) { }
|
2023-02-10 19:55:59 -07:00
|
|
|
|
2023-02-11 15:04:50 -07:00
|
|
|
// Value returns the slider's value.
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) Value () (value float64) {
|
2023-02-10 19:55:59 -07:00
|
|
|
return element.value
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:04:50 -07:00
|
|
|
// SetValue sets the slider's value.
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) SetValue (value float64) {
|
2023-02-10 19:55:59 -07:00
|
|
|
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-04-14 23:45:11 -06:00
|
|
|
element.entity.Invalidate()
|
2023-02-10 19:55:59 -07:00
|
|
|
}
|
|
|
|
|
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-04-19 23:04:03 -06:00
|
|
|
func (element *slider) OnSlide (callback func ()) {
|
2023-02-10 19:55:59 -07:00
|
|
|
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-04-19 23:04:03 -06:00
|
|
|
func (element *slider) OnRelease (callback func ()) {
|
2023-02-10 19:55:59 -07:00
|
|
|
element.onRelease = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTheme sets the element's theme.
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) SetTheme (new tomo.Theme) {
|
2023-02-10 19:55:59 -07:00
|
|
|
if new == element.theme.Theme { return }
|
|
|
|
element.theme.Theme = new
|
2023-04-14 23:45:11 -06:00
|
|
|
element.entity.Invalidate()
|
2023-02-10 19:55:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the element's configuration.
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) SetConfig (new tomo.Config) {
|
2023-02-10 19:55:59 -07:00
|
|
|
if new == element.config.Config { return }
|
|
|
|
element.config.Config = new
|
|
|
|
element.updateMinimumSize()
|
2023-04-14 23:45:11 -06:00
|
|
|
element.entity.Invalidate()
|
2023-02-10 19:55:59 -07:00
|
|
|
}
|
|
|
|
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) changeValue (delta float64) {
|
2023-03-07 17:13:08 -07:00
|
|
|
element.value += delta
|
|
|
|
if element.value < 0 {
|
|
|
|
element.value = 0
|
|
|
|
}
|
|
|
|
if element.value > 1 {
|
|
|
|
element.value = 1
|
|
|
|
}
|
|
|
|
if element.onRelease != nil {
|
|
|
|
element.onRelease()
|
|
|
|
}
|
2023-04-14 23:45:11 -06:00
|
|
|
element.entity.Invalidate()
|
2023-03-07 17:13:08 -07:00
|
|
|
}
|
|
|
|
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) valueFor (x, y int) (value float64) {
|
2023-02-10 19:55:59 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:04:03 -06:00
|
|
|
func (element *slider) updateMinimumSize () {
|
2023-03-31 11:55:45 -06:00
|
|
|
gutterPadding := element.theme.Padding(tomo.PatternGutter)
|
|
|
|
handlePadding := element.theme.Padding(tomo.PatternHandle)
|
2023-02-10 19:55:59 -07:00
|
|
|
if element.vertical {
|
2023-04-14 23:45:11 -06:00
|
|
|
element.entity.SetMinimumSize (
|
2023-03-31 11:55:45 -06:00
|
|
|
gutterPadding.Horizontal() + handlePadding.Horizontal(),
|
|
|
|
gutterPadding.Vertical() + handlePadding.Vertical() * 2)
|
2023-02-10 19:55:59 -07:00
|
|
|
} else {
|
2023-04-14 23:45:11 -06:00
|
|
|
element.entity.SetMinimumSize (
|
2023-03-31 11:55:45 -06:00
|
|
|
gutterPadding.Horizontal() + handlePadding.Horizontal() * 2,
|
|
|
|
gutterPadding.Vertical() + handlePadding.Vertical())
|
2023-02-10 19:55:59 -07:00
|
|
|
}
|
|
|
|
}
|