1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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()
}
|