-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlabel.go
More file actions
415 lines (351 loc) · 10.1 KB
/
label.go
File metadata and controls
415 lines (351 loc) · 10.1 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package graphics
import (
"math"
"strings"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text/v2"
"github.com/quasilyte/ebitengine-graphics/internal/cache"
"github.com/quasilyte/gmath"
)
type AlignVertical uint8
const (
AlignVerticalTop AlignVertical = iota
AlignVerticalCenter
AlignVerticalBottom
)
type AlignHorizontal uint8
const (
AlignHorizontalLeft AlignHorizontal = iota
AlignHorizontalCenter
AlignHorizontalRight
)
type GrowVertical uint8
const (
GrowVerticalDown GrowVertical = iota
GrowVerticalUp
GrowVerticalBoth
GrowVerticalNone
)
type GrowHorizontal uint8
const (
GrowHorizontalRight GrowHorizontal = iota
GrowHorizontalLeft
GrowHorizontalBoth
GrowHorizontalNone
)
// Label is a simple text rendering object.
//
// It supports different kinds of grow/aling settings.
// The text color can be changed, but only for the whole text.
//
// Label implements gscene Graphics interface.
type Label struct {
colorScale ColorScale
ebitenColorScale ebiten.ColorScale
text string
Pos gmath.Pos
shadow *labelShadowData
flags labelFlag
fontID uint16
width uint16
height uint16
boundsWidth uint16
boundsHeight uint16
}
type labelShadowData struct {
enabled bool
colorScale ColorScale
ebitenColorScale ebiten.ColorScale
}
var disabledShadow = &labelShadowData{
enabled: false,
}
type labelFlag uint16
const (
// bit0
labelFlagVisible labelFlag = 1 << iota
// bit1, bit2
labelFlagAlignVerticalBit1
labelFlagAlignVerticalBit2
// bit3, bit4
labelFlagAlignHorizontalBit1
labelFlagAlignHorizontalBit2
// bit5, bit6
labelFlagGrowHorizontalBit1
labelFlagGrowHorizontalBit2
// bit7, bit8
labelFlagGrowVerticalBit1
labelFlagGrowVerticalBit2
// bit9
labelFlagDisposed
)
func NewLabel(ff text.Face) *Label {
fontID := cache.Global.InternFontFace(ff)
return &Label{
fontID: fontID,
flags: labelFlagVisible,
shadow: disabledShadow,
}
}
func (l *Label) SetFont(ff text.Face) {
l.fontID = cache.Global.InternFontFace(ff)
}
// SetShadow enables rendered text shadows.
//
// The shadow support is experimental and is inefficient.
// Right now only a 1 pixel-tall, straight vertical shadow is supported.
//
// Experimental: the API will change in the future.
func (l *Label) SetShadow(cs ColorScale) {
if cs.A == 0 {
l.shadow = disabledShadow
return
}
if l.shadow == disabledShadow {
l.shadow = &labelShadowData{enabled: true}
}
l.shadow.colorScale = cs
l.shadow.ebitenColorScale = cs.ToEbitenColorScale()
}
// GetColorScale is used to retrieve the current color scale value of the label's text.
// Use SetColorScale to change it.
func (l *Label) GetColorScale() ColorScale {
return l.colorScale
}
// SetColorScale assigns a new ColorScale to this label's text.
// Use GetColorScale to retrieve the current color scale.
func (l *Label) SetColorScale(cs ColorScale) {
if l.colorScale == cs {
return
}
l.colorScale = cs
l.ebitenColorScale = l.colorScale.ToEbitenColorScale()
}
// GetAlpha is a shorthand for GetColorScale().A expression.
// It's mostly provided for a symmetry with SetAlpha.
func (l *Label) GetAlpha() float32 { return l.colorScale.A }
// SetAlpha is a convenient way to change the alpha value of the ColorScale.
// It also changes the shadow alpha (if any).
func (l *Label) SetAlpha(a float32) {
if l.colorScale.A == a {
return
}
l.colorScale.A = a
l.ebitenColorScale = l.colorScale.ToEbitenColorScale()
if l.shadow.enabled {
l.shadow.colorScale.A = a
l.shadow.ebitenColorScale = l.shadow.colorScale.ToEbitenColorScale()
}
}
func (l *Label) Dispose() {
l.flags |= labelFlagDisposed
}
func (l *Label) IsDisposed() bool {
return l.flags&labelFlagDisposed != 0
}
func (l *Label) GetSize() (w, h int) {
return int(l.width), int(l.height)
}
func (l *Label) SetSize(w, h int) {
l.width = uint16(w)
l.height = uint16(h)
}
func (l *Label) GetAlignVertical() AlignVertical {
return AlignVertical((l.flags >> 1) & 0b11)
}
func (l *Label) SetAlignVertical(a AlignVertical) {
l.flags &^= labelFlagAlignVerticalBit1 | labelFlagAlignVerticalBit2
l.flags |= labelFlag(a&0b11) << 1
}
func (l *Label) GetAlignHorizontal() AlignHorizontal {
return AlignHorizontal((l.flags >> 3) & 0b11)
}
func (l *Label) SetAlignHorizontal(a AlignHorizontal) {
l.flags &^= labelFlagAlignHorizontalBit1 | labelFlagAlignHorizontalBit2
l.flags |= labelFlag(a&0b11) << 3
}
func (l *Label) GetGrowVertical() GrowVertical {
return GrowVertical((l.flags >> 5) & 0b11)
}
func (l *Label) SetGrowVertical(g GrowVertical) {
l.flags &^= labelFlagGrowVerticalBit1 | labelFlagGrowVerticalBit2
l.flags |= labelFlag(g&0b11) << 5
}
func (l *Label) GetGrowHorizontal() GrowHorizontal {
return GrowHorizontal((l.flags >> 7) & 0b11)
}
func (l *Label) SetGrowHorizontal(g GrowHorizontal) {
l.flags &^= labelFlagGrowHorizontalBit1 | labelFlagGrowHorizontalBit2
l.flags |= labelFlag(g&0b11) << 7
}
func (l *Label) IsVisible() bool {
return l.flags&labelFlagVisible != 0
}
// SetVisibility changes the Visible flag value.
// It can be used to show or hide the label.
// Use IsVisible to get the current flag value.
func (l *Label) SetVisibility(visible bool) {
setFlag(&l.flags, labelFlagVisible, visible)
}
func (l *Label) SetText(s string) {
l.text = s
fontInfo := cache.Global.FontInfoList[l.fontID]
w, h := text.Measure(l.text, fontInfo.Face, fontInfo.LineHeight)
l.boundsWidth = uint16(w)
l.boundsHeight = uint16(h)
if l.shadow.enabled {
l.boundsHeight++
}
}
func (l *Label) BoundsRect() gmath.Rect {
return l.containerRect(l.Pos.Resolve())
}
func (l *Label) Draw(dst *ebiten.Image) {
l.DrawWithOptions(dst, DrawOptions{})
}
func (l *Label) DrawWithOptions(dst *ebiten.Image, opts DrawOptions) {
if !l.IsVisible() || l.text == "" {
return
}
pos := l.Pos.Resolve()
offset := opts.Offset
numLines := strings.Count(l.text, "\n") + 1
containerRect := l.containerRect(pos)
switch l.GetAlignVertical() {
case AlignVerticalTop:
// Do nothing.
case AlignVerticalCenter:
pos.Y += float64(int(containerRect.Height()-l.estimateHeight(numLines)) / 2)
case AlignVerticalBottom:
pos.Y += float64(int(containerRect.Height() - l.estimateHeight(numLines)))
}
if l.shadow.enabled {
l.drawText(dst, opts.Blend, containerRect, pos, offset.Add(gmath.Vec{Y: 1}), l.shadow.ebitenColorScale)
}
l.drawText(dst, opts.Blend, containerRect, pos, offset, l.ebitenColorScale)
}
func (l *Label) drawText(dst *ebiten.Image, blend *ebiten.Blend, rect gmath.Rect, pos, offset gmath.Vec, clr ebiten.ColorScale) {
fontInfo := cache.Global.FontInfoList[l.fontID]
containerRect := rect
var drawOptions text.DrawOptions
if blend != nil {
drawOptions.Blend = *blend
}
drawOptions.ColorScale = clr
drawOptions.Filter = ebiten.FilterLinear
drawOptions.LineSpacing = fontInfo.LineHeight
if l.GetAlignHorizontal() == AlignHorizontalLeft {
drawOptions.GeoM.Translate(math.Round(pos.X), math.Round(pos.Y))
drawOptions.GeoM.Translate(offset.X, offset.Y)
text.Draw(dst, l.text, fontInfo.Face, &drawOptions)
return
}
// TODO: use ebitengine new layout options for alignment?
textRemaining := l.text
offsetY := 0.0
for {
nextLine := strings.IndexByte(textRemaining, '\n')
lineText := textRemaining
if nextLine != -1 {
lineText = textRemaining[:nextLine]
textRemaining = textRemaining[nextLine+len("\n"):]
}
lineBoundsWidth, _ := text.Measure(lineText, fontInfo.Face, fontInfo.LineHeight)
offsetX := 0.0
switch l.GetAlignHorizontal() {
case AlignHorizontalCenter:
offsetX = (containerRect.Width() - lineBoundsWidth) / 2
case AlignHorizontalRight:
offsetX = containerRect.Width() - lineBoundsWidth
}
drawOptions.GeoM.Reset()
drawOptions.GeoM.Translate(math.Round(pos.X+offsetX), math.Round(pos.Y+offsetY))
drawOptions.GeoM.Translate(offset.X, offset.Y)
text.Draw(dst, lineText, fontInfo.Face, &drawOptions)
if nextLine == -1 {
break
}
offsetY += fontInfo.LineHeight
}
}
func (l *Label) containerRect(pos gmath.Vec) gmath.Rect {
var containerRect gmath.Rect
boundsWidth := float64(l.boundsWidth)
boundsHeight := float64(l.boundsHeight)
fwidth := float64(l.width)
fheight := float64(l.height)
if l.width == 0 && l.height == 0 {
// Auto-sized container.
switch l.GetGrowHorizontal() {
case GrowHorizontalRight:
containerRect.Min.X = pos.X
containerRect.Max.X = pos.X + boundsWidth
case GrowHorizontalLeft:
containerRect.Min.X = pos.X - boundsWidth
containerRect.Max.X = pos.X
pos.X -= boundsWidth
case GrowHorizontalBoth:
containerRect.Min.X = pos.X - boundsWidth/2
containerRect.Max.X = pos.X + boundsWidth/2
pos.X -= boundsWidth / 2
}
switch l.GetGrowVertical() {
case GrowVerticalDown:
containerRect.Min.Y = pos.Y
containerRect.Max.Y = pos.Y + boundsHeight
case GrowVerticalUp:
containerRect.Min.Y = pos.Y - boundsHeight
containerRect.Max.Y = pos.Y
pos.Y -= boundsHeight
case GrowVerticalBoth:
containerRect.Min.Y = pos.Y - boundsHeight/2
containerRect.Max.Y = pos.Y + boundsHeight/2
pos.Y -= boundsHeight / 2
}
} else {
containerRect = gmath.Rect{
Min: pos,
Max: pos.Add(gmath.Vec{X: fwidth, Y: fheight}),
}
if delta := boundsWidth - fwidth; delta > 0 {
switch l.GetGrowHorizontal() {
case GrowHorizontalRight:
containerRect.Max.X += delta
case GrowHorizontalLeft:
containerRect.Min.X -= delta
case GrowHorizontalBoth:
containerRect.Min.X -= delta / 2
containerRect.Max.X += delta / 2
case GrowHorizontalNone:
// Do nothing.
}
}
if delta := boundsHeight - fheight; delta > 0 {
switch l.GetGrowVertical() {
case GrowVerticalDown:
containerRect.Min.Y += delta
case GrowVerticalUp:
containerRect.Min.Y -= delta
pos.Y -= delta
case GrowVerticalBoth:
containerRect.Min.Y -= delta / 2
containerRect.Max.Y += delta / 2
pos.Y -= delta / 2
case GrowVerticalNone:
// Do nothing.
}
}
}
return containerRect
}
func (l *Label) estimateHeight(numLines int) float64 {
fontInfo := cache.Global.FontInfoList[l.fontID]
estimatedHeight := fontInfo.LineHeight
if numLines >= 2 {
estimatedHeight += (float64(numLines) - 1) * fontInfo.LineHeight
}
if l.shadow.enabled {
estimatedHeight++
}
return estimatedHeight
}