From e41e4adb89834a83564b35d0f044551aebaeb869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Cuadros?= Date: Fri, 10 Mar 2017 14:43:04 +0100 Subject: [PATCH] toolkit: PlayAnimation fuction --- toolkit.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/toolkit.go b/toolkit.go index ede8938..d8639b8 100644 --- a/toolkit.go +++ b/toolkit.go @@ -43,6 +43,35 @@ func (tk *ToolKit) PlayImage(i image.Image, delay time.Duration) error { return tk.Canvas.Render() } +type Animation interface { + Next() (image.Image, time.Duration, error) +} + +// PlayAnimation play the image during the delay returned by Next, until an err +// is returned, if io.EOF is returned, PlayAnimation finish without an error +func (tk *ToolKit) PlayAnimation(a Animation) error { + var err error + var i image.Image + var d time.Duration + + for { + i, d, err = a.Next() + if err != nil { + break + } + + if err := tk.PlayImage(i, d); err != nil { + return err + } + } + + if err == io.EOF { + return nil + } + + return err +} + // PlayImages draws a sequence of images during the given delays, the len of // images should be equal to the len of delay. If loop is true the function // loops over images until a true is sent to the returned chan