2019-03-05 20:52:18 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
type Context interface {
|
2019-04-10 19:20:39 +00:00
|
|
|
Animate()
|
2019-03-05 20:52:18 +00:00
|
|
|
HasQuit() bool
|
|
|
|
Quit()
|
|
|
|
Renderer() Renderer
|
|
|
|
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{}
|
|
|
|
r Renderer
|
|
|
|
view Control
|
|
|
|
textures *Textures
|
|
|
|
style *Style
|
2019-03-05 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 19:20:39 +00:00
|
|
|
func (c *context) Animate() { c.animate = true }
|
|
|
|
|
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 12:20:07 +00:00
|
|
|
func (c *context) Renderer() Renderer { return c.r }
|
|
|
|
|
|
|
|
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) {
|
|
|
|
switch e.(type) {
|
|
|
|
case *DisplayCloseEvent:
|
|
|
|
c.Quit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.view.Handle(c, e)
|
|
|
|
}
|