Skip to content

Commit c644b3e

Browse files
committed
debugui: add Context.Loop
Updates #43
1 parent 4400d03 commit c644b3e

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

dropdown.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package debugui
55

66
import (
77
"image"
8-
"strconv"
98

109
"github.com/hajimehoshi/ebiten/v2"
1110
)
@@ -58,17 +57,16 @@ func (c *Context) dropdown(selectedIndex *int, options []string, id widgetID) (E
5857
}
5958
c.SetGridLayout([]int{-1}, nil)
6059

61-
for i, option := range options {
62-
c.IDScope(strconv.Itoa(i), func() {
63-
c.Button(option).On(func() {
64-
*selectedIndex = i
65-
if cnt := c.container(dropdownID, 0); cnt != nil {
66-
// Start the close delay timer (0.1 seconds at TPS rate)
67-
cnt.dropdownCloseDelay = ebiten.TPS() / 10
68-
}
69-
})
60+
c.Loop(len(options), func(i int) {
61+
option := options[i]
62+
c.Button(option).On(func() {
63+
*selectedIndex = i
64+
if cnt := c.container(dropdownID, 0); cnt != nil {
65+
// Start the close delay timer (0.1 seconds at TPS rate)
66+
cnt.dropdownCloseDelay = ebiten.TPS() / 10
67+
}
7068
})
71-
}
69+
})
7270
}); err != nil {
7371
return nil, err
7472
}

example/gallery/ui.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,10 @@ func (g *Game) logWindow(ctx *debugui.Context) {
216216
func (g *Game) buttonWindows(ctx *debugui.Context) {
217217
ctx.Window("Button Windows", image.Rect(350, 300, 650, 500), func(layout debugui.ContainerLayout) {
218218
ctx.SetGridLayout([]int{-1, -1, -1, -1}, nil)
219-
for i := 0; i < 100; i++ {
220-
ctx.IDScope(fmt.Sprintf("%d", i), func() {
221-
ctx.Button("Button").On(func() {
222-
g.writeLog(fmt.Sprintf("Pressed button %d in Button Window", i))
223-
})
219+
ctx.Loop(100, func(i int) {
220+
ctx.Button("Button").On(func() {
221+
g.writeLog(fmt.Sprintf("Pressed button %d in Button Window", i))
224222
})
225-
}
223+
})
226224
})
227225
}

id.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,27 @@ func caller() uintptr {
1818
return pc
1919
}
2020

21+
// Loop creates a loop to iterate by the given count.
22+
// Loop creates a unique ID scope for each iteration.
23+
func (c *Context) Loop(count int, f func(i int)) {
24+
pc := caller()
25+
c.idStack = append(c.idStack, widgetID(fmt.Sprintf("caller:%d", pc)))
26+
defer func() {
27+
c.idStack = slices.Delete(c.idStack, len(c.idStack)-1, len(c.idStack))
28+
}()
29+
for i := range count {
30+
c.idStack = append(c.idStack, widgetID(fmt.Sprintf("string:%d", i)))
31+
f(i)
32+
c.idStack = slices.Delete(c.idStack, len(c.idStack)-1, len(c.idStack))
33+
}
34+
}
35+
2136
// IDScope creates a new scope for widget IDs.
2237
// IDScope creates a unique scope based on the caller's position and the given name string.
2338
//
2439
// IDScope is useful when you want to create multiple widgets at the same position e.g. in a for loop.
40+
//
41+
// IDScope is a low level API. For a simple loop, use [Loop] instead.
2542
func (c *Context) IDScope(name string, f func()) {
2643
pc := caller()
2744
c.idStack = append(c.idStack, widgetID(fmt.Sprintf("caller:%d", pc)))

0 commit comments

Comments
 (0)