202 lines
4.2 KiB
Go
202 lines
4.2 KiB
Go
package ui
|
|
|
|
import (
|
|
"opslag.de/schobers/geom"
|
|
"opslag.de/schobers/zntg/allg5"
|
|
)
|
|
|
|
var _ Control = &ContentScrollbar{}
|
|
|
|
type ContentScrollbarValueChangedFn func(float32)
|
|
|
|
type ContentScrollbar struct {
|
|
ControlBase
|
|
Length float32
|
|
Value float32
|
|
Orientation Orientation
|
|
OnChanged ContentScrollbarValueChangedFn
|
|
handle *contentScrollbarHandle
|
|
barLength float32
|
|
scrollDistance float32
|
|
}
|
|
|
|
type contentScrollbarHandle struct {
|
|
ControlBase
|
|
}
|
|
|
|
func (h *contentScrollbarHandle) Render(ctx Context) {
|
|
var c = ctx.Palette().Primary()
|
|
if h.IsOver {
|
|
if h.IsPressed {
|
|
} else {
|
|
}
|
|
}
|
|
var min = h.Bounds.Min
|
|
var max = h.Bounds.Max
|
|
allg5.DrawFilledRectangle(min.X, min.Y, max.X, max.Y, c)
|
|
}
|
|
|
|
func (s *ContentScrollbar) Created(ctx Context, p Container) error {
|
|
s.ControlBase.Created(ctx, p)
|
|
s.handle = &contentScrollbarHandle{}
|
|
s.handle.Created(ctx, nil)
|
|
return nil
|
|
}
|
|
|
|
func (s *ContentScrollbar) Destroyed(ctx Context) {
|
|
s.handle.Destroyed(ctx)
|
|
}
|
|
|
|
func (s *ContentScrollbar) length() (float32, float32) {
|
|
var min, max float32
|
|
switch s.Orientation {
|
|
case OrientationHorizontal:
|
|
min = s.Bounds.Min.X
|
|
max = s.Bounds.Max.X
|
|
default:
|
|
min = s.Bounds.Min.Y
|
|
max = s.Bounds.Max.Y
|
|
}
|
|
return max - min, min
|
|
}
|
|
|
|
func (s *ContentScrollbar) updateBarLength() {
|
|
var length, _ = s.length()
|
|
var bar = length
|
|
if s.Length > length {
|
|
bar = length * length / s.Length
|
|
}
|
|
if bar < 20 {
|
|
if length < 40 {
|
|
bar = .5 * length
|
|
} else {
|
|
bar = 20
|
|
}
|
|
}
|
|
s.barLength = bar
|
|
var d = s.Length - length
|
|
if d < 0 {
|
|
d = 0
|
|
}
|
|
s.scrollDistance = d
|
|
}
|
|
|
|
func (s *ContentScrollbar) barViewCenter() float32 {
|
|
switch s.Orientation {
|
|
case OrientationHorizontal:
|
|
return (s.Bounds.Min.Y + s.Bounds.Max.Y) * .5
|
|
default:
|
|
return (s.Bounds.Min.X + s.Bounds.Max.X) * .5
|
|
}
|
|
}
|
|
|
|
func (s *ContentScrollbar) toValue(x, y float32) float32 {
|
|
var pos = y
|
|
if OrientationHorizontal == s.Orientation {
|
|
pos = x
|
|
}
|
|
var length, min = s.length()
|
|
if length == 0 {
|
|
return 0
|
|
}
|
|
var offset = (pos - .5*s.barLength - min) / (length - s.barLength)
|
|
if offset < 0 {
|
|
return 0
|
|
} else if offset > 1 {
|
|
return s.scrollDistance
|
|
}
|
|
return s.scrollDistance * offset
|
|
}
|
|
|
|
func (s *ContentScrollbar) change(v float32) {
|
|
if v != s.Value {
|
|
s.Value = v
|
|
var onChanged = s.OnChanged
|
|
if nil != onChanged {
|
|
onChanged(v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *ContentScrollbar) snapTo(x, y int) {
|
|
var val = s.toValue(float32(x), float32(y))
|
|
s.change(val)
|
|
}
|
|
|
|
func (s *ContentScrollbar) increment(d int) {
|
|
if s.Orientation == OrientationVertical {
|
|
d *= -1
|
|
}
|
|
var val = s.Value + float32(d)*ScrollbarWidth
|
|
if val < 0 {
|
|
val = 0
|
|
} else if val > s.scrollDistance {
|
|
val = s.scrollDistance
|
|
}
|
|
s.change(val)
|
|
}
|
|
|
|
func (s *ContentScrollbar) Handle(ctx Context, ev allg5.Event) {
|
|
s.ControlBase.Handle(ctx, ev)
|
|
s.handle.Handle(ctx, ev)
|
|
switch e := ev.(type) {
|
|
case *allg5.MouseMoveEvent:
|
|
if s.handle.IsPressed {
|
|
s.snapTo(e.X, e.Y)
|
|
}
|
|
if 0 != e.DeltaZ && s.IsOver {
|
|
var d = e.DeltaZ
|
|
if allg5.IsAnyKeyDown(allg5.KeyLShift, allg5.KeyRShift) {
|
|
d *= 10
|
|
}
|
|
s.increment(d)
|
|
}
|
|
case *allg5.MouseButtonDownEvent:
|
|
if !s.handle.IsPressed && s.IsOver {
|
|
s.snapTo(e.X, e.Y)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *ContentScrollbar) DesiredSize(Context) geom.PointF32 {
|
|
switch s.Orientation {
|
|
case OrientationHorizontal:
|
|
return geom.PtF32(geom.NaN32(), ScrollbarWidth)
|
|
}
|
|
return geom.PtF32(ScrollbarWidth, geom.NaN32())
|
|
}
|
|
|
|
func (s *ContentScrollbar) SetRect(rect geom.RectangleF32) {
|
|
switch s.Orientation {
|
|
case OrientationHorizontal:
|
|
if rect.Dy() > ScrollbarWidth {
|
|
rect.Min.Y = rect.Max.Y - ScrollbarWidth
|
|
}
|
|
default:
|
|
if rect.Dx() > ScrollbarWidth {
|
|
rect.Min.X = rect.Max.X - ScrollbarWidth
|
|
}
|
|
}
|
|
s.ControlBase.SetRect(rect)
|
|
s.updateBarLength()
|
|
|
|
var offset float32
|
|
if 0 < s.scrollDistance {
|
|
offset = s.Value / s.scrollDistance
|
|
}
|
|
var length, min = s.length()
|
|
var begin = min + (length-s.barLength)*offset
|
|
var end = begin + s.barLength
|
|
switch s.Orientation {
|
|
case OrientationHorizontal:
|
|
s.handle.SetRect(geom.RectF32(begin, rect.Min.Y, end, rect.Max.Y))
|
|
default:
|
|
s.handle.SetRect(geom.RectF32(rect.Min.X, begin, rect.Max.X, end))
|
|
}
|
|
}
|
|
|
|
func (s *ContentScrollbar) Render(ctx Context) {
|
|
s.handle.Render(ctx)
|
|
s.ControlBase.Render(ctx)
|
|
}
|