Skip to content

Commit a69d45b

Browse files
committed
debugui: remove Response
1 parent 8646ebd commit a69d45b

3 files changed

Lines changed: 36 additions & 49 deletions

File tree

control.go

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ const (
2424
sliderFmt = "%.2f"
2525
)
2626

27-
type Response int
28-
29-
const (
30-
ResponseActive Response = (1 << 0)
31-
ResponseSubmit Response = (1 << 1)
32-
ResponseChange Response = (1 << 2)
33-
)
34-
3527
type option int
3628

3729
const (
@@ -104,12 +96,12 @@ func (c *Context) updateControl(id controlID, rect image.Rectangle, opt option)
10496
}
10597
}
10698

107-
func (c *Context) Control(idStr string, f func(bounds image.Rectangle) Response) Response {
99+
func (c *Context) Control(idStr string, f func(bounds image.Rectangle) bool) bool {
108100
id := c.idFromString(idStr)
109101
return c.control(id, 0, f)
110102
}
111103

112-
func (c *Context) control(id controlID, opt option, f func(bounds image.Rectangle) Response) Response {
104+
func (c *Context) control(id controlID, opt option, f func(bounds image.Rectangle) bool) bool {
113105
r := c.layoutNext()
114106
c.updateControl(id, r, opt)
115107
return f(r)
@@ -121,7 +113,7 @@ func (c *Context) Text(text string) {
121113
var endIdx, p int
122114
c.SetGridLayout([]int{-1}, []int{lineHeight()})
123115
for endIdx < len(text) {
124-
c.control(0, 0, func(bounds image.Rectangle) Response {
116+
c.control(0, 0, func(bounds image.Rectangle) bool {
125117
w := 0
126118
endIdx = p
127119
startIdx := endIdx
@@ -142,47 +134,47 @@ func (c *Context) Text(text string) {
142134
}
143135
c.drawText(text[startIdx:endIdx], bounds.Min, color)
144136
p = endIdx + 1
145-
return 0
137+
return false
146138
})
147139
}
148140
})
149141
}
150142

151143
func (c *Context) Label(text string) {
152-
c.control(0, 0, func(bounds image.Rectangle) Response {
144+
c.control(0, 0, func(bounds image.Rectangle) bool {
153145
c.drawControlText(text, bounds, ColorText, 0)
154-
return 0
146+
return false
155147
})
156148
}
157149

158150
func (c *Context) button(label string, opt option) (controlID, bool) {
159151
label, idStr, _ := strings.Cut(label, idSeparator)
160152
id := c.idFromString(idStr)
161-
return id, c.control(id, opt, func(bounds image.Rectangle) Response {
162-
var res Response
153+
return id, c.control(id, opt, func(bounds image.Rectangle) bool {
154+
var res bool
163155
// handle click
164156
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) && c.focus == id {
165-
res |= ResponseSubmit
157+
res = true
166158
}
167159
// draw
168160
c.drawControlFrame(id, bounds, ColorButton, opt)
169161
if len(label) > 0 {
170162
c.drawControlText(label, bounds, ColorText, opt)
171163
}
172164
return res
173-
}) != 0
165+
})
174166
}
175167

176168
func (c *Context) Checkbox(label string, state *bool) bool {
177169
id := c.idFromString(fmt.Sprintf("%p", state))
178170

179-
return c.control(id, 0, func(bounds image.Rectangle) Response {
180-
var res Response
171+
return c.control(id, 0, func(bounds image.Rectangle) bool {
172+
var res bool
181173
box := image.Rect(bounds.Min.X, bounds.Min.Y, bounds.Min.X+bounds.Dy(), bounds.Max.Y)
182174
c.updateControl(id, bounds, 0)
183175
// handle click
184176
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) && c.focus == id {
185-
res |= ResponseChange
177+
res = true
186178
*state = !*state
187179
}
188180
// draw
@@ -193,7 +185,7 @@ func (c *Context) Checkbox(label string, state *bool) bool {
193185
bounds = image.Rect(bounds.Min.X+box.Dx(), bounds.Min.Y, bounds.Max.X, bounds.Max.Y)
194186
c.drawControlText(label, bounds, ColorText, 0)
195187
return res
196-
}) != 0
188+
})
197189
}
198190

199191
func (c *Context) textField(id controlID) *textinput.Field {
@@ -209,9 +201,9 @@ func (c *Context) textField(id controlID) *textinput.Field {
209201
return c.textFields[id]
210202
}
211203

212-
func (c *Context) textBoxRaw(buf *string, id controlID, opt option) Response {
213-
return c.control(id, opt|optionHoldFocus, func(bounds image.Rectangle) Response {
214-
var res Response
204+
func (c *Context) textBoxRaw(buf *string, id controlID, opt option) bool {
205+
return c.control(id, opt|optionHoldFocus, func(bounds image.Rectangle) bool {
206+
var res bool
215207

216208
if c.focus == id {
217209
// handle text input
@@ -222,11 +214,10 @@ func (c *Context) textBoxRaw(buf *string, id controlID, opt option) Response {
222214
handled, err := f.HandleInput(x, y)
223215
if err != nil {
224216
fmt.Fprintln(os.Stderr, err)
225-
return 0
217+
return false
226218
}
227219
if *buf != f.TextForRendering() {
228220
*buf = f.TextForRendering()
229-
res |= ResponseChange
230221
}
231222

232223
if !handled {
@@ -235,13 +226,12 @@ func (c *Context) textBoxRaw(buf *string, id controlID, opt option) Response {
235226
_, size := utf8.DecodeLastRuneInString(*buf)
236227
*buf = (*buf)[:len(*buf)-size]
237228
f.SetTextAndSelection(*buf, len(*buf), len(*buf))
238-
res |= ResponseChange
239229
}
240230

241231
// handle return
242232
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
243233
c.setFocus(0)
244-
res |= ResponseSubmit
234+
res = true
245235
f.SetTextAndSelection("", 0, 0)
246236
}
247237
}
@@ -280,7 +270,7 @@ func (c *Context) numberTextBox(value *float64, id controlID) bool {
280270
}
281271
if c.numberEdit == id {
282272
res := c.textBoxRaw(&c.numberEditBuf, id, 0)
283-
if (res&ResponseSubmit) != 0 || c.focus != id {
273+
if res || c.focus != id {
284274
nval, err := strconv.ParseFloat(c.numberEditBuf, 32)
285275
if err != nil {
286276
nval = 0
@@ -295,7 +285,7 @@ func (c *Context) numberTextBox(value *float64, id controlID) bool {
295285

296286
func (c *Context) textBox(buf *string, opt option) bool {
297287
id := c.idFromString(fmt.Sprintf("%p", buf))
298-
return c.textBoxRaw(buf, id, opt)&ResponseSubmit != 0
288+
return c.textBoxRaw(buf, id, opt)
299289
}
300290

301291
func formatNumber(v float64, digits int) string {
@@ -313,8 +303,8 @@ func (c *Context) slider(value *float64, low, high, step float64, digits int, op
313303
}
314304

315305
// handle normal mode
316-
return c.control(id, opt, func(bounds image.Rectangle) Response {
317-
var res Response
306+
return c.control(id, opt, func(bounds image.Rectangle) bool {
307+
var res bool
318308
// handle input
319309
if c.focus == id && ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
320310
x, _ := ebiten.CursorPosition()
@@ -327,7 +317,7 @@ func (c *Context) slider(value *float64, low, high, step float64, digits int, op
327317
*value = clamp(v, low, high)
328318
v = *value
329319
if last != v {
330-
res |= ResponseChange
320+
res = true
331321
}
332322

333323
// draw base
@@ -342,7 +332,7 @@ func (c *Context) slider(value *float64, low, high, step float64, digits int, op
342332
c.drawControlText(text, bounds, ColorText, opt)
343333

344334
return res
345-
}) != 0
335+
})
346336
}
347337

348338
func (c *Context) number(value *float64, step float64, digits int, opt option) bool {
@@ -355,15 +345,15 @@ func (c *Context) number(value *float64, step float64, digits int, opt option) b
355345
}
356346

357347
// handle normal mode
358-
return c.control(id, opt, func(bounds image.Rectangle) Response {
359-
var res Response
348+
return c.control(id, opt, func(bounds image.Rectangle) bool {
349+
var res bool
360350
// handle input
361351
if c.focus == id && ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
362352
*value += float64(c.mouseDelta().X) * step
363353
}
364354
// set flag if value changed
365355
if *value != last {
366-
res |= ResponseChange
356+
res = true
367357
}
368358

369359
// draw base
@@ -373,7 +363,7 @@ func (c *Context) number(value *float64, step float64, digits int, opt option) b
373363
c.drawControlText(text, bounds, ColorText, opt)
374364

375365
return res
376-
}) != 0
366+
})
377367
}
378368

379369
func (c *Context) header(label string, istreenode bool, opt option, f func()) {
@@ -389,7 +379,7 @@ func (c *Context) header(label string, istreenode bool, opt option, f func()) {
389379
expanded = toggled
390380
}
391381

392-
if c.control(id, 0, func(bounds image.Rectangle) Response {
382+
if c.control(id, 0, func(bounds image.Rectangle) bool {
393383
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) && c.focus == id {
394384
if toggled {
395385
delete(c.toggledIDs, id)
@@ -423,11 +413,8 @@ func (c *Context) header(label string, istreenode bool, opt option, f func()) {
423413
bounds.Min.X += bounds.Dy() - c.style.padding
424414
c.drawControlText(label, bounds, ColorText, 0)
425415

426-
if expanded {
427-
return ResponseActive
428-
}
429-
return 0
430-
}) != 0 {
416+
return expanded
417+
}) {
431418
f()
432419
}
433420
}

example/ui.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (g *Game) testWindow(ctx *debugui.Context) {
116116
ctx.Slider(&g.bg[2], 0, 255, 1, 0)
117117
})
118118
// color preview
119-
ctx.Control("", func(bounds image.Rectangle) debugui.Response {
119+
ctx.Control("", func(bounds image.Rectangle) bool {
120120
ctx.DrawControl(func(screen *ebiten.Image) {
121121
vector.DrawFilledRect(
122122
screen,
@@ -133,7 +133,7 @@ func (g *Game) testWindow(ctx *debugui.Context) {
133133
op.SecondaryAlign = text.AlignCenter
134134
debugui.DrawText(screen, txt, op)
135135
})
136-
return 0
136+
return false
137137
})
138138
})
139139

layout.go

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

7070
func (c *Context) Division(f func()) {
71-
c.control(0, 0, func(bounds image.Rectangle) Response {
71+
c.control(0, 0, func(bounds image.Rectangle) bool {
7272
c.pushLayout(bounds, image.Pt(0, 0))
7373
defer c.popLayout()
7474
f()
@@ -79,7 +79,7 @@ func (c *Context) Division(f func()) {
7979
a.nextRowY = max(a.nextRowY, b.nextRowY+b.body.Min.Y-a.body.Min.Y)
8080
a.max.X = max(a.max.X, b.max.X)
8181
a.max.Y = max(a.max.Y, b.max.Y)
82-
return 0
82+
return false
8383
})
8484
}
8585

0 commit comments

Comments
 (0)