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