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