-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
94 lines (79 loc) · 2.04 KB
/
messages.go
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
package main
import (
"fmt"
"image/color"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/text"
)
type Message struct {
content string
dupes int
}
func (m *Message) ToString() string {
if m.dupes > 0 {
return fmt.Sprintf("%s (%d)", m.content, m.dupes+1)
}
return m.content
}
type MessageQueue struct {
// TODO duplicate message indicator
queue []*Message
max int
redraw bool
canvas *ebiten.Image
op *ebiten.DrawImageOptions
}
func CreateMessages() MessageQueue {
q := make([]*Message, 0)
return MessageQueue{
queue: q,
max: 8,
redraw: true,
}
}
func (m *MessageQueue) AddMessage(content string) {
// get the last message. if the content matches, increment the value
if len(m.queue) > 0 {
lm := m.queue[len(m.queue)-1]
if lm.content == content {
lm.dupes++
return
}
}
queue := append(m.queue, &Message{
content: content,
dupes: 0,
})
if len(queue) > m.max {
// dequeue
queue = queue[1:]
}
m.queue = queue
m.redraw = true
}
func (m *MessageQueue) DrawMessages(layer *ebiten.Image, x, y int) {
if m.redraw || m.canvas == nil {
canvas := ebiten.NewImage(300, 100)
for i := 0; i < len(messages.queue); i++ {
// TODO calculate longest string, justify each message and translate
// the canvas accordingly (instead of using static coordinates)
cy := 0 + (i * 14)
msg := messages.queue[len(messages.queue)-1-i].ToString()
bounds := text.BoundString(fontDetail, msg)
textImg := ebiten.NewImage(bounds.Dx(), bounds.Dy()+16)
text.Draw(textImg, msg, fontDetail, 0, bounds.Dy(), color.White)
textImgOps := &ebiten.DrawImageOptions{}
alpha := 1 - (0.3 * float64(i))
textImgOps.ColorM.Scale(1, 1, 1, alpha)
// TODO make it so we can draw bottom to top also via a bool or something
// and/or justify right to left
textImgOps.GeoM.Translate(0, float64(cy))
canvas.DrawImage(textImg, textImgOps)
}
m.canvas = canvas
}
m.redraw = false
ops := &ebiten.DrawImageOptions{}
ops.GeoM.Translate(float64(x), float64(y))
layer.DrawImage(m.canvas, ops)
}