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