diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2022-12-27 14:29:39 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-08-20 15:46:38 +0200 |
| commit | d6b5eeaa5e639c5ab89b5e1c93075a1b765794dc (patch) | |
| tree | f87422bb91c2197f88aa8d29092592858764049d /go/ui/buffer.go | |
| parent | 3c2ee4ee48480a28dc606878503f314ec8862d45 (diff) | |
| download | muhqs-game-d6b5eeaa5e639c5ab89b5e1c93075a1b765794dc.tar.gz muhqs-game-d6b5eeaa5e639c5ab89b5e1c93075a1b765794dc.zip | |
intermediate commit
Diffstat (limited to 'go/ui/buffer.go')
| -rw-r--r-- | go/ui/buffer.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/go/ui/buffer.go b/go/ui/buffer.go new file mode 100644 index 00000000..c451d7fc --- /dev/null +++ b/go/ui/buffer.go @@ -0,0 +1,66 @@ +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() +} |
