gui/views/view_scrollbar.go

74 lines
1.7 KiB
Go
Raw Normal View History

2023-01-22 12:38:03 +01:00
package views
import (
"fmt"
"image"
"git.milar.in/milarin/gui"
)
type ScrollbarView struct {
gui.ViewTmpl
Orientation gui.Orientation
Thickness int
ViewSize int
ViewportPos int
ViewportSize int
width, height int
}
var _ gui.View = &ScrollbarView{}
func NewScrollbarView(orientation gui.Orientation) *ScrollbarView {
return &ScrollbarView{
Orientation: orientation,
Thickness: 15,
}
}
func (v *ScrollbarView) Layout(ctx gui.AppContext) (prefWidth, prefHeight int) {
if v.Orientation == gui.Horizontal {
return -1, v.Thickness
}
return v.Thickness, -1
}
func (v *ScrollbarView) Draw(img *gui.Image, ctx gui.AppContext) {
v.ViewTmpl.Draw(img, ctx)
v.width, v.height = img.Bounds().Dx(), img.Bounds().Dy()
var x, y, w, h int
if v.Orientation == gui.Vertical {
x = img.Bounds().Min.X
y = img.Bounds().Min.Y + v.ViewportPos*img.Bounds().Dy()/v.ViewSize
w = img.Bounds().Max.X
h = v.ViewportSize*img.Bounds().Dy()/v.ViewSize + 1
} else if v.Orientation == gui.Horizontal {
x = img.Bounds().Min.X + v.ViewportPos*img.Bounds().Dx()/v.ViewSize
y = img.Bounds().Min.Y
w = v.ViewportSize*img.Bounds().Dx()/v.ViewSize + 1
h = img.Bounds().Max.Y
}
img.DrawRect(image.Rect(x, y, x+w, y+h), v.Foreground(ctx))
}
func (v *ScrollbarView) OnMouseMove(event gui.MouseEvent) (consumed bool) {
if v.Orientation == gui.Vertical {
y := v.ViewportPos * v.height / v.ViewSize
h := v.ViewportSize*v.height/v.ViewSize + 1
fmt.Println(event.Buttons)
// TODO because MouseMove is only fired when mouse is moved, JustPressed button events don't work
if event.Position.Y > y && event.Position.Y < y+h && event.JustPressed(gui.MouseButtonLeft) {
fmt.Println("move start")
}
}
return true
}