1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-10-31 16:27:18 -04:00
OpenDiablo2/UI/Label.go
Tim Sarbin 1afd86005e
Changes necessary for migration to org account (#67)
* Replaced old repo path with org path.

* Added editorconfig. Fixed line endings to LF.

* Fixed travis build badge path
2019-11-02 17:38:39 -04:00

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
}