1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-07-03 11:55:22 +00:00
OpenDiablo2/d2render/d2ui/label.go

101 lines
2.2 KiB
Go
Raw Normal View History

2019-11-10 13:51:02 +00:00
package d2ui
2019-10-24 13:31:59 +00:00
import (
"image/color"
2019-10-24 13:31:59 +00:00
"github.com/hajimehoshi/ebiten"
)
2019-10-25 23:37:04 +00:00
// LabelAlignment represents a label's alignment
type LabelAlignment uint8
const (
2019-10-25 23:37:04 +00:00
// LabelAlignLeft represents a left-aligned label
LabelAlignLeft LabelAlignment = 0
// LabelAlignCenter represents a center-aligned label
LabelAlignCenter LabelAlignment = 1
// LabelAlignRight represents a right-aligned label
LabelAlignRight LabelAlignment = 2
)
2019-10-25 23:37:04 +00:00
// Label represents a user interface label
type Label struct {
2019-10-24 13:31:59 +00:00
text string
X int
Y int
Width int
Height int
2019-10-25 23:37:04 +00:00
Alignment LabelAlignment
font *Font
2019-10-24 13:31:59 +00:00
imageData *ebiten.Image
Color color.Color
2019-10-24 13:31:59 +00:00
}
2019-10-25 23:37:04 +00:00
// CreateLabel creates a new instance of a UI label
func CreateLabel(fontPath, palettePath string) Label {
result := Label{
2019-10-25 23:37:04 +00:00
Alignment: LabelAlignLeft,
Color: color.White,
font: GetFont(fontPath, palettePath),
2019-10-24 13:31:59 +00:00
}
return result
}
// Render draws the label on the screen
func (v *Label) Render(target *ebiten.Image) {
2019-10-24 13:31:59 +00:00
if len(v.text) == 0 {
return
}
v.cacheImage()
opts := &ebiten.DrawImageOptions{}
2019-10-25 23:37:04 +00:00
if v.Alignment == LabelAlignCenter {
opts.GeoM.Translate(float64(v.X-int(v.Width/2)), float64(v.Y))
2019-10-25 23:37:04 +00:00
} else if v.Alignment == LabelAlignRight {
opts.GeoM.Translate(float64(v.X-int(v.Width)), float64(v.Y))
} else {
opts.GeoM.Translate(float64(v.X), float64(v.Y))
}
2019-10-24 13:31:59 +00:00
opts.CompositeMode = ebiten.CompositeModeSourceAtop
opts.Filter = ebiten.FilterNearest
target.DrawImage(v.imageData, opts)
}
// SetPosition moves the label to the specified location
func (v *Label) SetPosition(x, y int) {
2019-10-24 13:31:59 +00:00
v.X = x
v.Y = y
}
func (v *Label) GetTextMetrics(text string) (width, height int) {
return v.font.GetTextMetrics(text)
}
2019-10-25 23:37:04 +00:00
func (v *Label) cacheImage() {
2019-10-24 13:31:59 +00:00
if v.imageData != nil {
return
}
width, height := v.font.GetTextMetrics(v.text)
2019-10-24 13:31:59 +00:00
v.Width = width
v.Height = height
v.imageData, _ = ebiten.NewImage(int(width), int(height), ebiten.FilterNearest)
v.font.Render(0, 0, v.text, v.Color, v.imageData)
2019-10-24 13:31:59 +00:00
}
2019-10-25 23:12:42 +00:00
// SetText sets the label's text
2019-10-25 23:37:04 +00:00
func (v *Label) SetText(newText string) {
2019-10-24 13:31:59 +00:00
if v.text == newText {
return
}
v.text = newText
v.imageData = nil
}
2019-10-26 14:34:55 +00:00
// GetSize returns the size of the label
func (v Label) GetSize() (width, height int) {
2019-10-26 14:34:55 +00:00
v.cacheImage()
width = v.Width
height = v.Height
return
}