-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.go
142 lines (125 loc) · 3.23 KB
/
text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package builtin
import (
"encoding/xml"
"errors"
"github.com/bhollier/ui/pkg/ui/element"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"net/http"
)
// Element type for a text
type Text struct {
// A text element is an
// element
element.Impl
// It also has text
element.TextImpl
// The window the element
// is in
window *pixelgl.Window
// The parent bounds
parentBounds *pixel.Rect
}
// Function to create a new text
func NewText(fs http.FileSystem, name xml.Name, parent element.Layout) element.Element {
return &Text{Impl: element.NewElement(fs, name, parent)}
}
// The XML name of the element
var TextTypeName = xml.Name{Space: "http://github.com/bhollier/ui/api/schema", Local: "Text"}
// Function to set the text content
func (e *Text) SetText(s string) error {
// Set the text
err := e.TextImpl.SetText(s)
if err != nil {
return err
}
// If the width depends on the content
if e.GetRelWidth().MatchContent ||
e.GetRelHeight().MatchContent {
// Reset the element
e.Reset()
// Update the width and/or height
if e.GetRelWidth().MatchContent {
newWidth := e.GetSprite().Bounds().Size().X
e.SetActualWidth(&newWidth)
}
if e.GetRelHeight().MatchContent {
newHeight := e.GetSprite().Bounds().Size().Y
e.SetActualHeight(&newHeight)
}
// Re-initialise the element
err = e.Init(e.window, e.parentBounds)
if err != nil {
return err
}
}
return nil
}
// Function to unmarshal an XML element into
// an element. This function is usually only
// called by xml.Unmarshal
func (e *Text) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
// Unmarshal the element
err = e.Impl.UnmarshalXML(d, start)
if err != nil {
return err
}
// Set the element's attributes
err = element.SetAttrs(e, start.Attr)
if err != nil {
return err
}
return d.Skip()
}
// Function to reset the element
func (e *Text) Reset() {
e.Impl.Reset()
e.TextImpl.Reset()
}
// Function to determine whether
// the element is initialised
func (e *Text) IsInitialised() bool {
// If the element is initialised
return e.Impl.IsInitialised() &&
// And the image has been initialised
e.TextImpl.IsInitialised()
}
// Function to initialise the element
// (load textures, create sprites, set
// sprite locations, etc.)
func (e *Text) Init(window *pixelgl.Window, bounds *pixel.Rect) error {
// Save the window and parent bounds
e.window = window
e.parentBounds = bounds
// Initialise the element
err := e.Impl.Init(window, bounds)
if err != nil {
return err
}
// Initialise the text
err = element.InitText(e, &e.TextImpl)
if err != nil {
return err
}
// If no text was given
if e.TextImpl.GetField() == "" {
// If it wants to match the content
if e.GetRelWidth().MatchContent {
return errors.New("invalid width attribute value 'match_content' on XML element '" +
element.FullName(e, ".", false) +
"': no content to match")
} else if e.GetRelHeight().MatchContent {
return errors.New("invalid height attribute value 'match_content' on XML element '" +
element.FullName(e, ".", false) +
"': no content to match")
}
}
return nil
}
// Function to draw the element
func (e *Text) Draw() {
// Draw the element
e.Impl.Draw()
// Draw the text
element.DrawText(e, &e.TextImpl)
}