Keyboard control for sliders

This commit is contained in:
Sasha Koshka 2023-03-07 19:13:08 -05:00
parent 423e6869c0
commit f3c1c95a57
2 changed files with 52 additions and 11 deletions

View File

@ -238,6 +238,9 @@ func (window *Window) reallocateCanvas () {
newHeight := window.metrics.height
larger := newWidth > previousWidth || newHeight > previousHeight
smaller := newWidth < previousWidth / 2 || newHeight < previousHeight / 2
allocStep := 128
if larger || smaller {
if window.xCanvas != nil {
window.xCanvas.Destroy()
@ -246,8 +249,8 @@ func (window *Window) reallocateCanvas () {
window.backend.connection,
image.Rect (
0, 0,
(newWidth / 64) * 64 + 64,
(newHeight / 64) * 64 + 64))
(newWidth / allocStep + 1) * allocStep,
(newHeight / allocStep + 1) * allocStep))
window.xCanvas.CreatePixmap()
}
@ -291,16 +294,23 @@ func (window *Window) childDrawCallback (region canvas.Canvas) {
func (window *Window) paste (canvas canvas.Canvas) (updatedRegion image.Rectangle) {
data, stride := canvas.Buffer()
bounds := canvas.Bounds().Intersect(window.xCanvas.Bounds())
dstStride := window.xCanvas.Stride
dstData := window.xCanvas.Pix
// debug.PrintStack()
for x := bounds.Min.X; x < bounds.Max.X; x ++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y ++ {
rgba := data[x + y * stride]
index := x * 4 + y * window.xCanvas.Stride
window.xCanvas.Pix[index + 0] = rgba.B
window.xCanvas.Pix[index + 1] = rgba.G
window.xCanvas.Pix[index + 2] = rgba.R
window.xCanvas.Pix[index + 3] = rgba.A
}}
srcYComponent := y * stride
dstYComponent := y * dstStride
for x := bounds.Min.X; x < bounds.Max.X; x ++ {
rgba := data[srcYComponent + x]
index := dstYComponent + x * 4
dstData[index + 0] = rgba.B
dstData[index + 1] = rgba.G
dstData[index + 2] = rgba.R
dstData[index + 3] = rgba.A
}
}
return bounds
}

View File

@ -83,7 +83,24 @@ func (element *Slider) HandleMouseMove (x, y int) {
func (element *Slider) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }
func (element *Slider) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
// TODO: handle left and right arrows
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)
}
}
}
func (element *Slider) HandleKeyUp (key input.Key, modifiers input.Modifiers) { }
@ -135,6 +152,20 @@ func (element *Slider) SetConfig (new config.Config) {
element.redo()
}
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()
}
func (element *Slider) valueFor (x, y int) (value float64) {
if element.vertical {
value =