OpenDiablo2/UI/Label.go

100 lines
2.3 KiB
Go
Raw Normal View History

2019-10-25 23:37:04 +00:00
package UI
2019-10-24 13:31:59 +00:00
import (
"image/color"
"github.com/OpenDiablo2/OpenDiablo2/Common"
"github.com/OpenDiablo2/OpenDiablo2/PaletteDefs"
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 uint32
Height uint32
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(provider Common.FileProvider, font string, palette PaletteDefs.PaletteType) *Label {
2019-10-25 23:37:04 +00:00
result := &Label{
Alignment: LabelAlignLeft,
Color: color.White,
2019-10-25 23:37:04 +00:00
font: GetFont(font, palette, provider),
2019-10-24 13:31:59 +00:00
}
return result
}
// Draw draws the label on the screen
2019-10-25 23:37:04 +00:00
func (v *Label) Draw(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)
}
2019-10-25 23:12:42 +00:00
// MoveTo moves the label to the specified location
2019-10-25 23:37:04 +00:00
func (v *Label) MoveTo(x, y int) {
2019-10-24 13:31:59 +00:00
v.X = x
v.Y = y
}
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.Draw(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 uint32) {
v.cacheImage()
width = v.Width
height = v.Height
return
}