-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwidget.go
181 lines (159 loc) · 4.55 KB
/
widget.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024 The Ebitengine Authors
package debugui
import (
"image"
)
// widgetID is a unique identifier for a widget.
//
// Do not rely on the string value of widgetID, as it is not guaranteed to be stable across different runs of the program.
type widgetID string
const emptyWidgetID widgetID = ""
type option int
const (
optionAlignCenter option = (1 << iota)
optionAlignRight
optionNoInteract
optionNoFrame
optionNoResize
optionNoScroll
optionNoClose
optionNoTitle
optionHoldFocus
optionAutoSize
optionPopup
optionClosed
optionExpanded
)
func (c *Context) pointingOver(bounds image.Rectangle) bool {
p := c.pointingPosition()
if !p.In(bounds) {
return false
}
if !p.In(c.clipRect()) {
return false
}
return c.hoveringRootContainer() == c.currentRootContainer()
}
func (c *Context) pointingDelta() image.Point {
return c.pointingPosition().Sub(c.lastPointingPos)
}
func (c *Context) pointingPosition() image.Point {
p := c.pointing.position()
p.X /= c.Scale()
p.Y /= c.Scale()
return p
}
func (c *Context) handleInputForWidget(id widgetID, bounds image.Rectangle, opt option) (wasFocused bool) {
if id == emptyWidgetID {
return false
}
if c.focus == id {
c.keepFocus = true
}
if (opt & optionNoInteract) != 0 {
return false
}
hover := c.pointingOver(bounds)
if hover {
c.hover = id
}
if c.focus == id {
if c.pointing.justPressed() && !hover {
c.setFocus(emptyWidgetID)
wasFocused = true
}
if !c.pointing.pressed() && (^opt&optionHoldFocus) != 0 {
c.setFocus(emptyWidgetID)
wasFocused = true
}
}
if c.hover == id {
if c.pointing.justPressed() {
c.setFocus(id)
} else if !hover {
c.hover = emptyWidgetID
}
}
return
}
func (c *Context) widget(id widgetID, opt option, layout func(bounds image.Rectangle), handleInput func(bounds image.Rectangle, wasFocused bool) EventHandler, draw func(bounds image.Rectangle)) (EventHandler, error) {
c.currentID = id
bounds, err := c.layoutNext()
if err != nil {
return nil, err
}
if layout != nil {
if err := c.pushLayout(bounds, image.Pt(0, 0), false); err != nil {
return nil, err
}
defer func() {
b := &c.layoutStack[len(c.layoutStack)-1]
// inherit position/next_row/max from child layout if they are greater
a := &c.layoutStack[len(c.layoutStack)-2]
a.position.X = max(a.position.X, b.position.X+b.body.Min.X-a.body.Min.X)
a.nextRowY = max(a.nextRowY, b.nextRowY+b.body.Min.Y-a.body.Min.Y)
a.max.X = max(a.max.X, b.max.X)
a.max.Y = max(a.max.Y, b.max.Y)
if err2 := c.popLayout(); err2 != nil && err == nil {
err = err2
}
}()
layout(bounds)
}
wasFocused := c.handleInputForWidget(id, bounds, opt)
var e EventHandler
if handleInput != nil {
e = handleInput(bounds, wasFocused)
}
if draw != nil {
draw(bounds)
}
return e, nil
}
func (c *Context) widgetWithBounds(id widgetID, opt option, bounds image.Rectangle, handleInput func(bounds image.Rectangle, wasFocused bool) EventHandler, draw func(bounds image.Rectangle)) EventHandler {
c.currentID = id
wasFocused := c.handleInputForWidget(id, bounds, opt)
var e EventHandler
if handleInput != nil {
e = handleInput(bounds, wasFocused)
}
if draw != nil {
draw(bounds)
}
return e
}
// Checkbox creates a checkbox with the given boolean state and text label.
//
// A Checkbox widget is uniquely determined by its call location.
// Function calls made in different locations will create different widgets.
// If you want to generate different widgets with the same function call in a loop (such as a for loop), use [IDScope].
func (c *Context) Checkbox(state *bool, label string) EventHandler {
pc := caller()
id := c.idFromCaller(pc)
return c.wrapEventHandlerAndError(func() (EventHandler, error) {
return c.widget(id, 0, nil, func(bounds image.Rectangle, wasFocused bool) EventHandler {
var e EventHandler
c.handleInputForWidget(id, bounds, 0)
if c.pointing.justPressed() && c.focus == id {
e = &eventHandler{}
*state = !*state
}
return e
}, func(bounds image.Rectangle) {
box := image.Rect(bounds.Min.X, bounds.Min.Y+(bounds.Dy()-lineHeight())/2, bounds.Min.X+lineHeight(), bounds.Max.Y-(bounds.Dy()-lineHeight())/2)
c.drawWidgetFrame(id, box, colorBase, 0)
if *state {
c.drawIcon(iconCheck, box, c.style().colors[colorText])
}
if label != "" {
bounds = image.Rect(bounds.Min.X+lineHeight(), bounds.Min.Y, bounds.Max.X, bounds.Max.Y)
c.drawWidgetText(label, bounds, colorText, 0)
}
})
})
}
func (c *Context) setFocus(id widgetID) {
c.focus = id
c.keepFocus = true
}