1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-10-31 16:27:18 -04:00
OpenDiablo2/d2render/d2ui/label.go

105 lines
2.4 KiB
Go
Raw Normal View History

2019-11-10 08:51:02 -05:00
package d2ui
2019-10-24 09:31:59 -04:00
import (
"image/color"
"github.com/OpenDiablo2/D2Shared/d2common/d2interface"
2019-11-10 03:36:53 -05:00
"github.com/OpenDiablo2/D2Shared/d2common/d2enum"
2019-11-10 03:36:53 -05:00
2019-10-24 09:31:59 -04:00
"github.com/hajimehoshi/ebiten"
)
2019-10-25 19:37:04 -04:00
// LabelAlignment represents a label's alignment
type LabelAlignment uint8
const (
2019-10-25 19:37:04 -04: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 19:37:04 -04:00
// Label represents a user interface label
type Label struct {
2019-10-24 09:31:59 -04:00
text string
X int
Y int
Width uint32
Height uint32
2019-10-25 19:37:04 -04:00
Alignment LabelAlignment
font *Font
2019-10-24 09:31:59 -04:00
imageData *ebiten.Image
Color color.Color
2019-10-24 09:31:59 -04:00
}
2019-10-25 19:37:04 -04:00
// CreateLabel creates a new instance of a UI label
func CreateLabel(provider d2interface.FileProvider, font string, palette d2enum.PaletteType) Label {
result := Label{
2019-10-25 19:37:04 -04:00
Alignment: LabelAlignLeft,
Color: color.White,
2019-10-25 19:37:04 -04:00
font: GetFont(font, palette, provider),
2019-10-24 09:31:59 -04:00
}
return result
}
// Draw draws the label on the screen
2019-10-25 19:37:04 -04:00
func (v *Label) Draw(target *ebiten.Image) {
2019-10-24 09:31:59 -04:00
if len(v.text) == 0 {
return
}
v.cacheImage()
opts := &ebiten.DrawImageOptions{}
2019-10-25 19:37:04 -04:00
if v.Alignment == LabelAlignCenter {
opts.GeoM.Translate(float64(v.X-int(v.Width/2)), float64(v.Y))
2019-10-25 19:37:04 -04: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 09:31:59 -04:00
opts.CompositeMode = ebiten.CompositeModeSourceAtop
opts.Filter = ebiten.FilterNearest
target.DrawImage(v.imageData, opts)
}
2019-10-25 19:12:42 -04:00
// MoveTo moves the label to the specified location
2019-10-25 19:37:04 -04:00
func (v *Label) MoveTo(x, y int) {
2019-10-24 09:31:59 -04:00
v.X = x
v.Y = y
}
func (v *Label) GetTextMetrics(text string) (width, height uint32) {
return v.font.GetTextMetrics(text)
}
2019-10-25 19:37:04 -04:00
func (v *Label) cacheImage() {
2019-10-24 09:31:59 -04:00
if v.imageData != nil {
return
}
width, height := v.font.GetTextMetrics(v.text)
2019-10-24 09:31:59 -04: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 09:31:59 -04:00
}
2019-10-25 19:12:42 -04:00
// SetText sets the label's text
2019-10-25 19:37:04 -04:00
func (v *Label) SetText(newText string) {
2019-10-24 09:31:59 -04:00
if v.text == newText {
return
}
v.text = newText
v.imageData = nil
}
2019-10-26 10:34:55 -04:00
// GetSize returns the size of the label
func (v Label) GetSize() (width, height uint32) {
2019-10-26 10:34:55 -04:00
v.cacheImage()
width = v.Width
height = v.Height
return
}