2019-10-25 19:37:04 -04:00
|
|
|
package UI
|
2019-10-24 09:31:59 -04:00
|
|
|
|
|
|
|
import (
|
2019-10-24 19:13:30 -04:00
|
|
|
"image/color"
|
|
|
|
|
2019-11-02 17:38:39 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/Common"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/PaletteDefs"
|
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
|
2019-10-24 19:13:30 -04:00
|
|
|
|
|
|
|
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-24 19:13:30 -04:00
|
|
|
)
|
|
|
|
|
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
|
2019-10-25 22:20:36 -04:00
|
|
|
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
|
2019-11-01 14:12:23 -04:00
|
|
|
func CreateLabel(provider Common.FileProvider, font string, palette PaletteDefs.PaletteType) *Label {
|
2019-10-25 19:37:04 -04:00
|
|
|
result := &Label{
|
|
|
|
Alignment: LabelAlignLeft,
|
2019-10-25 22:20:36 -04:00
|
|
|
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-24 19:13:30 -04:00
|
|
|
|
2019-10-25 19:37:04 -04:00
|
|
|
if v.Alignment == LabelAlignCenter {
|
2019-10-24 19:13:30 -04:00
|
|
|
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 {
|
2019-10-24 19:13:30 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2019-10-25 22:20:36 -04:00
|
|
|
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)
|
2019-10-25 22:20:36 -04:00
|
|
|
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) {
|
|
|
|
v.cacheImage()
|
|
|
|
width = v.Width
|
|
|
|
height = v.Height
|
|
|
|
return
|
|
|
|
}
|