2019-11-10 08:51:02 -05:00
|
|
|
package d2ui
|
2019-10-24 09:31:59 -04:00
|
|
|
|
|
|
|
import (
|
2020-07-07 20:16:22 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2gui"
|
2019-10-24 19:13:30 -04:00
|
|
|
"image/color"
|
2020-07-07 20:16:22 -04:00
|
|
|
"log"
|
|
|
|
"strings"
|
2019-10-24 19:13:30 -04:00
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2019-10-24 09:31:59 -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
|
2020-07-07 20:16:22 -04:00
|
|
|
Alignment d2gui.HorizontalAlign
|
|
|
|
font d2interface.Font
|
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
|
2020-07-07 20:16:22 -04:00
|
|
|
func CreateLabel(fontPath, palettePath string) Label {
|
|
|
|
font, _ := d2asset.LoadFont(fontPath+".tbl", fontPath+".dc6", palettePath)
|
2019-11-10 12:28:41 -05:00
|
|
|
result := Label{
|
2020-07-07 20:16:22 -04:00
|
|
|
Alignment: d2gui.HorizontalAlignLeft,
|
2019-10-25 22:20:36 -04:00
|
|
|
Color: color.White,
|
2020-07-07 20:16:22 -04:00
|
|
|
font: font,
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
2020-06-25 17:28:48 -04:00
|
|
|
|
2019-10-24 09:31:59 -04:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:16:22 -04:00
|
|
|
// Render draws the label on the screen, respliting the lines to allow for other alignments
|
2020-06-29 00:41:58 -04:00
|
|
|
func (v *Label) Render(target d2interface.Surface) {
|
2020-07-07 20:16:22 -04:00
|
|
|
v.font.SetColor(v.Color)
|
|
|
|
target.PushTranslation(v.X, v.Y)
|
2019-10-24 19:13:30 -04:00
|
|
|
|
2020-07-07 20:16:22 -04:00
|
|
|
lines := strings.Split(v.text, "\n")
|
|
|
|
yOffset := 0
|
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
lw, lh := v.GetTextMetrics(line)
|
|
|
|
target.PushTranslation(v.getAlignOffset(lw), yOffset)
|
|
|
|
|
|
|
|
_ = v.font.RenderText(line, target)
|
|
|
|
|
|
|
|
yOffset += lh
|
2019-12-28 16:46:08 -05:00
|
|
|
|
2020-07-07 20:16:22 -04:00
|
|
|
target.Pop()
|
|
|
|
}
|
2019-12-28 16:46:08 -05:00
|
|
|
|
2020-07-07 20:16:22 -04:00
|
|
|
target.Pop()
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
// SetPosition moves the label to the specified location
|
|
|
|
func (v *Label) SetPosition(x, y int) {
|
2019-10-24 09:31:59 -04:00
|
|
|
v.X = x
|
|
|
|
v.Y = y
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:16:22 -04:00
|
|
|
// GetSize returns the size of the label
|
|
|
|
func (v *Label) GetSize() (width, height int) {
|
|
|
|
return v.font.GetTextMetrics(v.text)
|
2019-11-10 12:28:41 -05:00
|
|
|
}
|
|
|
|
|
2020-07-07 20:16:22 -04:00
|
|
|
// GetTextMetrics returns the width and height of the enclosing rectangle in Pixels.
|
|
|
|
func (v *Label) GetTextMetrics(text string) (width, height int) {
|
|
|
|
return v.font.GetTextMetrics(text)
|
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
|
|
|
v.text = newText
|
|
|
|
}
|
2019-10-26 10:34:55 -04:00
|
|
|
|
2020-07-07 20:16:22 -04:00
|
|
|
func (v *Label) getAlignOffset(textWidth int) int {
|
|
|
|
switch v.Alignment {
|
|
|
|
case d2gui.HorizontalAlignLeft:
|
|
|
|
return 0
|
|
|
|
case d2gui.HorizontalAlignCenter:
|
|
|
|
return -textWidth / 2
|
|
|
|
case d2gui.HorizontalAlignRight:
|
|
|
|
return -textWidth
|
|
|
|
default:
|
|
|
|
log.Fatal("Invalid Alignment")
|
|
|
|
return 0
|
|
|
|
}
|
2019-10-26 10:34:55 -04:00
|
|
|
}
|