From d9d758b5fc4a4cb4861c2a433282f98173a2f8f8 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 24 Aug 2024 22:09:31 -0400 Subject: [PATCH] Slider no longer embeds tomo.ContainerBox --- slider.go | 70 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/slider.go b/slider.go index 71001f8..0723b49 100644 --- a/slider.go +++ b/slider.go @@ -6,9 +6,11 @@ import "git.tebibyte.media/tomo/tomo" import "git.tebibyte.media/tomo/tomo/input" 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. type Slider struct { - tomo.ContainerBox + box tomo.ContainerBox handle *sliderHandle layout sliderLayout dragging bool @@ -26,8 +28,8 @@ type sliderHandle struct { } func newSlider (orient string, value float64) *Slider { - this := &Slider { - ContainerBox: tomo.NewContainerBox(), + slider := &Slider { + box: tomo.NewContainerBox(), handle: &sliderHandle { Box: tomo.NewBox(), }, @@ -38,23 +40,23 @@ func newSlider (orient string, value float64) *Slider { step: 0.05, } - this.Add(this.handle) - this.SetFocusable(true) - this.SetValue(value) - - this.SetInputMask(true) - this.OnKeyUp(this.handleKeyUp) - this.OnKeyDown(this.handleKeyDown) - this.OnButtonDown(this.handleButtonDown) - this.OnButtonUp(this.handleButtonUp) - this.OnMouseMove(this.handleMouseMove) - this.OnScroll(this.handleScroll) + slider.handle.SetRole(tomo.R("objects", "SliderHandle")) + slider.handle.SetTag(orient, true) + slider.box.SetRole(tomo.R("objects", "Slider")) + slider.box.SetTag(orient, true) - this.handle.SetRole(tomo.R("objects", "SliderHandle")) - this.handle.SetTag(orient, true) - this.SetRole(tomo.R("objects", "Slider")) - this.SetTag(orient, true) - return this + slider.box.Add(slider.handle) + slider.box.SetFocusable(true) + slider.SetValue(value) + + slider.box.SetInputMask(true) + slider.box.OnKeyUp(slider.handleKeyUp) + slider.box.OnKeyDown(slider.handleKeyDown) + slider.box.OnButtonDown(slider.handleButtonDown) + 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. @@ -67,13 +69,25 @@ func NewHorizontalSlider (value float64) *Slider { 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. 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 - this.SetAttr(tomo.ALayout(this.layout)) + this.box.SetAttr(tomo.ALayout(this.layout)) } // 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 { case input.KeyUp, input.KeyLeft: - if this.Window().Modifiers().Alt { + if this.box.Window().Modifiers().Alt { this.SetValue(0) } else { this.SetValue(this.Value() - increment) @@ -110,7 +124,7 @@ func (this *Slider) handleKeyDown (key input.Key, numpad bool) bool { this.on.valueChange.Broadcast() return true case input.KeyDown, input.KeyRight: - if this.Window().Modifiers().Alt { + if this.box.Window().Modifiers().Alt { this.SetValue(1) } else { 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 { - pointer := this.Window().MousePosition() + pointer := this.box.Window().MousePosition() handle := this.handle.Bounds() within := pointer.In(handle) @@ -156,7 +170,7 @@ func (this *Slider) handleButtonDown (button input.Button) bool { this.dragging = true this.dragOffset = pointer.Sub(this.handle.Bounds().Min). - Add(this.InnerBounds().Min) + Add(this.box.InnerBounds().Min) this.drag() } else { this.dragOffset = this.fallbackDragOffset() @@ -209,8 +223,8 @@ func (this *Slider) handleScroll (x, y float64) bool { } func (this *Slider) drag () { - pointer := this.Window().MousePosition().Sub(this.dragOffset) - gutter := this.InnerBounds() + pointer := this.box.Window().MousePosition().Sub(this.dragOffset) + gutter := this.box.InnerBounds() handle := this.handle.Bounds() if this.layout.vertical { @@ -228,10 +242,10 @@ func (this *Slider) drag () { func (this *Slider) fallbackDragOffset () image.Point { if this.layout.vertical { - return this.InnerBounds().Min. + return this.box.InnerBounds().Min. Add(image.Pt(0, this.handle.Bounds().Dy() / 2)) } else { - return this.InnerBounds().Min. + return this.box.InnerBounds().Min. Add(image.Pt(this.handle.Bounds().Dx() / 2, 0)) } }