package ui import ( "image/color" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/text" "muhq.space/muhqs-game/go/font" ) var ( BUFFER_BACKGROUND = Gray BUFFER_FOREGROUND = color.White ) type Buffer struct { WidgetBase lines []string pos int } func NewBuffer(x, y int, width, height int) *Buffer { b := &Buffer{NewWidgetBase(x, y, width, height), []string{}, 0} b.renderImpl = func() *ebiten.Image { return b.render() } return b } func (b *Buffer) render() *ebiten.Image { img := ebiten.NewImage(b.Width, b.Height) img.Fill(BUFFER_BACKGROUND) y := 0 for _, line := range b.lines[b.pos:] { bounds := text.BoundString(font.Font18, line) y += bounds.Dy() if y > b.Height { break } text.Draw(img, line, font.Font18, 0, y, BUFFER_FOREGROUND) } return img } func (b *Buffer) AddLines(lines []string) { b.lines = append(b.lines, lines...) b.ForceRedraw() } func (b *Buffer) AddLine(line string) { b.AddLines([]string{line}) } func (b *Buffer) ClearLines() { b.lines = []string{} b.ForceRedraw() } func (b *Buffer) RemoveLast() { b.lines = b.lines[:len(b.lines)-1] b.ForceRedraw() } func (b *Buffer) Remove(idx int) { b.lines = append(b.lines[:idx], b.lines[idx+1:]...) b.ForceRedraw() } func (b *Buffer) Scroll(x, y int) { if y == 0 { return } if y < 0 && b.pos > 0 { b.pos-- } else if y > 0 && b.pos < len(b.lines)-1 { b.pos++ } b.ForceRedraw() }