-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtext.go
125 lines (114 loc) · 3.26 KB
/
text.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
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
package rpg
import (
"fmt"
"log"
"unicode"
"github.com/aerth/rpg/assets"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
"github.com/golang/freetype/truetype"
"golang.org/x/image/colornames"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
)
var GraphicRanges = []*unicode.RangeTable{
unicode.L, unicode.M, unicode.N, unicode.P, unicode.S, unicode.Zs,
}
func NewTextSmooth(size float64) *text.Text {
font := ttfFromBytesMust(goregular.TTF, size)
basicAtlas := text.NewAtlas(font, text.ASCII, text.RangeTable(unicode.Common))
basicTxt := text.New(pixel.V(0, 0), basicAtlas)
return basicTxt
}
func NewTitleText(size float64) *text.Text {
b, err := assets.Asset("font/admtas.ttf")
if err != nil {
panic(err)
}
font := ttfFromBytesMust(b, size)
basicAtlas := text.NewAtlas(font, text.ASCII, text.RangeTable(unicode.Common))
basicTxt := text.New(pixel.V(0, 0), basicAtlas)
basicTxt.Dot = pixel.V(10, 10)
basicTxt.Orig = pixel.V(10, 10)
return basicTxt
}
func NewText(size float64) *text.Text {
b, err := assets.Asset("font/TerminusTTF-4.40.1.ttf")
if err != nil {
panic(err)
}
font := ttfFromBytesMust(b, size)
basicAtlas := text.NewAtlas(font, text.ASCII, text.RangeTable(unicode.Common))
basicTxt := text.New(pixel.V(0, 0), basicAtlas)
basicTxt.Dot = pixel.V(10, 10)
basicTxt.Orig = pixel.V(10, 10)
return basicTxt
}
func DrawText(winbounds pixel.Rect, t *text.Text, canvas pixel.Target, format string, i ...interface{}) {
imd := imdraw.New(nil)
color := pixel.ToRGBA(colornames.Darkslategrey)
imd.Color = color.Scaled(0.5)
imd.Push(pixel.V(0, winbounds.Max.Y-50), pixel.V(winbounds.Max.XY()))
imd.Rectangle(0)
imd.Push(pixel.V(0, 0), pixel.V(winbounds.Max.X, 80))
imd.Rectangle(0)
imd.Draw(canvas)
t.Dot = pixel.V(10, 10)
t.Orig = pixel.V(10, 10)
fmt.Fprintf(t, format, i...)
t.Draw(canvas, pixel.IM)
}
func ttfFromBytesMust(b []byte, size float64) font.Face {
ttf, err := truetype.Parse(b)
if err != nil {
panic(err)
}
return truetype.NewFace(ttf, &truetype.Options{
Size: size,
GlyphCacheEntries: 1,
})
}
func DrawScore(winbounds pixel.Rect, t *text.Text, canvas pixel.Target, format string, i ...interface{}) {
imd := imdraw.New(nil)
color := pixel.ToRGBA(colornames.Darkslategrey)
imd.Color = color.Scaled(0.9)
imd.Push(pixel.V(0, winbounds.Max.Y-50), pixel.V(winbounds.Max.XY()))
imd.Rectangle(0)
imd.Push(pixel.V(0, 0), pixel.V(winbounds.Max.X, 80))
imd.Rectangle(0)
imd.Draw(canvas)
t.Dot = pixel.V(10, winbounds.Max.Y-40)
t.Orig = t.Dot
fmt.Fprintf(t, format, i...)
t.Draw(canvas, pixel.IM)
}
func (w *World) Message(s string) {
log.Println(s)
}
func (w *World) TextBox(win *pixelgl.Window, msg string) {
if w.text == nil {
w.text = NewText(36)
} else {
w.text.Clear()
}
imd := imdraw.New(nil)
color := pixel.ToRGBA(colornames.Darkslategrey)
imd.Color = color.Scaled(0.9)
winbounds := win.Bounds()
imd.Push(pixel.ZV, pixel.V(winbounds.Max.XY()))
imd.Rectangle(0)
w.text.Dot = pixel.V(10, 400)
w.text.Orig = w.text.Dot
fmt.Fprintf(w.text, msg)
for !win.Closed() {
win.Clear(colornames.Black)
imd.Draw(win)
w.text.Draw(win, pixel.IM)
if win.JustPressed(pixelgl.KeySpace) {
break
}
win.Update()
}
}