From 67c02d1103bfaf7175f909fec3c5224d2a559a34 Mon Sep 17 00:00:00 2001 From: Caleb Foust Date: Mon, 11 Nov 2024 15:08:53 +0800 Subject: [PATCH] feat: break out all animations into packages --- pkg/anim/{collapse.go => collapse/module.go} | 15 +++--- pkg/anim/{conway.go => conway/module.go} | 11 ++-- pkg/anim/{cos.go => cos/module.go} | 8 ++- pkg/anim/{form.go => cy/module.go} | 11 ++-- pkg/anim/{fluid.go => fluid/anim.go} | 22 +++----- pkg/anim/{animation.go => meta/module.go} | 9 +--- pkg/anim/{midjo.go => midjo/module.go} | 11 ++-- pkg/anim/module.go | 51 +++++++++++++++++++ .../module.go} | 15 +++--- pkg/anim/{slime.go => slime/anim.go} | 18 +++---- pkg/anim/{static.go => static/module.go} | 5 +- pkg/layout/pane/static.go | 3 +- pkg/mux/screen/splash/module.go | 8 ++- 13 files changed, 104 insertions(+), 83 deletions(-) rename pkg/anim/{collapse.go => collapse/module.go} (84%) rename pkg/anim/{conway.go => conway/module.go} (92%) rename pkg/anim/{cos.go => cos/module.go} (85%) rename pkg/anim/{form.go => cy/module.go} (86%) rename pkg/anim/{fluid.go => fluid/anim.go} (81%) rename pkg/anim/{animation.go => meta/module.go} (52%) rename pkg/anim/{midjo.go => midjo/module.go} (88%) create mode 100644 pkg/anim/module.go rename pkg/anim/{reform.go => musicforprogramming/module.go} (98%) rename pkg/anim/{slime.go => slime/anim.go} (79%) rename pkg/anim/{static.go => static/module.go} (88%) diff --git a/pkg/anim/collapse.go b/pkg/anim/collapse/module.go similarity index 84% rename from pkg/anim/collapse.go rename to pkg/anim/collapse/module.go index 09cb4b1b..44925a1d 100644 --- a/pkg/anim/collapse.go +++ b/pkg/anim/collapse/module.go @@ -1,18 +1,23 @@ -package anim +package collapse import ( "time" + "github.com/cfoust/cy/pkg/anim/meta" "github.com/cfoust/cy/pkg/emu" "github.com/cfoust/cy/pkg/geom/image" ) +const ( + TICKS_PER_SECOND = 10 +) + type Collapse struct { last time.Duration current image.Image } -var _ Animation = (*Collapse)(nil) +var _ meta.Animation = (*Collapse)(nil) func (c *Collapse) Init(start image.Image) { c.current = start @@ -45,9 +50,3 @@ func (c *Collapse) Update(delta time.Duration) image.Image { return c.current } - -func init() { - registerAnimation("collapse", func() Animation { - return &Collapse{} - }) -} diff --git a/pkg/anim/conway.go b/pkg/anim/conway/module.go similarity index 92% rename from pkg/anim/conway.go rename to pkg/anim/conway/module.go index 4d75ca07..1c243469 100644 --- a/pkg/anim/conway.go +++ b/pkg/anim/conway/module.go @@ -1,8 +1,9 @@ -package anim +package conway import ( "time" + "github.com/cfoust/cy/pkg/anim/meta" "github.com/cfoust/cy/pkg/emu" "github.com/cfoust/cy/pkg/geom/image" ) @@ -12,7 +13,7 @@ type Conway struct { image image.Image } -var _ Animation = (*Conway)(nil) +var _ meta.Animation = (*Conway)(nil) func (c *Conway) Init(start image.Image) { c.image = start.Clone() @@ -94,9 +95,3 @@ func (c *Conway) Update(delta time.Duration) image.Image { c.image = next return c.image } - -func init() { - registerAnimation("conway", func() Animation { - return &Conway{} - }) -} diff --git a/pkg/anim/cos.go b/pkg/anim/cos/module.go similarity index 85% rename from pkg/anim/cos.go rename to pkg/anim/cos/module.go index 50e2237f..a403c528 100644 --- a/pkg/anim/cos.go +++ b/pkg/anim/cos/module.go @@ -1,9 +1,10 @@ -package anim +package cos import ( "math" "time" + "github.com/cfoust/cy/pkg/anim/meta" "github.com/cfoust/cy/pkg/geom/image" ) @@ -12,7 +13,7 @@ type Cos struct { out image.Image } -var _ Animation = (*Cos)(nil) +var _ meta.Animation = (*Cos)(nil) func (cos *Cos) Init(start image.Image) { cos.in = start.Clone() @@ -37,7 +38,4 @@ func (cos *Cos) Update(delta time.Duration) image.Image { } func init() { - registerAnimation("cos", func() Animation { - return &Cos{} - }) } diff --git a/pkg/anim/form.go b/pkg/anim/cy/module.go similarity index 86% rename from pkg/anim/form.go rename to pkg/anim/cy/module.go index 41990fb8..53eae36e 100644 --- a/pkg/anim/form.go +++ b/pkg/anim/cy/module.go @@ -1,9 +1,10 @@ -package anim +package cy import ( "time" "unicode" + "github.com/cfoust/cy/pkg/anim/meta" "github.com/cfoust/cy/pkg/geom/image" ) @@ -11,7 +12,7 @@ type Cyform struct { start image.Image } -var _ Animation = (*Cyform)(nil) +var _ meta.Animation = (*Cyform)(nil) func (c *Cyform) Init(start image.Image) { c.start = start @@ -51,9 +52,3 @@ func (c *Cyform) Update(delta time.Duration) image.Image { return c.start } - -func init() { - registerAnimation("cy", func() Animation { - return &Cyform{} - }) -} diff --git a/pkg/anim/fluid.go b/pkg/anim/fluid/anim.go similarity index 81% rename from pkg/anim/fluid.go rename to pkg/anim/fluid/anim.go index a656491b..89c50b6d 100644 --- a/pkg/anim/fluid.go +++ b/pkg/anim/fluid/anim.go @@ -1,21 +1,21 @@ -package anim +package fluid import ( "time" - "github.com/cfoust/cy/pkg/anim/fluid" + "github.com/cfoust/cy/pkg/anim/meta" "github.com/cfoust/cy/pkg/geom" "github.com/cfoust/cy/pkg/geom/image" ) type Fluid struct { last time.Duration - sim *fluid.Simulator + sim *Simulator start image.Image current image.Image } -var _ Animation = (*Fluid)(nil) +var _ meta.Animation = (*Fluid)(nil) const ( POSITION_FACTOR = 10 @@ -26,13 +26,13 @@ func (f *Fluid) Init(start image.Image) { size := start.Size() - var particles []fluid.Particle + var particles []Particle for row := 0; row < size.R; row++ { for col := 0; col < size.C; col++ { if start[row][col].IsEmpty() { continue } - particles = append(particles, fluid.NewParticle( + particles = append(particles, NewParticle( float64(col)*POSITION_FACTOR, float64(row)*POSITION_FACTOR, 0, @@ -41,7 +41,7 @@ func (f *Fluid) Init(start image.Image) { } } - f.sim = fluid.New( + f.sim = New( float64(size.C)*POSITION_FACTOR, float64(size.R)*POSITION_FACTOR, particles, @@ -66,7 +66,7 @@ func (f *Fluid) Update(delta time.Duration) image.Image { i := f.current particles := f.sim.Particles() - var particle fluid.Particle + var particle Particle var index, destRow, destCol int for row := 0; row < size.R; row++ { for col := 0; col < size.C; col++ { @@ -96,9 +96,3 @@ func (f *Fluid) Update(delta time.Duration) image.Image { return i } - -func init() { - registerAnimation("fluid", func() Animation { - return &Fluid{} - }) -} diff --git a/pkg/anim/animation.go b/pkg/anim/meta/module.go similarity index 52% rename from pkg/anim/animation.go rename to pkg/anim/meta/module.go index fb57d4f8..2593f93d 100644 --- a/pkg/anim/animation.go +++ b/pkg/anim/meta/module.go @@ -1,4 +1,4 @@ -package anim +package meta import ( "time" @@ -6,8 +6,6 @@ import ( "github.com/cfoust/cy/pkg/geom/image" ) -const DEFAULT_FPS = 30 - type Animation interface { Init(image.Image) Update(delta time.Duration) image.Image @@ -15,8 +13,3 @@ type Animation interface { type Creator func() Animation -var Animations = map[string]Creator{} - -func registerAnimation(name string, animation Creator) { - Animations[name] = animation -} diff --git a/pkg/anim/midjo.go b/pkg/anim/midjo/module.go similarity index 88% rename from pkg/anim/midjo.go rename to pkg/anim/midjo/module.go index d4e259d2..31ac00d7 100644 --- a/pkg/anim/midjo.go +++ b/pkg/anim/midjo/module.go @@ -1,9 +1,10 @@ -package anim +package midjo import ( "math" "time" + "github.com/cfoust/cy/pkg/anim/meta" "github.com/cfoust/cy/pkg/emu" "github.com/cfoust/cy/pkg/geom/image" ) @@ -13,7 +14,7 @@ type Midjo struct { out image.Image } -var _ Animation = (*Midjo)(nil) +var _ meta.Animation = (*Midjo)(nil) func (m *Midjo) Init(start image.Image) { m.in = start.Clone() @@ -44,9 +45,3 @@ func (mid *Midjo) Update(delta time.Duration) image.Image { } return mid.out } - -func init() { - registerAnimation("midjo", func() Animation { - return &Midjo{} - }) -} diff --git a/pkg/anim/module.go b/pkg/anim/module.go new file mode 100644 index 00000000..2bac6b76 --- /dev/null +++ b/pkg/anim/module.go @@ -0,0 +1,51 @@ +package anim + +import ( + "github.com/cfoust/cy/pkg/anim/collapse" + "github.com/cfoust/cy/pkg/anim/conway" + "github.com/cfoust/cy/pkg/anim/cos" + "github.com/cfoust/cy/pkg/anim/cy" + "github.com/cfoust/cy/pkg/anim/fluid" + "github.com/cfoust/cy/pkg/anim/meta" + "github.com/cfoust/cy/pkg/anim/midjo" + "github.com/cfoust/cy/pkg/anim/musicforprogramming" + "github.com/cfoust/cy/pkg/anim/slime" +) + +const DEFAULT_FPS = 30 + +type Creator = meta.Creator +type Animation = meta.Animation + +var Animations = map[string]Creator{} + +func registerAnimation(name string, animation Creator) { + Animations[name] = animation +} + +func init() { + registerAnimation("collapse", func() Animation { + return &collapse.Collapse{} + }) + registerAnimation("conway", func() Animation { + return &conway.Conway{} + }) + registerAnimation("cos", func() Animation { + return &cos.Cos{} + }) + registerAnimation("cy", func() Animation { + return &cy.Cyform{} + }) + registerAnimation("fluid", func() Animation { + return &fluid.Fluid{} + }) + registerAnimation("midjo", func() Animation { + return &midjo.Midjo{} + }) + registerAnimation("musicforprogramming", func() Animation { + return musicforprogramming.New() + }) + registerAnimation("slime", func() Animation { + return &slime.Slime{} + }) +} diff --git a/pkg/anim/reform.go b/pkg/anim/musicforprogramming/module.go similarity index 98% rename from pkg/anim/reform.go rename to pkg/anim/musicforprogramming/module.go index de554ff9..60a2d362 100644 --- a/pkg/anim/reform.go +++ b/pkg/anim/musicforprogramming/module.go @@ -1,4 +1,4 @@ -package anim +package musicforprogramming import ( "fmt" @@ -6,6 +6,7 @@ import ( "math/rand" "time" + "github.com/cfoust/cy/pkg/anim/meta" "github.com/cfoust/cy/pkg/emu" "github.com/cfoust/cy/pkg/geom" "github.com/cfoust/cy/pkg/geom/image" @@ -52,7 +53,7 @@ type MFP struct { lastReverse bool } -var _ Animation = (*MFP)(nil) +var _ meta.Animation = (*MFP)(nil) func (r *MFP) Init(start image.Image) { r.render = taro.NewRenderer() @@ -424,10 +425,8 @@ func (r *MFP) Update(delta time.Duration) image.Image { return r.out } -func init() { - registerAnimation("musicforprogramming", func() Animation { - return &MFP{ - duration: 5 * time.Second, - } - }) +func New() *MFP { + return &MFP{ + duration: 5 * time.Second, + } } diff --git a/pkg/anim/slime.go b/pkg/anim/slime/anim.go similarity index 79% rename from pkg/anim/slime.go rename to pkg/anim/slime/anim.go index b42f5dca..254fd413 100644 --- a/pkg/anim/slime.go +++ b/pkg/anim/slime/anim.go @@ -1,10 +1,10 @@ -package anim +package slime import ( "math" "time" - "github.com/cfoust/cy/pkg/anim/slime" + "github.com/cfoust/cy/pkg/anim/meta" "github.com/cfoust/cy/pkg/geom/image" ) @@ -12,18 +12,18 @@ type Slime struct { frame int zoomFrame int last time.Duration - sim *slime.Simulator + sim *Simulator start image.Image current image.Image } -var _ Animation = (*Slime)(nil) +var _ meta.Animation = (*Slime)(nil) func (f *Slime) Init(start image.Image) { f.start = start size := start.Size() - f.sim = slime.New(size.R, size.C) + f.sim = New(size.R, size.C) } func (f *Slime) Update(delta time.Duration) image.Image { @@ -34,7 +34,7 @@ func (f *Slime) Update(delta time.Duration) image.Image { f.last = delta size := f.start.Size() - cursor := slime.Cursor{} + cursor := Cursor{} // This determines how often we zoom zoom := math.Sin(float64(f.frame) / 80) @@ -62,9 +62,3 @@ func (f *Slime) Update(delta time.Duration) image.Image { f.frame++ return f.current } - -func init() { - registerAnimation("slime", func() Animation { - return &Slime{} - }) -} diff --git a/pkg/anim/static.go b/pkg/anim/static/module.go similarity index 88% rename from pkg/anim/static.go rename to pkg/anim/static/module.go index dcea5cf5..a2038eb7 100644 --- a/pkg/anim/static.go +++ b/pkg/anim/static/module.go @@ -1,10 +1,11 @@ -package anim +package static import ( "math/rand" "time" "unicode/utf8" + "github.com/cfoust/cy/pkg/anim/meta" "github.com/cfoust/cy/pkg/geom/image" ) @@ -12,7 +13,7 @@ type Static struct { in image.Image } -var _ Animation = (*Static)(nil) +var _ meta.Animation = (*Static)(nil) var BRAILLE = func() (runes []rune) { bytes := []byte{226, 161, 0} diff --git a/pkg/layout/pane/static.go b/pkg/layout/pane/static.go index 9bffeef3..26278b3d 100644 --- a/pkg/layout/pane/static.go +++ b/pkg/layout/pane/static.go @@ -4,6 +4,7 @@ import ( "context" "github.com/cfoust/cy/pkg/anim" + "github.com/cfoust/cy/pkg/anim/static" "github.com/cfoust/cy/pkg/frames" "github.com/cfoust/cy/pkg/geom" "github.com/cfoust/cy/pkg/geom/image" @@ -46,7 +47,7 @@ func (s *Static) resetScreen(size geom.Vec2) taro.Cmd { if s.shouldAnimate { bg = anim.NewAnimator( s.Ctx(), - &anim.Static{}, + &static.Static{}, initial, 1, ) diff --git a/pkg/mux/screen/splash/module.go b/pkg/mux/screen/splash/module.go index 8af4bb32..af6628e9 100644 --- a/pkg/mux/screen/splash/module.go +++ b/pkg/mux/screen/splash/module.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/cfoust/cy/pkg/anim" + "github.com/cfoust/cy/pkg/anim/midjo" "github.com/cfoust/cy/pkg/frames" "github.com/cfoust/cy/pkg/geom" "github.com/cfoust/cy/pkg/geom/image" @@ -106,7 +107,12 @@ func New(ctx context.Context, size geom.Size, shouldAnimate bool) *taro.Program var bg mux.Screen initial := generateBackground(render, size.Scalar(2)) if shouldAnimate { - bg = anim.NewAnimator(lifetime.Ctx(), &anim.Midjo{}, initial, 23) + bg = anim.NewAnimator( + lifetime.Ctx(), + &midjo.Midjo{}, + initial, + 23, + ) } else { bg = frames.NewFramer( ctx,