/
label.go
58 lines (48 loc) · 1.15 KB
/
label.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
package widget
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
"golang.org/x/image/font"
"image"
"image/color"
)
type Label struct {
fontFace font.Face
position image.Point
text string
color color.Color
size image.Point
}
func NewLabel(fontFace font.Face, x, y int, text string, color color.Color) *Label {
return &Label{
fontFace: fontFace,
position: image.Pt(x, y),
text: text,
color: color,
size: labelSize(fontFace, text),
}
}
func (l *Label) Update(*Ui) {
}
func (l *Label) Draw(dst *ebiten.Image) {
text.Draw(dst, l.text, l.fontFace, l.position.X, l.position.Y, l.color)
}
func (l *Label) GetPosition() image.Point {
return l.position
}
func (l *Label) SetPosition(x, y int) {
l.position = image.Pt(x, y)
}
func labelSize(fontFace font.Face, text string) image.Point {
bounds, _ := font.BoundString(fontFace, text)
x := (bounds.Max.X - bounds.Min.X).Ceil()
y := (bounds.Max.Y - bounds.Min.Y).Ceil()
return image.Pt(x, y)
}
func (l *Label) Size() image.Point {
return l.size
}
func (l *Label) SetText(text string) {
l.text = text
l.size = labelSize(l.fontFace, l.text)
}