Skip to content

Commit 109af04

Browse files
committed
debugui: implement spin buttons for numbers
Closes #33
1 parent b462594 commit 109af04

File tree

4 files changed

+79
-21
lines changed

4 files changed

+79
-21
lines changed

button.go

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,75 @@ func (c *Context) Button(text string) EventHandler {
1717
pc := caller()
1818
id := c.idFromCaller(pc)
1919
return c.wrapEventHandlerAndError(func() (EventHandler, error) {
20-
return c.button(text, iconNone, optionAlignCenter, id)
20+
return c.button(text, optionAlignCenter, id)
2121
})
2222
}
2323

24-
func (c *Context) iconButton(icon icon) EventHandler {
24+
func (c *Context) button(text string, opt option, id widgetID) (EventHandler, error) {
25+
return c.widget(id, opt, nil, func(bounds image.Rectangle, wasFocused bool) EventHandler {
26+
var e EventHandler
27+
if c.pointing.justPressed() && c.focus == id {
28+
e = &eventHandler{}
29+
}
30+
return e
31+
}, func(bounds image.Rectangle) {
32+
c.drawWidgetFrame(id, bounds, colorButton, opt)
33+
if len(text) > 0 {
34+
c.drawWidgetText(text, bounds, colorText, opt)
35+
}
36+
})
37+
}
38+
39+
func (c *Context) spinButtons() (up, down EventHandler) {
2540
pc := caller()
2641
id := c.idFromCaller(pc)
27-
return c.wrapEventHandlerAndError(func() (EventHandler, error) {
28-
return c.button("", icon, optionAlignCenter, id)
42+
c.idScopeFromID(id, func() {
43+
upID := c.idFromString("up")
44+
downID := c.idFromString("down")
45+
c.GridCell(func(bounds image.Rectangle) {
46+
c.setGridLayout(nil, []int{-1, -1})
47+
up = c.wrapEventHandlerAndError(func() (EventHandler, error) {
48+
var e EventHandler
49+
var err error
50+
e, err = c.spinButton(true, optionAlignCenter, upID, downID)
51+
if err != nil {
52+
return nil, err
53+
}
54+
return e, nil
55+
})
56+
down = c.wrapEventHandlerAndError(func() (EventHandler, error) {
57+
var e EventHandler
58+
var err error
59+
c.idScopeFromID(id, func() {
60+
e, err = c.spinButton(false, optionAlignCenter, upID, downID)
61+
})
62+
if err != nil {
63+
return nil, err
64+
}
65+
return e, nil
66+
})
67+
})
2968
})
69+
return up, down
3070
}
3171

32-
func (c *Context) button(text string, icon icon, opt option, id widgetID) (EventHandler, error) {
72+
func (c *Context) spinButton(up bool, opt option, upID, downID widgetID) (EventHandler, error) {
73+
id := downID
74+
if up {
75+
id = upID
76+
}
3377
return c.widget(id, opt, nil, func(bounds image.Rectangle, wasFocused bool) EventHandler {
3478
var e EventHandler
35-
if c.pointing.justPressed() && c.focus == id {
79+
if c.pointing.repeated() && (c.focus == upID || c.focus == downID) && c.pointing.position().In(bounds) {
3680
e = &eventHandler{}
3781
}
3882
return e
3983
}, func(bounds image.Rectangle) {
4084
c.drawWidgetFrame(id, bounds, colorButton, opt)
41-
if len(text) > 0 {
42-
c.drawWidgetText(text, bounds, colorText, opt)
43-
}
44-
if icon != iconNone {
45-
c.drawIcon(icon, bounds, c.style().colors[colorText])
85+
icon := iconDown
86+
if up {
87+
icon = iconUp
4688
}
89+
c.drawIcon(icon, bounds, c.style().colors[colorText])
4790
})
4891
}

draw.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ func lineHeight() int {
4747
type icon int
4848

4949
const (
50-
iconNone icon = iota
51-
iconCheck
50+
iconCheck icon = iota + 1
5251
iconCollapsed
5352
iconExpanded
5453
iconDown
@@ -63,10 +62,6 @@ var (
6362
)
6463

6564
func iconImage(icon icon) *ebiten.Image {
66-
if icon == iconNone {
67-
return nil
68-
}
69-
7065
iconM.Lock()
7166
defer iconM.Unlock()
7267

input.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type pointing struct {
1616
touchIDs []ebiten.TouchID
1717
hasPrimaryTouchID bool
1818
primaryTouchID ebiten.TouchID
19+
duration int
1920
}
2021

2122
func (p *pointing) update() {
@@ -29,6 +30,12 @@ func (p *pointing) update() {
2930
p.hasPrimaryTouchID = true
3031
p.primaryTouchID = p.touchIDs[0]
3132
}
33+
34+
if p.pressed() {
35+
p.duration++
36+
} else {
37+
p.duration = 0
38+
}
3239
}
3340

3441
func (p *pointing) isTouchActive() bool {
@@ -58,3 +65,14 @@ func (p *pointing) justPressed() bool {
5865
}
5966
return inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft)
6067
}
68+
69+
func (p *pointing) repeated() bool {
70+
if p.duration == 1 {
71+
return true
72+
}
73+
delay := ebiten.TPS() * 24 / 60
74+
if p.duration < delay {
75+
return false
76+
}
77+
return (p.duration-delay)%4 == 0
78+
}

textfield.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,12 @@ func (c *Context) numberField(value *int, step int, id widgetID, opt option) (Ev
184184
c.GridCell(func(bounds image.Rectangle) {
185185
c.idScopeFromID(id, func() {
186186
c.SetGridLayout(nil, []int{-1, -1})
187-
c.iconButton(iconUp).On(func() {
187+
up, down := c.spinButtons()
188+
up.On(func() {
188189
*value += step
189190
e = &eventHandler{}
190191
})
191-
c.iconButton(iconDown).On(func() {
192+
down.On(func() {
192193
*value -= step
193194
e = &eventHandler{}
194195
})
@@ -234,11 +235,12 @@ func (c *Context) numberFieldF(value *float64, step float64, digits int, id widg
234235
c.GridCell(func(bounds image.Rectangle) {
235236
c.idScopeFromID(id, func() {
236237
c.SetGridLayout(nil, []int{-1, -1})
237-
c.iconButton(iconUp).On(func() {
238+
up, down := c.spinButtons()
239+
up.On(func() {
238240
*value += step
239241
e = &eventHandler{}
240242
})
241-
c.iconButton(iconDown).On(func() {
243+
down.On(func() {
242244
*value -= step
243245
e = &eventHandler{}
244246
})

0 commit comments

Comments
 (0)