zntg/ui/context.go

112 lines
2.2 KiB
Go
Raw Normal View History

package ui
2020-05-15 14:39:53 +00:00
import (
"opslag.de/schobers/geom"
)
type Context interface {
2019-04-10 19:20:39 +00:00
Animate()
Fonts() *Fonts
HasQuit() bool
KeyModifiers() KeyModifier
2020-05-15 14:39:53 +00:00
MousePosition() geom.PointF32
2020-05-15 14:02:54 +00:00
Overlays() *Overlays
Quit()
Renderer() Renderer
Resources() Resources
2020-05-15 14:39:53 +00:00
ShowTooltip(t string)
Style() *Style
2020-05-15 12:20:07 +00:00
Textures() *Textures
}
var _ Context = &context{}
var _ EventTarget = &context{}
type context struct {
animate bool
quit chan struct{}
renderer Renderer
view Control
modifiers KeyModifier
mouse geom.PointF32
tooltip *Tooltip
overlays *Overlays
fonts *Fonts
textures *Textures
style *Style
}
func newContext(r Renderer, s *Style, view Control) *context {
2020-05-15 14:39:53 +00:00
ctx := &context{
quit: make(chan struct{}),
renderer: r,
style: s,
view: view,
2020-05-15 14:39:53 +00:00
tooltip: &Tooltip{},
2020-05-15 14:02:54 +00:00
overlays: NewOverlays(view),
fonts: NewFonts(r),
textures: NewTextures(r)}
2020-05-15 14:39:53 +00:00
ctx.overlays.AddOnTop(uiDefaultTooltipOverlay, ctx.tooltip, false)
2020-05-24 18:15:40 +00:00
ctx.overlays.AddOnTop(DefaultDebugOverlay, NewDebugOverlay(view), false)
2020-05-15 14:39:53 +00:00
return ctx
}
2019-04-10 19:20:39 +00:00
func (c *context) Animate() { c.animate = true }
func (c *context) Destroy() {
c.fonts.Destroy()
c.textures.Destroy()
}
func (c *context) Fonts() *Fonts { return c.fonts }
func (c *context) HasQuit() bool {
select {
case <-c.quit:
return true
default:
return false
}
}
func (c *context) KeyModifiers() KeyModifier { return c.modifiers }
2020-05-15 14:39:53 +00:00
func (c *context) MousePosition() geom.PointF32 { return c.mouse }
2020-05-15 14:02:54 +00:00
func (c *context) Overlays() *Overlays { return c.overlays }
func (c *context) Renderer() Renderer { return c.renderer }
2020-05-15 12:20:07 +00:00
func (c *context) Resources() Resources { return c.renderer.Resources() }
2020-05-15 14:39:53 +00:00
func (c *context) ShowTooltip(t string) {
c.tooltip.Text = t
}
2020-05-15 12:20:07 +00:00
func (c *context) Style() *Style { return c.style }
func (c *context) Quit() {
if !c.HasQuit() {
close(c.quit)
}
}
2020-05-15 12:20:07 +00:00
func (c *context) Textures() *Textures { return c.textures }
// Handle implement EventTarget
func (c *context) Handle(e Event) {
2020-05-15 14:39:53 +00:00
switch e := e.(type) {
case *DisplayCloseEvent:
c.Quit()
return
2020-05-15 14:39:53 +00:00
case *MouseMoveEvent:
c.mouse = e.Pos()
case *KeyDownEvent:
c.modifiers = e.Modifiers
case *KeyUpEvent:
c.modifiers = e.Modifiers
}
2020-05-15 14:02:54 +00:00
c.overlays.Handle(c, e)
}