2019-03-05 20:52:18 +00:00
|
|
|
package ui
|
|
|
|
|
2020-05-15 14:39:53 +00:00
|
|
|
import (
|
|
|
|
"opslag.de/schobers/geom"
|
|
|
|
)
|
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
type Context interface {
|
2019-04-10 19:20:39 +00:00
|
|
|
Animate()
|
2020-05-15 13:42:24 +00:00
|
|
|
Fonts() *Fonts
|
2019-03-05 20:52:18 +00:00
|
|
|
HasQuit() bool
|
2020-05-15 14:39:53 +00:00
|
|
|
MousePosition() geom.PointF32
|
2020-05-15 14:02:54 +00:00
|
|
|
Overlays() *Overlays
|
2019-03-05 20:52:18 +00:00
|
|
|
Quit()
|
|
|
|
Renderer() Renderer
|
2020-05-15 14:39:53 +00:00
|
|
|
ShowTooltip(t string)
|
2019-03-05 20:52:18 +00:00
|
|
|
Style() *Style
|
2020-05-15 12:20:07 +00:00
|
|
|
Textures() *Textures
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Context = &context{}
|
|
|
|
var _ EventTarget = &context{}
|
|
|
|
|
|
|
|
type context struct {
|
2020-05-12 21:03:43 +00:00
|
|
|
animate bool
|
|
|
|
quit chan struct{}
|
2020-05-15 13:42:24 +00:00
|
|
|
renderer Renderer
|
2020-05-12 21:03:43 +00:00
|
|
|
view Control
|
2020-05-15 14:39:53 +00:00
|
|
|
mouse geom.PointF32
|
|
|
|
tooltip *Tooltip
|
2020-05-15 14:02:54 +00:00
|
|
|
overlays *Overlays
|
2020-05-15 13:42:24 +00:00
|
|
|
fonts *Fonts
|
2020-05-12 21:03:43 +00:00
|
|
|
textures *Textures
|
|
|
|
style *Style
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:42:24 +00:00
|
|
|
func newContext(r Renderer, s *Style, view Control) *context {
|
2020-05-15 14:39:53 +00:00
|
|
|
ctx := &context{
|
2020-05-15 13:42:24 +00:00
|
|
|
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),
|
2020-05-15 13:42:24 +00:00
|
|
|
fonts: NewFonts(r),
|
|
|
|
textures: NewTextures(r)}
|
2020-05-15 14:39:53 +00:00
|
|
|
ctx.overlays.AddOnTop(uiDefaultTooltipOverlay, ctx.tooltip, false)
|
|
|
|
return ctx
|
2020-05-15 13:42:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 19:20:39 +00:00
|
|
|
func (c *context) Animate() { c.animate = true }
|
|
|
|
|
2020-05-15 13:42:24 +00:00
|
|
|
func (c *context) Destroy() {
|
|
|
|
c.fonts.Destroy()
|
|
|
|
c.textures.Destroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) Fonts() *Fonts { return c.fonts }
|
|
|
|
|
2019-06-21 08:49:06 +00:00
|
|
|
func (c *context) HasQuit() bool {
|
|
|
|
select {
|
|
|
|
case <-c.quit:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 20:52:18 +00:00
|
|
|
|
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 }
|
|
|
|
|
2020-05-15 13:42:24 +00:00
|
|
|
func (c *context) Renderer() Renderer { return c.renderer }
|
2020-05-15 12:20:07 +00:00
|
|
|
|
2020-05-15 14:39:53 +00:00
|
|
|
func (c *context) ShowTooltip(t string) {
|
|
|
|
c.overlays.Show(uiDefaultTooltipOverlay)
|
|
|
|
c.tooltip.Text = t
|
|
|
|
}
|
|
|
|
|
2020-05-15 12:20:07 +00:00
|
|
|
func (c *context) Style() *Style { return c.style }
|
2019-03-05 20:52:18 +00:00
|
|
|
|
2019-06-21 08:49:06 +00:00
|
|
|
func (c *context) Quit() {
|
|
|
|
if !c.HasQuit() {
|
|
|
|
close(c.quit)
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 20:52:18 +00:00
|
|
|
|
2020-05-15 12:20:07 +00:00
|
|
|
func (c *context) Textures() *Textures { return c.textures }
|
2019-03-05 20:52:18 +00:00
|
|
|
|
|
|
|
// Handle implement EventTarget
|
2020-05-15 07:20:44 +00:00
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
func (c *context) Handle(e Event) {
|
2020-05-15 14:39:53 +00:00
|
|
|
switch e := e.(type) {
|
2019-03-05 20:52:18 +00:00
|
|
|
case *DisplayCloseEvent:
|
|
|
|
c.Quit()
|
|
|
|
return
|
2020-05-15 14:39:53 +00:00
|
|
|
case *MouseMoveEvent:
|
|
|
|
c.mouse = e.Pos()
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
2020-05-15 14:02:54 +00:00
|
|
|
c.overlays.Handle(c, e)
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|