-
Notifications
You must be signed in to change notification settings - Fork 0
/
textRenderSystem.go
81 lines (65 loc) · 1.75 KB
/
textRenderSystem.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
package foodzy
import (
"github.com/co0p/foodzy/asset"
"github.com/co0p/foodzy/component"
"github.com/co0p/foodzy/internal/ecs"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"image/color"
"log"
)
var (
FontSmall font.Face
FontMedium font.Face
FontBig font.Face
FontHuge font.Face
)
var (
PrimaryColor = color.RGBA{R: 0, G: 20, B: 27, A: 240}
SecondaryColor = color.RGBA{R: 0, G: 20, B: 27, A: 200}
)
func init() {
FontSmall = loadFont(asset.StopBullyingFont, 14)
FontMedium = loadFont(asset.StopBullyingFont, 24)
FontBig = loadFont(asset.StopBullyingFont, 46)
FontHuge = loadFont(asset.StopBullyingFont, 96)
}
type TextRenderSystem struct {
manager *ecs.EntityManager
}
func NewTextRenderSystem(manager *ecs.EntityManager) *TextRenderSystem {
return &TextRenderSystem{manager: manager}
}
func (s *TextRenderSystem) Draw(screen *ebiten.Image) {
candidates := s.manager.QueryByComponents(component.TextType, component.TransformType)
for _, e := range candidates {
if !e.Active {
continue
}
txt := e.GetComponent(component.TextType).(*component.Text)
pos := e.GetComponent(component.TransformType).(*component.Transform)
// we have to adjust the rendering due to the dot-positioning
_, h := txt.Dimensions()
text.Draw(screen, txt.Value, *txt.Font, int(pos.X), int(pos.Y+h), txt.Color)
}
}
func (s *TextRenderSystem) Update() error {
return nil
}
func loadFont(ttfFont []byte, size float64) font.Face {
tt, err := opentype.Parse(ttfFont)
if err != nil {
log.Fatal(err)
}
f, err := opentype.NewFace(tt, &opentype.FaceOptions{
Size: size,
DPI: 72,
Hinting: font.HintingFull,
})
if err != nil {
log.Fatal(err)
}
return f
}