mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-11-18 02:16:23 -05:00
60e6fcb7ac
* moved filter constants back to d2enum * moving key and mouse button enums into d2enum * moving render type enum into d2enum * moving input even priority enums into d2enum * moving terminal enums into d2enum
66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package d2gui
|
|
|
|
import (
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
)
|
|
|
|
type Label struct {
|
|
widgetBase
|
|
|
|
renderer d2interface.Renderer
|
|
text string
|
|
font d2interface.Font
|
|
surface d2interface.Surface
|
|
}
|
|
|
|
func createLabel(renderer d2interface.Renderer, text string, fontStyle FontStyle) (*Label, error) {
|
|
font, err := loadFont(fontStyle)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
label := &Label{
|
|
font: font,
|
|
renderer: renderer,
|
|
}
|
|
|
|
_ = label.setText(text)
|
|
label.SetVisible(true)
|
|
|
|
return label, nil
|
|
}
|
|
|
|
func (l *Label) render(target d2interface.Surface) error {
|
|
return target.Render(l.surface)
|
|
}
|
|
|
|
func (l *Label) getSize() (int, int) {
|
|
return l.surface.GetSize()
|
|
}
|
|
|
|
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)
|
|
surface, err := l.renderer.NewSurface(width, height, d2enum.FilterNearest)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := l.font.RenderText(text, surface); err != nil {
|
|
return err
|
|
}
|
|
l.surface = surface
|
|
l.text = text
|
|
return nil
|
|
}
|