Slider no longer embeds tomo.ContainerBox

This commit is contained in:
Sasha Koshka 2024-08-24 22:09:31 -04:00
parent a11bab452b
commit d9d758b5fc

View File

@ -6,9 +6,11 @@ import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input" import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event" import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(Slider)
// Slider is a control that selects a numeric value between 0 and 1. // Slider is a control that selects a numeric value between 0 and 1.
type Slider struct { type Slider struct {
tomo.ContainerBox box tomo.ContainerBox
handle *sliderHandle handle *sliderHandle
layout sliderLayout layout sliderLayout
dragging bool dragging bool
@ -26,8 +28,8 @@ type sliderHandle struct {
} }
func newSlider (orient string, value float64) *Slider { func newSlider (orient string, value float64) *Slider {
this := &Slider { slider := &Slider {
ContainerBox: tomo.NewContainerBox(), box: tomo.NewContainerBox(),
handle: &sliderHandle { handle: &sliderHandle {
Box: tomo.NewBox(), Box: tomo.NewBox(),
}, },
@ -38,23 +40,23 @@ func newSlider (orient string, value float64) *Slider {
step: 0.05, step: 0.05,
} }
this.Add(this.handle) slider.handle.SetRole(tomo.R("objects", "SliderHandle"))
this.SetFocusable(true) slider.handle.SetTag(orient, true)
this.SetValue(value) slider.box.SetRole(tomo.R("objects", "Slider"))
slider.box.SetTag(orient, true)
this.SetInputMask(true) slider.box.Add(slider.handle)
this.OnKeyUp(this.handleKeyUp) slider.box.SetFocusable(true)
this.OnKeyDown(this.handleKeyDown) slider.SetValue(value)
this.OnButtonDown(this.handleButtonDown)
this.OnButtonUp(this.handleButtonUp)
this.OnMouseMove(this.handleMouseMove)
this.OnScroll(this.handleScroll)
this.handle.SetRole(tomo.R("objects", "SliderHandle")) slider.box.SetInputMask(true)
this.handle.SetTag(orient, true) slider.box.OnKeyUp(slider.handleKeyUp)
this.SetRole(tomo.R("objects", "Slider")) slider.box.OnKeyDown(slider.handleKeyDown)
this.SetTag(orient, true) slider.box.OnButtonDown(slider.handleButtonDown)
return this slider.box.OnButtonUp(slider.handleButtonUp)
slider.box.OnMouseMove(slider.handleMouseMove)
slider.box.OnScroll(slider.handleScroll)
return slider
} }
// NewVerticalSlider creates a new vertical slider with the specified value. // NewVerticalSlider creates a new vertical slider with the specified value.
@ -67,13 +69,25 @@ func NewHorizontalSlider (value float64) *Slider {
return newSlider("horizontal", value) return newSlider("horizontal", value)
} }
// GetBox returns the underlying box.
func (this *Slider) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this slider has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *Slider) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// SetValue sets the value of the slider between 0 and 1. // SetValue sets the value of the slider between 0 and 1.
func (this *Slider) SetValue (value float64) { func (this *Slider) SetValue (value float64) {
if value < 0 { value = 0 } if value < 0 { value = 0 }
if value > 1 { value = 1 } if value > 1 { value = 1 }
if value == this.layout.value { return } if value == this.layout.value { return }
this.layout.value = value this.layout.value = value
this.SetAttr(tomo.ALayout(this.layout)) this.box.SetAttr(tomo.ALayout(this.layout))
} }
// Value returns the value of the slider between 0 and 1. // Value returns the value of the slider between 0 and 1.
@ -102,7 +116,7 @@ func (this *Slider) handleKeyDown (key input.Key, numpad bool) bool {
switch key { switch key {
case input.KeyUp, input.KeyLeft: case input.KeyUp, input.KeyLeft:
if this.Window().Modifiers().Alt { if this.box.Window().Modifiers().Alt {
this.SetValue(0) this.SetValue(0)
} else { } else {
this.SetValue(this.Value() - increment) this.SetValue(this.Value() - increment)
@ -110,7 +124,7 @@ func (this *Slider) handleKeyDown (key input.Key, numpad bool) bool {
this.on.valueChange.Broadcast() this.on.valueChange.Broadcast()
return true return true
case input.KeyDown, input.KeyRight: case input.KeyDown, input.KeyRight:
if this.Window().Modifiers().Alt { if this.box.Window().Modifiers().Alt {
this.SetValue(1) this.SetValue(1)
} else { } else {
this.SetValue(this.Value() + increment) this.SetValue(this.Value() + increment)
@ -140,7 +154,7 @@ func (this *Slider) handleKeyUp (key input.Key, numpad bool) bool {
} }
func (this *Slider) handleButtonDown (button input.Button) bool { func (this *Slider) handleButtonDown (button input.Button) bool {
pointer := this.Window().MousePosition() pointer := this.box.Window().MousePosition()
handle := this.handle.Bounds() handle := this.handle.Bounds()
within := pointer.In(handle) within := pointer.In(handle)
@ -156,7 +170,7 @@ func (this *Slider) handleButtonDown (button input.Button) bool {
this.dragging = true this.dragging = true
this.dragOffset = this.dragOffset =
pointer.Sub(this.handle.Bounds().Min). pointer.Sub(this.handle.Bounds().Min).
Add(this.InnerBounds().Min) Add(this.box.InnerBounds().Min)
this.drag() this.drag()
} else { } else {
this.dragOffset = this.fallbackDragOffset() this.dragOffset = this.fallbackDragOffset()
@ -209,8 +223,8 @@ func (this *Slider) handleScroll (x, y float64) bool {
} }
func (this *Slider) drag () { func (this *Slider) drag () {
pointer := this.Window().MousePosition().Sub(this.dragOffset) pointer := this.box.Window().MousePosition().Sub(this.dragOffset)
gutter := this.InnerBounds() gutter := this.box.InnerBounds()
handle := this.handle.Bounds() handle := this.handle.Bounds()
if this.layout.vertical { if this.layout.vertical {
@ -228,10 +242,10 @@ func (this *Slider) drag () {
func (this *Slider) fallbackDragOffset () image.Point { func (this *Slider) fallbackDragOffset () image.Point {
if this.layout.vertical { if this.layout.vertical {
return this.InnerBounds().Min. return this.box.InnerBounds().Min.
Add(image.Pt(0, this.handle.Bounds().Dy() / 2)) Add(image.Pt(0, this.handle.Bounds().Dy() / 2))
} else { } else {
return this.InnerBounds().Min. return this.box.InnerBounds().Min.
Add(image.Pt(this.handle.Bounds().Dx() / 2, 0)) Add(image.Pt(this.handle.Bounds().Dx() / 2, 0))
} }
} }