...
1 package etk
2
3 import (
4 "image"
5 "image/color"
6
7 "code.rocket9labs.com/tslocum/etk/messeji"
8 "github.com/hajimehoshi/ebiten/v2"
9 "golang.org/x/image/font/sfnt"
10 )
11
12
13 type Text struct {
14 *Box
15 field *messeji.TextField
16 children []Widget
17 }
18
19
20 func NewText(text string) *Text {
21 f := newText()
22 f.SetText(text)
23 f.SetForegroundColor(Style.TextColorLight)
24 f.SetHandleKeyboard(true)
25
26 return &Text{
27 Box: NewBox(),
28 field: f,
29 }
30 }
31
32
33 func (t *Text) SetRect(r image.Rectangle) {
34 t.Lock()
35 defer t.Unlock()
36
37 t.rect = r
38 t.field.SetRect(r)
39 }
40
41
42 func (t *Text) Foreground() color.RGBA {
43 t.Lock()
44 defer t.Unlock()
45
46 return t.field.ForegroundColor()
47 }
48
49
50 func (t *Text) SetForeground(c color.RGBA) {
51 t.Lock()
52 defer t.Unlock()
53
54 t.field.SetForegroundColor(c)
55 }
56
57
58 func (t *Text) Focus() bool {
59 return false
60 }
61
62
63 func (t *Text) SetFocus(focus bool) bool {
64 return false
65 }
66
67
68 func (t *Text) SetScrollBarWidth(width int) {
69 t.Lock()
70 defer t.Unlock()
71
72 t.field.SetScrollBarWidth(width)
73 }
74
75
76 func (t *Text) SetScrollBarColors(area color.RGBA, handle color.RGBA) {
77 t.Lock()
78 defer t.Unlock()
79
80 t.field.SetScrollBarColors(Style.ScrollAreaColor, Style.ScrollHandleColor)
81 }
82
83
84
85 func (t *Text) SetScrollBorderColors(top color.RGBA, right color.RGBA, bottom color.RGBA, left color.RGBA) {
86 t.Lock()
87 defer t.Unlock()
88
89 t.field.SetScrollBorderColors(top, right, bottom, left)
90 }
91
92
93 func (t *Text) SetWordWrap(wrap bool) {
94 t.Lock()
95 defer t.Unlock()
96
97 t.field.SetWordWrap(wrap)
98 }
99
100
101 func (t *Text) SetHorizontal(h Alignment) {
102 t.Lock()
103 defer t.Unlock()
104
105 t.field.SetHorizontal(messeji.Alignment(h))
106 }
107
108
109 func (t *Text) SetVertical(h Alignment) {
110 t.Lock()
111 defer t.Unlock()
112
113 t.field.SetVertical(messeji.Alignment(h))
114 }
115
116
117 func (t *Text) Write(p []byte) (n int, err error) {
118 t.Lock()
119 defer t.Unlock()
120
121 return t.field.Write(p)
122 }
123
124
125 func (t *Text) Text() string {
126 t.Lock()
127 defer t.Unlock()
128
129 return t.field.Text()
130 }
131
132
133 func (t *Text) SetText(text string) {
134 t.Lock()
135 defer t.Unlock()
136
137 t.field.SetText(text)
138 }
139
140
141 func (t *Text) SetScrollBarVisible(scrollVisible bool) {
142 t.Lock()
143 defer t.Unlock()
144
145 t.field.SetScrollBarVisible(scrollVisible)
146 }
147
148
149
150 func (t *Text) SetAutoHideScrollBar(autoHide bool) {
151 t.Lock()
152 defer t.Unlock()
153
154 t.field.SetAutoHideScrollBar(autoHide)
155 }
156
157
158 func (t *Text) SetFont(fnt *sfnt.Font, size int) {
159 t.Lock()
160 defer t.Unlock()
161
162 t.field.SetFont(FontFace(fnt, size), fontMutex)
163 }
164
165
166 func (t *Text) Padding() int {
167 t.Lock()
168 defer t.Unlock()
169
170 return t.field.Padding()
171 }
172
173
174 func (t *Text) SetPadding(padding int) {
175 t.Lock()
176 defer t.Unlock()
177
178 t.field.SetPadding(padding)
179 }
180
181
182
183 func (t *Text) SetFollow(follow bool) {
184 t.Lock()
185 defer t.Unlock()
186
187 t.field.SetFollow(follow)
188 }
189
190
191
192 func (t *Text) SetSingleLine(single bool) {
193 t.Lock()
194 defer t.Unlock()
195
196 t.field.SetSingleLine(single)
197 }
198
199
200 func (t *Text) SetMask(r rune) {
201 t.Lock()
202 defer t.Unlock()
203
204 t.field.SetMask(r)
205 }
206
207
208 func (t *Text) HandleKeyboard(key ebiten.Key, r rune) (handled bool, err error) {
209 return t.field.HandleKeyboardEvent(key, r)
210 }
211
212
213 func (t *Text) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
214 return t.field.HandleMouseEvent(cursor, pressed, clicked)
215 }
216
217
218 func (t *Text) Draw(screen *ebiten.Image) error {
219 t.field.Draw(screen)
220 return nil
221 }
222
223
224 func (t *Text) Children() []Widget {
225 t.Lock()
226 defer t.Unlock()
227
228 return t.children
229 }
230
231
232 func (t *Text) AddChild(w ...Widget) {
233 t.Lock()
234 defer t.Unlock()
235
236 t.children = append(t.children, w...)
237 }
238
239 var _ Widget = &Text{}
240
View as plain text