2020-05-15 14:02:54 +00:00
|
|
|
package ui
|
|
|
|
|
2020-05-16 08:12:54 +00:00
|
|
|
import (
|
|
|
|
"opslag.de/schobers/geom"
|
|
|
|
"opslag.de/schobers/zntg"
|
|
|
|
)
|
2020-05-15 14:02:54 +00:00
|
|
|
|
|
|
|
var _ Control = &Overlays{}
|
|
|
|
|
2020-05-16 08:12:54 +00:00
|
|
|
type OverlayVisibilityChangedArgs struct {
|
|
|
|
Name string
|
|
|
|
Visible bool
|
|
|
|
}
|
|
|
|
|
2020-05-15 14:02:54 +00:00
|
|
|
type Overlays struct {
|
|
|
|
Proxy
|
|
|
|
|
2020-05-16 08:57:14 +00:00
|
|
|
overlays map[string]Overlay
|
2020-05-15 14:02:54 +00:00
|
|
|
order []string
|
|
|
|
visible map[string]bool
|
2020-05-16 08:12:54 +00:00
|
|
|
|
|
|
|
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},
|
|
|
|
|
2020-05-16 08:57:14 +00:00
|
|
|
overlays: map[string]Overlay{},
|
2020-05-15 14:02:54 +00:00
|
|
|
visible: map[string]bool{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 08:12:54 +00:00
|
|
|
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 {
|
2020-05-16 08:57:14 +00:00
|
|
|
overlay.Shown()
|
2020-05-16 08:12:54 +00:00
|
|
|
} else {
|
2020-05-16 08:57:14 +00:00
|
|
|
overlay.Hidden()
|
2020-05-16 08:12:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
o.visibilityChanged.Notify(OverlayVisibilityChangedArgs{Name: name, Visible: visible})
|
|
|
|
if visible {
|
|
|
|
o.overlayShown.Notify(name)
|
|
|
|
} else {
|
|
|
|
o.overlayHidden.Notify(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 08:57:14 +00:00
|
|
|
func (o *Overlays) AddOnTop(name string, overlay Overlay, visible bool) {
|
2020-05-15 14:02:54 +00:00
|
|
|
o.order = append(o.order, name)
|
2020-05-16 08:57:14 +00:00
|
|
|
o.overlays[name] = overlay
|
2020-05-15 14:02:54 +00:00
|
|
|
o.visible[name] = visible
|
|
|
|
}
|
|
|
|
|
2020-05-16 08:57:14 +00:00
|
|
|
func (o *Overlays) AddOnBottom(name string, overlay Overlay, visible bool) {
|
2020-05-15 14:02:54 +00:00
|
|
|
o.order = append([]string{name}, o.order...)
|
2020-05-16 08:57:14 +00:00
|
|
|
o.overlays[name] = overlay
|
2020-05-15 14:02:54 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 17:00:43 +00:00
|
|
|
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 {
|
2020-05-15 17:00:43 +00:00
|
|
|
o.overlays[overlay].Handle(ctx, e) // ignore handled state on overlays
|
2020-05-15 14:02:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-15 17:00:43 +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]
|
|
|
|
}
|