Skip to content

Commit bb5399f

Browse files
committed
debugui: change TextField to return true when the content is determined
This change also added (*Context).SetTextFieldValue
1 parent aab0ee2 commit bb5399f

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

control.go

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ func (c *Context) mouseOver(rect image.Rectangle) bool {
6161
return p.In(rect) && p.In(c.clipRect()) && c.inHoverRoot()
6262
}
6363

64-
func (c *Context) updateControl(id controlID, rect image.Rectangle, opt option) {
64+
func (c *Context) updateControl(id controlID, rect image.Rectangle, opt option) (wasFocused bool) {
6565
if id == 0 {
66-
return
66+
return false
6767
}
6868

6969
mouseover := c.mouseOver(rect)
@@ -72,7 +72,7 @@ func (c *Context) updateControl(id controlID, rect image.Rectangle, opt option)
7272
c.keepFocus = true
7373
}
7474
if (opt & optionNoInteract) != 0 {
75-
return
75+
return false
7676
}
7777
if mouseover && !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
7878
c.hover = id
@@ -81,9 +81,11 @@ func (c *Context) updateControl(id controlID, rect image.Rectangle, opt option)
8181
if c.focus == id {
8282
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) && !mouseover {
8383
c.setFocus(0)
84+
wasFocused = true
8485
}
8586
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) && (^opt&optionHoldFocus) != 0 {
8687
c.setFocus(0)
88+
wasFocused = true
8789
}
8890
}
8991

@@ -94,17 +96,21 @@ func (c *Context) updateControl(id controlID, rect image.Rectangle, opt option)
9496
c.hover = 0
9597
}
9698
}
99+
100+
return
97101
}
98102

99103
func (c *Context) Control(idStr string, f func(bounds image.Rectangle) bool) bool {
100104
id := c.idFromString(idStr)
101-
return c.control(id, 0, f)
105+
return c.control(id, 0, func(bounds image.Rectangle, wasFocused bool) bool {
106+
return f(bounds)
107+
})
102108
}
103109

104-
func (c *Context) control(id controlID, opt option, f func(bounds image.Rectangle) bool) bool {
110+
func (c *Context) control(id controlID, opt option, f func(bounds image.Rectangle, wasFocused bool) bool) bool {
105111
r := c.layoutNext()
106-
c.updateControl(id, r, opt)
107-
return f(r)
112+
wasFocused := c.updateControl(id, r, opt)
113+
return f(r, wasFocused)
108114
}
109115

110116
func (c *Context) Text(text string) {
@@ -113,7 +119,7 @@ func (c *Context) Text(text string) {
113119
var endIdx, p int
114120
c.SetGridLayout([]int{-1}, []int{lineHeight()})
115121
for endIdx < len(text) {
116-
c.control(0, 0, func(bounds image.Rectangle) bool {
122+
c.control(0, 0, func(bounds image.Rectangle, wasFocused bool) bool {
117123
w := 0
118124
endIdx = p
119125
startIdx := endIdx
@@ -141,7 +147,7 @@ func (c *Context) Text(text string) {
141147
}
142148

143149
func (c *Context) Label(text string) {
144-
c.control(0, 0, func(bounds image.Rectangle) bool {
150+
c.control(0, 0, func(bounds image.Rectangle, wasFocused bool) bool {
145151
c.drawControlText(text, bounds, ColorText, 0)
146152
return false
147153
})
@@ -150,7 +156,7 @@ func (c *Context) Label(text string) {
150156
func (c *Context) button(label string, opt option) (controlID, bool) {
151157
label, idStr, _ := strings.Cut(label, idSeparator)
152158
id := c.idFromString(idStr)
153-
return id, c.control(id, opt, func(bounds image.Rectangle) bool {
159+
return id, c.control(id, opt, func(bounds image.Rectangle, wasFocused bool) bool {
154160
var res bool
155161
// handle click
156162
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) && c.focus == id {
@@ -166,9 +172,9 @@ func (c *Context) button(label string, opt option) (controlID, bool) {
166172
}
167173

168174
func (c *Context) Checkbox(label string, state *bool) bool {
169-
id := c.idFromString(fmt.Sprintf("%p", state))
175+
id := c.idFromGlobalUniqueString(fmt.Sprintf("%p", state))
170176

171-
return c.control(id, 0, func(bounds image.Rectangle) bool {
177+
return c.control(id, 0, func(bounds image.Rectangle, wasFocused bool) bool {
172178
var res bool
173179
box := image.Rect(bounds.Min.X, bounds.Min.Y, bounds.Min.X+bounds.Dy(), bounds.Max.Y)
174180
c.updateControl(id, bounds, 0)
@@ -203,7 +209,7 @@ func (c *Context) textInputTextField(id controlID) *textinput.Field {
203209
}
204210

205211
func (c *Context) textFieldRaw(buf *string, id controlID, opt option) bool {
206-
return c.control(id, opt|optionHoldFocus, func(bounds image.Rectangle) bool {
212+
return c.control(id, opt|optionHoldFocus, func(bounds image.Rectangle, wasFocused bool) bool {
207213
var res bool
208214

209215
if c.focus == id {
@@ -222,24 +228,23 @@ func (c *Context) textFieldRaw(buf *string, id controlID, opt option) bool {
222228
}
223229

224230
if !handled {
225-
// handle backspace
226231
if inpututil.IsKeyJustPressed(ebiten.KeyBackspace) && len(*buf) > 0 {
227232
_, size := utf8.DecodeLastRuneInString(*buf)
228233
*buf = (*buf)[:len(*buf)-size]
229234
f.SetTextAndSelection(*buf, len(*buf), len(*buf))
230235
}
231-
232-
// handle return
233236
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
234237
res = true
235-
f.SetTextAndSelection("", 0, 0)
236238
}
237239
}
238240
} else {
239241
f := c.textInputTextField(id)
240242
if *buf != f.TextForRendering() {
241243
f.SetTextAndSelection(*buf, len(*buf), len(*buf))
242244
}
245+
if wasFocused {
246+
res = true
247+
}
243248
}
244249

245250
// draw
@@ -262,6 +267,12 @@ func (c *Context) textFieldRaw(buf *string, id controlID, opt option) bool {
262267
})
263268
}
264269

270+
func (c *Context) SetTextFieldValue(value *string) {
271+
id := c.idFromGlobalUniqueString(fmt.Sprintf("%p", value))
272+
f := c.textInputTextField(id)
273+
f.SetTextAndSelection(*value, 0, 0)
274+
}
275+
265276
func (c *Context) numberTextField(value *float64, id controlID) bool {
266277
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) && ebiten.IsKeyPressed(ebiten.KeyShift) &&
267278
c.hover == id {
@@ -284,7 +295,7 @@ func (c *Context) numberTextField(value *float64, id controlID) bool {
284295
}
285296

286297
func (c *Context) textField(buf *string, opt option) bool {
287-
id := c.idFromString(fmt.Sprintf("%p", buf))
298+
id := c.idFromGlobalUniqueString(fmt.Sprintf("%p", buf))
288299
return c.textFieldRaw(buf, id, opt)
289300
}
290301

@@ -295,7 +306,7 @@ func formatNumber(v float64, digits int) string {
295306
func (c *Context) slider(value *float64, low, high, step float64, digits int, opt option) bool {
296307
last := *value
297308
v := last
298-
id := c.idFromString(fmt.Sprintf("%p", value))
309+
id := c.idFromGlobalUniqueString(fmt.Sprintf("%p", value))
299310

300311
// handle text input mode
301312
if c.numberTextField(&v, id) {
@@ -304,7 +315,7 @@ func (c *Context) slider(value *float64, low, high, step float64, digits int, op
304315
}
305316

306317
// handle normal mode
307-
return c.control(id, opt, func(bounds image.Rectangle) bool {
318+
return c.control(id, opt, func(bounds image.Rectangle, wasFocused bool) bool {
308319
var res bool
309320
// handle input
310321
if c.focus == id && ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
@@ -337,7 +348,7 @@ func (c *Context) slider(value *float64, low, high, step float64, digits int, op
337348
}
338349

339350
func (c *Context) number(value *float64, step float64, digits int, opt option) bool {
340-
id := c.idFromString(fmt.Sprintf("%p", value))
351+
id := c.idFromGlobalUniqueString(fmt.Sprintf("%p", value))
341352
last := *value
342353

343354
// handle text input mode
@@ -346,7 +357,7 @@ func (c *Context) number(value *float64, step float64, digits int, opt option) b
346357
}
347358

348359
// handle normal mode
349-
return c.control(id, opt, func(bounds image.Rectangle) bool {
360+
return c.control(id, opt, func(bounds image.Rectangle, wasFocused bool) bool {
350361
var res bool
351362
// handle input
352363
if c.focus == id && ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
@@ -380,7 +391,7 @@ func (c *Context) header(label string, istreenode bool, opt option, f func()) {
380391
expanded = toggled
381392
}
382393

383-
if c.control(id, 0, func(bounds image.Rectangle) bool {
394+
if c.control(id, 0, func(bounds image.Rectangle, wasFocused bool) bool {
384395
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) && c.focus == id {
385396
if toggled {
386397
delete(c.toggledIDs, id)

example/ui.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,17 @@ func (g *Game) logWindow(ctx *debugui.Context) {
164164
var submitted bool
165165
ctx.SetGridLayout([]int{-1, 70}, nil)
166166
if ctx.TextField(&g.logSubmitBuf) {
167-
submitted = true
167+
if ebiten.IsKeyPressed(ebiten.KeyEnter) {
168+
submitted = true
169+
}
168170
}
169171
if ctx.Button("Submit") {
170172
submitted = true
171173
}
172174
if submitted {
173175
g.writeLog(g.logSubmitBuf)
174176
g.logSubmitBuf = ""
177+
ctx.SetTextFieldValue(&g.logSubmitBuf)
175178
}
176179
})
177180
})

layout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (c *Context) popLayout() {
6868
}
6969

7070
func (c *Context) GridCell(f func()) {
71-
c.control(0, 0, func(bounds image.Rectangle) bool {
71+
c.control(0, 0, func(bounds image.Rectangle, wasFocused bool) bool {
7272
c.pushLayout(bounds, image.Pt(0, 0))
7373
defer c.popLayout()
7474
f()

0 commit comments

Comments
 (0)