-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwidgetstate.go
289 lines (248 loc) · 5.39 KB
/
widgetstate.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024 Hajime Hoshi
package guigui
import (
"image"
"github.com/hajimehoshi/ebiten/v2"
)
type widgetsAndBounds struct {
bounds map[Widget]image.Rectangle
}
func (w *widgetsAndBounds) reset() {
clear(w.bounds)
}
func (w *widgetsAndBounds) append(widget Widget, bounds image.Rectangle) {
if w.bounds == nil {
w.bounds = map[Widget]image.Rectangle{}
}
w.bounds[widget] = bounds
}
func (w *widgetsAndBounds) equals(currentWidgets []Widget) bool {
if len(w.bounds) != len(currentWidgets) {
return false
}
for _, widget := range currentWidgets {
b, ok := w.bounds[widget]
if !ok {
return false
}
if b != Bounds(widget) {
return false
}
}
return true
}
func (w *widgetsAndBounds) redrawIfAboveParentZ() {
for widget := range w.bounds {
if isAboveParentZ(widget) {
RequestRedraw(widget)
}
}
}
type widgetState struct {
root bool
position image.Point
parent Widget
children []Widget
prev widgetsAndBounds
hidden bool
disabled bool
transparency float64
offscreen *ebiten.Image
}
func Position(widget Widget) image.Point {
return widget.widgetState().position
}
func SetPosition(widget Widget, position image.Point) {
widget.widgetState().position = position
// Rerendering happens at a.addInvalidatedRegions if necessary.
}
func Bounds(widget Widget) image.Rectangle {
widgetState := widget.widgetState()
width, height := widget.Size(&theApp.context)
return image.Rectangle{
Min: widgetState.position,
Max: widgetState.position.Add(image.Point{width, height}),
}
}
func VisibleBounds(widget Widget) image.Rectangle {
parent := widget.widgetState().parent
if parent == nil {
return theApp.bounds()
}
if isAboveParentZ(widget) {
return Bounds(widget)
}
return VisibleBounds(parent).Intersect(Bounds(widget))
}
func (w *widgetState) isInTree() bool {
p := w
for ; p.parent != nil; p = p.parent.widgetState() {
}
return p.root
}
func Show(widget Widget) {
widgetState := widget.widgetState()
if !widgetState.hidden {
return
}
widgetState.hidden = false
RequestRedraw(widget)
}
func Hide(widget Widget) {
widgetState := widget.widgetState()
if widgetState.hidden {
return
}
widgetState.hidden = true
Blur(widget)
RequestRedraw(widget)
}
func IsVisible(widget Widget) bool {
return widget.widgetState().isVisible()
}
func (w *widgetState) isVisible() bool {
if w.parent != nil {
return !w.hidden && IsVisible(w.parent)
}
return !w.hidden
}
func Enable(widget Widget) {
widgetState := widget.widgetState()
if !widgetState.disabled {
return
}
widgetState.disabled = false
RequestRedraw(widget)
}
func Disable(widget Widget) {
widgetState := widget.widgetState()
if widgetState.disabled {
return
}
widgetState.disabled = true
Blur(widget)
RequestRedraw(widget)
}
func IsEnabled(widget Widget) bool {
return widget.widgetState().isEnabled()
}
func (w *widgetState) isEnabled() bool {
if w.parent != nil {
return !w.disabled && IsEnabled(w.parent)
}
return !w.disabled
}
func Focus(widget Widget) {
widgetState := widget.widgetState()
if !widgetState.isVisible() {
return
}
if !widgetState.isEnabled() {
return
}
if !widgetState.isInTree() {
return
}
if theApp.focusedWidget == widget {
return
}
var oldWidget Widget
if theApp.focusedWidget != nil {
oldWidget = theApp.focusedWidget
}
theApp.focusedWidget = widget
RequestRedraw(theApp.focusedWidget)
if oldWidget != nil {
RequestRedraw(oldWidget)
}
}
func Blur(widget Widget) {
widgetState := widget.widgetState()
if !widgetState.isInTree() {
return
}
if theApp.focusedWidget != widget {
return
}
theApp.focusedWidget = nil
RequestRedraw(widget)
}
func IsFocused(widget Widget) bool {
widgetState := widget.widgetState()
return widgetState.isInTree() && theApp.focusedWidget == widget && widgetState.isVisible()
}
func HasFocusedChildWidget(widget Widget) bool {
widgetState := widget.widgetState()
if IsFocused(widget) {
return true
}
for _, child := range widgetState.children {
if HasFocusedChildWidget(child) {
return true
}
}
return false
}
func Opacity(widget Widget) float64 {
return widget.widgetState().opacity()
}
func (w *widgetState) opacity() float64 {
return 1 - w.transparency
}
func SetOpacity(widget Widget, opacity float64) {
if opacity < 0 {
opacity = 0
}
if opacity > 1 {
opacity = 1
}
widgetState := widget.widgetState()
if widgetState.transparency == 1-opacity {
return
}
widgetState.transparency = 1 - opacity
RequestRedraw(widget)
}
func RequestRedraw(widget Widget) {
theApp.requestRedrawWidget(widget)
}
func (w *widgetState) ensureOffscreen(bounds image.Rectangle) *ebiten.Image {
if w.offscreen != nil {
if !w.offscreen.Bounds().In(bounds) {
w.offscreen.Deallocate()
w.offscreen = nil
}
}
if w.offscreen == nil {
w.offscreen = ebiten.NewImage(bounds.Max.X, bounds.Max.Y)
}
return w.offscreen.SubImage(bounds).(*ebiten.Image)
}
func traverseWidget(widget Widget, f func(widget Widget)) {
f(widget)
for _, child := range widget.widgetState().children {
traverseWidget(child, f)
}
}
func z(widget Widget) int {
parent := widget.widgetState().parent
if parent == nil {
if widget.IsPopup() {
return 1
}
return 0
}
z := z(parent)
if widget.IsPopup() {
z++
}
return z
}
func isAboveParentZ(widget Widget) bool {
parent := widget.widgetState().parent
if parent == nil {
return false
}
return z(widget) > z(parent)
}