zntg/ui/overlays.go

105 lines
2.1 KiB
Go
Raw Normal View History

2020-05-15 14:02:54 +00:00
package ui
import (
"opslag.de/schobers/geom"
"opslag.de/schobers/zntg"
)
2020-05-15 14:02:54 +00:00
var _ Control = &Overlays{}
type OverlayVisibilityChangedArgs struct {
Name string
Visible bool
}
2020-05-15 14:02:54 +00:00
type Overlays struct {
Proxy
overlays map[string]Control
order []string
visible map[string]bool
overlayHidden zntg.Events
overlayShown zntg.Events
visibilityChanged zntg.Events
2020-05-15 14:02:54 +00:00
}
func NewOverlays(over Control) *Overlays {
return &Overlays{
Proxy: Proxy{Content: over},
overlays: map[string]Control{},
visible: map[string]bool{},
}
}
func (o *Overlays) setVisibility(name string, visible bool) {
if o.visible[name] == visible {
return
}
o.visible[name] = visible
overlay, ok := o.overlays[name].(Overlay)
if ok {
if visible {
overlay.OverlayShown()
} else {
overlay.OverlayHidden()
}
}
o.visibilityChanged.Notify(OverlayVisibilityChangedArgs{Name: name, Visible: visible})
if visible {
o.overlayShown.Notify(name)
} else {
o.overlayHidden.Notify(name)
}
}
2020-05-15 14:02:54 +00:00
func (o *Overlays) AddOnTop(name string, control Control, visible bool) {
o.order = append(o.order, name)
o.overlays[name] = control
o.visible[name] = visible
}
func (o *Overlays) AddOnBottom(name string, control Control, visible bool) {
o.order = append([]string{name}, o.order...)
o.overlays[name] = control
o.visible[name] = visible
}
func (o *Overlays) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.PointF32, parent Control) {
o.Proxy.Arrange(ctx, bounds, offset, parent)
for _, overlay := range o.overlays {
overlay.Arrange(ctx, bounds, offset, parent)
}
}
func (o *Overlays) Handle(ctx Context, e Event) bool {
2020-05-15 14:02:54 +00:00
for overlay, visible := range o.visible {
if visible {
o.overlays[overlay].Handle(ctx, e) // ignore handled state on overlays
2020-05-15 14:02:54 +00:00
}
}
return o.Proxy.Handle(ctx, e)
2020-05-15 14:02:54 +00:00
}
func (o *Overlays) Hide(name string) {
o.visible[name] = false
}
func (o *Overlays) Render(ctx Context) {
o.Proxy.Render(ctx)
for overlay, visible := range o.visible {
if visible {
o.overlays[overlay].Render(ctx)
}
}
}
func (o *Overlays) Show(name string) {
o.visible[name] = true
}
func (o *Overlays) Toggle(name string) {
o.visible[name] = !o.visible[name]
}