Checkpoint
This commit is contained in:
160 files changed
+76293
-387
No files matched your search
+152
@@ -0,0 +1,152 @@
|
||||
package draw
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
|
||||
"github.com/BeatGlow/beatglow/draw/pixel"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Scroller struct {
|
||||
Canvas *Canvas
|
||||
Width int
|
||||
Wrap bool
|
||||
origin image.Point
|
||||
buff image.Image
|
||||
scroll int
|
||||
scrollDir int
|
||||
scrollWidth int
|
||||
scrollRequired bool
|
||||
}
|
||||
|
||||
func NewScroller(dst Image, width int, wrap bool) *Scroller {
|
||||
return &Scroller{
|
||||
Canvas: NewCanvas(dst),
|
||||
Width: width,
|
||||
Wrap: wrap,
|
||||
scrollDir: +1,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scroller) log() *logrus.Entry {
|
||||
return logrus.WithField("draw", "scroller")
|
||||
}
|
||||
|
||||
func (s *Scroller) Text(x, y int, text string) {
|
||||
if text == "" {
|
||||
s.buff = nil
|
||||
return
|
||||
}
|
||||
|
||||
width, height := s.Canvas.Style.Font.MeasureText(text)
|
||||
if s.Wrap && width >= s.Canvas.Image.Bounds().Size().X-x {
|
||||
width += 42 // 3 * (s.Canvas.Font.GlyphWith + s.Canvas.Font.Spacing)
|
||||
}
|
||||
buff := pixel.NewImage[pixel.Monochrome](width, height)
|
||||
|
||||
logrus.Printf("scroll color: %#+v", s.Canvas.Style.Foreground)
|
||||
s.Canvas.Style.Font.Draw(buff, image.Point{}, image.NewUniform(s.Canvas.Style.Foreground), text)
|
||||
s.scrollRequired = width > (s.Canvas.Image.Bounds().Size().X - x)
|
||||
if s.Wrap && s.scrollRequired {
|
||||
s.buff = wrapAround{buff, image.Pt(width, height)}
|
||||
} else {
|
||||
s.buff = buff
|
||||
}
|
||||
s.origin = image.Pt(x, y)
|
||||
s.scrollWidth = width
|
||||
|
||||
s.log().WithFields(logrus.Fields{
|
||||
"width": width,
|
||||
"height": height,
|
||||
"origin": s.origin,
|
||||
"scroll": s.scrollRequired,
|
||||
}).Debug("scroll write text")
|
||||
}
|
||||
|
||||
func (s *Scroller) Advance() {
|
||||
if s.buff == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
log = s.log()
|
||||
r image.Rectangle
|
||||
sp image.Point
|
||||
)
|
||||
|
||||
if s.scrollRequired {
|
||||
log = log.WithFields(logrus.Fields{
|
||||
"scroll": s.scroll,
|
||||
"scroll_new": s.scroll + s.scrollDir,
|
||||
})
|
||||
|
||||
s.scroll += s.scrollDir
|
||||
if s.scroll > s.scrollWidth {
|
||||
s.scroll = 0
|
||||
} else if s.scroll < -s.scrollWidth {
|
||||
s.scroll = 0
|
||||
}
|
||||
|
||||
// Copy the visible part of the buffer.
|
||||
r = image.Rectangle{
|
||||
Min: s.origin,
|
||||
Max: s.origin.Add(image.Point{X: s.Width, Y: s.buff.Bounds().Dy()}),
|
||||
}
|
||||
sp = image.Point{X: s.scroll}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"bounds": r,
|
||||
"origin": sp,
|
||||
}).Trace("scroller advance")
|
||||
} else {
|
||||
// Just copy the whole buffered image.
|
||||
r = image.Rectangle{
|
||||
Min: s.origin,
|
||||
Max: s.origin.Add(s.buff.Bounds().Size()),
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"bounds": r,
|
||||
"origin": sp,
|
||||
}).Trace("scroller advance (static)")
|
||||
}
|
||||
draw.Draw(s.Canvas.Image, r, s.buff, sp, draw.Src)
|
||||
}
|
||||
|
||||
func (s *Scroller) Reset() {
|
||||
s.scroll = 0
|
||||
}
|
||||
|
||||
func (s *Scroller) SetDirection(dir int) {
|
||||
if dir < 0 {
|
||||
dir = -1
|
||||
}
|
||||
if dir > 0 {
|
||||
dir = 1
|
||||
}
|
||||
s.scrollDir = dir
|
||||
}
|
||||
|
||||
type wrapAround struct {
|
||||
image.Image
|
||||
Size image.Point
|
||||
}
|
||||
|
||||
func (i wrapAround) At(x, y int) color.Color {
|
||||
for x < 0 {
|
||||
x += i.Size.X
|
||||
}
|
||||
for x >= i.Size.X {
|
||||
x -= i.Size.X
|
||||
}
|
||||
return i.Image.At(x, y)
|
||||
}
|
||||
|
||||
func (i wrapAround) Bounds() image.Rectangle {
|
||||
b := i.Image.Bounds()
|
||||
b.Min.X -= i.Size.X
|
||||
b.Max.X += i.Size.X
|
||||
return b
|
||||
}
|
||||
Reference in new issue
Block a user