-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimgui_widgets.lua
More file actions
7808 lines (6774 loc) · 333 KB
/
Copy pathimgui_widgets.lua
File metadata and controls
7808 lines (6774 loc) · 333 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
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--- ImGui Sincerely WIP
-- (Widgets Code)
--- @type ImGuiContext?
local GImGui
-- Sets local `GImGui` in this file(imgui_widgets.lua).
-- This is currently only used in main code `ImGui.SetCurrentContext()`
--- @param ctx ImGuiContext?
function ImGui._SetCurrentContext_Widgets(ctx)
GImGui = ctx
end
local DRAG_MOUSE_THRESHOLD_FACTOR = 0.50 -- Multiplier for the default value of io.MouseDragThreshold to make DragFloat/DragInt react faster to mouse drags
local IM_S8_MIN = -128
local IM_S8_MAX = 127
local IM_U8_MIN = 0
local IM_U8_MAX = 0xFF
local IM_S16_MIN = -32768
local IM_S16_MAX = 32767
local IM_U16_MIN = 0
local IM_U16_MAX = 0xFFFF
local IM_S32_MIN = INT_MIN
local IM_S32_MAX = INT_MAX
local IM_U32_MIN = 0
local IM_U32_MAX = UINT_MAX
----------------------------------------------------------------
-- [SECTION] TEXT
----------------------------------------------------------------
--- @param text string
--- @param text_end? int
--- @param flags? ImGuiTextFlags
function ImGui.TextEx(text, text_end, flags)
if not flags then flags = 0 end
local g = GImGui
local window = g.CurrentWindow
if window.SkipItems then
return
end
if not text or text == "" then
text = ""
text_end = 1
end
if text_end == nil then
text_end = #text + 1
end
local text_pos = ImVec2(window.DC.CursorPos.x, window.DC.CursorPos.y + window.DC.CurrLineTextBaseOffset)
local wrap_pos_x = window.DC.TextWrapPos
local wrap_enabled = (wrap_pos_x >= 0.0)
if (text_end - 1 <= 2000) or wrap_enabled then
local wrap_width = wrap_enabled and ImGui.CalcWrapWidthForPos(window.DC.CursorPos, wrap_pos_x) or 0.0
local text_size = ImGui.CalcTextSize(text, text_end, false, wrap_width)
local bb = ImRect(text_pos, text_pos + text_size)
ImGui.ItemSize(text_size, 0.0)
if not ImGui.ItemAdd(bb, 0) then
return
end
ImGui.RenderTextWrapped(bb.Min, text, text_end, wrap_width)
else
local line = 1
local line_height = ImGui.GetTextLineHeight()
local text_size = ImVec2(0, 0)
local pos = ImVec2(text_pos.x, text_pos.y)
if not g.LogEnabled then
local lines_skippable = ImFloor((window.ClipRect.Min.y - text_pos.y) / line_height)
if lines_skippable > 0 then
local lines_skipped = 0
while line < text_end and lines_skipped < lines_skippable do
local line_end = ImMemchr(text, "\n", line)
if not line_end then
line_end = text_end
end
if bit.band(flags, ImGuiTextFlags.NoWidthForLargeClippedText) == 0 then
local line_size = ImGui.CalcTextSizeEx(text, line, line_end)
text_size.x = ImMax(text_size.x, line_size.x)
end
line = line_end + 1
lines_skipped = lines_skipped + 1
end
pos.y = pos.y + lines_skipped * line_height
end
end
if line < text_end then
local line_rect = ImRect(pos, pos + ImVec2(FLT_MAX, line_height))
while line < text_end do
if ImGui.IsClippedEx(line_rect, 0) then
break
end
local line_end = ImMemchr(text, "\n", line)
if not line_end then
line_end = text_end
end
local line_size = ImGui.CalcTextSizeEx(text, line, line_end)
text_size.x = ImMax(text_size.x, line_size.x)
ImGui.RenderText(pos, text, line, nil, false)
line = line_end + 1
line_rect.Min.y = line_rect.Min.y + line_height
line_rect.Max.y = line_rect.Max.y + line_height
pos.y = pos.y + line_height
end
local lines_skipped = 0
while line < text_end do
local line_end = ImMemchr(text, "\n", line)
if not line_end then
line_end = text_end
end
if bit.band(flags, ImGuiTextFlags.NoWidthForLargeClippedText) == 0 then
local line_size = ImGui.CalcTextSizeEx(text, line, line_end)
text_size.x = ImMax(text_size.x, line_size.x)
end
line = line_end + 1
lines_skipped = lines_skipped + 1
end
pos.y = pos.y + lines_skipped * line_height
end
text_size.y = pos.y - text_pos.y
local bb = ImRect(text_pos, text_pos + text_size)
ImGui.ItemSize(text_size, 0.0)
ImGui.ItemAdd(bb, 0)
end
end
--- @param text string
--- @param text_end? int
function ImGui.TextUnformatted(text, text_end)
ImGui.TextEx(text, text_end, ImGuiTextFlags.NoWidthForLargeClippedText)
end
--- @param fmt string
--- @param ... any
function ImGui.TextV(fmt, ...)
local window = ImGui.GetCurrentWindow()
if window.SkipItems then
return
end
local text = ImFormatString(fmt, ...)
ImGui.TextEx(text, nil, ImGuiTextFlags.NoWidthForLargeClippedText)
end
--- @param fmt string
--- @param ... any
function ImGui.Text(fmt, ...)
if select('#', ...) > 0 then
ImGui.TextV(fmt, ...)
else
ImGui.TextEx(fmt)
end
end
--- @param col ImVec4
--- @param fmt string
--- @param ... any
function ImGui.TextColored(col, fmt, ...)
ImGui.PushStyleColor(ImGuiCol.Text, col)
ImGui.TextV(fmt, ...)
ImGui.PopStyleColor()
end
--- @param fmt string
--- @param ... any
function ImGui.TextDisabled(fmt, ...)
local g = GImGui
ImGui.PushStyleColor(ImGuiCol.Text, g.Style.Colors[ImGuiCol.TextDisabled])
ImGui.TextV(fmt, ...)
ImGui.PopStyleColor()
end
--- @param fmt string
--- @param ... any
function ImGui.TextWrapped(fmt, ...)
local g = GImGui
local need_backup = (g.CurrentWindow.DC.TextWrapPos < 0.0)
if need_backup then
ImGui.PushTextWrapPos(0.0)
end
ImGui.TextV(fmt, ...)
if need_backup then
ImGui.PopTextWrapPos()
end
end
-- align_x: 0.0f = left, 0.5f = center, 1.0f = right.
-- size_x : 0.0f = shortcut for GetContentRegionAvail().x
-- FIXME-WIP: Works but API is likely to be reworked. This is designed for 1 item on the line. (#7024)
--- @param align_x float
--- @param size_x float
--- @param fmt string
--- @param ... any
function ImGui.TextAligned(align_x, size_x, fmt, ...)
local window = ImGui.GetCurrentWindow()
if window.SkipItems then
return
end
local text = ImFormatString(fmt, ...)
local text_end = #text + 1
local text_size = ImGui.CalcTextSize(text, text_end)
size_x = ImGui.CalcItemSize(ImVec2(size_x, 0.0), 0.0, text_size.y).x
local pos = ImVec2(window.DC.CursorPos.x, window.DC.CursorPos.y + window.DC.CurrLineTextBaseOffset)
local pos_max = ImVec2(pos.x + size_x, window.ClipRect.Max.y)
local size = ImVec2(ImMin(size_x, text_size.x), text_size.y)
window.DC.CursorMaxPos.x = ImMax(window.DC.CursorMaxPos.x, pos.x + text_size.x)
window.DC.IdealMaxPos.x = ImMax(window.DC.IdealMaxPos.x, pos.x + text_size.x)
if align_x > 0.0 and text_size.x < size_x then
pos.x = pos.x + ImTrunc((size_x - text_size.x) * align_x)
end
ImGui.RenderTextEllipsis(window.DrawList, pos, pos_max, pos_max.x, text, text_end, text_size)
local backup_max_pos = ImVec2()
ImVec2_Copy(backup_max_pos, window.DC.CursorMaxPos)
ImGui.ItemSize(size)
ImGui.ItemAdd(ImRect(pos, pos + size), 0)
window.DC.CursorMaxPos.x = backup_max_pos.x -- Cancel out extending content size because right-aligned text would otherwise mess it up
if size_x < text_size.x and ImGui.IsItemHovered(bit.bor(ImGuiHoveredFlags.NoNavOverride, ImGuiHoveredFlags.AllowWhenDisabled, ImGuiHoveredFlags.ForTooltip)) then
ImGui.SetTooltip("%.*s", text_end - 1, text)
end
end
-- Add a label+text combo aligned to other label+value widgets
--- @param label string
--- @param fmt string
--- @param ... any
function ImGui.LabelText(label, fmt, ...)
local window = ImGui.GetCurrentWindow()
if window.SkipItems then
return
end
local g = GImGui
local style = g.Style
local w = ImGui.CalcItemWidth()
local value_text = ImFormatString(fmt, ...)
local value_text_end = #value_text + 1
local value_size = ImGui.CalcTextSize(value_text, value_text_end, false)
local label_end = ImGui.FindRenderedTextEnd(label)
local label_size = ImGui.CalcTextSize(label, label_end, false)
local pos = ImVec2()
ImVec2_Copy(pos, window.DC.CursorPos)
local value_bb = ImRect(pos, pos + ImVec2(w, value_size.y + style.FramePadding.y * 2))
local total_bb = ImRect(pos, pos + ImVec2(w + ((label_size.x > 0.0) and (style.ItemInnerSpacing.x + label_size.x) or 0.0), ImMax(value_size.y, label_size.y) + style.FramePadding.y * 2))
ImGui.ItemSize(total_bb, style.FramePadding.y)
if not ImGui.ItemAdd(total_bb, 0) then
return
end
-- Render
ImGui.RenderTextClipped(value_bb.Min + style.FramePadding, value_bb.Max, value_text, value_text_end, value_size, ImVec2(0.0, 0.0))
if label_size.x > 0.0 then
ImGui.RenderText(ImVec2(value_bb.Max.x + style.ItemInnerSpacing.x, value_bb.Min.y + style.FramePadding.y), label, 1, label_end, false)
end
end
--- @param fmt string
--- @param ... any
function ImGui.BulletText(fmt, ...)
local window = ImGui.GetCurrentWindow()
if window.SkipItems then
return
end
local g = GImGui
local style = g.Style
local text = ImFormatString(fmt, ...)
local text_end = #text + 1
local label_size = ImGui.CalcTextSize(text, text_end, false)
local total_size = ImVec2(g.FontSize + ((label_size.x > 0.0) and (label_size.x + style.FramePadding.x * 2) or 0.0), label_size.y) -- Empty text doesn't add padding
local pos = ImVec2()
ImVec2_Copy(pos, window.DC.CursorPos)
pos.y = pos.y + window.DC.CurrLineTextBaseOffset
ImGui.ItemSize(total_size, 0.0)
local bb = ImRect(pos, pos + total_size)
if not ImGui.ItemAdd(bb, 0) then
return
end
-- Render
local text_col = ImGui.GetColorU32(ImGuiCol.Text)
ImGui.RenderBullet(window.DrawList, bb.Min + ImVec2(style.FramePadding.x + g.FontSize * 0.5, g.FontSize * 0.5), text_col)
ImGui.RenderText(bb.Min + ImVec2(g.FontSize + style.FramePadding.x * 2, 0.0), text, 1, text_end, false)
end
----------------------------------------------------------------
-- [SECTION] MAIN: BUTTONS, SCROLLBARS, ...
----------------------------------------------------------------
--- @param bb ImRect
--- @param id ImGuiID
--- @param flags? ImGuiButtonFlags
function ImGui.ButtonBehavior(bb, id, flags)
if flags == nil then flags = 0 end
local g = GImGui
local window = g.CurrentWindow
local item_flags = (g.LastItemData.ID == id) and g.LastItemData.ItemFlags or g.CurrentItemFlags
if bit.band(flags, ImGuiButtonFlags.AllowOverlap) ~= 0 then
item_flags = bit.bor(item_flags, ImGuiItemFlags.AllowOverlap)
end
if bit.band(item_flags, ImGuiItemFlags.NoFocus) ~= 0 then
flags = bit.bor(flags, ImGuiButtonFlags.NoFocus, ImGuiButtonFlags.NoNavFocus)
end
-- Default only reacts to left mouse button
if bit.band(flags, ImGuiButtonFlags.MouseButtonMask_) == 0 then
flags = bit.bor(flags, ImGuiButtonFlags.MouseButtonLeft)
end
-- Default behavior requires click + release inside bounding box
if bit.band(flags, ImGuiButtonFlags.PressedOnMask_) == 0 then
flags = bit.bor(flags, (bit.band(item_flags, ImGuiItemFlags.ButtonRepeat) ~= 0) and ImGuiButtonFlags.PressedOnClick or ImGuiButtonFlags.PressedOnDefault_)
end
local backup_hovered_window = g.HoveredWindow
local flatten_hovered_children = (bit.band(flags, ImGuiButtonFlags.FlattenChildren) ~= 0) and g.HoveredWindow and g.HoveredWindow.RootWindowDockTree == window.RootWindowDockTree
if flatten_hovered_children then
g.HoveredWindow = window
end
local pressed = false
local hovered = ImGui.ItemHoverable(bb, id, item_flags)
if g.DragDropActive then
if (bit.band(flags, ImGuiButtonFlags.PressedOnDragDropHold) ~= 0) and (bit.band(g.DragDropSourceFlags, ImGuiDragDropFlags.SourceNoHoldToOpenOthers) == 0) and ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenBlockedByActiveItem) then
hovered = true
ImGui.SetHoveredID(id)
if (g.HoveredIdTimer - g.IO.DeltaTime <= DRAGDROP_HOLD_TO_OPEN_TIMER) and (g.HoveredIdTimer >= DRAGDROP_HOLD_TO_OPEN_TIMER) then
pressed = true
g.DragDropHoldJustPressedId = id
ImGui.FocusWindow(window)
end
end
if (g.DragDropAcceptIdPrev == id) and (bit.band(g.DragDropAcceptFlagsPrev, ImGuiDragDropFlags.AcceptDrawAsHovered) ~= 0) then
hovered = true
end
end
if (flatten_hovered_children) then
g.HoveredWindow = backup_hovered_window
end
local test_owner_id = (bit.band(flags, ImGuiButtonFlags.NoTestKeyOwner) ~= 0) and ImGuiKeyOwner_Any or id
if hovered then
IM_ASSERT(id ~= 0)
local mouse_button_clicked = -1
local mouse_button_released = -1
for button = 0, 2 do
if bit.band(flags, bit.lshift(ImGuiButtonFlags.MouseButtonLeft, button)) ~= 0 then -- Handle ImGuiButtonFlags.MouseButtonRight and ImGuiButtonFlags.MouseButtonMiddle here.
if (ImGui.IsMouseClickedEx(button, ImGuiInputFlags.None, test_owner_id) and mouse_button_clicked == -1) then mouse_button_clicked = button end
if (ImGui.IsMouseReleased(button, test_owner_id) and mouse_button_released == -1) then mouse_button_released = button end
end
end
local mods_ok = (bit.band(flags, ImGuiButtonFlags.NoKeyModsAllowed) == 0) or (not g.IO.KeyCtrl and not g.IO.KeyShift and not g.IO.KeyAlt)
if mods_ok then
if mouse_button_clicked ~= -1 and g.ActiveId ~= id then
--- @cast mouse_button_clicked ImGuiMouseButton
if bit.band(flags, ImGuiButtonFlags.NoSetKeyOwner) == 0 then
ImGui.SetKeyOwner(ImGui.MouseButtonToKey(mouse_button_clicked), id)
end
if bit.band(flags, bit.bor(ImGuiButtonFlags.PressedOnClickRelease, ImGuiButtonFlags.PressedOnClickReleaseAnywhere)) ~= 0 then
ImGui.SetActiveID(id, window)
g.ActiveIdMouseButton = mouse_button_clicked
if bit.band(flags, ImGuiButtonFlags.NoNavFocus) == 0 then
ImGui.SetFocusID(id, window)
ImGui.FocusWindow(window)
elseif bit.band(flags, ImGuiButtonFlags.NoFocus) == 0 then
ImGui.FocusWindow(window, ImGuiFocusRequestFlags.RestoreFocusedChild)
end
end
if (bit.band(flags, ImGuiButtonFlags.PressedOnClick) ~= 0) or ((bit.band(flags, ImGuiButtonFlags.PressedOnDoubleClick) ~= 0) and g.IO.MouseClickedCount[mouse_button_clicked] == 2) then
pressed = true
if bit.band(flags, ImGuiButtonFlags.NoHoldingActiveId) ~= 0 then
ImGui.ClearActiveID()
else
ImGui.SetActiveID(id, window)
end
g.ActiveIdMouseButton = mouse_button_clicked
if bit.band(flags, ImGuiButtonFlags.NoNavFocus) == 0 then
ImGui.SetFocusID(id, window)
ImGui.FocusWindow(window)
elseif bit.band(flags, ImGuiButtonFlags.NoFocus) == 0 then
ImGui.FocusWindow(window, ImGuiFocusRequestFlags.RestoreFocusedChild)
end
end
if bit.band(flags, ImGuiButtonFlags.PressedOnRelease) ~= 0 then
-- FIXME: Traditionally ImGuiButtonFlags.PressedOnRelease never took ActiveId. Adding it in 2026-03-20 since ImGuiButtonFlags_NoHoldingActiveId can always be added.
-- We don't yet perform an explicit ClearActiveID() to reduce scope of change, but this possibility could be investigated.
if bit.band(flags, ImGuiButtonFlags.NoHoldingActiveId) == 0 then
ImGui.SetActiveID(id, window) -- Hold on ID
end
g.ActiveIdMouseButton = mouse_button_clicked
end
end
if bit.band(flags, ImGuiButtonFlags.PressedOnRelease) ~= 0 then
if mouse_button_released ~= -1 then
local has_repeated_at_least_once = (bit.band(item_flags, ImGuiItemFlags.ButtonRepeat) ~= 0) and g.IO.MouseDownDurationPrev[mouse_button_released] >= g.IO.KeyRepeatDelay
if not has_repeated_at_least_once then
pressed = true
end
if bit.band(flags, ImGuiButtonFlags.NoNavFocus) == 0 then
ImGui.SetFocusID(id, window) -- FIXME: Lack of FocusWindow() call here is inconsistent with other paths. Research why.
end
ImGui.ClearActiveID()
end
end
-- 'Repeat' mode acts when held regardless of _PressedOn flags (see table above).
-- Relies on repeat logic of IsMouseClicked() but we may as well do it ourselves if we end up exposing finer RepeatDelay/RepeatRate settings.
if g.ActiveId == id and (bit.band(item_flags, ImGuiItemFlags.ButtonRepeat) ~= 0) then
if g.IO.MouseDownDuration[g.ActiveIdMouseButton] > 0.0 and ImGui.IsMouseClickedEx(g.ActiveIdMouseButton, ImGuiInputFlags.Repeat, test_owner_id) then
pressed = true
end
end
end
if pressed and g.IO.ConfigNavCursorVisibleAuto then
g.NavCursorVisible = false
end
end
-- Keyboard/Gamepad navigation handling
-- We report navigated and navigation-activated items as hovered but we don't set g.HoveredId to not interfere with mouse
if bit.band(item_flags, ImGuiItemFlags.Disabled) == 0 then
if g.NavId == id and g.NavCursorVisible and g.NavHighlightItemUnderNav then
if bit.band(flags, ImGuiButtonFlags.NoHoveredOnFocus) == 0 then
hovered = true
end
end
if g.NavActivateDownId == id then
local nav_activated_by_code = (g.NavActivateId == id)
local nav_activated_by_inputs = (g.NavActivatePressedId == id)
if not nav_activated_by_inputs and bit.band(item_flags, ImGuiItemFlags.ButtonRepeat) ~= 0 then
-- Avoid pressing multiple keys from triggering excessive amount of repeat events
local key1 = ImGui.GetKeyData(g, ImGuiKey.Space)
local key2 = ImGui.GetKeyData(g, ImGuiKey.Enter)
local key3 = ImGui.GetKeyData(g, ImGuiKey.NavGamepadActivate)
local t1 = ImMax(ImMax(key1.DownDuration, key2.DownDuration), key3.DownDuration)
nav_activated_by_inputs = ImGui.CalcTypematicRepeatAmount(t1 - g.IO.DeltaTime, t1, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate) > 0
end
if nav_activated_by_code or nav_activated_by_inputs then
-- Set active id so it can be queried by user via IsItemActive(), equivalent of holding the mouse button.
pressed = true
ImGui.SetActiveID(id, window)
g.ActiveIdSource = g.NavInputSource
if bit.band(flags, ImGuiButtonFlags.NoNavFocus) == 0 and bit.band(g.NavActivateFlags, ImGuiActivateFlags.FromShortcut) == 0 then
ImGui.SetFocusID(id, window)
end
if bit.band(g.NavActivateFlags, ImGuiActivateFlags.FromShortcut) ~= 0 then
g.ActiveIdFromShortcut = true
end
end
end
end
local held = false
if g.ActiveId == id then
if g.ActiveIdSource == ImGuiInputSource.Mouse then
if g.ActiveIdIsJustActivated then
g.ActiveIdClickOffset = g.IO.MousePos - bb.Min
end
local mouse_button = g.ActiveIdMouseButton
if mouse_button == -1 then
-- Fallback for the rare situation were g.ActiveId was set programmatically or from another widget (e.g. #6304).
ImGui.ClearActiveID()
elseif ImGui.IsMouseDown(mouse_button, test_owner_id) then
held = true
else
local release_in = hovered and (bit.band(flags, ImGuiButtonFlags.PressedOnClickRelease) ~= 0)
local release_anywhere = (bit.band(flags, ImGuiButtonFlags.PressedOnClickReleaseAnywhere) ~= 0)
if (release_in or release_anywhere) and not g.DragDropActive then
-- Report as pressed when releasing the mouse (this is the most common path)
local is_double_click_release = (bit.band(flags, ImGuiButtonFlags.PressedOnDoubleClick) ~= 0) and g.IO.MouseReleased[mouse_button] and g.IO.MouseClickedLastCount[mouse_button] == 2
local is_repeating_already = (bit.band(item_flags, ImGuiItemFlags.ButtonRepeat) ~= 0) and g.IO.MouseDownDurationPrev[mouse_button] >= g.IO.KeyRepeatDelay
local is_button_avail_or_owned = ImGui.TestKeyOwner(ImGui.MouseButtonToKey(mouse_button), test_owner_id)
if not is_double_click_release and not is_repeating_already and is_button_avail_or_owned then
pressed = true
end
end
ImGui.ClearActiveID()
end
if bit.band(flags, ImGuiButtonFlags.NoNavFocus) == 0 and g.IO.ConfigNavCursorVisibleAuto then
g.NavCursorVisible = false
end
elseif g.ActiveIdSource == ImGuiInputSource.Keyboard or g.ActiveIdSource == ImGuiInputSource.Gamepad then
-- When activated using Nav, we hold on the ActiveID until activation button is released
if g.NavActivateDownId == id then
held = true -- hovered == true not true as we are already likely hovered on direct activation.
else
ImGui.ClearActiveID()
end
end
if pressed then
g.ActiveIdHasBeenPressedBefore = true
end
end
if g.NavHighlightActivatedId == id and (bit.band(item_flags, ImGuiItemFlags.Disabled) == 0) then
hovered = true
end
return pressed, hovered, held
end
--- @param label string
--- @param size_arg? ImVec2
--- @param flags? ImGuiButtonFlags
--- @return bool
function ImGui.ButtonEx(label, size_arg, flags)
if size_arg == nil then size_arg = ImVec2(0, 0) end
if flags == nil then flags = 0 end
local window = ImGui.GetCurrentWindow()
if window.SkipItems then
return false
end
local g = GImGui
local style = g.Style
local id = window:GetID(label)
local label_end = ImGui.FindRenderedTextEnd(label)
local label_size = ImGui.CalcTextSize(label, label_end, false)
local pos = ImVec2() -- Don't modify the cursor!
ImVec2_Copy(pos, window.DC.CursorPos)
if bit.band(flags, ImGuiButtonFlags.AlignTextBaseLine) ~= 0 and style.FramePadding.y < window.DC.CurrLineTextBaseOffset then
pos.y = pos.y + window.DC.CurrLineTextBaseOffset - style.FramePadding.y
end
local size = ImGui.CalcItemSize(size_arg, label_size.x + style.FramePadding.x * 2.0, label_size.y + style.FramePadding.y * 2.0)
local bb = ImRect(pos, pos + size)
ImGui.ItemSize(size, style.FramePadding.y)
if not ImGui.ItemAdd(bb, id) then
return false
end
local pressed, hovered, held = ImGui.ButtonBehavior(bb, id, flags)
local col
if held and hovered then
col = ImGui.GetColorU32(ImGuiCol.ButtonActive)
elseif hovered then
col = ImGui.GetColorU32(ImGuiCol.ButtonHovered)
else
col = ImGui.GetColorU32(ImGuiCol.Button)
end
ImGui.RenderNavCursor(bb, id)
ImGui.RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding)
-- if (g.LogEnabled)
-- LogSetNextTextDecoration("[", "]");
ImGui.RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, label, label_end, label_size, style.ButtonTextAlign, bb)
-- Automatically close popups
--if (pressed && !(flags & ImGuiButtonFlags.DontClosePopups) && (window->Flags & ImGuiWindowFlags.Popup))
-- CloseCurrentPopup();
-- IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags);
return pressed
end
--- @param label string
--- @param size_arg? ImVec2
--- @return bool
function ImGui.Button(label, size_arg)
return ImGui.ButtonEx(label, size_arg, ImGuiButtonFlags.None)
end
--- @param label string
--- @return bool
function ImGui.SmallButton(label)
local g = GImGui
local backup_padding_y = g.Style.FramePadding.y
g.Style.FramePadding.y = 0.0
local pressed = ImGui.ButtonEx(label, ImVec2(0, 0), ImGuiButtonFlags.AlignTextBaseLine)
g.Style.FramePadding.y = backup_padding_y
return pressed
end
--- @param str_id string
--- @param size_arg ImVec2
--- @param flags? ImGuiButtonFlags
function ImGui.InvisibleButton(str_id, size_arg, flags)
if flags == nil then flags = 0 end
local window = ImGui.GetCurrentWindow()
if window.SkipItems then
return false
end
-- Ensure zero-size fits to contents
local size = ImGui.CalcItemSize(ImVec2(size_arg.x ~= 0.0 and size_arg.x or -FLT_MIN, size_arg.y ~= 0.0 and size_arg.y or -FLT_MIN), 0.0, 0.0)
local id = window:GetID(str_id)
local bb = ImRect(window.DC.CursorPos, window.DC.CursorPos + size)
ImGui.ItemSize(size)
local item_flags = (bit.band(flags, ImGuiButtonFlags.EnableNav) ~= 0) and ImGuiItemFlags.None or ImGuiItemFlags.NoNav
if not ImGui.ItemAdd(bb, id, nil, item_flags) then
return false
end
local pressed, hovered, held = ImGui.ButtonBehavior(bb, id, flags)
ImGui.RenderNavCursor(bb, id)
-- IMGUI_TEST_ENGINE_ITEM_INFO(id, str_id, g.LastItemData.StatusFlags)
return pressed
end
--- @param str_id string
--- @param dir ImGuiDir
--- @param size ImVec2
--- @param flags? ImGuiButtonFlags
function ImGui.ArrowButtonEx(str_id, dir, size, flags)
if flags == nil then flags = 0 end
local window = ImGui.GetCurrentWindow()
if window.SkipItems then
return false
end
local g = GImGui
local id = window:GetID(str_id)
local bb = ImRect(window.DC.CursorPos, window.DC.CursorPos + size)
local default_size = ImGui.GetFrameHeight()
ImGui.ItemSize(size, (size.y >= default_size) and g.Style.FramePadding.y or -1.0)
if not ImGui.ItemAdd(bb, id) then
return false
end
local pressed, hovered, held = ImGui.ButtonBehavior(bb, id, flags)
-- Render
local bg_col = ImGui.GetColorU32((held and hovered) and ImGuiCol.ButtonActive or hovered and ImGuiCol.ButtonHovered or ImGuiCol.Button)
local text_col = ImGui.GetColorU32(ImGuiCol.Text)
ImGui.RenderNavCursor(bb, id)
ImGui.RenderFrame(bb.Min, bb.Max, bg_col, true, g.Style.FrameRounding)
ImGui.RenderArrow(window.DrawList, bb.Min + ImVec2(ImMax(0.0, (size.x - g.FontSize) * 0.5), ImMax(0.0, (size.y - g.FontSize) * 0.5)), text_col, dir)
-- IMGUI_TEST_ENGINE_ITEM_INFO(id, str_id, g.LastItemData.StatusFlags)
return pressed
end
--- @param str_id string
--- @param dir ImGuiDir
function ImGui.ArrowButton(str_id, dir)
local sz = ImGui.GetFrameHeight()
return ImGui.ArrowButtonEx(str_id, dir, ImVec2(sz, sz), ImGuiButtonFlags.None)
end
--- @param id ImGuiID
--- @param pos ImVec2
--- @return bool
function ImGui.CloseButton(id, pos)
local g = GImGui
local window = g.CurrentWindow
local bb = ImRect(pos, pos + ImVec2(g.FontSize, g.FontSize))
local bb_interact = ImRect()
ImRect_Copy(bb_interact, bb)
local area_to_visible_ratio = window.OuterRectClipped:GetArea() / bb:GetArea()
if area_to_visible_ratio < 1.5 then
bb_interact:ExpandV2(ImTruncV2(bb_interact:GetSize() * -0.25))
end
local is_clipped = not ImGui.ItemAdd(bb_interact, id)
local pressed, hovered, held = ImGui.ButtonBehavior(bb, id)
if is_clipped then
return pressed
end
local bg_col
if held then
bg_col = ImGui.GetColorU32(ImGuiCol.ButtonActive)
else
bg_col = ImGui.GetColorU32(ImGuiCol.ButtonHovered)
end
if hovered then
window.DrawList:AddRectFilled(bb.Min, bb.Max, bg_col)
end
local cross_center = bb:GetCenter() - ImVec2(0.5, 0.5)
local cross_extent = g.FontSize * 0.5 * 0.7071 - 1
local cross_col = ImGui.GetColorU32(ImGuiCol.Text)
local cross_thickness = 1.0 -- FIXME-DPI
window.DrawList:AddLine(cross_center + ImVec2(cross_extent, cross_extent), cross_center + ImVec2(-cross_extent, -cross_extent), cross_col, cross_thickness)
window.DrawList:AddLine(cross_center + ImVec2(cross_extent, -cross_extent), cross_center + ImVec2(-cross_extent, cross_extent), cross_col, cross_thickness)
return pressed
end
--- @return bool
function ImGui.CollapseButton(id, pos)
local g = GImGui
local window = g.CurrentWindow
local bb = ImRect(pos, pos + ImVec2(g.FontSize, g.FontSize))
local is_clipped = not ImGui.ItemAdd(bb, id)
local pressed, hovered = ImGui.ButtonBehavior(bb, id)
if hovered then
window.DrawList:AddRectFilled(bb.Min, bb.Max, ImGui.GetColorU32(ImGuiCol.ButtonHovered))
end
if window.Collapsed then
ImGui.RenderArrow(window.DrawList, bb.Min, ImGui.GetColorU32(ImGuiCol.Text), ImGuiDir.Right, 1)
else
ImGui.RenderArrow(window.DrawList, bb.Min, ImGui.GetColorU32(ImGuiCol.Text), ImGuiDir.Down, 1)
end
return pressed
end
--- @param window ImGuiWindow
--- @param axis ImGuiAxis
--- @return ImGuiID
function ImGui.GetWindowScrollbarID(window, axis)
if axis == ImGuiAxis.X then
return window:GetID("#SCROLLX")
else
return window:GetID("#SCROLLY")
end
end
--- @param window ImGuiWindow
--- @param axis ImGuiAxis
--- @return ImRect
--- @nodiscard
function ImGui.GetWindowScrollbarRect(window, axis)
local g = GImGui
local outer_rect = window:Rect()
local inner_rect = window.InnerRect
-- (ScrollbarSizes.x = width of Y scrollbar; ScrollbarSizes.y = height of X scrollbar)
local scrollbar_size = window.ScrollbarSizes[axis == ImGuiAxis.X and ImGuiAxis.Y or ImGuiAxis.X]
IM_ASSERT(scrollbar_size >= 0.0)
local border_size = IM_ROUND(window.WindowBorderSize * 0.5)
local border_top = (bit.band(window.Flags, ImGuiWindowFlags.MenuBar) ~= 0) and IM_ROUND(g.Style.FrameBorderSize * 0.5) or (bit.band(window.Flags, ImGuiWindowFlags.NoTitleBar) ~= 0 and border_size or 0)
if axis == ImGuiAxis.X then
return ImRect(inner_rect.Min.x + border_size, ImMax(outer_rect.Min.y + border_size, outer_rect.Max.y - border_size - scrollbar_size), inner_rect.Max.x - border_size, outer_rect.Max.y - border_size)
else
return ImRect(ImMax(outer_rect.Min.x, outer_rect.Max.x - border_size - scrollbar_size), inner_rect.Min.y + border_top, outer_rect.Max.x - border_size, inner_rect.Max.y - border_size)
end
end
--- @param window ImGuiWindow
--- @param bb ImRect
--- @param threshold float
--- @param axis ImGuiAxis
function ImGui.ExtendHitBoxWhenNearViewportEdge(window, bb, threshold, axis)
local window_rect = window.RootWindow:Rect()
local viewport_rect = window.Viewport:GetMainRect()
if window_rect.Min[axis] == viewport_rect.Min[axis] and bb.Min[axis] > window_rect.Min[axis] and bb.Min[axis] - threshold <= window_rect.Min[axis] then
bb.Min[axis] = window_rect.Min[axis]
end
if window_rect.Max[axis] == viewport_rect.Max[axis] and bb.Max[axis] < window_rect.Max[axis] and bb.Max[axis] + threshold >= window_rect.Max[axis] then
bb.Max[axis] = window_rect.Max[axis]
end
end
--- @param bb_frame ImRect
--- @param id ImGuiID
--- @param axis ImGuiAxis
--- @param p_scroll_v number
--- @param size_visible_v number
--- @param size_contents_v number
--- @param draw_rounding_flags ImDrawFlags
--- @return bool is_held
--- @return number scroll_v # Updated p_scroll_v
function ImGui.ScrollbarEx(bb_frame, id, axis, p_scroll_v, size_visible_v, size_contents_v, draw_rounding_flags)
local g = GImGui
local window = g.CurrentWindow
if window.SkipItems then
return false, p_scroll_v
end
local bb_frame_width = bb_frame:GetWidth()
local bb_frame_height = bb_frame:GetHeight()
if bb_frame_width <= 0.0 or bb_frame_height <= 0.0 then
return false, p_scroll_v
end
local alpha = 1.0
if axis == ImGuiAxis.Y and bb_frame_height < bb_frame_width then
alpha = ImSaturate(bb_frame_height / ImMax(bb_frame_width * 2.0, 1.0))
end
if alpha <= 0.0 then
return false, p_scroll_v
end
local style = g.Style
local allow_interaction = (alpha >= 1.0)
local bb = ImRect()
ImRect_Copy(bb, bb_frame)
local padding = IM_TRUNC(ImMin(style.ScrollbarPadding, ImMin(bb_frame_width, bb_frame_height) * 0.5))
bb:Expand(-padding)
-- V denote the main, longer axis of the scrollbar (= height for a vertical scrollbar)
local scrollbar_size_v
if axis == ImGuiAxis.X then
scrollbar_size_v = bb:GetWidth()
else
scrollbar_size_v = bb:GetHeight()
end
if scrollbar_size_v < 1.0 then
return false, p_scroll_v
end
IM_ASSERT(ImMax(size_contents_v, size_visible_v) > 0.0)
local win_size_v = ImMax(ImMax(size_contents_v, size_visible_v), 1)
local grab_h_minsize = ImMin(bb:GetSize()[axis], style.GrabMinSize)
local grab_h_pixels = ImTrunc(ImClamp(scrollbar_size_v * (size_visible_v / win_size_v), grab_h_minsize, scrollbar_size_v))
local grab_h_norm = grab_h_pixels / scrollbar_size_v
-- As a special thing, we allow scrollbar near the edge of a screen/viewport to be reachable with mouse at the extreme edge (#9276)
local bb_hit = ImRect()
ImRect_Copy(bb_hit, bb_frame)
ImGui.ExtendHitBoxWhenNearViewportEdge(window, bb_hit, g.Style.WindowBorderSize, ImGuiAxis.X + ImGuiAxis.Y - axis) -- swap axis here
ImGui.ItemAdd(bb_frame, id, nil, ImGuiItemFlags.NoNav)
local pressed, hovered, held = ImGui.ButtonBehavior(bb_hit, id, ImGuiButtonFlags.NoNavFocus)
local scroll_max = ImMax(1, size_contents_v - size_visible_v)
local scroll_ratio = ImSaturate(p_scroll_v / scroll_max)
local grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v
if held and allow_interaction and grab_h_norm < 1.0 then
local scrollbar_pos_v = bb.Min[axis]
local mouse_pos_v = g.IO.MousePos[axis]
local clicked_v_norm = ImSaturate((mouse_pos_v - scrollbar_pos_v) / scrollbar_size_v)
local held_dir
if clicked_v_norm < grab_v_norm then
held_dir = -1
elseif clicked_v_norm > grab_v_norm + grab_h_norm then
held_dir = 1
else
held_dir = 0
end
if g.ActiveIdIsJustActivated then
local scroll_to_clicked_location = (g.IO.ConfigScrollbarScrollByPage == false) or g.IO.KeyShift or held_dir == 0
if scroll_to_clicked_location then
g.ScrollbarSeekMode = 0
else
g.ScrollbarSeekMode = held_dir
end
if held_dir == 0 and not g.IO.KeyShift then
g.ScrollbarClickDeltaToGrabCenter = clicked_v_norm - grab_v_norm - grab_h_norm * 0.5
else
g.ScrollbarClickDeltaToGrabCenter = 0.0
end
end
if g.ScrollbarSeekMode == 0 then
scroll_v_norm = ImSaturate((clicked_v_norm - g.ScrollbarClickDeltaToGrabCenter - grab_h_norm * 0.5) / (1.0 - grab_h_norm))
p_scroll_v = scroll_v_norm * scroll_max
else
if ImGui.IsMouseClickedEx(ImGuiMouseButton.Left, ImGuiInputFlags.Repeat) and held_dir == g.ScrollbarSeekMode then
local page_dir
if g.ScrollbarSeekMode > 0.0 then
page_dir = 1.0
else
page_dir = -1.0
end
p_scroll_v = ImClamp(p_scroll_v + page_dir * size_visible_v, 0, scroll_max)
end
end
scroll_ratio = ImSaturate(p_scroll_v / scroll_max)
grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v
end
local bg_col = ImGui.GetColorU32(ImGuiCol.ScrollbarBg)
local grab_col
if held then
grab_col = ImGui.GetColorU32(ImGuiCol.ScrollbarGrabActive, alpha)
elseif hovered then
grab_col = ImGui.GetColorU32(ImGuiCol.ScrollbarGrabHovered, alpha)
else
grab_col = ImGui.GetColorU32(ImGuiCol.ScrollbarGrab, alpha)
end
window.DrawList:AddRectFilled(bb_frame.Min, bb_frame.Max, bg_col, window.WindowRounding, draw_rounding_flags)
local grab_rect
if axis == ImGuiAxis.X then
local x1 = ImLerp(bb.Min.x, bb.Max.x, grab_v_norm)
grab_rect = ImRect(x1, bb.Min.y, x1 + grab_h_pixels, bb.Max.y)
else
local y1 = ImLerp(bb.Min.y, bb.Max.y, grab_v_norm)
grab_rect = ImRect(bb.Min.x, y1, bb.Max.x, y1 + grab_h_pixels)
end
window.DrawList:AddRectFilled(grab_rect.Min, grab_rect.Max, grab_col, style.ScrollbarRounding)
return held, p_scroll_v
end
--- @param axis ImGuiAxis
function ImGui.Scrollbar(axis)
local g = GImGui
local window = g.CurrentWindow
local id = ImGui.GetWindowScrollbarID(window, axis)
-- Calculate scrollbar bounding box
local bb = ImGui.GetWindowScrollbarRect(window, axis)
local rounding_corners = ImGui.CalcRoundingFlagsForRectInRect(bb, window:Rect(), g.Style.WindowBorderSize)
local size_visible = window.InnerRect.Max[axis] - window.InnerRect.Min[axis]
local size_contents = window.ContentSize[axis] + window.WindowPadding[axis] * 2.0
local scroll = window.Scroll[axis]
local held
held, scroll = ImGui.ScrollbarEx(bb, id, axis, scroll, size_visible, size_contents, rounding_corners)
window.Scroll[axis] = scroll
end
-- - `uv0` and `uv1` are texture coordinates
--- @param tex_ref ImTextureRef
--- @param image_size ImVec2
--- @param uv0? ImVec2
--- @param uv1? ImVec2
--- @param bg_col? ImVec4
--- @param tint_col? ImVec4
function ImGui.ImageWithBg(tex_ref, image_size, uv0, uv1, bg_col, tint_col)
if uv0 == nil then uv0 = ImVec2(0, 0) end
if uv1 == nil then uv1 = ImVec2(1, 1) end
if bg_col == nil then bg_col = ImVec4(0, 0, 0, 0) end
if tint_col == nil then tint_col = ImVec4(1, 1, 1, 1) end
local window = ImGui.GetCurrentWindow()
if window.SkipItems then
return
end
local g = GImGui
local padding = ImVec2(g.Style.ImageBorderSize, g.Style.ImageBorderSize)
local bb = ImRect(window.DC.CursorPos, window.DC.CursorPos + image_size + padding * 2.0)
ImGui.ItemSize(bb)
if not ImGui.ItemAdd(bb, 0) then
return
end
-- Render
local rounding = g.Style.ImageRounding