forked from zeozeozeo/microui-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.go
243 lines (207 loc) · 5.43 KB
/
helpers.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024 The Ebitengine Authors
package debugui
import (
"image"
"sort"
"unsafe"
)
func clamp(x, a, b int) int {
return min(b, max(a, x))
}
func clampF(x, a, b float64) float64 {
return min(b, max(a, x))
}
func fnv1a(init controlID, data []byte) controlID {
h := init
for i := 0; i < len(data); i++ {
h = (h ^ controlID(data[i])) * 1099511628211
}
return h
}
func ptrToBytes(ptr unsafe.Pointer) []byte {
return unsafe.Slice((*byte)(unsafe.Pointer(&ptr)), unsafe.Sizeof(ptr))
}
// idFromBytes returns a hash value based on the data and the last ID on the stack.
func (c *Context) idFromBytes(data []byte) controlID {
if len(data) == 0 {
return 0
}
const (
// hashInitial is the initial value for the FNV-1a hash.
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
hashInitial = 14695981039346656037
)
var init controlID = hashInitial
if len(c.idStack) > 0 {
init = c.idStack[len(c.idStack)-1]
}
id := fnv1a(init, data)
c.lastID = id
return id
}
func (c *Context) pushID(data []byte) controlID {
// push()
id := c.idFromBytes(data)
c.idStack = append(c.idStack, id)
return id
}
func (c *Context) popID() {
c.idStack = c.idStack[:len(c.idStack)-1]
}
func (c *Context) pushClipRect(rect image.Rectangle) {
last := c.clipRect()
// push()
c.clipStack = append(c.clipStack, rect.Intersect(last))
}
func (c *Context) popClipRect() {
c.clipStack = c.clipStack[:len(c.clipStack)-1]
}
func (c *Context) clipRect() image.Rectangle {
return c.clipStack[len(c.clipStack)-1]
}
func (c *Context) checkClip(r image.Rectangle) int {
cr := c.clipRect()
if !r.Overlaps(cr) {
return clipAll
}
if r.In(cr) {
return 0
}
return clipPart
}
func (c *Context) layout() *layout {
return &c.layoutStack[len(c.layoutStack)-1]
}
func (c *Context) popContainer() {
cnt := c.currentContainer()
layout := c.layout()
cnt.layout.ContentSize.X = layout.max.X - layout.body.Min.X
cnt.layout.ContentSize.Y = layout.max.Y - layout.body.Min.Y
// pop container, layout and id
// pop()
c.containerStack = c.containerStack[:len(c.containerStack)-1]
// pop()
c.layoutStack = c.layoutStack[:len(c.layoutStack)-1]
}
func (c *Context) currentContainer() *container {
return c.containerStack[len(c.containerStack)-1]
}
func (c *Context) SetScroll(scroll image.Point) {
c.currentContainer().layout.Scroll = scroll
}
func (c *Context) container(id controlID, opt option) *container {
// try to get existing container from pool
if idx := c.poolGet(c.containerPool[:], id); idx >= 0 {
if c.containers[idx].open || (^opt&optionClosed) != 0 {
c.poolUpdate(c.containerPool[:], idx)
}
return &c.containers[idx]
}
if (opt & optionClosed) != 0 {
return nil
}
// container not found in pool: init new container
idx := c.poolInit(c.containerPool[:], id)
cnt := &c.containers[idx]
*cnt = container{}
cnt.headIdx = -1
cnt.tailIdx = -1
cnt.open = true
c.bringToFront(cnt)
return cnt
}
func (c *Context) Container(name string) *container {
id := c.idFromBytes([]byte(name))
return c.container(id, 0)
}
func (c *Context) bringToFront(cnt *container) {
c.lastZIndex++
cnt.zIndex = c.lastZIndex
}
func (c *Context) SetFocus() {
c.setFocus(c.lastID)
}
func (c *Context) setFocus(id controlID) {
c.focus = id
c.keepFocus = true
}
func (c *Context) update(f func(ctx *Context)) {
c.begin()
defer c.end()
f(c)
}
func (c *Context) begin() {
c.updateInput()
c.commandList = c.commandList[:0]
c.rootList = c.rootList[:0]
c.scrollTarget = nil
c.hoverRoot = c.nextHoverRoot
c.nextHoverRoot = nil
c.mouseDelta.X = c.mousePos.X - c.lastMousePos.X
c.mouseDelta.Y = c.mousePos.Y - c.lastMousePos.Y
c.tick++
}
func (c *Context) end() {
// check stacks
if len(c.containerStack) > 0 {
panic("container stack not empty")
}
if len(c.clipStack) > 0 {
panic("clip stack not empty")
}
if len(c.idStack) > 0 {
panic("id stack not empty")
}
if len(c.layoutStack) > 0 {
panic("layout stack not empty")
}
// handle scroll input
if c.scrollTarget != nil {
c.scrollTarget.layout.Scroll.X += c.scrollDelta.X
c.scrollTarget.layout.Scroll.Y += c.scrollDelta.Y
}
// unset focus if focus id was not touched this frame
if !c.keepFocus {
c.focus = 0
}
c.keepFocus = false
// bring hover root to front if mouse was pressed
if c.mousePressed != 0 && c.nextHoverRoot != nil &&
c.nextHoverRoot.zIndex < c.lastZIndex &&
c.nextHoverRoot.zIndex >= 0 {
c.bringToFront(c.nextHoverRoot)
}
// reset input state
c.keyPressed = 0
c.mousePressed = 0
c.scrollDelta = image.Pt(0, 0)
c.lastMousePos = c.mousePos
// sort root containers by zindex
sort.SliceStable(c.rootList, func(i, j int) bool {
return c.rootList[i].zIndex < c.rootList[j].zIndex
})
// set root container jump commands
for i := 0; i < len(c.rootList); i++ {
cnt := c.rootList[i]
// if this is the first container then make the first command jump to it.
// otherwise set the previous container's tail to jump to this one
if i == 0 {
cmd := c.commandList[0]
if cmd.typ != commandJump {
panic("expected jump command")
}
cmd.jump.dstIdx = cnt.headIdx + 1
if cnt.headIdx >= len(c.commandList) {
panic("invalid head index")
}
} else {
prev := c.rootList[i-1]
c.commandList[prev.tailIdx].jump.dstIdx = cnt.headIdx + 1
}
// make the last container's tail jump to the end of command list
if i == len(c.rootList)-1 {
c.commandList[cnt.tailIdx].jump.dstIdx = len(c.commandList)
}
}
}