2020-02-17 22:11:52 -05:00
|
|
|
package d2gui
|
|
|
|
|
|
|
|
import (
|
2020-07-06 21:26:08 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-02-17 22:11:52 -05:00
|
|
|
)
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// Label is renderable text
|
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
|
2020-07-05 15:56:48 -04:00
|
|
|
font d2interface.Font
|
2020-07-03 14:00:56 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
func (l *Label) getSize() (width, height int) {
|
2020-02-17 22:11:52 -05:00
|
|
|
return l.surface.GetSize()
|
|
|
|
}
|
2020-06-25 16:27:16 -04:00
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// GetText returns the label text
|
2020-06-25 16:27:16 -04:00
|
|
|
func (l *Label) GetText() string {
|
|
|
|
return l.text
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// SetText sets the label text
|
2020-06-25 16:27:16 -04:00
|
|
|
func (l *Label) SetText(text string) error {
|
|
|
|
if text == l.text {
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-06-25 16:27:16 -04:00
|
|
|
return l.setText(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Label) setText(text string) error {
|
|
|
|
width, height := l.font.GetTextMetrics(text)
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-07-06 21:26:08 -04:00
|
|
|
surface, err := l.renderer.NewSurface(width, height, d2enum.FilterNearest)
|
2020-06-25 16:27:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-06-25 16:27:16 -04:00
|
|
|
if err := l.font.RenderText(text, surface); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-06-25 16:27:16 -04:00
|
|
|
l.surface = surface
|
|
|
|
l.text = text
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-06-25 16:27:16 -04:00
|
|
|
return nil
|
|
|
|
}
|