zntg/ui/scroll.go

78 lines
1.8 KiB
Go
Raw Normal View History

package ui
import (
"opslag.de/schobers/geom"
2018-09-23 07:42:23 +00:00
"opslag.de/schobers/zntg/allg5"
)
type scroll struct {
Wrapper
Content Control
Bar *ContentScrollbar
}
func Scroll(c Control, o Orientation) Control {
var dock = NewDockPanel(nil)
var bar = &ContentScrollbar{Orientation: o}
switch o {
case OrientationHorizontal:
dock.Append(DockBottom, TopMargin(bar, ScrollbarWidth))
dock.Append(DockBottom, c)
case OrientationVertical:
dock.Append(DockRight, LeftMargin(bar, ScrollbarWidth))
dock.Append(DockRight, c)
}
var s = &scroll{Wrap(dock), c, bar}
2018-09-23 07:42:23 +00:00
bar.OnChanged = func(v float32) {
}
return s
}
func (s *scroll) Handle(ctx Context, ev allg5.Event) {
s.Wrapper.Handle(ctx, ev)
switch e := ev.(type) {
case *allg5.MouseMoveEvent:
2018-09-23 07:42:23 +00:00
if 0 != e.DeltaZ && !s.Bar.IsOver && geom.PtF32(float32(e.X), float32(e.Y)).In(s.Bounds) {
var d = e.DeltaZ
if allg5.IsAnyKeyDown(allg5.KeyLShift, allg5.KeyRShift) {
d *= 10
}
s.Bar.increment(d)
}
}
}
func (s *scroll) Render(ctx Context) {
var bounds = s.Content.Rect()
var w, h = bounds.Dx(), bounds.Dy()
var bmp, err = allg5.NewVideoBitmap(int(w), int(h))
if nil != err {
return
}
defer bmp.Destroy()
bmp.SetAsTarget()
switch s.Bar.Orientation {
case OrientationHorizontal:
2018-09-23 07:42:23 +00:00
Arrange(ctx, s.Content, geom.RectF32(-s.Bar.Value, 0, w, h))
case OrientationVertical:
2018-09-23 07:42:23 +00:00
Arrange(ctx, s.Content, geom.RectF32(0, -s.Bar.Value, w, h))
}
s.Content.Render(ctx)
ctx.Display().SetAsTarget()
2018-09-23 07:42:23 +00:00
var min = s.Bounds.Min
bmp.Draw(min.X, min.Y)
s.Bar.Render(ctx)
}
2018-09-23 07:42:23 +00:00
func (s *scroll) Arrange(ctx Context, rect geom.RectangleF32) {
var sz = s.Content.DesiredSize(ctx)
switch s.Bar.Orientation {
case OrientationHorizontal:
s.Bar.Length = sz.X
case OrientationVertical:
s.Bar.Length = sz.Y
}
s.Wrapper.Arrange(ctx, rect)
}