mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-11-19 02:46:35 -05:00
100 lines
2.3 KiB
Go
100 lines
2.3 KiB
Go
package ui
|
|
|
|
import (
|
|
"image/color"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/common"
|
|
"github.com/OpenDiablo2/OpenDiablo2/palettedefs"
|
|
"github.com/hajimehoshi/ebiten"
|
|
)
|
|
|
|
// LabelAlignment represents a label's alignment
|
|
type LabelAlignment uint8
|
|
|
|
const (
|
|
// 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
|
|
)
|
|
|
|
// Label represents a user interface label
|
|
type Label struct {
|
|
text string
|
|
X int
|
|
Y int
|
|
Width uint32
|
|
Height uint32
|
|
Alignment LabelAlignment
|
|
font *Font
|
|
imageData *ebiten.Image
|
|
Color color.Color
|
|
}
|
|
|
|
// CreateLabel creates a new instance of a UI label
|
|
func CreateLabel(provider common.FileProvider, font string, palette palettedefs.PaletteType) *Label {
|
|
result := &Label{
|
|
Alignment: LabelAlignLeft,
|
|
Color: color.White,
|
|
font: GetFont(font, palette, provider),
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// Draw draws the label on the screen
|
|
func (v *Label) Draw(target *ebiten.Image) {
|
|
if len(v.text) == 0 {
|
|
return
|
|
}
|
|
v.cacheImage()
|
|
opts := &ebiten.DrawImageOptions{}
|
|
|
|
if v.Alignment == LabelAlignCenter {
|
|
opts.GeoM.Translate(float64(v.X-int(v.Width/2)), float64(v.Y))
|
|
} 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))
|
|
}
|
|
opts.CompositeMode = ebiten.CompositeModeSourceAtop
|
|
opts.Filter = ebiten.FilterNearest
|
|
target.DrawImage(v.imageData, opts)
|
|
}
|
|
|
|
// MoveTo moves the label to the specified location
|
|
func (v *Label) MoveTo(x, y int) {
|
|
v.X = x
|
|
v.Y = y
|
|
}
|
|
|
|
func (v *Label) cacheImage() {
|
|
if v.imageData != nil {
|
|
return
|
|
}
|
|
width, height := v.font.GetTextMetrics(v.text)
|
|
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)
|
|
}
|
|
|
|
// SetText sets the label's text
|
|
func (v *Label) SetText(newText string) {
|
|
if v.text == newText {
|
|
return
|
|
}
|
|
v.text = newText
|
|
v.imageData = nil
|
|
}
|
|
|
|
// GetSize returns the size of the label
|
|
func (v *Label) GetSize() (width, height uint32) {
|
|
v.cacheImage()
|
|
width = v.Width
|
|
height = v.Height
|
|
return
|
|
}
|