Skip to content

Commit 67f0756

Browse files
committed
debugui: use strings for control IDs
1 parent 62d3e9b commit 67f0756

7 files changed

Lines changed: 26 additions & 37 deletions

File tree

control.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *Context) mouseOver(bounds image.Rectangle) bool {
5555
}
5656

5757
func (c *Context) updateControl(id controlID, bounds image.Rectangle, opt option) (wasFocused bool) {
58-
if id == 0 {
58+
if id == emptyControlID {
5959
return false
6060
}
6161

@@ -73,11 +73,11 @@ func (c *Context) updateControl(id controlID, bounds image.Rectangle, opt option
7373

7474
if c.focus == id {
7575
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) && !mouseover {
76-
c.setFocus(0)
76+
c.setFocus(emptyControlID)
7777
wasFocused = true
7878
}
7979
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) && (^opt&optionHoldFocus) != 0 {
80-
c.setFocus(0)
80+
c.setFocus(emptyControlID)
8181
wasFocused = true
8282
}
8383
}
@@ -86,7 +86,7 @@ func (c *Context) updateControl(id controlID, bounds image.Rectangle, opt option
8686
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
8787
c.setFocus(id)
8888
} else if !mouseover {
89-
c.hover = 0
89+
c.hover = emptyControlID
9090
}
9191
}
9292

@@ -129,7 +129,7 @@ func (c *Context) Text(text string) {
129129
var endIdx, p int
130130
c.SetGridLayout([]int{-1}, []int{lineHeight()})
131131
for endIdx < len(text) {
132-
if _, err := c.control(0, 0, func(bounds image.Rectangle, wasFocused bool) (bool, error) {
132+
if _, err := c.control(emptyControlID, 0, func(bounds image.Rectangle, wasFocused bool) (bool, error) {
133133
w := 0
134134
endIdx = p
135135
startIdx := endIdx
@@ -180,7 +180,7 @@ func (c *Context) button(label string, opt option, callerPC uintptr) (controlID,
180180
return res, nil
181181
})
182182
if err != nil {
183-
return 0, false, err
183+
return emptyControlID, false, err
184184
}
185185
return id, res, nil
186186
}
@@ -484,5 +484,5 @@ func (c *Context) isCapturingInput() bool {
484484
return false
485485
}
486486

487-
return c.hoverRoot != nil || c.focus != 0
487+
return c.hoverRoot != nil || c.focus != emptyControlID
488488
}

debugui_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ func TestMultipleButtonsInForLoop(t *testing.T) {
1818
var id debugui.ControlID
1919
for range 10 {
2020
id2 := ctx.ButtonID("a")
21-
if id2 == 0 {
21+
if id2 == debugui.EmptyControlID {
2222
t.Errorf("Caller() returned 0")
2323
continue
2424
}
25-
if id == 0 {
25+
if id == debugui.EmptyControlID {
2626
id = id2
2727
continue
2828
}
2929
if id != id2 {
30-
t.Errorf("Caller() returned different values: %d and %d", id, id2)
30+
t.Errorf("Caller() returned different values: %q and %q", id, id2)
3131
}
3232
}
33-
if id == 0 {
33+
if id == debugui.EmptyControlID {
3434
t.Errorf("Caller() returned 0")
3535
}
3636
})
@@ -47,11 +47,11 @@ func TestMultipleButtonsOnOneLine(t *testing.T) {
4747
idA1 := ctx.ButtonID("a")
4848
idA2 := ctx.ButtonID("a")
4949
if idA1 == idA2 {
50-
t.Errorf("Button() returned the same value twice: %d", idA1)
50+
t.Errorf("Button() returned the same value twice: %q", idA1)
5151
}
5252
idB1, idB2 := ctx.ButtonID("b"), ctx.ButtonID("b")
5353
if idB1 == idB2 {
54-
t.Errorf("Button() returned the same value twice: %d", idB1)
54+
t.Errorf("Button() returned the same value twice: %q", idB1)
5555
}
5656
})
5757
return nil

export_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import "github.com/ebitengine/debugui/internal/caller"
77

88
type ControlID = controlID
99

10+
const EmptyControlID = emptyControlID
11+
1012
func (c *Context) ButtonID(label string) ControlID {
1113
pc := caller.Caller()
1214
var id controlID

helpers.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package debugui
66
import (
77
"errors"
88
"fmt"
9-
"hash/fnv"
109
"image"
1110
"slices"
1211
"sort"
@@ -21,33 +20,19 @@ func clamp[T int | float64](x, a, b T) T {
2120
}
2221

2322
func (c *Context) idFromGlobalUniquePointer(pointer unsafe.Pointer) controlID {
24-
return c.idFromBytes([]byte(fmt.Sprintf("!pointer:%p", pointer)))
23+
return controlID(fmt.Sprintf("!pointer:%p", pointer))
2524
}
2625

2726
func (c *Context) idFromGlobalUniqueString(str string) controlID {
28-
return c.idFromBytes([]byte(fmt.Sprintf("!string:%s", str)))
27+
return controlID(fmt.Sprintf("!string:%s", str))
2928
}
3029

3130
// idFromCaller returns a hash value based on the caller's file and line number.
3231
func (c *Context) idFromCaller(callerPC uintptr, str string) controlID {
3332
if len(str) > 0 {
34-
return c.idFromBytes([]byte(fmt.Sprintf("!caller:%d:%s", callerPC, str)))
33+
return controlID(fmt.Sprintf("!caller:%d:%s", callerPC, str))
3534
}
36-
return c.idFromBytes([]byte(fmt.Sprintf("!caller:%d", callerPC)))
37-
}
38-
39-
func (c *Context) idFromBytes(data []byte) controlID {
40-
if len(data) == 0 {
41-
return 0
42-
}
43-
44-
h := fnv.New64a()
45-
if _, err := h.Write(data); err != nil {
46-
panic(err)
47-
}
48-
id := controlID(h.Sum64())
49-
c.lastID = id
50-
return id
35+
return controlID(fmt.Sprintf("!caller:%d", callerPC))
5136
}
5237

5338
func (c *Context) popContainer() {
@@ -165,7 +150,7 @@ func (c *Context) end() error {
165150

166151
// unset focus if focus id was not touched this frame
167152
if !c.keepFocus {
168-
c.focus = 0
153+
c.focus = emptyControlID
169154
}
170155
c.keepFocus = false
171156

layout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (c *Context) GridCell(f func()) {
8787
}
8888

8989
func (c *Context) gridCell(f func() error) error {
90-
_, err := c.control(0, 0, func(bounds image.Rectangle, wasFocused bool) (res bool, err error) {
90+
_, err := c.control(emptyControlID, 0, func(bounds image.Rectangle, wasFocused bool) (res bool, err error) {
9191
c.pushLayout(bounds, image.Pt(0, 0))
9292
defer func() {
9393
if err2 := c.popLayout(); err2 != nil && err == nil {

textfield.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (c *Context) TextField(buf *string) bool {
3535
}
3636

3737
func (c *Context) textInputTextField(id controlID) *textinput.Field {
38-
if id == 0 {
38+
if id == emptyControlID {
3939
return nil
4040
}
4141
if _, ok := c.textInputTextFields[id]; !ok {
@@ -198,7 +198,7 @@ func (c *Context) numberTextField(value *float64, id controlID) (bool, error) {
198198
nval = 0
199199
}
200200
*value = float64(nval)
201-
c.numberEdit = 0
201+
c.numberEdit = emptyControlID
202202
}
203203
return true, nil
204204
}

type.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"github.com/hajimehoshi/ebiten/v2/exp/textinput"
1010
)
1111

12-
type controlID uint64
12+
type controlID string
13+
14+
const emptyControlID controlID = ""
1315

1416
type container struct {
1517
layout ContainerLayout

0 commit comments

Comments
 (0)