2020-02-17 22:11:52 -05:00
|
|
|
package d2gui
|
|
|
|
|
|
|
|
import (
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-06-25 16:27:16 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-02-17 22:11:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Label struct {
|
|
|
|
widgetBase
|
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
renderer d2interface.Renderer
|
|
|
|
text string
|
|
|
|
font *d2asset.Font
|
|
|
|
surface d2interface.Surface
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
func createLabel(renderer d2interface.Renderer, text string, fontStyle FontStyle) (*Label, error) {
|
2020-02-24 22:35:21 -05:00
|
|
|
font, err := loadFont(fontStyle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
label := &Label{
|
|
|
|
font: font,
|
|
|
|
renderer: renderer,
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = label.setText(text)
|
2020-02-24 22:35:21 -05:00
|
|
|
label.SetVisible(true)
|
2020-02-17 22:11:52 -05:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
return label, nil
|
|
|
|
}
|
2020-02-17 22:11:52 -05:00
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (l *Label) render(target d2interface.Surface) error {
|
2020-02-24 22:35:21 -05:00
|
|
|
return target.Render(l.surface)
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Label) getSize() (int, int) {
|
|
|
|
return l.surface.GetSize()
|
|
|
|
}
|
2020-06-25 16:27:16 -04:00
|
|
|
|
|
|
|
func (l *Label) GetText() string {
|
|
|
|
return l.text
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Label) SetText(text string) error {
|
|
|
|
if text == l.text {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return l.setText(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Label) setText(text string) error {
|
|
|
|
width, height := l.font.GetTextMetrics(text)
|
2020-07-03 14:00:56 -04:00
|
|
|
surface, err := l.renderer.NewSurface(width, height, d2interface.FilterNearest)
|
2020-06-25 16:27:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := l.font.RenderText(text, surface); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
l.surface = surface
|
|
|
|
l.text = text
|
|
|
|
return nil
|
|
|
|
}
|