148 lines
2.8 KiB
Go
148 lines
2.8 KiB
Go
package widget
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
|
|
"github.com/BeatGlow/beatglow/ui"
|
|
"github.com/BeatGlow/display"
|
|
"github.com/BeatGlow/display/draw"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type Scroller struct {
|
|
Display display.Display
|
|
Position image.Point
|
|
Style ui.Style
|
|
width int
|
|
height int
|
|
buffer draw.Image
|
|
mask image.Image
|
|
text string
|
|
dir int
|
|
scroll int
|
|
scrollWidth int
|
|
required bool
|
|
changed bool
|
|
}
|
|
|
|
func NewScroller(display display.Display, at image.Point, width, height int) *Scroller {
|
|
r := display.Bounds()
|
|
if width <= 0 {
|
|
width = r.Dx()
|
|
}
|
|
if height <= 0 {
|
|
height = r.Dy()
|
|
}
|
|
|
|
w := &Scroller{
|
|
Display: display,
|
|
Position: at,
|
|
width: width,
|
|
height: height,
|
|
dir: +1,
|
|
}
|
|
w.SetStyle(ui.StylesFor(display.ColorModel()).Track)
|
|
w.Set("")
|
|
return w
|
|
}
|
|
|
|
func (w *Scroller) Set(text string) *Scroller {
|
|
width, _ := w.Style.Font.MeasureText(text)
|
|
if w.required = width > w.width; w.required {
|
|
// add spaces to the text and re-measure
|
|
width, _ = w.Style.Font.MeasureText(text + " ")
|
|
}
|
|
if width < w.width {
|
|
width = w.width
|
|
}
|
|
logrus.WithFields(logrus.Fields{
|
|
"size": fmt.Sprintf("%dx%d", width, w.height),
|
|
"text": text,
|
|
"widget": "scroller",
|
|
}).Debug("create new buffer")
|
|
w.buffer = ui.BufferFor(w.Display.ColorModel(), width, w.height)
|
|
draw.Draw(w.buffer, w.buffer.Bounds(), w.Style.Background, image.Point{}, draw.Src)
|
|
w.Style.Font.Draw(w.buffer, image.Point{}, w.mask, text)
|
|
if w.required {
|
|
w.buffer = horizontalRepeatingImage{
|
|
Image: w.buffer,
|
|
Size: image.Pt(width, w.height),
|
|
}
|
|
}
|
|
w.changed = true
|
|
w.text = text
|
|
w.scroll = 0
|
|
w.scrollWidth = width
|
|
return w
|
|
}
|
|
|
|
func (w *Scroller) SetDir(dir int) *Scroller {
|
|
w.dir = dir
|
|
if w.text != "" {
|
|
w.Set(w.text)
|
|
}
|
|
return w
|
|
}
|
|
|
|
func (w *Scroller) SetStyle(style ui.Style) *Scroller {
|
|
w.Style = style
|
|
w.mask = image.NewUniform(w.Style.Foreground)
|
|
if w.text != "" {
|
|
w.Set(w.text)
|
|
}
|
|
return w
|
|
}
|
|
|
|
func (w *Scroller) nextFrame() {
|
|
if !w.required || w.dir == 0 {
|
|
return
|
|
}
|
|
|
|
w.scroll += w.dir
|
|
if w.scroll > w.scrollWidth || w.scroll < -w.scrollWidth {
|
|
w.scroll = 0
|
|
}
|
|
w.changed = true
|
|
}
|
|
|
|
func (w *Scroller) Render(redraw bool) bool {
|
|
w.nextFrame()
|
|
|
|
if !(redraw || w.changed) {
|
|
return false
|
|
}
|
|
|
|
sp := image.Pt(w.scroll, 0)
|
|
draw.Draw(w.Display, image.Rectangle{
|
|
Min: w.Position,
|
|
Max: w.Position.Add(image.Point{X: w.width, Y: w.height}),
|
|
}, w.buffer, sp, draw.Src)
|
|
|
|
w.changed = false
|
|
return true
|
|
}
|
|
|
|
type horizontalRepeatingImage struct {
|
|
draw.Image
|
|
Size image.Point
|
|
}
|
|
|
|
func (i horizontalRepeatingImage) 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 horizontalRepeatingImage) Bounds() image.Rectangle {
|
|
b := i.Image.Bounds()
|
|
b.Min.X -= i.Size.X
|
|
b.Max.X += i.Size.X
|
|
return b
|
|
}
|