2023-08-10 15:52:01 -06:00
|
|
|
package objects
|
|
|
|
|
2024-06-20 15:48:41 -06:00
|
|
|
import "math"
|
2023-08-11 23:02:48 -06:00
|
|
|
import "image"
|
2023-08-10 15:52:01 -06:00
|
|
|
import "git.tebibyte.media/tomo/tomo"
|
2023-08-11 23:02:48 -06:00
|
|
|
import "git.tebibyte.media/tomo/tomo/input"
|
|
|
|
import "git.tebibyte.media/tomo/tomo/event"
|
2023-08-10 15:52:01 -06:00
|
|
|
|
2023-09-14 15:03:19 -06:00
|
|
|
// Slider is a control that selects a numeric value between 0 and 1.
|
2023-08-10 15:52:01 -06:00
|
|
|
type Slider struct {
|
|
|
|
tomo.ContainerBox
|
2023-08-11 23:02:48 -06:00
|
|
|
handle *SliderHandle
|
|
|
|
layout sliderLayout
|
|
|
|
dragging bool
|
|
|
|
dragOffset image.Point
|
2024-05-16 23:22:47 -06:00
|
|
|
step float64
|
2023-08-24 22:09:34 -06:00
|
|
|
|
2023-08-11 23:02:48 -06:00
|
|
|
on struct {
|
2024-06-27 12:01:14 -06:00
|
|
|
valueChange event.FuncBroadcaster
|
2024-06-27 12:09:58 -06:00
|
|
|
confirm event.FuncBroadcaster
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
2023-08-10 15:52:01 -06:00
|
|
|
}
|
|
|
|
|
2023-09-14 15:03:19 -06:00
|
|
|
// SliderHandle is a simple object that serves as a handle for sliders and
|
|
|
|
// scrollbars. It is completely inert.
|
2023-08-10 15:52:01 -06:00
|
|
|
type SliderHandle struct {
|
|
|
|
tomo.Box
|
|
|
|
}
|
|
|
|
|
2023-08-11 23:02:48 -06:00
|
|
|
func newSlider (orient string, value float64) *Slider {
|
2023-08-10 15:52:01 -06:00
|
|
|
this := &Slider {
|
|
|
|
ContainerBox: tomo.NewContainerBox(),
|
|
|
|
handle: &SliderHandle {
|
|
|
|
Box: tomo.NewBox(),
|
|
|
|
},
|
2023-08-11 23:02:48 -06:00
|
|
|
layout: sliderLayout {
|
|
|
|
vertical: orient == "vertical",
|
2024-06-20 15:48:41 -06:00
|
|
|
value: math.NaN(),
|
2023-08-11 23:02:48 -06:00
|
|
|
},
|
2024-05-16 23:22:47 -06:00
|
|
|
step: 0.05,
|
2023-08-10 15:52:01 -06:00
|
|
|
}
|
2023-08-24 22:09:34 -06:00
|
|
|
|
2023-08-10 15:52:01 -06:00
|
|
|
this.Add(this.handle)
|
2023-08-11 23:02:48 -06:00
|
|
|
this.SetFocusable(true)
|
|
|
|
this.SetValue(value)
|
2024-07-25 10:58:38 -06:00
|
|
|
|
|
|
|
this.SetInputMask(true)
|
2024-07-21 09:48:28 -06:00
|
|
|
this.OnKeyUp(this.handleKeyUp)
|
2023-08-11 23:02:48 -06:00
|
|
|
this.OnKeyDown(this.handleKeyDown)
|
2024-07-21 09:48:28 -06:00
|
|
|
this.OnButtonDown(this.handleButtonDown)
|
|
|
|
this.OnButtonUp(this.handleButtonUp)
|
2023-08-11 23:02:48 -06:00
|
|
|
this.OnMouseMove(this.handleMouseMove)
|
2024-05-16 23:22:47 -06:00
|
|
|
this.OnScroll(this.handleScroll)
|
2024-06-03 19:13:18 -06:00
|
|
|
|
2024-07-21 09:48:28 -06:00
|
|
|
this.handle.SetRole(tomo.R("objects", "SliderHandle"))
|
|
|
|
this.handle.SetTag(orient, true)
|
|
|
|
this.SetRole(tomo.R("objects", "Slider"))
|
|
|
|
this.SetTag(orient, true)
|
2023-08-10 15:52:01 -06:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2023-09-14 15:03:19 -06:00
|
|
|
// NewVerticalSlider creates a new vertical slider with the specified value.
|
2023-08-11 23:02:48 -06:00
|
|
|
func NewVerticalSlider (value float64) *Slider {
|
|
|
|
return newSlider("vertical", value)
|
|
|
|
}
|
|
|
|
|
2023-09-14 15:03:19 -06:00
|
|
|
// NewHorizontalSlider creates a new horizontal slider with the specified value.
|
2023-08-11 23:02:48 -06:00
|
|
|
func NewHorizontalSlider (value float64) *Slider {
|
|
|
|
return newSlider("horizontal", value)
|
|
|
|
}
|
|
|
|
|
2023-09-14 15:03:19 -06:00
|
|
|
// SetValue sets the value of the slider between 0 and 1.
|
2023-08-11 23:02:48 -06:00
|
|
|
func (this *Slider) SetValue (value float64) {
|
|
|
|
if value < 0 { value = 0 }
|
|
|
|
if value > 1 { value = 1 }
|
|
|
|
if value == this.layout.value { return }
|
|
|
|
this.layout.value = value
|
2024-07-25 10:58:38 -06:00
|
|
|
this.SetAttr(tomo.ALayout(this.layout))
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
|
|
|
|
2023-09-14 15:03:19 -06:00
|
|
|
// Value returns the value of the slider between 0 and 1.
|
2023-08-11 23:02:48 -06:00
|
|
|
func (this *Slider) Value () float64 {
|
|
|
|
return this.layout.value
|
2023-08-10 15:52:01 -06:00
|
|
|
}
|
|
|
|
|
2024-05-07 16:24:44 -06:00
|
|
|
// OnValueChange specifies a function to be called when the user moves the
|
|
|
|
// slider.
|
2024-06-27 12:01:14 -06:00
|
|
|
func (this *Slider) OnValueChange (callback func ()) event.Cookie {
|
|
|
|
return this.on.valueChange.Connect(callback)
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
|
|
|
|
2024-06-27 12:09:58 -06:00
|
|
|
// OnConfirm specifies a function to be called when the user stops moving the
|
2024-06-25 00:33:13 -06:00
|
|
|
// slider.
|
2024-06-27 12:09:58 -06:00
|
|
|
func (this *Slider) OnConfirm (callback func ()) event.Cookie {
|
|
|
|
return this.on.confirm.Connect(callback)
|
2024-06-25 00:33:13 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *Slider) handleKeyDown (key input.Key, numpad bool) bool {
|
2023-08-24 22:09:34 -06:00
|
|
|
var increment float64; if this.layout.vertical {
|
|
|
|
increment = -0.05
|
|
|
|
} else {
|
|
|
|
increment = 0.05
|
|
|
|
}
|
|
|
|
|
2023-08-11 23:02:48 -06:00
|
|
|
switch key {
|
|
|
|
case input.KeyUp, input.KeyLeft:
|
2024-07-21 09:48:28 -06:00
|
|
|
if this.Window().Modifiers().Alt {
|
2023-08-11 23:02:48 -06:00
|
|
|
this.SetValue(0)
|
|
|
|
} else {
|
2023-08-24 22:09:34 -06:00
|
|
|
this.SetValue(this.Value() - increment)
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
2024-06-27 12:01:14 -06:00
|
|
|
this.on.valueChange.Broadcast()
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2023-08-11 23:02:48 -06:00
|
|
|
case input.KeyDown, input.KeyRight:
|
2024-07-21 09:48:28 -06:00
|
|
|
if this.Window().Modifiers().Alt {
|
2023-08-11 23:02:48 -06:00
|
|
|
this.SetValue(1)
|
|
|
|
} else {
|
2023-08-24 22:09:34 -06:00
|
|
|
this.SetValue(this.Value() + increment)
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
2024-06-27 12:01:14 -06:00
|
|
|
this.on.valueChange.Broadcast()
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2023-08-11 23:02:48 -06:00
|
|
|
case input.KeyHome:
|
|
|
|
this.SetValue(0)
|
2024-06-27 12:01:14 -06:00
|
|
|
this.on.valueChange.Broadcast()
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2023-08-11 23:02:48 -06:00
|
|
|
case input.KeyEnd:
|
|
|
|
this.SetValue(1)
|
2024-06-27 12:01:14 -06:00
|
|
|
this.on.valueChange.Broadcast()
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
2024-07-25 10:58:38 -06:00
|
|
|
return false
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *Slider) handleKeyUp (key input.Key, numpad bool) bool {
|
|
|
|
switch key {
|
|
|
|
case input.KeyUp, input.KeyLeft: return true
|
|
|
|
case input.KeyDown, input.KeyRight: return true
|
|
|
|
case input.KeyHome: return true
|
|
|
|
case input.KeyEnd: return true
|
|
|
|
}
|
|
|
|
return false
|
2024-07-21 09:48:28 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *Slider) handleButtonDown (button input.Button) bool {
|
2024-07-21 09:48:28 -06:00
|
|
|
pointer := this.Window().MousePosition()
|
2023-08-11 23:02:48 -06:00
|
|
|
handle := this.handle.Bounds()
|
2023-09-14 19:40:06 -06:00
|
|
|
|
|
|
|
within := pointer.In(handle)
|
|
|
|
var above bool; if this.layout.vertical {
|
|
|
|
above = pointer.Y < handle.Min.Y + handle.Dy() / 2
|
2023-08-11 23:02:48 -06:00
|
|
|
} else {
|
2023-09-14 19:40:06 -06:00
|
|
|
above = pointer.X < handle.Min.X + handle.Dx() / 2
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
2023-08-24 22:09:34 -06:00
|
|
|
|
2023-08-11 23:02:48 -06:00
|
|
|
switch button {
|
|
|
|
case input.ButtonLeft:
|
|
|
|
if within {
|
|
|
|
this.dragging = true
|
|
|
|
this.dragOffset =
|
|
|
|
pointer.Sub(this.handle.Bounds().Min).
|
|
|
|
Add(this.InnerBounds().Min)
|
|
|
|
this.drag()
|
|
|
|
} else {
|
|
|
|
this.dragOffset = this.fallbackDragOffset()
|
|
|
|
this.dragging = true
|
|
|
|
this.drag()
|
|
|
|
}
|
|
|
|
case input.ButtonMiddle:
|
|
|
|
if above {
|
|
|
|
this.SetValue(0)
|
2024-06-27 12:01:14 -06:00
|
|
|
this.on.valueChange.Broadcast()
|
2024-06-27 12:09:58 -06:00
|
|
|
this.on.confirm.Broadcast()
|
2023-08-11 23:02:48 -06:00
|
|
|
} else {
|
|
|
|
this.SetValue(1)
|
2024-06-27 12:01:14 -06:00
|
|
|
this.on.valueChange.Broadcast()
|
2024-06-27 12:09:58 -06:00
|
|
|
this.on.confirm.Broadcast()
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
|
|
|
case input.ButtonRight:
|
|
|
|
if above {
|
2024-05-16 23:22:47 -06:00
|
|
|
this.SetValue(this.Value() - this.step)
|
2024-06-27 12:01:14 -06:00
|
|
|
this.on.valueChange.Broadcast()
|
2024-06-27 12:09:58 -06:00
|
|
|
this.on.confirm.Broadcast()
|
2023-08-11 23:02:48 -06:00
|
|
|
} else {
|
2024-05-16 23:22:47 -06:00
|
|
|
this.SetValue(this.Value() + this.step)
|
2024-06-27 12:01:14 -06:00
|
|
|
this.on.valueChange.Broadcast()
|
2024-06-27 12:09:58 -06:00
|
|
|
this.on.confirm.Broadcast()
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
|
|
|
}
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *Slider) handleButtonUp (button input.Button) bool {
|
|
|
|
if button != input.ButtonLeft || !this.dragging { return true }
|
2023-08-11 23:02:48 -06:00
|
|
|
this.dragging = false
|
2024-06-27 12:09:58 -06:00
|
|
|
this.on.confirm.Broadcast()
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *Slider) handleMouseMove () bool {
|
|
|
|
if !this.dragging { return false }
|
2023-08-11 23:02:48 -06:00
|
|
|
this.drag()
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *Slider) handleScroll (x, y float64) bool {
|
2024-05-16 23:22:47 -06:00
|
|
|
delta := (x + y) * 0.005
|
|
|
|
this.SetValue(this.Value() + delta)
|
2024-06-27 12:01:14 -06:00
|
|
|
this.on.valueChange.Broadcast()
|
2024-06-27 12:09:58 -06:00
|
|
|
this.on.confirm.Broadcast()
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2024-05-16 23:22:47 -06:00
|
|
|
}
|
|
|
|
|
2023-08-11 23:02:48 -06:00
|
|
|
func (this *Slider) drag () {
|
2024-07-21 09:48:28 -06:00
|
|
|
pointer := this.Window().MousePosition().Sub(this.dragOffset)
|
2023-08-11 23:02:48 -06:00
|
|
|
gutter := this.InnerBounds()
|
|
|
|
handle := this.handle.Bounds()
|
|
|
|
|
|
|
|
if this.layout.vertical {
|
|
|
|
this.SetValue (
|
2023-08-24 22:09:34 -06:00
|
|
|
1 -
|
2023-08-11 23:02:48 -06:00
|
|
|
float64(pointer.Y) /
|
|
|
|
float64(gutter.Dy() - handle.Dy()))
|
|
|
|
} else {
|
|
|
|
this.SetValue (
|
|
|
|
float64(pointer.X) /
|
|
|
|
float64(gutter.Dx() - handle.Dx()))
|
|
|
|
}
|
2024-06-27 12:01:14 -06:00
|
|
|
this.on.valueChange.Broadcast()
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Slider) fallbackDragOffset () image.Point {
|
|
|
|
if this.layout.vertical {
|
|
|
|
return this.InnerBounds().Min.
|
|
|
|
Add(image.Pt(0, this.handle.Bounds().Dy() / 2))
|
|
|
|
} else {
|
|
|
|
return this.InnerBounds().Min.
|
|
|
|
Add(image.Pt(this.handle.Bounds().Dx() / 2, 0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type sliderLayout struct {
|
|
|
|
vertical bool
|
|
|
|
value float64
|
|
|
|
}
|
|
|
|
|
2024-07-21 09:48:28 -06:00
|
|
|
func (sliderLayout) MinimumSize (hints tomo.LayoutHints, boxes tomo.BoxQuerier) image.Point {
|
|
|
|
if boxes.Len() != 1 { return image.Pt(0, 0) }
|
|
|
|
return boxes.MinimumSize(0)
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
|
|
|
|
2024-07-21 09:48:28 -06:00
|
|
|
func (this sliderLayout) Arrange (hints tomo.LayoutHints, boxes tomo.BoxArranger) {
|
|
|
|
if boxes.Len() != 1 { return }
|
|
|
|
handle := image.Rectangle { Max: boxes.MinimumSize(0) }
|
2023-08-11 23:02:48 -06:00
|
|
|
gutter := hints.Bounds
|
2023-08-24 22:09:34 -06:00
|
|
|
|
2023-08-11 23:02:48 -06:00
|
|
|
if this.vertical {
|
|
|
|
height := gutter.Dy() - handle.Dy()
|
2023-08-24 22:09:34 -06:00
|
|
|
offset := int(float64(height) * (1 - this.value))
|
2023-08-20 15:41:14 -06:00
|
|
|
handle.Max.X = gutter.Dx()
|
2024-07-21 09:48:28 -06:00
|
|
|
boxes.SetBounds (
|
|
|
|
0,
|
|
|
|
handle.Add(image.Pt(0, offset)).Add(gutter.Min))
|
2023-08-11 23:02:48 -06:00
|
|
|
} else {
|
|
|
|
width := gutter.Dx() - handle.Dx()
|
|
|
|
offset := int(float64(width) * this.value)
|
2023-08-20 15:41:14 -06:00
|
|
|
handle.Max.Y = gutter.Dy()
|
2024-07-21 09:48:28 -06:00
|
|
|
boxes.SetBounds (
|
|
|
|
0,
|
|
|
|
handle.Add(image.Pt(offset, 0)).Add(gutter.Min))
|
2023-08-11 23:02:48 -06:00
|
|
|
}
|
2023-08-10 15:52:01 -06:00
|
|
|
}
|
2024-06-11 15:17:11 -06:00
|
|
|
|
2024-07-21 09:48:28 -06:00
|
|
|
func (this sliderLayout) RecommendedHeight (hints tomo.LayoutHints, boxes tomo.BoxQuerier, width int) int {
|
|
|
|
return this.MinimumSize(hints, boxes).X
|
2024-06-11 15:17:11 -06:00
|
|
|
}
|
|
|
|
|
2024-07-21 09:48:28 -06:00
|
|
|
func (this sliderLayout) RecommendedWidth (hints tomo.LayoutHints, boxes tomo.BoxQuerier, height int) int {
|
|
|
|
return this.MinimumSize(hints, boxes).Y
|
2024-06-11 15:17:11 -06:00
|
|
|
}
|