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