diff options
Diffstat (limited to 'go/ui/buffer.go')
| -rw-r--r-- | go/ui/buffer.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/go/ui/buffer.go b/go/ui/buffer.go new file mode 100644 index 00000000..cc9f2377 --- /dev/null +++ b/go/ui/buffer.go @@ -0,0 +1,81 @@ +package ui + +import ( + "image/color" + + "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/text" +) + +var ( + BUFFER_BACKGROUND = gray + BUFFER_FOREGROUND = color.White +) + +type Buffer struct { + X, Y int + Width, Height int + img *ebiten.Image + lines []string + pos int +} + +func NewBuffer(x, y int, width, height int) *Buffer { + return &Buffer{x, y, width, height, nil, []string{}, 0} +} + +func (b *Buffer) render() { + img := ebiten.NewImage(b.Width, b.Height) + img.Fill(BUFFER_BACKGROUND) + y := 0 + for _, line := range b.lines[b.pos:] { + bounds := text.BoundString(Font, line) + y += bounds.Dy() + if y > b.Height { + break + } + text.Draw(img, line, Font, 0, y, BUFFER_FOREGROUND) + } + b.img = img +} + +func (b *Buffer) Draw(screen *ebiten.Image) { + if b.img == nil { + b.render() + } + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(float64(b.X), float64(b.Y)) + screen.DrawImage(b.img, op) +} + +func (b *Buffer) FindObjectAt(x, y int) interface{} { + return nil +} + +func (b *Buffer) ForceRedraw() { + b.img = nil +} + +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() +} |
