1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-16 04:25:23 +00:00
OpenDiablo2/d2core/d2ui/label.go

108 lines
2.5 KiB
Go
Raw Normal View History

2019-11-10 13:51:02 +00:00
package d2ui
2019-10-24 13:31:59 +00:00
import (
"image/color"
2020-06-30 13:58:53 +00:00
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
2019-10-24 13:31:59 +00:00
)
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
renderer d2interface.Renderer
2019-10-24 13:31:59 +00:00
X int
Y int
Width int
Height int
2019-10-25 23:37:04 +00:00
Alignment LabelAlignment
font *Font
imageData map[string]d2interface.Surface
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(renderer d2interface.Renderer, fontPath, palettePath string) Label {
result := Label{
renderer: renderer,
2019-10-25 23:37:04 +00:00
Alignment: LabelAlignLeft,
Color: color.White,
font: GetFont(fontPath, palettePath),
imageData: make(map[string]d2interface.Surface),
2019-10-24 13:31:59 +00:00
}
2020-06-25 21:28:48 +00:00
2019-10-24 13:31:59 +00:00
return result
}
// Render draws the label on the screen
func (v *Label) Render(target d2interface.Surface) {
2019-10-24 13:31:59 +00:00
if len(v.text) == 0 {
return
}
v.cacheImage()
x, y := v.X, v.Y
2019-10-25 23:37:04 +00:00
if v.Alignment == LabelAlignCenter {
x, y = v.X-v.Width/2, v.Y
2019-10-25 23:37:04 +00:00
} else if v.Alignment == LabelAlignRight {
x, y = v.X-v.Width, v.Y
}
target.PushFilter(d2interface.FilterNearest)
2020-06-30 13:58:53 +00:00
target.PushCompositeMode(d2enum.CompositeModeSourceAtop)
target.PushTranslation(x, y)
defer target.PopN(3)
_ = target.Render(v.imageData[v.text])
2019-10-24 13:31:59 +00:00
}
// SetPosition moves the label to the specified location
func (v *Label) SetPosition(x, y int) {
2019-10-24 13:31:59 +00:00
v.X = x
v.Y = y
}
func (v *Label) GetTextMetrics(text string) (width, height int) {
return v.font.GetTextMetrics(text)
}
2019-10-25 23:37:04 +00:00
func (v *Label) cacheImage() {
2020-06-25 21:28:48 +00:00
if v.imageData[v.text] != nil {
2019-10-24 13:31:59 +00:00
return
}
width, height := v.font.GetTextMetrics(v.text)
2019-10-24 13:31:59 +00:00
v.Width = width
v.Height = height
v.imageData[v.text], _ = v.renderer.NewSurface(width, height, d2interface.FilterNearest)
surface, _ := v.renderer.CreateSurface(v.imageData[v.text])
v.font.Render(0, 0, v.text, v.Color, surface)
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
}
2019-10-26 14:34:55 +00:00
2020-06-21 22:40:37 +00:00
// Size returns the size of the label
func (v Label) GetSize() (width, height int) {
2019-10-26 14:34:55 +00:00
v.cacheImage()
width = v.Width
height = v.Height
return
}