aboutsummaryrefslogtreecommitdiff
path: root/go/ui/buffer.go
blob: 5004416329dd3d2cf8eefe3485f9538deb4035e1 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package ui

import (
	"image/color"
	"iter"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/text/v2"

	"muhq.space/muhqs-game/go/font"
	"muhq.space/muhqs-game/go/game"
)

var (
	BUFFER_BACKGROUND = Gray
	BUFFER_FOREGROUND = color.White
)

type Buffer struct {
	WidgetBase
	lines       []string
	highlights  map[int]color.Color
	pos         int
	font        *text.GoTextFace
	lineSpacing float64
	xMargin     float64
	bg          color.Color
	fg          color.Color
}

func NewBuffer(x, y int, width, height int) *Buffer {
	b := &Buffer{
		NewWidgetBase(x, y, width, height),
		[]string{},
		make(map[int]color.Color),
		0,
		font.Font18,
		1.5,
		10.,
		BUFFER_BACKGROUND,
		BUFFER_FOREGROUND,
	}
	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(b.bg)
	y := -b.font.Size
	for i, line := range b.lines[b.pos:] {
		_, h := text.Measure(line, b.font, b.font.Size*b.lineSpacing)
		y += h
		if y > float64(b.Height) {
			break
		}
		op := &text.DrawOptions{}
		if clr, highlighted := b.highlights[i]; highlighted {
			op.ColorScale.ScaleWithColor(clr)
		} else {
			op.ColorScale.ScaleWithColor(b.fg)
		}
		op.GeoM.Translate(b.xMargin, float64(y))
		op.LineSpacing = b.font.Size * b.lineSpacing
		text.Draw(img, line, b.font, op)
	}
	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) AddText(text string) {
	// TODO: auto break text
	lines := []string{text}
	b.AddLines(lines)
}

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) PrefixLine(idx int, prefix string) {
	b.lines[idx] = prefix + b.lines[idx]
	b.ForceRedraw()
}

func (b *Buffer) LinesSeq() iter.Seq[string] {
	return func(yield func(string) bool) {
		for _, l := range b.lines {
			if !yield(l) {
				return
			}
		}
	}
}

func (*Buffer) IsScrollable() bool { return true }

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()
}

func (b *Buffer) Bg(bg color.Color) *Buffer {
	b.bg = bg
	return b
}

func (b *Buffer) Fg(fg color.Color) *Buffer {
	b.fg = fg
	return b
}

type StackBuffer struct {
	Buffer
	actions []game.Action
}

func NewStackBuffer(x, y int, width, height int, actions []game.Action) *StackBuffer {
	sb := &StackBuffer{Buffer: *NewBuffer(x, y, width, height)}
	// This is needed because we do not use the pointer created in NewBuffer
	sb.renderImpl = func() *ebiten.Image { return sb.render() }

	for _, action := range actions {
		sb.AddAction(action)
	}
	return sb
}

func (b *StackBuffer) AddAction(a game.Action) {
	b.AddLine(a.String())
	b.actions = append(b.actions, a)
}

func (b *StackBuffer) AddHighlight(a game.Action, clr color.Color) {
	for i, ba := range b.actions {
		if a == ba {
			b.highlights[i] = clr
		}
	}
	b.ForceRedraw()
}

func (b *StackBuffer) ClearHighlights() {
	b.highlights = make(map[int]color.Color)
	b.ForceRedraw()
}

func (b *StackBuffer) RemoveLast() {
	b.Buffer.RemoveLast()

	// Remove potential highlight
	i := len(b.actions) - 1
	delete(b.highlights, i)

	b.actions = b.actions[:i]
}

func (b *StackBuffer) FindObjectAt(x, y int) any {
	if !b.Contains(x, y) {
		return nil
	}

	var _y float64 = 0
	for i, line := range b.lines[b.pos:] {
		_, h := text.Measure(line, b.font, b.font.Size*b.lineSpacing)
		_y += h

		if int(_y)+b.Y >= y {
			return b.actions[i]
		}
	}
	return nil
}