diff --git a/pkg/tui/banner/snow.go b/pkg/tui/banner/snow.go index 96e4c2b678..a18961476b 100644 --- a/pkg/tui/banner/snow.go +++ b/pkg/tui/banner/snow.go @@ -17,6 +17,12 @@ const ( snowSeed = 0x5eed // Drift height cap in braille dots (two rows). maxDrift = 8 + // Max parallax shift in braille dots at pane edges; near shifts more than far for depth. + nearParallaxX = 14.0 + nearParallaxY = 8.0 + farParallaxX = 4.0 + farParallaxY = 2.0 + parallaxSmooth = 6.0 ) var ( @@ -30,14 +36,33 @@ func Season(now time.Time) bool { } // Snow is a flake field over a pane that piles into a drift along its bottom. -// Flake positions derive from elapsed time; only the drift carries state. +// Flake positions derive from elapsed time; only the drift and parallax carry state. type Snow struct { - elapsed time.Duration - size image.Point + elapsed time.Duration + size image.Point + targetX, targetY float64 + offsetX, offsetY float64 // ground holds settled dots per dot column. ground []int } +// Point steers the parallax camera toward a pane-relative cell position. +func (s *Snow) Point(x, y int, size image.Point) { + if size.X <= 0 || size.Y <= 0 { + return + } + nx := (float64(x)+0.5)/float64(size.X)*2 - 1 + ny := (float64(y)+0.5)/float64(size.Y)*2 - 1 + s.targetX = max(-1, min(1, nx)) + s.targetY = max(-1, min(1, ny)) +} + +// Leave relaxes the parallax camera back to center. +func (s *Snow) Leave() { + s.targetX = 0 + s.targetY = 0 +} + type flake struct { x0, y0, speed, sway, freq, phase float64 near bool @@ -60,7 +85,7 @@ func newFlake(i, w, h int) flake { return f } -func (f flake) x(t float64, w int) int { return wrap(f.x0+f.sway*math.Sin(f.freq*t+f.phase), w) } +func (f flake) driftX(t float64) float64 { return f.x0 + f.sway*math.Sin(f.freq*t+f.phase) } // fall is the unwrapped fall distance; each wrap past the bottom is a landing. func (f flake) fall(t float64) float64 { return f.y0 + f.speed*t } @@ -74,6 +99,11 @@ func (s *Snow) Advance(d time.Duration, size image.Point) { s.size = size s.ground = initialDrift(size.X * 2) } + if dt := d.Seconds(); dt > 0 { + alpha := 1 - math.Exp(-parallaxSmooth*dt) + s.offsetX += (s.targetX - s.offsetX) * alpha + s.offsetY += (s.targetY - s.offsetY) * alpha + } t0 := s.elapsed.Seconds() s.elapsed += d t1 := s.elapsed.Seconds() @@ -84,7 +114,7 @@ func (s *Snow) Advance(d time.Duration, size image.Point) { for i := range flakeCount(size) { f := newFlake(i, w, h) if math.Floor(f.fall(t1)/float64(h)) > math.Floor(f.fall(t0)/float64(h)) { - s.settle(f.x(t1, w)) + s.settle(wrap(f.driftX(t1), w)) } } } @@ -146,15 +176,17 @@ func (s *Snow) Paint(c *Canvas, area, behind image.Rectangle) { t := s.elapsed.Seconds() for _, layerNear := range []bool{true, false} { fg := far + dx, dy := -s.offsetX*farParallaxX, -s.offsetY*farParallaxY if layerNear { fg = near + dx, dy = -s.offsetX*nearParallaxX, -s.offsetY*nearParallaxY } for i := range flakeCount(area.Size()) { f := newFlake(i, w, h) if f.near != layerNear { continue } - x, y := f.x(t, w), wrap(f.fall(t), h) + x, y := wrap(f.driftX(t)+dx, w), wrap(f.fall(t)+dy, h) if len(ground) > 0 && y >= h-ground[x] { continue } diff --git a/pkg/tui/banner/snow_test.go b/pkg/tui/banner/snow_test.go index d14ba906ef..1d2fe5f74e 100644 --- a/pkg/tui/banner/snow_test.go +++ b/pkg/tui/banner/snow_test.go @@ -155,3 +155,57 @@ func TestSnowFlakesAreSingleDots(t *testing.T) { } } } + +func TestSnowParallax(t *testing.T) { + t.Parallel() + + size := image.Pt(60, 20) + rect := image.Rectangle{Max: size} + behind := image.Rect(15, 7, 45, 13) + + var base, parallax Snow + parallax.Point(0, 0, image.Point{}) // zero size is a no-op + assert.Zero(t, parallax.targetX) + assert.Zero(t, parallax.targetY) + + parallax.Point(size.X-1, size.Y-1, size) + assert.Greater(t, parallax.targetX, 0.9) + assert.Greater(t, parallax.targetY, 0.9) + + // Smoothly approaches target over ticks without snapping immediately. + base.Advance(time.Second/14, size) + parallax.Advance(time.Second/14, size) + assert.Positive(t, parallax.offsetX) + assert.Less(t, parallax.offsetX, parallax.targetX) + + for range 20 { + base.Advance(time.Second/14, size) + parallax.Advance(time.Second/14, size) + } + assert.InDelta(t, parallax.targetX, parallax.offsetX, 0.05) + assert.InDelta(t, parallax.targetY, parallax.offsetY, 0.05) + + cBase := NewCanvas(blankFrame(size.X, size.Y), rect, nil) + base.Paint(cBase, rect, behind) + cParallax := NewCanvas(blankFrame(size.X, size.Y), rect, nil) + parallax.Paint(cParallax, rect, behind) + assert.NotEqual(t, ansi.Strip(cBase.Render()), ansi.Strip(cParallax.Render())) + + // Parallax flakes still stay behind the wordmark. + for y, line := range strings.Split(ansi.Strip(cParallax.Render()), "\n") { + for x, r := range []rune(line) { + if image.Pt(x, y).In(behind) { + assert.Equal(t, ' ', r, "flake in front of wordmark at %d,%d", x, y) + } + } + } + + // Leaving relaxes parallax back to center. + parallax.Leave() + for range 40 { + base.Advance(time.Second/14, size) + parallax.Advance(time.Second/14, size) + } + assert.InDelta(t, 0, parallax.offsetX, 0.01) + assert.InDelta(t, 0, parallax.offsetY, 0.01) +} diff --git a/pkg/tui/page/chat/banner.go b/pkg/tui/page/chat/banner.go index 75fea1b07a..bfb623c07c 100644 --- a/pkg/tui/page/chat/banner.go +++ b/pkg/tui/page/chat/banner.go @@ -21,6 +21,7 @@ func (p *chatPage) cancelBanner() { p.bannerMotion.Stop() p.bannerFlock = banner.Flock{} p.bannerSnowMotion.Stop() + p.bannerSnow.Leave() } // SyncBannerSnow keeps snow ticking exactly while the banner is on screen. @@ -47,6 +48,7 @@ func LeaveBanner(page Page) tea.Cmd { } func (p *chatPage) leaveBanner() tea.Cmd { + p.bannerSnow.Leave() if p.bannerFlock.Leave() { return p.bannerMotion.Start() } @@ -58,6 +60,7 @@ func ResetBanner(page Page) { if p, ok := page.(*chatPage); ok { p.bannerMotion.Stop() p.bannerFlock = banner.Flock{} + p.bannerSnow.Leave() } } @@ -110,6 +113,15 @@ func CompositeBanner(page Page, base string, width, height, top int, masks []ima func (p *chatPage) pointBanner(x, y int, click bool) tea.Cmd { sl := p.computeSidebarLayout() + if p.bannerSnowMotion.IsActive() { + size := image.Pt(sl.chatWidth, sl.chatHeight) + pt := image.Pt(x, y).Sub(p.chatOrigin(sl)) + if pt.In(image.Rectangle{Max: size}) { + p.bannerSnow.Point(pt.X, pt.Y, size) + } else { + p.bannerSnow.Leave() + } + } origin := p.bannerOrigin(sl) x -= origin.X y -= origin.Y @@ -120,7 +132,10 @@ func (p *chatPage) pointBanner(x, y int, click bool) tea.Cmd { } inside := x >= left && x < right && y >= 0 && y < banner.Height if !p.bannerInteractive || !p.bannerVisible(sl) || (!inside && !p.bannerFlock.Near(float64(x), float64(y), p.bannerProgress(), sl.chatWidth)) { - return p.leaveBanner() + if p.bannerFlock.Leave() { + return p.bannerMotion.Start() + } + return nil } p.bannerFlock.Point(float64(x), float64(y), click, p.bannerProgress(), sl.chatWidth) return p.bannerMotion.Start() diff --git a/pkg/tui/page/chat/banner_animation_test.go b/pkg/tui/page/chat/banner_animation_test.go index fc5eb968f2..0d324f4bbf 100644 --- a/pkg/tui/page/chat/banner_animation_test.go +++ b/pkg/tui/page/chat/banner_animation_test.go @@ -241,6 +241,44 @@ func TestBannerSnowStopsWhenCancelled(t *testing.T) { assert.EqualValues(t, 0, ar.ActiveCount()) } +func TestBannerSnowParallaxFollowsPointer(t *testing.T) { + pBase, arBase := bannerPage(t) + pBase.bannerSnowEnabled = true + cmdBase := SyncBannerSnow(pBase, true) + require.NotNil(t, cmdBase) + + pHover, arHover := bannerPage(t) + pHover.bannerSnowEnabled = true + cmdHover := SyncBannerSnow(pHover, true) + require.NotNil(t, cmdHover) + + sl := pHover.computeSidebarLayout() + origin := pHover.chatOrigin(sl) + // Point near the right edge of the chat pane, outside the wordmark. + PointBanner(pHover, origin.X+sl.chatWidth-1, origin.Y+sl.chatHeight-1, false) + + step := func(p *chatPage, ar *animation.Runtime, cmd tea.Cmd) tea.Cmd { + tick, ok := ar.Accept(cmd().(animation.TickMsg)) + require.True(t, ok) + _, _ = p.Update(tick) + return ar.Continue() + } + for range 15 { + cmdBase = step(pBase, arBase, cmdBase) + cmdHover = step(pHover, arHover, cmdHover) + } + + render := func(p *chatPage) string { return CompositeBanner(p, p.View(), 100, 20, 0, nil) } + assert.NotEqual(t, render(pBase), render(pHover), "pointer over pane should shift snow via parallax") + + LeaveBanner(pHover) + for range 40 { + cmdBase = step(pBase, arBase, cmdBase) + cmdHover = step(pHover, arHover, cmdHover) + } + assert.Equal(t, render(pBase), render(pHover), "leaving should relax snow parallax back to center") +} + type viewCountingMessages struct { messages.Model