-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathui.go
More file actions
504 lines (439 loc) · 13.5 KB
/
ui.go
File metadata and controls
504 lines (439 loc) · 13.5 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package ebitenui
import (
"image"
"sort"
"github.com/ebitenui/ebitenui/event"
"github.com/ebitenui/ebitenui/input"
"github.com/ebitenui/ebitenui/utilities/sliceutil"
"github.com/ebitenui/ebitenui/widget"
"github.com/hajimehoshi/ebiten/v2"
)
// UI encapsulates a complete user interface that can be rendered onto the screen.
// There should only be exactly one UI per application.
type UI struct {
// Container is the root container of the UI hierarchy.
Container widget.Containerer
// If true the default tab/shift-tab to focus will be disabled
DisableDefaultFocus bool
// If true the default relayering of Windows will be disabled
DisableWindowRelayering bool
// This exposes a Render call before the Container is drawn,
// but after the Windows with DrawLayer < 0 are drawn.
PreRenderHook widget.RenderFunc
// This exposes a Render call after the Container is drawn,
// but before the Windows with DrawLayer >= 0 (all by default) are drawn.
PostRenderHook widget.RenderFunc
// Theme settings
PrimaryTheme *widget.Theme
previousTheme *widget.Theme
focusedWidget widget.Focuser
focusedWindow *widget.Window
focusedWindowIndex int
inputLayerers []input.Layerer
windows []*widget.Window
previousContainer widget.Containerer
previousRemoveHandlerFuncs []event.RemoveHandlerFunc
tabWasPressed bool
updObj *widget.UpdateObject
debugMode bool
}
// Update updates u. This method should be called in the Ebiten Update function.
func (u *UI) Update() {
input.Update()
defer input.AfterUpdate()
if u.previousContainer == nil || u.previousContainer != u.Container {
for _, removeHandler := range u.previousRemoveHandlerFuncs {
removeHandler()
}
u.previousRemoveHandlerFuncs = []event.RemoveHandlerFunc{
u.Container.GetWidget().ContextMenuEvent.AddHandler(u.handleContextMenu),
u.Container.GetWidget().FocusEvent.AddHandler(u.handleFocusEvent),
u.Container.GetWidget().ToolTipEvent.AddHandler(u.handleToolTipEvent),
u.Container.GetWidget().DragAndDropEvent.AddHandler(u.handleDragAndDropEvent),
}
u.previousContainer = u.Container
// Close all Ephemeral Windows (tooltip/dnd/etc).
u.closeEphemeralWindows(0)
}
u.setTheme()
u.handleFocusChangeRequest()
// If widget is not visible or disabled, change focus to next widget.
if u.focusedWidget != nil && (u.focusedWidget.GetWidget().Disabled || !u.focusedWidget.GetWidget().IsVisible()) {
u.ChangeFocus(widget.FOCUS_NEXT)
}
resortFocusedWindow := false
index := 0
for ; index < len(u.windows); index++ {
if u.windows[index].DrawLayer < 0 {
u.resetUpdateObject()
u.windows[index].Update(u.updObj)
if u.updObj.RelayoutRequested {
u.windows[index].RequestRelayout()
}
if u.windows[index].FocusedWindow {
u.windows[index].FocusedWindow = false
u.focusedWindow = u.windows[index]
u.focusedWindowIndex = index
resortFocusedWindow = true
}
} else {
break
}
}
u.resetUpdateObject()
u.Container.Update(u.updObj)
if u.updObj.RelayoutRequested {
u.Container.RequestRelayout()
}
if u.updObj.CloseEphemeralWindows {
u.closeEphemeralWindows(0)
}
for ; index < len(u.windows); index++ {
u.resetUpdateObject()
u.windows[index].Update(u.updObj)
if u.updObj.RelayoutRequested {
u.windows[index].RequestRelayout()
}
if u.windows[index].FocusedWindow {
u.windows[index].FocusedWindow = false
u.focusedWindow = u.windows[index]
u.focusedWindowIndex = index
resortFocusedWindow = true
}
}
if !u.DisableWindowRelayering && resortFocusedWindow {
u.windows = sliceutil.ShiftEnd(u.windows, u.focusedWindowIndex)
}
event.ExecuteDeferred()
}
func (u *UI) resetUpdateObject() {
// Reset update object
if u.updObj == nil {
u.updObj = &widget.UpdateObject{}
}
u.updObj.RelayoutRequested = false
u.updObj.CloseEphemeralWindows = false
u.updObj.DebugMode = u.debugMode
}
// Draw renders u onto screen. This function should be called in the Ebiten Draw function.
func (u *UI) Draw(screen *ebiten.Image) {
u.setTheme()
input.Draw(screen)
defer input.AfterDraw(screen)
x, y := screen.Bounds().Dx(), screen.Bounds().Dy()
rect := image.Rect(0, 0, x, y)
u.setupInputLayers()
u.Container.SetLocation(rect)
u.render(screen)
// Render elements that pop up (like combobox) on top of everything else
widget.RenderDeferred(screen)
}
func (u *UI) setupInputLayers() {
num := 1
if len(u.windows) > 0 {
num += len(u.windows)
}
if cap(u.inputLayerers) < num {
u.inputLayerers = make([]input.Layerer, num)
}
u.inputLayerers = u.inputLayerers[:0]
u.inputLayerers = append(u.inputLayerers, u.Container)
for _, w := range u.windows {
u.inputLayerers = append(u.inputLayerers, w)
}
input.SetupInputLayersWithDeferred(u.inputLayerers)
}
func (u *UI) render(screen *ebiten.Image) {
index := 0
for ; index < len(u.windows); index++ {
if u.windows[index].DrawLayer < 0 {
u.windows[index].Render(screen)
} else {
break
}
}
if u.PreRenderHook != nil {
u.PreRenderHook(screen)
}
u.Container.Render(screen)
if u.PostRenderHook != nil {
u.PostRenderHook(screen)
}
for ; index < len(u.windows); index++ {
u.windows[index].Render(screen)
}
}
func (u *UI) setTheme() {
// Handle the user setting a new theme.
if u.Container != nil {
if (u.PrimaryTheme != nil && u.Container.GetWidget().GetTheme() == nil) || u.PrimaryTheme != u.previousTheme {
u.Container.GetWidget().SetTheme(u.PrimaryTheme)
u.previousTheme = u.PrimaryTheme
// Validate the main container with the new theme.
u.Container.Validate()
} else if !u.Container.IsValidated() {
u.Container.Validate()
}
}
}
func (u *UI) handleContextMenu(args interface{}) {
if a, ok := args.(*widget.WidgetContextMenuEventArgs); ok {
x, y := a.Widget.ContextMenu.PreferredSize()
r := image.Rect(0, 0, x, y)
r = r.Add(a.Location)
a.Widget.ContextMenuWindow = widget.NewWindow(
widget.WindowOpts.Contents(a.Widget.ContextMenu),
widget.WindowOpts.CloseMode(a.Widget.ContextMenuCloseMode),
widget.WindowOpts.Modal(),
widget.WindowOpts.Location(r),
)
u.AddWindow(a.Widget.ContextMenuWindow)
}
}
func (u *UI) handleFocusEvent(args interface{}) {
if a, ok := args.(*widget.WidgetFocusEventArgs); ok {
switch {
case a.Focused: // New widget focused
u.focusedWidget = a.Widget
case a.Widget == u.focusedWidget: // Current widget focus removed
u.focusedWidget = nil
case a.Widget == nil: // Clicked out of focusable widgets
// If we didnt just click on the same widget
if !a.Location.In(u.focusedWidget.GetWidget().Rect) {
u.focusedWidget.Focus(false)
u.focusedWidget = nil
}
}
}
}
func (u *UI) handleToolTipEvent(args interface{}) {
if a, ok := args.(*widget.WidgetToolTipEventArgs); ok {
a.Window.Ephemeral = true
if a.Show {
u.addWindow(a.Window)
} else {
u.removeWindow(a.Window)
}
}
}
func (u *UI) handleDragAndDropEvent(args interface{}) {
if a, ok := args.(*widget.WidgetDragAndDropEventArgs); ok {
if a.Show {
a.Window.Ephemeral = true
a.DnD.AvailableDropTargets = u.getDropTargets()
u.addWindow(a.Window)
} else {
a.DnD.AvailableDropTargets = nil
u.removeWindow(a.Window)
}
}
}
func (u *UI) getDropTargets() []widget.HasWidget {
dropTargets := u.Container.GetDropTargets()
// Loop through the windows array in reverse. If we find a modal window, only loop through its droppable widgets
for i := len(u.windows) - 1; i >= 0; i-- {
if !u.windows[i].Modal {
dropTargets = append(dropTargets, u.windows[i].GetContainer().GetDropTargets()...)
} else {
dropTargets = u.windows[i].GetContainer().GetDropTargets()
break
}
}
return dropTargets
}
func (u *UI) handleFocusChangeRequest() {
if !u.DisableDefaultFocus {
if input.KeyPressed(ebiten.KeyTab) {
if !u.tabWasPressed {
u.tabWasPressed = true
if input.KeyPressed(ebiten.KeyShift) {
u.ChangeFocus(widget.FOCUS_PREVIOUS)
} else {
u.ChangeFocus(widget.FOCUS_NEXT)
}
}
} else {
u.tabWasPressed = false
}
}
}
func (u *UI) ChangeFocus(direction widget.FocusDirection) {
focusableWidgets := u.Container.GetFocusers()
// Loop through the windows array in reverse. If we find a modal window, only loop through its focusable widgets
for i := len(u.windows) - 1; i >= 0; i-- {
if !u.windows[i].Modal {
focusableWidgets = append(focusableWidgets, u.windows[i].GetContainer().GetFocusers()...)
} else {
focusableWidgets = u.windows[i].GetContainer().GetFocusers()
break
}
}
fwLen := len(focusableWidgets)
if direction == widget.FOCUS_NEXT || direction == widget.FOCUS_PREVIOUS {
if fwLen == 1 {
if u.focusedWidget != nil && u.focusedWidget != focusableWidgets[0] {
u.focusedWidget.Focus(false)
}
focusableWidgets[0].Focus(true)
u.focusedWidget = focusableWidgets[0]
} else if fwLen > 0 {
sort.SliceStable(focusableWidgets, func(i, j int) bool {
return focusableWidgets[i].TabOrder() < focusableWidgets[j].TabOrder()
})
if u.focusedWidget != nil {
if direction == widget.FOCUS_PREVIOUS {
for i := 0; i < fwLen; i++ {
if focusableWidgets[i] == u.focusedWidget {
u.focusedWidget.Focus(false)
if i == 0 {
focusableWidgets[fwLen-1].Focus(true)
} else {
focusableWidgets[i-1].Focus(true)
u.focusedWidget = focusableWidgets[i-1]
}
return
}
}
} else {
for i := 0; i < fwLen-1; i++ {
if focusableWidgets[i] == u.focusedWidget {
u.focusedWidget.Focus(false)
focusableWidgets[i+1].Focus(true)
u.focusedWidget = focusableWidgets[i+1]
return
}
}
}
u.focusedWidget.Focus(false)
}
focusableWidgets[0].Focus(true)
u.focusedWidget = focusableWidgets[0]
}
} else {
if u.focusedWidget != nil {
if next := u.focusedWidget.GetFocus(direction); next != nil {
if !next.GetWidget().Disabled && next.GetWidget().IsVisible() {
u.focusedWidget.Focus(false)
next.Focus(true)
u.focusedWidget = next
}
}
} else if fwLen > 0 {
focusableWidgets[0].Focus(true)
u.focusedWidget = focusableWidgets[0]
}
}
}
// AddWindow adds window w to ui for rendering. It returns a function to remove w from ui.
func (u *UI) AddWindow(w *widget.Window) widget.RemoveWindowFunc {
return u.AddWindowQuietly(w, true)
}
// AddWindowQuietly adds window w to ui for rendering. It returns a function to remove w from ui.
// This function allows you to specify if you would like it to close any open ephemeralWindows (tooltip/dnd/etc)
func (u *UI) AddWindowQuietly(w *widget.Window, closeEphemeralWindows bool) widget.RemoveWindowFunc {
if u.addWindow(w) {
w.GetContainer().GetWidget().ContextMenuEvent.AddHandler(u.handleContextMenu)
w.GetContainer().GetWidget().FocusEvent.AddHandler(u.handleFocusEvent)
w.GetContainer().GetWidget().ToolTipEvent.AddHandler(u.handleToolTipEvent)
w.GetContainer().GetWidget().DragAndDropEvent.AddHandler(u.handleDragAndDropEvent)
if w.Modal && u.focusedWidget != nil {
u.focusedWidget.Focus(false)
}
if closeEphemeralWindows {
// Close all Ephemeral Windows (tooltip/dnd/etc)
u.closeEphemeralWindows(0)
}
}
return w.GetCloseFunction()
}
func (u *UI) addWindow(w *widget.Window) bool {
if u.IsWindowOpen(w) {
return false
}
if w.GetContainer().GetWidget().GetTheme() == nil {
w.GetContainer().GetWidget().SetTheme(u.PrimaryTheme)
}
w.GetContainer().Validate()
closeFunc := func() {
u.removeWindow(w)
}
w.SetCloseFunction(closeFunc)
u.windows = append(u.windows, w)
u.SortWindows()
return true
}
func (u *UI) removeWindow(w *widget.Window) {
windowIdx := -1
for i := range u.windows {
if u.windows[i] == w {
u.windows = append(u.windows[:i], u.windows[i+1:]...)
windowIdx = i
break
}
}
if windowIdx != -1 && !w.Ephemeral {
u.closeEphemeralWindows(windowIdx)
}
}
// Used to close tooltips/dnd etc
func (u *UI) closeEphemeralWindows(windowIdx int) {
for i := len(u.windows) - 1; i >= windowIdx; i-- {
if u.windows[i].Ephemeral {
u.windows = append(u.windows[:i], u.windows[i+1:]...)
}
}
}
// This function returns true if the provided window object is currently active in this UI.
func (u *UI) IsWindowOpen(w *widget.Window) bool {
for i := range u.windows {
if u.windows[i] == w {
return true
}
}
return false
}
// This function will re-sort the current windows attached to this UI based on its DrawLayer value.
func (u *UI) SortWindows() {
sort.SliceStable(u.windows, func(i, j int) bool {
return u.windows[i].DrawLayer < u.windows[j].DrawLayer
})
}
// This function will return true if any widget is currently focused or a Modal window is open.
func (u *UI) HasFocus() bool {
for i := len(u.windows) - 1; i >= 0; i-- {
if u.windows[i].Modal {
return true
}
}
return u.focusedWidget != nil
}
// This function will unfocus the currently focused widget
func (u *UI) ClearFocus() {
if u.focusedWidget != nil {
u.focusedWidget.Focus(false)
u.focusedWidget = nil
}
}
// This function will return the currently focused widget if available otherwise it returns nil
func (u *UI) GetFocusedWidget() widget.Focuser {
return u.focusedWidget
}
// This function will set a specific focusable widget as the currently focused widget.
func (u *UI) SetFocusedWidget(focused widget.Focuser) {
if u.focusedWidget != nil {
u.focusedWidget.Focus(false)
}
u.focusedWidget = focused
if u.focusedWidget != nil {
u.focusedWidget.Focus(true)
}
}
// SetDebugMode enables or disables DebugMode which shows the
// margins of the Widgets when hovering the cursor on them
func (u *UI) SetDebugMode(dm bool) {
u.debugMode = dm
}
// GetDebugMode get's the current value of DebugMode
func (u *UI) GetDebugMode() bool {
return u.debugMode
}