From fa7796a4ae0f12114cc7b187a3bf489fe474f453 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Wed, 10 Apr 2019 21:20:39 +0200 Subject: [PATCH] Added support for animations. --- ui/context.go | 14 +++++++++----- ui/ui.go | 13 +++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/ui/context.go b/ui/context.go index a3eef44..fa62457 100644 --- a/ui/context.go +++ b/ui/context.go @@ -1,6 +1,7 @@ package ui type Context interface { + Animate() HasQuit() bool Images() *Images Quit() @@ -12,13 +13,16 @@ var _ Context = &context{} var _ EventTarget = &context{} type context struct { - quit bool - r Renderer - view Control - ims *Images - style *Style + animate bool + quit bool + r Renderer + view Control + ims *Images + style *Style } +func (c *context) Animate() { c.animate = true } + func (c *context) HasQuit() bool { return c.quit } func (c *context) Images() *Images { return c.ims } diff --git a/ui/ui.go b/ui/ui.go index d6e35e9..8969ca6 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -1,6 +1,8 @@ package ui import ( + "time" + "opslag.de/schobers/geom" ) @@ -16,6 +18,16 @@ func RunWait(r Renderer, s *Style, view Control, wait bool) error { if ok { root.Init(ctx) } + anim := time.NewTicker(30 * time.Millisecond) + go func() { + for range anim.C { + if ctx.animate && !ctx.quit { + r.Refresh() + } + ctx.animate = false + } + }() + ctx.Renderer().Refresh() for !ctx.quit { var size = r.Size() var bounds = geom.RectF32(0, 0, size.X, size.Y) @@ -26,5 +38,6 @@ func RunWait(r Renderer, s *Style, view Control, wait bool) error { } r.PushEvents(ctx, wait) } + anim.Stop() return nil }