Added overlays.

This commit is contained in:
Sander Schobers 2020-05-15 16:02:54 +02:00
parent 3591e22c97
commit 6db13c8f46
3 changed files with 80 additions and 3 deletions

View File

@ -4,6 +4,7 @@ type Context interface {
Animate() Animate()
Fonts() *Fonts Fonts() *Fonts
HasQuit() bool HasQuit() bool
Overlays() *Overlays
Quit() Quit()
Renderer() Renderer Renderer() Renderer
Style() *Style Style() *Style
@ -18,6 +19,7 @@ type context struct {
quit chan struct{} quit chan struct{}
renderer Renderer renderer Renderer
view Control view Control
overlays *Overlays
fonts *Fonts fonts *Fonts
textures *Textures textures *Textures
style *Style style *Style
@ -29,6 +31,7 @@ func newContext(r Renderer, s *Style, view Control) *context {
renderer: r, renderer: r,
style: s, style: s,
view: view, view: view,
overlays: NewOverlays(view),
fonts: NewFonts(r), fonts: NewFonts(r),
textures: NewTextures(r)} textures: NewTextures(r)}
} }
@ -51,6 +54,8 @@ func (c *context) HasQuit() bool {
} }
} }
func (c *context) Overlays() *Overlays { return c.overlays }
func (c *context) Renderer() Renderer { return c.renderer } func (c *context) Renderer() Renderer { return c.renderer }
func (c *context) Style() *Style { return c.style } func (c *context) Style() *Style { return c.style }
@ -71,5 +76,5 @@ func (c *context) Handle(e Event) {
c.Quit() c.Quit()
return return
} }
c.view.Handle(c, e) c.overlays.Handle(c, e)
} }

71
ui/overlays.go Normal file
View File

@ -0,0 +1,71 @@
package ui
import "opslag.de/schobers/geom"
var _ Control = &Overlays{}
type Overlays struct {
Proxy
overlays map[string]Control
order []string
visible map[string]bool
}
func NewOverlays(over Control) *Overlays {
return &Overlays{
Proxy: Proxy{Content: over},
overlays: map[string]Control{},
visible: map[string]bool{},
}
}
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) {
for overlay, visible := range o.visible {
if visible {
o.overlays[overlay].Handle(ctx, e)
}
}
o.Proxy.Handle(ctx, e)
}
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]
}

View File

@ -39,12 +39,13 @@ func RunWait(r Renderer, s *Style, view Control, wait bool) error {
}() }()
defer anim.Stop() defer anim.Stop()
overlays := ctx.Overlays()
ctx.Renderer().Refresh() ctx.Renderer().Refresh()
for !ctx.HasQuit() { for !ctx.HasQuit() {
var size = r.Size() var size = r.Size()
var bounds = geom.RectF32(0, 0, size.X, size.Y) var bounds = geom.RectF32(0, 0, size.X, size.Y)
view.Arrange(ctx, bounds, geom.ZeroPtF32, nil) overlays.Arrange(ctx, bounds, geom.ZeroPtF32, nil)
view.Render(ctx) overlays.Render(ctx)
if ctx.HasQuit() { if ctx.HasQuit() {
return nil return nil
} }