-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUiBrowser.lua
650 lines (550 loc) · 14.8 KB
/
UiBrowser.lua
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
local UiBrowser
local UiOutputBox, UiOutputField
local UiBackButtonBox
local UiNavButtonBox, UiNavButton
local framecolor = sdl.rgba(13, 15, 23, 128)
local bordercolor = sdl.rgb(73, 92, 121, 128)
local buttoncolor = sdl.rgb(24, 28, 41, 128)
local function getCheckboxSurfaces()
return
deco.surfaces.checkboxChecked,
deco.surfaces.checkboxUnchecked,
deco.surfaces.checkboxHoveredChecked,
deco.surfaces.checkboxHoveredUnchecked
end
local function onclicked_highlightSelf(self, button)
if self.checked then
uiInspector.highlightSelf = true
else
uiInspector.highlightSelf = false
end
return true
end
local function onclicked_highlightInspectedUi(self, button)
uiInspector.highlightInspectedUi = self.checked
return true
end
local function onclicked_inspectCustomTooltip(self, button)
uiInspector.inspectCustomTooltip = self.checked
return true
end
local function onclicked_filterOutFunctions(self, button)
uiInspector.filterOutFunctions = self.checked
if uiInspector.filterOutFunctions then
uiInspector.browser.outputBox:filterOut("function")
end
return true
end
local function onclicked_filterOutTables(self, button)
uiInspector.filterOutTables = self.checked
if uiInspector.filterOutTables then
uiInspector.browser.outputBox:filterOut("table")
end
return true
end
local function onclicked_selectFocusedElement(self, button)
uiInspector.selectFocusedElement = self.checked
return true
end
function updateWidth(self, decoIndex, additionalWidth)
local decoText = self.decorations[decoIndex]
if decoText and decoText.surface then
self:widthpx(decoText.surface:w() + self.padl + self.padr + additionalWidth)
end
return self
end
-- //////////////////////////////////////////////
UiBackButtonBox = Class.inherit(UiFlowLayout)
function UiBackButtonBox:new()
UiFlowLayout.new(self)
self._debugName = "UiBackButtonBox"
end
function UiBackButtonBox:addButton(inspectedObject)
UiNavButton(inspectedObject)
:settooltip("Click to return to this ui element")
:updateWidth(2, 12):heightpx(40)
:addTo(self)
end
function UiBackButtonBox:clear()
local children = self.children
for i = #children, 1, -1 do
children[i]:detach()
end
end
function UiBackButtonBox:update()
local navCurrent = uiInspector.browser.navCurrent
if navCurrent == nil then
self:clear()
return
end
local parent = navCurrent
local parents = {}
-- Build list of all ancestors
while parent do
parents[#parents+1] = parent
parent = parent.parent
end
parents = reverse_table(parents)
-- Iterate ancestors to verify that
-- our back buttons match them
local children = self.children
for i, parent in ipairs(parents) do
local child = children[i]
if true
and child ~= nil
and child.target ~= parent
then
-- Target mismatch -> detach tail
for i = #children, i, -1 do
children[i]:detach()
end
end
if children[i] == nil then
-- Build new back buttons for
-- remaining ancestors
for i = i, #parents do
self:addButton(parents[i])
end
break
end
end
-- Detach superrfluous back buttons
for i = #children, #parents + 1, -1 do
children[i]:detach()
end
end
-- //////////////////////////////////////////////
UiNavButtonBox = Class.inherit(UiBoxLayout)
function UiNavButtonBox:new()
UiBoxLayout.new(self)
self._debugName = "UiNavButtonBox"
end
function UiNavButtonBox:clear(exceptions)
local children = self.children
for i = #children, 1, -1 do
local child = children[i]
local id = child.target
if false
or exceptions == nil
or exceptions[id] == nil
then
child:detach()
end
end
end
function UiNavButtonBox:inspectObject(uiInstance)
local exceptions = {}
for _, child in ipairs(uiInstance.children) do
exceptions[child] = true
end
local targets = {}
for _, button in ipairs(self.children) do
targets[button.target] = true
end
self:clear(exceptions)
for _, child in ipairs(uiInstance.children) do
if targets[child] == nil then
UiNavButton(child)
:settooltip("Click to inspect this ui element")
:width(1):heightpx(40)
:addTo(self)
end
end
end
-- //////////////////////////////////////////////
UiNavButton = Class.inherit(Ui)
function UiNavButton:new(target)
Assert.True(target ~= nil)
Ui.new(self)
self._debugName = "UiOutputBox"
self.target = target
self:clip()
:decorate{
DecoButton(framecolor, bordercolor),
DecoText(target._debugName)
}
self.updateWidth = updateWidth
end
function UiNavButton:onclicked(button)
uiInspector.browser.navCurrent = self.target
uiInspector.browser.backButtons:update()
return true
end
-- //////////////////////////////////////////////
UiOutputBox = Class.inherit(UiBoxLayout)
function UiOutputBox:new()
UiBoxLayout.new(self)
self._debugName = "UiOutputBox"
self.inspectedObject = nil
self.inspectedFields = {}
end
function UiOutputBox:insertSort(key, value)
local children = self.children
local function getValue(i)
local child = children[i]
return child and child.key or tostring(INT_MAX)
end
local index = BinarySearch(key, 1, #children + 1, getValue, "up")
local obj = UiOutputField(key, value)
:width(1):heightpx(40)
self:add(obj, index)
end
function UiOutputBox:remove(child)
self.inspectedFields[child.key] = nil
UiBoxLayout.remove(self, child)
end
function UiOutputBox:clear()
local children = self.children
for i = #children, 1, -1 do
children[i]:detach()
end
self.inspectedObject = nil
-- This table should be empty now
Assert.True(next(self.inspectedFields) == nil)
end
function UiOutputBox:filterOut(filterType)
local navCurrent = uiInspector.browser.navCurrent
if navCurrent == nil then
return
end
local children = self.children
for i = #children, 1, -1 do
local child = children[i]
if type(navCurrent[child.key]) == filterType then
child:detach()
end
end
end
function UiOutputBox:inspectObject(uiInstance)
if uiInstance ~= self.inspectedObject then
self:clear()
self.inspectedObject = uiInstance
end
local filterOutFunctions = uiInspector.filterOutFunctions
local filterOutTables = uiInspector.filterOutTables
local fields = self.inspectedFields
local newFields = {}
while(uiInstance) do
for _,instance in ipairs{uiInstance, uiInstance.__index} do
if instance then
for k,v in pairs(instance) do
if true
and fields[k] == nil
and k:find("^__") == nil
and (filterOutFunctions == false or type(v) ~= "function")
and (filterOutTables == false or type(v) ~= "table")
then
fields[k] = v
newFields[k] = v
end
end
end
end
uiInstance = uiInstance.__super
end
for key, value in pairs(newFields) do
self:insertSort(key, value)
end
end
-- //////////////////////////////////////////////
UiOutputField = Class.inherit(UiInputField)
function UiOutputField:new(key, value)
UiInputField.new(self)
self._debugName = "UiInputField"
self.key = key
self:clip()
:decorate{
DecoFrame(framecolor),
DecoText(key),
DecoInputField{
alignH = "right",
alignV = "center",
offsetX = -5,
},
}
end
local function getOutputTooltip(valueType)
if false
or valueType == "number"
or valueType == "string"
or valueType == "boolean"
then
return
"Warning: editing "..valueType.."s may cause unexpected errors or crashes",
"Click to edit "..valueType,
true
elseif false
or valueType == "userdata"
or valueType == "function"
or valueType == "table"
then
return
valueType.." values may not be edited",
"Protected type",
true
else
return
"This value may not be edited",
"Unknown type",
true
end
end
function UiOutputField:relayout()
local value = uiInspector.browser.navCurrent[self.key]
local valueType = type(value)
local stringifiedValue = tostring(value)
local editable = false
or valueType == "number"
or valueType == "string"
or valueType == "boolean"
if true
and self.focused ~= true
and self.textfield ~= stringifiedValue
then
self:setText(stringifiedValue)
:setVar("editable", editable)
:settooltip(getOutputTooltip(valueType))
end
UiInputField.relayout(self)
end
function UiOutputField:onEnter()
local key = self.key
local input = self.textfield
local navCurrent = uiInspector.browser.navCurrent
local currentValue = navCurrent[key]
local currentValueType = type(currentValue)
local inputAsNumber = tonumber(input)
local inputAsString = tostring(input)
local inputAsBoolean = tostring(input):lower()
if currentValueType == "number" and type(inputAsNumber) == "number" then
navCurrent[key] = inputAsNumber
elseif currentValueType == "string" and type(inputAsString) == "string" then
navCurrent[key] = inputAsString
elseif currentValueType == "boolean" and inputAsBoolean == "true" or inputAsBoolean == "false" then
navCurrent[key] = inputAsBoolean
end
UiInputField.onEnter(self)
end
-- //////////////////////////////////////////////
UiClickDetector = Class.inherit(Ui)
function UiClickDetector:new()
Ui.new(self)
self:size(1,1)
self.translucent = true
end
function UiClickDetector:mousedown(mx, my, button)
if true
-- and button == 1
and self.root.focuschild
and uiInspector.selectFocusedElement
then
uiInspector.browser:inspectObject(self.root.focuschild)
return true
end
return false
end
-- //////////////////////////////////////////////
UiBrowser = Class.inherit(Ui)
function UiBrowser:new()
Ui.new(self)
self._debugName = "UiBrowser"
self.navCurrent = nil
self.header = Ui()
self.navButtons = UiNavButtonBox()
self.backButtons = UiBackButtonBox()
self.outputBox = UiOutputBox()
self.clickDetector = UiClickDetector()
self
:sizepx(500,500)
:decorate{ DecoFrame(framecolor, nil, 5) }
:registerDragResize(5, 500)
:beginUi(UiWeightLayout)
:size(1,1)
:setVar("translucent", true)
-- Draggable Header
:beginUi(self.header)
:width(1):heightpx(40)
:padding(0)
:setVar("translucent", true)
:decorate{
DecoSolid(buttoncolor),
DecoText(
"Ui Inspector",
deco.uifont.title.font,
deco.uifont.title.set
)
}
:endUi()
-- Back Buttons
:beginUi(self.backButtons)
:width(1):heightpx(0):clip()
:dynamicResize(true)
:orientation(modApi.constants.ORIENTATION_HORIZONTAL)
:vgap(10):hgap(10)
:padding(10)
:endUi()
:orientation(modApi.constants.ORIENTATION_VERTICAL)
-- Main Window
:beginUi(UiWeightLayout)
:size(1,1)
:orientation(modApi.constants.ORIENTATION_HORIZONTAL)
-- Left scrollarea
:beginUi(UiScrollArea)
:size(0.2,1)
-- navButtons
:beginUi(self.navButtons)
:size(1,1)
:vgap(4)
:endUi()
:endUi()
-- Right scrollarea
:beginUi(UiScrollArea)
:size(0.8,1)
:beginUi(self.outputBox)
:size(1,1)
:vgap(4)
:endUi()
:endUi()
:endUi()
-- Bottom bar - Config buttons
:beginUi(UiFlowLayout)
:width(1):heightpx(0):clip()
:dynamicResize(true)
:hgap(20):vgap(20)
:padding(10)
:orientation(modApi.constants.ORIENTATION_HORIZONTAL)
:beginUi(UiCheckbox)
:heightpx(40)
:setVar("onclicked", onclicked_highlightInspectedUi)
:setVar("checked", uiInspector.highlightInspectedUi)
:settooltip("Highlight hovered ui objects")
:decorate{
DecoButton(framecolor, bordercolor),
DecoText("Highlight Inspected Ui"),
DecoAnchor("right"),
DecoAlign(-40),
DecoCheckbox(getCheckboxSurfaces()),
}
:format(updateWidth, 2, 52)
:endUi()
:beginUi(UiCheckbox)
:heightpx(40)
:setVar("onclicked", onclicked_inspectCustomTooltip)
:setVar("checked", uiInspector.inspectCustomTooltip)
:settooltip("Automatically inspect last seen custom tooltip")
:decorate{
DecoButton(framecolor, bordercolor),
DecoText("Inspect Custom Tooltip"),
DecoAnchor("right"),
DecoAlign(-40),
DecoCheckbox(getCheckboxSurfaces()),
}
:format(updateWidth, 2, 52)
:endUi()
:beginUi(UiCheckbox)
:heightpx(40)
:setVar("onclicked", onclicked_filterOutFunctions)
:setVar("checked", uiInspector.filterOutFunctions)
:settooltip("Filter out functions from output")
:decorate{
DecoButton(framecolor, bordercolor),
DecoText("Filter out functions"),
DecoAnchor("right"),
DecoAlign(-40),
DecoCheckbox(getCheckboxSurfaces()),
}
:format(updateWidth, 2, 52)
:endUi()
:beginUi(UiCheckbox)
:heightpx(40)
:setVar("onclicked", onclicked_filterOutTables)
:setVar("checked", uiInspector.filterOutTables)
:settooltip("Filter out tables from output")
:decorate{
DecoButton(framecolor, bordercolor),
DecoText("Filter out tables"),
DecoAnchor("right"),
DecoAlign(-40),
DecoCheckbox(getCheckboxSurfaces()),
}
:format(updateWidth, 2, 52)
:endUi()
:beginUi(UiCheckbox)
:heightpx(40)
:setVar("onclicked", onclicked_selectFocusedElement)
:setVar("checked", uiInspector.selectFocusedElement)
:settooltip("Select focused element")
:decorate{
DecoButton(framecolor, bordercolor),
DecoText("Select focused element"),
DecoAnchor("right"),
DecoAlign(-40),
DecoCheckbox(getCheckboxSurfaces()),
}
:format(updateWidth, 2, 52)
:endUi()
:beginUi(UiCheckbox)
:heightpx(40)
:setVar("onclicked", onclicked_highlightSelf)
:setVar("checked", uiInspector.highlightSelf)
:settooltip("Highlight ui elements in UiInspector")
:decorate{
DecoButton(framecolor, bordercolor),
DecoText("Hover Self"),
DecoAnchor("right"),
DecoAlign(-40),
DecoCheckbox(getCheckboxSurfaces()),
}
:format(updateWidth, 2, 52)
:endUi()
:endUi()
:endUi()
end
function UiBrowser:updateState()
local mx, my = sdl.mouse.x(), sdl.mouse.y()
-- Make object both draggable and resizable
if true
and not self.dragResizing
and not self.dragMoving
then
local isEdge = UiDraggable.isEdge(self, mx, my, self.__resizeHandle)
if self.header.containsMouse then
self:settooltip("Click and drag to move")
self.dragMovable = true
self.dragResizable = false
self.canDragResize = false
elseif isEdge then
self:settooltip("Click and drag to resize")
self.dragMovable = false
self.dragResizable = true
self.canDragResize = true
else
self:settooltip()
self.dragMovable = false
self.dragResizable = false
self.canDragResize = false
end
end
Ui.updateState(self)
end
function UiBrowser:inspectObject(inspectedObject)
self.navCurrent = inspectedObject
self.backButtons:update()
end
function UiBrowser:relayout()
if uiInspector.inspectCustomTooltip then
local tooltipManager = sdlext.getUiRoot().tooltipUi
if tooltipManager.currentTooltip ~= tooltipManager.standardTooltip then
self:inspectObject(tooltipManager.currentTooltip)
end
elseif false
or self.navCurrent == nil
or self.navCurrent.root == nil
then
self:inspectObject(sdlext.getUiRoot())
end
self.navButtons:inspectObject(self.navCurrent)
self.outputBox:inspectObject(self.navCurrent)
Ui.relayout(self)
end
return UiBrowser