...
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 "github.com/hajimehoshi/ebiten/v2/text/v2"
10 )
11
12
13 type Text struct {
14 *Box
15 field *messeji.TextField
16 textFont *text.GoTextFaceSource
17 textSize int
18 scrollVisible bool
19 children []Widget
20 }
21
22
23 func NewText(text string) *Text {
24 f := newText()
25 f.SetText(text)
26 f.SetForegroundColor(Style.TextColorLight)
27 f.SetHandleKeyboard(true)
28
29 t := &Text{
30 Box: NewBox(),
31 field: f,
32 textFont: Style.TextFont,
33 textSize: Scale(Style.TextSize),
34 scrollVisible: true,
35 }
36 return t
37 }
38
39
40 func (t *Text) SetRect(r image.Rectangle) {
41 t.Lock()
42 defer t.Unlock()
43
44 t.rect = r
45 t.field.SetRect(r)
46 }
47
48
49 func (t *Text) Foreground() color.RGBA {
50 t.Lock()
51 defer t.Unlock()
52
53 return t.field.ForegroundColor()
54 }
55
56
57 func (t *Text) SetForeground(c color.RGBA) {
58 t.Lock()
59 defer t.Unlock()
60
61 t.field.SetForegroundColor(c)
62 }
63
64
65 func (t *Text) Focus() bool {
66 return false
67 }
68
69
70 func (t *Text) SetFocus(focus bool) bool {
71 return false
72 }
73
74
75 func (t *Text) SetScrollBarWidth(width int) {
76 t.Lock()
77 defer t.Unlock()
78
79 t.field.SetScrollBarWidth(width)
80 }
81
82
83 func (t *Text) SetScrollBarColors(area color.RGBA, handle color.RGBA) {
84 t.Lock()
85 defer t.Unlock()
86
87 t.field.SetScrollBarColors(Style.ScrollAreaColor, Style.ScrollHandleColor)
88 }
89
90
91
92 func (t *Text) SetScrollBorderColors(top color.RGBA, right color.RGBA, bottom color.RGBA, left color.RGBA) {
93 t.Lock()
94 defer t.Unlock()
95
96 t.field.SetScrollBorderColors(top, right, bottom, left)
97 }
98
99
100 func (t *Text) SetWordWrap(wrap bool) {
101 t.Lock()
102 defer t.Unlock()
103
104 t.field.SetWordWrap(wrap)
105 }
106
107
108 func (t *Text) SetHorizontal(h Alignment) {
109 t.Lock()
110 defer t.Unlock()
111
112 t.field.SetHorizontal(messeji.Alignment(h))
113 }
114
115
116 func (t *Text) SetVertical(v Alignment) {
117 t.Lock()
118 defer t.Unlock()
119
120 t.field.SetVertical(messeji.Alignment(v))
121 }
122
123
124
125 func (t *Text) Cursor() ebiten.CursorShapeType {
126 return ebiten.CursorShapeDefault
127 }
128
129
130 func (t *Text) Write(p []byte) (n int, err error) {
131 t.Lock()
132 defer t.Unlock()
133
134 n, err = t.field.Write(p)
135 if err != nil {
136 return n, err
137 }
138 return n, err
139 }
140
141
142 func (t *Text) Text() string {
143 t.Lock()
144 defer t.Unlock()
145
146 return t.field.Text()
147 }
148
149
150 func (t *Text) SetText(text string) {
151 t.Lock()
152 defer t.Unlock()
153
154 t.field.SetText(text)
155 }
156
157
158 func (t *Text) SetLast(text string) {
159 t.Lock()
160 defer t.Unlock()
161
162 t.field.SetLast(text)
163 }
164
165
166
167 func (t *Text) SetAutoResize(resize bool) {
168 t.Lock()
169 defer t.Unlock()
170
171 t.field.SetAutoResize(resize)
172 }
173
174 func (t *Text) scrollBarVisible() bool {
175 return t.scrollVisible
176 }
177
178
179 func (t *Text) SetScrollBarVisible(scrollVisible bool) {
180 t.Lock()
181 defer t.Unlock()
182
183 t.scrollVisible = scrollVisible
184 t.field.SetScrollBarVisible(t.scrollBarVisible())
185 }
186
187
188
189 func (t *Text) SetAutoHideScrollBar(autoHide bool) {
190 t.Lock()
191 defer t.Unlock()
192
193 t.field.SetAutoHideScrollBar(autoHide)
194 }
195
196
197 func (t *Text) FontSize() int {
198 t.Lock()
199 defer t.Unlock()
200
201 return t.textSize
202 }
203
204
205 func (t *Text) SetFont(fnt *text.GoTextFaceSource, size int) {
206 t.Lock()
207 defer t.Unlock()
208
209 t.textFont, t.textSize = fnt, size
210 t.field.SetFont(t.textFont, t.textSize, fontMutex)
211 }
212
213
214 func (t *Text) Padding() int {
215 t.Lock()
216 defer t.Unlock()
217
218 return t.field.Padding()
219 }
220
221
222 func (t *Text) SetPadding(padding int) {
223 t.Lock()
224 defer t.Unlock()
225
226 t.field.SetPadding(padding)
227 }
228
229
230
231 func (t *Text) SetFollow(follow bool) {
232 t.Lock()
233 defer t.Unlock()
234
235 t.field.SetFollow(follow)
236 }
237
238
239
240 func (t *Text) SetSingleLine(single bool) {
241 t.Lock()
242 defer t.Unlock()
243
244 t.field.SetSingleLine(single)
245 }
246
247
248 func (t *Text) SetMask(r rune) {
249 t.Lock()
250 defer t.Unlock()
251
252 t.field.SetMask(r)
253 }
254
255
256 func (t *Text) HandleKeyboard(key ebiten.Key, r rune) (handled bool, err error) {
257 return t.field.HandleKeyboardEvent(key, r)
258 }
259
260
261 func (t *Text) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
262 return t.field.HandleMouseEvent(cursor, pressed, clicked)
263 }
264
265
266 func (t *Text) Draw(screen *ebiten.Image) error {
267 t.field.Draw(screen)
268 return nil
269 }
270
271
272 func (t *Text) Children() []Widget {
273 t.Lock()
274 defer t.Unlock()
275
276 return t.children
277 }
278
279
280 func (t *Text) AddChild(w ...Widget) {
281 t.Lock()
282 defer t.Unlock()
283
284 t.children = append(t.children, w...)
285 }
286
287 var _ Widget = &Text{}
288
View as plain text