dancefloor-monorepo/go/patterns/scrolling_text.go
2020-05-04 01:29:45 +01:00

60 lines
1.4 KiB
Go

package patterns
type ScrollingText struct {
Text string `binding:"required"`
Foreground Colour `binding:"required_without=Background"`
Background Colour `binding:"required_without=Foreground"`
initiated bool
fullRender [][]Colour
step int
substep int
}
func (s *ScrollingText) Draw(width, height int) *[][]Colour {
if !s.initiated {
s.init(width, height)
s.initiated = true
}
out := s.fullRender[s.step : s.step+width]
s.substep++
s.substep %= 12
if s.substep == 0 {
s.step++
s.step %= len(s.fullRender) - width
}
return &out
}
func (s *ScrollingText) init(width, height int) {
// add extra cells so it flows in from the right
for i := 0; i < width-1; i++ {
s.fullRender = append(s.fullRender, s.blankCol(height))
}
var c rune
for _, c = range s.Text {
subpattern := Character{Character: c, Colour: s.Foreground, Background: s.Background}
drawnSubpattern := subpattern.Draw(0, height)
for _, v := range *drawnSubpattern {
s.fullRender = append(s.fullRender, v)
}
s.fullRender = append(s.fullRender, s.blankCol(height))
}
// add extra cells to avoid overflow
for i := 0; i < width-1; i++ {
s.fullRender = append(s.fullRender, s.blankCol(height))
}
}
func (s *ScrollingText) blankCol(height int) (col []Colour) {
col = make([]Colour, height)
for j := 0; j < height; j++ {
col[j] = s.Background
}
return
}