-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimgui.lua
More file actions
9789 lines (8384 loc) · 403 KB
/
Copy pathimgui.lua
File metadata and controls
9789 lines (8384 loc) · 403 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
-- (Core Code)
--- Set to disable some functions, then you need to write your own
-- IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS = true // Don't implement ImStd.ImFileOpen/ImStd.ImFileClose/ImStd.ImFileRead/ImFileWrite so you can implement them yourself
-- NOTE: you must implement ImStd.ImFileOpen if you are not in GMod
-- IMGUI_DISABLE_DEBUG_TOOLS = true // Disable metrics/debugger and other debug tools: ShowMetricsWindow(), ShowDebugLogWindow() and ShowIDStackToolWindow() will be empty
--- @type ImGuiContext?
local GImGui = nil
ImGui = {}
ImStd = {} -- Contains functions that originally don't belong to cpp namespaces
--- This executes Lua script at _filename and returns the result of the script.
--- @param _filename string
--- @return any
function IM_INCLUDE(_filename) end
--- [GMod] Platform specific include function
if gmod then
IM_INCLUDE = include
end
local function LUA_TableConstructorWrapper(T, start_idx, end_idx, userdata)
local p = {}
for i = 1, start_idx - 1 do p[i] = nil end
for i = start_idx, end_idx do p[i] = T() end
return p
end
local GImAllocatorAllocFunc = LUA_TableConstructorWrapper
local GImAllocatorFreeFunc = function(p0, p1, p2) end
local GImAllocatorUserData = nil
-- `constructor` won't be called before `start_idx`
--- @param constructor function
--- @param start_idx int
--- @param end_idx int
function ImGui.MemAlloc(constructor, start_idx, end_idx)
local p = GImAllocatorAllocFunc(constructor, start_idx, end_idx, GImAllocatorUserData)
local ctx = GImGui
if ctx then
ImGui.DebugAllocHook(ctx.DebugAllocInfo, ctx.FrameCount, p, end_idx - start_idx + 1)
end
return p
end
-- This does not set `owner[field]` to nil!
--- @param owner table
--- @param field string
function ImGui.MemFree(owner, field)
if owner ~= nil then
local ctx = GImGui
if ctx then
ImGui.DebugAllocHook(ctx.DebugAllocInfo, ctx.FrameCount, owner, -1)
end
end
return GImAllocatorFreeFunc(owner, field, GImAllocatorUserData)
end
--- @param info ImGuiDebugAllocInfo
--- @param frame_count int
--- @param ptr table
--- @param size size_t
function ImGui.DebugAllocHook(info, frame_count, ptr, size)
local entry = info.LastEntriesBuf[info.LastEntriesIdx]
if entry.FrameCount ~= frame_count then
info.LastEntriesIdx = info.LastEntriesIdx % (#info.LastEntriesBuf)
entry = info.LastEntriesBuf[info.LastEntriesIdx]
entry.FrameCount = frame_count
entry.AllocCount = 0
entry.FreeCount = 0
end
if size ~= -1 then
-- print(string.format("[%05d] MemAlloc(%d) -> %p", frame_count, size, ptr))
entry.AllocCount = entry.AllocCount + 1
info.TotalAllocCount = info.TotalAllocCount + 1
else
-- print(string.format("[%05d] MemFree(%p)", frame_count, ptr))
entry.FreeCount = entry.FreeCount + 1
info.TotalFreeCount = info.TotalFreeCount + 1
end
end
IM_INCLUDE"imgui_h.lua"
IM_INCLUDE"imgui_internal.lua"
IM_INCLUDE"imgui_draw.lua"
IM_INCLUDE"imgui_widgets.lua"
IM_INCLUDE"imgui_tables.lua"
local IMGUI_DEBUG_NAV_SCORING = false
local IMGUI_DEBUG_NAV_RECTS = false
local FONT_DEFAULT_SIZE_BASE = 20
local WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER = 0.04
local WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER = 0.70
local TOOLTIP_DEFAULT_OFFSET_MOUSE = ImVec2(16, 10) -- Multiplied by g.Style.MouseCursorScale
local TOOLTIP_DEFAULT_OFFSET_TOUCH = ImVec2(0, -20) -- Multiplied by g.Style.MouseCursorScale
local TOOLTIP_DEFAULT_PIVOT_TOUCH = ImVec2(0.5, 1.0) -- Multiplied by g.Style.MouseCursorScale
local IMGUI_VIEWPORT_DEFAULT_ID = 0x11111111
local string = string
ImFormatString = string.format -- TODO: an simplified version that operates on byte tables directly?
--- @module "imstd_minstdio"
ImStd.sscanf = IM_INCLUDE"imstd_minstdio.lua"
local math = math
local bit = bit
---------------------------------------------------------------------------------------
-- [SECTION] MISC HELPERS/UTILITIES (File functions)
---------------------------------------------------------------------------------------
--- This closes the _file.
--- @param _file any # File object
function IM_FILE_CLOSE(_file) end
--- This returns the size of _file in bytes.
--- @param _file any # File object
--- @return int
function IM_FILE_SIZE(_file) return -1 end
--- This writes _str into _file.
--- @param _file any # File object
--- @param _str string
function IM_FILE_WRITE(_file, _str) end
--- This reads the specified _count of chars and returns them as a binary string.
--- @param _file any # File object
--- @param _count int
--- @return string
function IM_FILE_READ(_file, _count) return "" end
--- This opens the file at _filename in _mode and returns the File object.
--- @param _filename string
--- @param _mode string # e.g. "rb"
--- @return any
function ImStd.ImFileOpen(_filename, _mode) end
--- [GMod] Platform specific
if gmod then
IM_FILE_CLOSE = FindMetaTable("File").Close --- @type function
IM_FILE_SIZE = FindMetaTable("File").Size --- @type function
IM_FILE_WRITE = FindMetaTable("File").Write --- @type function
IM_FILE_READ = FindMetaTable("File").Read --- @type function
end
if not IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS then
--- [GMod] Another Platform specific
if gmod then
function ImStd.ImFileOpen(filename, mode) return file.Open(filename, mode, "GAME") end
end
function ImStd.ImFileClose(f) IM_FILE_CLOSE(f) end
function ImStd.ImFileGetSize(f) return IM_FILE_SIZE(f) end
--- @param f any # File object
--- @param data table # 1-based table to store result data
--- @param count int # Amount of bytes to read from `f`
function ImStd.ImFileRead(f, data, count)
local CHUNK_SIZE = 8000 -- We don't read byte by byte
local offset = 0
while offset < count do
local read_size = math.min(CHUNK_SIZE, count - offset)
local str = IM_FILE_READ(f, read_size)
local bytes = {string.byte(str, 1, read_size)}
for i = 1, read_size do
data[offset + i] = bytes[i]
end
offset = offset + read_size
end
end
--- @param filename string
--- @param mode string
--- @return table?, integer?
function ImStd.ImFileLoadToMemory(filename, mode)
local f = ImStd.ImFileOpen(filename, mode)
if not f then return end
local file_size = ImStd.ImFileGetSize(f)
if file_size <= 0 then
ImStd.ImFileClose(f)
return
end
local file_data = {}
ImStd.ImFileRead(f, file_data, file_size)
if #file_data == 0 then
ImStd.ImFileClose(f)
return
end
ImStd.ImFileClose(f)
return file_data, file_size
end
end
--- Forward Declarations
local CalcNextScrollFromScrollTargetAndClamp
local MT = ImGui.GetMetatables()
--- @param scale_factor float
function MT.ImGuiStyle:ScaleAllSizes(scale_factor)
self._MainScale = self._MainScale * scale_factor
self.WindowPadding = ImTruncV2(self.WindowPadding * scale_factor)
self.WindowRounding = ImTrunc(self.WindowRounding * scale_factor)
self.WindowBorderSize = ImTrunc(self.WindowBorderSize * scale_factor)
self.WindowMinSize = ImTruncV2(self.WindowMinSize * scale_factor)
self.WindowBorderHoverPadding = ImTrunc(self.WindowBorderHoverPadding * scale_factor)
self.ChildRounding = ImTrunc(self.ChildRounding * scale_factor)
self.ChildBorderSize = ImTrunc(self.ChildBorderSize * scale_factor)
self.PopupRounding = ImTrunc(self.PopupRounding * scale_factor)
self.PopupBorderSize = ImTrunc(self.PopupBorderSize * scale_factor)
self.FramePadding = ImTruncV2(self.FramePadding * scale_factor)
self.FrameBorderSize = ImTrunc(self.FrameBorderSize * scale_factor)
self.FrameRounding = ImTrunc(self.FrameRounding * scale_factor)
self.ItemSpacing = ImTruncV2(self.ItemSpacing * scale_factor)
self.ItemInnerSpacing = ImTruncV2(self.ItemInnerSpacing * scale_factor)
self.CellPadding = ImTruncV2(self.CellPadding * scale_factor)
self.TouchExtraPadding = ImTruncV2(self.TouchExtraPadding * scale_factor)
self.IndentSpacing = ImTrunc(self.IndentSpacing * scale_factor)
self.ColumnsMinSpacing = ImTrunc(self.ColumnsMinSpacing * scale_factor)
self.ScrollbarSize = ImTrunc(self.ScrollbarSize * scale_factor)
self.ScrollbarRounding = ImTrunc(self.ScrollbarRounding * scale_factor)
self.ScrollbarPadding = ImTrunc(self.ScrollbarPadding * scale_factor)
self.GrabMinSize = ImTrunc(self.GrabMinSize * scale_factor)
self.GrabRounding = ImTrunc(self.GrabRounding * scale_factor)
self.LogSliderDeadzone = ImTrunc(self.LogSliderDeadzone * scale_factor)
self.ImageRounding = ImTrunc(self.ImageRounding * scale_factor)
self.ImageBorderSize = ImTrunc(self.ImageBorderSize * scale_factor)
self.TabRounding = ImTrunc(self.TabRounding * scale_factor)
self.TabBorderSize = ImTrunc(self.TabBorderSize * scale_factor)
self.TabMinWidthBase = ImTrunc(self.TabMinWidthBase * scale_factor)
self.TabMinWidthShrink = ImTrunc(self.TabMinWidthShrink * scale_factor)
self.TabCloseButtonMinWidthSelected = (self.TabCloseButtonMinWidthSelected > 0.0 and self.TabCloseButtonMinWidthSelected ~= FLT_MAX) and ImTrunc(self.TabCloseButtonMinWidthSelected * scale_factor) or self.TabCloseButtonMinWidthSelected
self.TabCloseButtonMinWidthUnselected = (self.TabCloseButtonMinWidthUnselected > 0.0 and self.TabCloseButtonMinWidthUnselected ~= FLT_MAX) and ImTrunc(self.TabCloseButtonMinWidthUnselected * scale_factor) or self.TabCloseButtonMinWidthUnselected
self.TabBarBorderSize = ImTrunc(self.TabBarBorderSize * scale_factor)
self.TabBarOverlineSize = ImTrunc(self.TabBarOverlineSize * scale_factor)
self.TreeLinesSize = ImTrunc(self.TreeLinesSize * scale_factor)
self.TreeLinesRounding = ImTrunc(self.TreeLinesRounding * scale_factor)
self.DragDropTargetRounding = ImTrunc(self.DragDropTargetRounding * scale_factor)
self.DragDropTargetBorderSize = ImTrunc(self.DragDropTargetBorderSize * scale_factor)
self.DragDropTargetPadding = ImTrunc(self.DragDropTargetPadding * scale_factor)
self.ColorMarkerSize = ImTrunc(self.ColorMarkerSize * scale_factor)
self.InputTextCursorSize = ImTrunc(self.InputTextCursorSize * scale_factor)
self.SeparatorSize = ImTrunc(self.SeparatorSize * scale_factor)
self.SeparatorTextBorderSize = ImTrunc(self.SeparatorTextBorderSize * scale_factor)
self.SeparatorTextPadding = ImTruncV2(self.SeparatorTextPadding * scale_factor)
self.DockingSeparatorSize = ImTrunc(self.DockingSeparatorSize * scale_factor)
self.DisplayWindowPadding = ImTruncV2(self.DisplayWindowPadding * scale_factor)
self.DisplaySafeAreaPadding = ImTruncV2(self.DisplaySafeAreaPadding * scale_factor)
self.MouseCursorScale = ImTrunc(self.MouseCursorScale * scale_factor)
end
local ImGuiResizeGripDef = {
{CornerPosN = ImVec2(1, 1), InnerDir = ImVec2(-1, -1), AngleMin12 = 0, AngleMax12 = 3}, -- Bottom right grip
{CornerPosN = ImVec2(0, 1), InnerDir = ImVec2( 1, -1), AngleMin12 = 3, AngleMax12 = 6} -- Bottom left
}
--- [1] Left, [2] Right, [3] Up, [4] Down
local ImGuiResizeBorderDef = {
{InnerDir = ImVec2( 1, 0), SegmentN1 = ImVec2( 0, 1), SegmentN2 = ImVec2( 0, 0), OuterAngle = IM_PI * 1.00},
{InnerDir = ImVec2(-1, 0), SegmentN1 = ImVec2( 1, 0), SegmentN2 = ImVec2( 1, 1), OuterAngle = IM_PI * 0.00},
{InnerDir = ImVec2( 0, 1), SegmentN1 = ImVec2( 0, 0), SegmentN2 = ImVec2( 1, 0), OuterAngle = IM_PI * 1.50},
{InnerDir = ImVec2( 0, -1), SegmentN1 = ImVec2( 1, 1), SegmentN2 = ImVec2( 0, 1), OuterAngle = IM_PI * 0.50}
}
--- @param window ImGuiWindow
--- @param border_n int
--- @param perp_padding float
--- @param thickness float
--- @return ImRect
--- @nodiscard
local function GetResizeBorderRect(window, border_n, perp_padding, thickness)
local rect = window:Rect()
if thickness == 0.0 then
ImVec2_CopyV(rect.Max, rect.Max.x - 1, rect.Max.y - 1)
end
if border_n == ImGuiDir.Left then
return ImRect(rect.Min.x - thickness, rect.Min.y + perp_padding, rect.Min.x + thickness, rect.Max.y - perp_padding)
end
if border_n == ImGuiDir.Right then
return ImRect(rect.Max.x - thickness, rect.Min.y + perp_padding, rect.Max.x + thickness, rect.Max.y - perp_padding)
end
if border_n == ImGuiDir.Up then
return ImRect(rect.Min.x + perp_padding, rect.Min.y - thickness, rect.Max.x - perp_padding, rect.Min.y + thickness)
end
if border_n == ImGuiDir.Down then
return ImRect(rect.Min.x + perp_padding, rect.Max.y - thickness, rect.Max.x - perp_padding, rect.Max.y + thickness)
end
IM_ASSERT(false)
return ImRect()
end
--- @param data table|number
--- @param size int? # size = -1 to indicate that `data` is a single number
--- @param seed int?
--- @return int
function ImHashData(data, size, seed)
seed = seed or 0
local FNV_OFFSET_BASIS = 0x811C9DC5
local FNV_PRIME = 0x01000193
local hash = bit.bxor(FNV_OFFSET_BASIS, seed)
if size == -1 then --- @cast data number
hash = bit.bxor(hash, data)
hash = bit.band(hash * FNV_PRIME, 0xFFFFFFFF)
else
size = size or #data
for i = 1, size do
hash = bit.bxor(hash, data[i])
hash = bit.band(hash * FNV_PRIME, 0xFFFFFFFF)
end
end
return hash
end
-- Use FNV1a, as one ImGui FIXME suggested
--- @param str string
--- @param size int?
--- @param seed int?
--- @return int
function ImHashStr(str, size, seed)
if size == nil then size = #str end
if seed == nil then seed = 0 end
if str == "" or size == 0 then
return seed -- need to match cpp code edge case behavior while using FNV ourselves
end
local FNV_OFFSET_BASIS = 0x811C9DC5
local FNV_PRIME = 0x01000193
local hash = bit.bxor(FNV_OFFSET_BASIS, seed)
local i = 1
local c
while i <= size do
c = string.byte(str, i)
-- `###` to reset back to initial hash value
if c == 35 and string.byte(str, i + 1) == 35 and string.byte(str, i + 2) == 35 then
hash = bit.bxor(FNV_OFFSET_BASIS, seed)
i = i + 2
else
hash = bit.bxor(hash, c)
hash = bit.band(hash * FNV_PRIME, 0xFFFFFFFF)
end
i = i + 1
end
return hash
end
do --[[CharFromUtf8]]
local lengths = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0}
local masks = {0x00, 0x7f, 0x1f, 0x0f, 0x07}
local mins = {0x400000, 0, 0x80, 0x800, 0x10000}
local shiftc = {0, 18, 12, 6, 0}
local shifte = {0, 6, 4, 2, 0}
local s = {0, 0, 0, 0}
--- @param in_text ImString
--- @param pos int
--- @param in_text_end int
--- @return int wanted
--- @return unsigned_int out_char
function ImStd.ImTextCharFromUtf8(in_text, pos, in_text_end)
local len = lengths[bit.rshift(ImStrByte(in_text, pos), 3) + 1]
local wanted = len > 0 and len or 1
if in_text_end == nil then
in_text_end = pos + wanted
end
s[1] = (pos < in_text_end) and ImStrByte(in_text, pos) or 0
s[2] = (pos + 1 < in_text_end) and ImStrByte(in_text, pos + 1) or 0
s[3] = (pos + 2 < in_text_end) and ImStrByte(in_text, pos + 2) or 0
s[4] = (pos + 3 < in_text_end) and ImStrByte(in_text, pos + 3) or 0
local out_char
out_char = bit.lshift(bit.band(s[1], masks[len + 1]), 18)
out_char = bit.bor(out_char, bit.lshift(bit.band(s[2], 0x3f), 12))
out_char = bit.bor(out_char, bit.lshift(bit.band(s[3], 0x3f), 6))
out_char = bit.bor(out_char, bit.lshift(bit.band(s[4], 0x3f), 0))
out_char = bit.rshift(out_char, shiftc[len + 1])
local e = 0
e = bit.lshift((out_char < mins[len + 1]) and 1 or 0, 6)
e = bit.bor(e, bit.lshift((bit.rshift(out_char, 11) == 0x1b) and 1 or 0, 7))
e = bit.bor(e, bit.lshift((out_char > IM_UNICODE_CODEPOINT_MAX) and 1 or 0, 8))
e = bit.bor(e, bit.rshift(bit.band(s[2], 0xc0), 2))
e = bit.bor(e, bit.rshift(bit.band(s[3], 0xc0), 4))
e = bit.bor(e, bit.rshift(s[4], 6))
e = bit.bxor(e, 0x2a)
e = bit.rshift(e, shifte[len + 1])
if e ~= 0 then
wanted = ImMin(wanted, (s[1] ~= 0 and 1 or 0) + (s[2] ~= 0 and 1 or 0) + (s[3] ~= 0 and 1 or 0) + (s[4] ~= 0 and 1 or 0))
out_char = IM_UNICODE_CODEPOINT_INVALID
end
return wanted, out_char
end
end
--- @param buf char[]
--- @param buf_size int
--- @param c unsigned_int
local function ImTextCharToUtf8_inline(buf, buf_size, c)
if c < 0x80 then
buf[1] = c
return 1
end
if c < 0x800 then
if buf_size < 2 then
return 0
end
buf[1] = (0xc0 + bit.rshift(c, 6))
buf[2] = (0x80 + bit.band(c, 0x3f))
return 2
end
if c < 0x10000 then
if buf_size < 3 then
return 0
end
buf[1] = (0xe0 + bit.rshift(c, 12))
buf[2] = (0x80 + bit.band(bit.rshift(c, 6), 0x3f))
buf[3] = (0x80 + bit.band(c, 0x3f))
return 3
end
if c <= 0x10FFFF then
if buf_size < 4 then
return 0
end
buf[1] = (0xf0 + bit.rshift(c, 18))
buf[2] = (0x80 + bit.band(bit.rshift(c, 12), 0x3f))
buf[3] = (0x80 + bit.band(bit.rshift(c, 6), 0x3f))
buf[4] = (0x80 + bit.band(c, 0x3f))
return 4
end
-- Invalid code point, the max unicode is 0x10FFFF
return 0
end
--- @param out_buf [char, char, char, char, char]
--- @param c unsigned_int
function ImStd.ImTextCharToUtf8(out_buf, c)
local count = ImTextCharToUtf8_inline(out_buf, 5, c)
out_buf[count + 1] = 0
return count
end
--- @param in_text ImString
--- @param pos int
--- @param in_text_end int
--- @return int
function ImStd.ImTextCountUtf8BytesFromChar(in_text, pos, in_text_end)
local bytes, unused = ImStd.ImTextCharFromUtf8(in_text, pos, in_text_end)
return bytes
end
--- @param text ImString
--- @param in_text_start int
--- @param in_p int
function ImStd.ImTextFindPreviousUtf8Codepoint(text, in_text_start, in_p)
while in_p > in_text_start do
in_p = in_p - 1
if bit.band(ImStrByte(text, in_p), 0xC0) ~= 0x80 then
return in_p
end
end
return in_text_start
end
--- @param text ImString
--- @param in_text_start int
--- @param in_text_end int
--- @param in_p int
--- @return int
function ImStd.ImTextFindValidUtf8CodepointEnd(text, in_text_start, in_text_end, in_p)
if in_text_start == in_p then
return in_text_start
end
local prev = ImStd.ImTextFindPreviousUtf8Codepoint(text, in_text_start, in_p)
local prev_c_len, prev_c = ImStd.ImTextCharFromUtf8(text, prev, in_text_end)
-- Check if the previous character is valid and fits within the range
if prev_c ~= IM_UNICODE_CODEPOINT_INVALID and prev_c_len <= (in_p - prev) then
return in_p
end
return prev
end
--- @param a ImVec2
--- @param b ImVec2
--- @param p ImVec2
--- @return ImVec2
--- @nodiscard
function ImStd.ImLineClosestPoint(a, b, p)
local ap = p - a
local ab_dir = b - a
local dot = ap.x * ab_dir.x + ap.y * ab_dir.y
if dot < 0.0 then
return a
end
local ab_len_sqr = ab_dir.x * ab_dir.x + ab_dir.y * ab_dir.y
if dot > ab_len_sqr then
return b
end
return a + ab_dir * (dot / ab_len_sqr)
end
--- @param a ImVec2
--- @param b ImVec2
--- @param c ImVec2
--- @param p ImVec2
function ImStd.ImTriangleContainsPoint(a, b, c, p)
local b1 = ((p.x - b.x) * (a.y - b.y) - (p.y - b.y) * (a.x - b.x)) < 0.0
local b2 = ((p.x - c.x) * (b.y - c.y) - (p.y - c.y) * (b.x - c.x)) < 0.0
local b3 = ((p.x - a.x) * (c.y - a.y) - (p.y - a.y) * (c.x - a.x)) < 0.0
return (b1 == b2) and (b2 == b3)
end
--- @param a ImVec2
--- @param b ImVec2
--- @param c ImVec2
--- @param p ImVec2
--- @return float, float, float
function ImStd.ImTriangleBarycentricCoords(a, b, c, p)
local v0 = b - a
local v1 = c - a
local v2 = p - a
local denom = v0.x * v1.y - v1.x * v0.y
local out_v = (v2.x * v1.y - v1.x * v2.y) / denom
local out_w = (v0.x * v2.y - v2.x * v0.y) / denom
local out_u = 1.0 - out_v - out_w
return out_u, out_v, out_w
end
--- @param a ImVec2
--- @param b ImVec2
--- @param c ImVec2
--- @param p ImVec2
--- @return ImVec2
--- @nodiscard
function ImStd.ImTriangleClosestPoint(a, b, c, p)
local proj_ab = ImStd.ImLineClosestPoint(a, b, p)
local proj_bc = ImStd.ImLineClosestPoint(b, c, p)
local proj_ca = ImStd.ImLineClosestPoint(c, a, p)
local dist2_ab = ImLengthSqr(p - proj_ab)
local dist2_bc = ImLengthSqr(p - proj_bc)
local dist2_ca = ImLengthSqr(p - proj_ca)
local m = ImMin(dist2_ab, ImMin(dist2_bc, dist2_ca))
if m == dist2_ab then
return proj_ab
elseif m == dist2_bc then
return proj_bc
else
return proj_ca
end
end
--- @param dst char[]
--- @param dst_pos int
--- @param src char[]
--- @param src_pos int
--- @param count size_t
function ImStd.ImStrncpy(dst, dst_pos, src, src_pos, count)
if count < 1 then
return
end
if count > 1 then
ImStd.memmove(dst, dst_pos, src, src_pos, count - 1)
end
dst[dst_pos + (count - 1)] = 0
end
--- @param str char[]
--- @param mid_line int
--- @param begin int
function ImStd.ImStrbol(str, mid_line, begin)
IM_ASSERT_PARANOID(mid_line >= begin and mid_line <= #str)
while mid_line > begin and str[mid_line - 1] ~= 10 do
mid_line = mid_line - 1
end
return mid_line
end
--- @param buf char[]
function ImStd.ImStrTrimBlanks(buf)
local p = 1 -- buf_begin
while buf[p] == 32 or buf[p] == 9 do
p = p + 1
end
local p_start = p
while buf[p] ~= 0 do
p = p + 1
end
while p > p_start and (buf[p - 1] == 32 or buf[p - 1] == 9) do
p = p - 1
end
if p_start > 1 then
ImStd.memmove(buf, 1, buf, p_start, p - p_start)
end
buf[p - p_start + 1] = 0
end
function ImGui.UpdateCurrentFontSize(restore_font_size_after_scaling)
local g = GImGui
local window = g.CurrentWindow
g.Style.FontSizeBase = g.FontSizeBase
-- if (window ~= nil and window.SkipItems) then
-- local table = g.CurrentTable
-- if (table == nil or (table.CurrentColumn ~= -1 and table.Columns[table.CurrentColumn].IsSkipItems == false)) then
-- return
-- end
-- end
local final_size = (restore_font_size_after_scaling > 0.0) and restore_font_size_after_scaling or 0.0
if final_size == 0.0 then
final_size = g.FontSizeBase
final_size = final_size * g.Style.FontScaleMain
final_size = final_size * g.Style.FontScaleDpi
if window ~= nil then
final_size = final_size * window.FontWindowScale
end
end
final_size = ImGui.GetRoundedFontSize(final_size)
final_size = ImClamp(final_size, 4.0, IMGUI_FONT_SIZE_MAX)
if (g.Font ~= nil and bit.band(g.IO.BackendFlags, ImGuiBackendFlags.RendererHasTextures) ~= 0) then
g.Font.CurrentRasterizerDensity = g.FontRasterizerDensity
end
g.FontSize = final_size
g.FontBaked = (g.Font ~= nil and window ~= nil) and g.Font:GetFontBaked(final_size) or nil
g.FontBakedScale = (g.FontBaked ~= nil) and (g.FontSize / g.FontBaked.Size) or 0.0
g.DrawListSharedData.FontSize = g.FontSize
g.DrawListSharedData.FontScale = g.FontBakedScale
end
function ImGui.SetCurrentFont(font, font_size_before_scaling, font_size_after_scaling)
local g = GImGui
g.Font = font
g.FontSizeBase = font_size_before_scaling
ImGui.UpdateCurrentFontSize(font_size_after_scaling)
if font ~= nil then
IM_ASSERT(font and font:IsLoaded())
local atlas = font.OwnerAtlas
g.DrawListSharedData.FontAtlas = atlas
g.DrawListSharedData.Font = font
ImFontAtlasUpdateDrawListsSharedData(atlas)
if (g.CurrentWindow ~= nil) then
g.CurrentWindow.DrawList:_SetTexture(atlas.TexRef)
end
end
end
function ImGui.PushFont(font, font_size_base)
local g = GImGui
if font == nil then
font = g.Font
end
IM_ASSERT(font ~= nil)
IM_ASSERT(font_size_base >= 0.0)
g.FontStack:push_back(ImFontStackData(font, g.FontSizeBase, g.FontSize))
if font_size_base == 0.0 then
font_size_base = g.FontSizeBase
end
ImGui.SetCurrentFont(font, font_size_base, 0.0)
end
function ImGui.PopFont()
local g = GImGui
if (g.FontStack.Size <= 0) then
IM_ASSERT_USER_ERROR(0, "Calling PopFont() too many times!")
return
end
local font_stack_data = g.FontStack:back()
ImGui.SetCurrentFont(font_stack_data.Font, font_stack_data.FontSizeBeforeScaling, font_stack_data.FontSizeAfterScaling)
g.FontStack:pop_back()
end
function ImGui.UpdateTexturesNewFrame()
local g = GImGui
local has_textures = bit.band(g.IO.BackendFlags, ImGuiBackendFlags.RendererHasTextures) ~= 0
for _, atlas in g.FontAtlases:iter() do
if (atlas.OwnerContext == g) then
ImFontAtlasUpdateNewFrame(atlas, g.FrameCount, has_textures)
else
IM_ASSERT(atlas.Builder ~= nil and atlas.Builder.FrameCount ~= -1)
IM_ASSERT(atlas.RendererHasTextures == has_textures)
end
end
for _, tex in g.UserTextures:iter() do
ImTextureDataUpdateNewFrame(tex)
end
end
function ImGui.UpdateTexturesEndFrame()
local g = GImGui
g.PlatformIO.Textures:resize(0)
for _, atlas in g.FontAtlases:iter() do
for _, tex in atlas.TexList:iter() do
tex.RefCount = atlas.RefCount
g.PlatformIO.Textures:push_back(tex)
end
end
for _, tex in g.UserTextures:iter() do
g.PlatformIO.Textures:push_back(tex)
end
end
function ImGui.UpdateFontsNewFrame()
local g = GImGui
if (bit.band(g.IO.BackendFlags, ImGuiBackendFlags.RendererHasTextures) == 0) then
for _, atlas in g.FontAtlases:iter() do
atlas.Locked = true
end
end
if (g.Style._NextFrameFontSizeBase ~= 0.0) then
g.Style.FontSizeBase = g.Style._NextFrameFontSizeBase
g.Style._NextFrameFontSizeBase = 0.0
end
local font = ImGui.GetDefaultFont()
if g.Style.FontSizeBase <= 0.0 then
g.Style.FontSizeBase = ((font.LegacySize > 0.0) and font.LegacySize or FONT_DEFAULT_SIZE_BASE)
end
g.Font = font
g.FontSizeBase = g.Style.FontSizeBase
g.FontSize = 0.0
local font_stack_data = ImFontStackData(font, g.Style.FontSizeBase, g.Style.FontSizeBase)
ImGui.SetCurrentFont(font_stack_data.Font, font_stack_data.FontSizeBeforeScaling, 0.0)
g.FontStack:push_back(font_stack_data)
IM_ASSERT(g.Font:IsLoaded())
end
function ImGui.UpdateFontsEndFrame()
ImGui.PopFont()
end
--- @return ImFont
function ImGui.GetDefaultFont()
local g = GImGui
local atlas = g.IO.Fonts
if (atlas.Builder == nil or atlas.Fonts.Size == 0) then
ImFontAtlasBuildMain(atlas)
end
return g.IO.FontDefault and g.IO.FontDefault or atlas.Fonts[1]
end
--- @param atlas ImFontAtlas
function ImGui.RegisterFontAtlas(atlas)
local g = GImGui
if (g.FontAtlases.Size == 0) then
IM_ASSERT(atlas == g.IO.Fonts)
end
atlas.RefCount = atlas.RefCount + 1
g.FontAtlases:push_back(atlas)
ImFontAtlasAddDrawListSharedData(atlas, g.DrawListSharedData)
for _, tex in atlas.TexList:iter() do
tex.RefCount = atlas.RefCount
end
end
--- @param atlas ImFontAtlas
function ImGui.UnregisterFontAtlas(atlas)
local g = GImGui
IM_ASSERT(atlas.RefCount > 0)
ImFontAtlasRemoveDrawListSharedData(atlas, g.DrawListSharedData)
g.FontAtlases:find_erase(atlas)
atlas.RefCount = atlas.RefCount - 1
for _, tex in atlas.TexList:iter() do
tex.RefCount = atlas.RefCount
end
end
function ImGui.GetCurrentContext()
return GImGui
end
--- @param ctx ImGuiContext?
function ImGui.SetCurrentContext(ctx)
GImGui = ctx
ImGui._SetCurrentContext_Internal(ctx)
ImGui._SetCurrentContext_Widgets(ctx)
ImGui._SetCurrentContext_Tables(ctx)
end
--- @param alloc_func ImGuiMemAllocFunc
--- @param free_func ImGuiMemFreeFunc
--- @param user_data any
function ImGui.SetAllocatorFunctions(alloc_func, free_func, user_data)
GImAllocatorAllocFunc = alloc_func
GImAllocatorFreeFunc = free_func
GImAllocatorUserData = user_data
end
--- @param key ImGuiLocKey
--- @param text string
--- @return ImGuiLocEntry
--- @nodiscard
--- @package
local function ImGuiLocEntry(key, text)
return { Key = key, Text = text }
end
local GLocalizationEntriesEnUS = {
ImGuiLocEntry(ImGuiLocKey.VersionStr, "ImGui Sincerely WIP"),
ImGuiLocEntry(ImGuiLocKey.TableSizeOne, "Size column to fit###SizeOne"),
ImGuiLocEntry(ImGuiLocKey.TableSizeAllFit, "Size all columns to fit###SizeAll"),
ImGuiLocEntry(ImGuiLocKey.TableSizeAllDefault, "Size all columns to default###SizeAll"),
ImGuiLocEntry(ImGuiLocKey.TableReset, "Reset"),
ImGuiLocEntry(ImGuiLocKey.TableResetOrder, "Reset order###ResetOrder"),
ImGuiLocEntry(ImGuiLocKey.TableResetVisibility, "Reset visibility###ResetVisibility"),
ImGuiLocEntry(ImGuiLocKey.WindowingMainMenuBar, "(Main menu bar)"),
ImGuiLocEntry(ImGuiLocKey.WindowingPopup, "(Popup)"),
ImGuiLocEntry(ImGuiLocKey.WindowingUntitled, "(Untitled)"),
ImGuiLocEntry(ImGuiLocKey.OpenLink_s, "Open '%s'"),
ImGuiLocEntry(ImGuiLocKey.CopyLink, "Copy Link###CopyLink"),
ImGuiLocEntry(ImGuiLocKey.DockingHideTabBar, "Hide tab bar###HideTabBar"),
ImGuiLocEntry(ImGuiLocKey.DockingHoldShiftToDock, "Hold SHIFT to enable Docking window."),
ImGuiLocEntry(ImGuiLocKey.DockingDragToUndockOrMoveNode, "Click and drag to move or undock whole node."),
}
function ImGui.Initialize()
local g = GImGui
IM_ASSERT(not g.Initialized and not g.SettingsLoaded)
ImGui.LocalizeRegisterEntries(GLocalizationEntriesEnUS, #GLocalizationEntriesEnUS)
local viewport = ImGuiViewportP()
viewport.ID = IMGUI_VIEWPORT_DEFAULT_ID
viewport.Idx = 1
viewport.PlatformWindowCreated = true
viewport.Flags = ImGuiViewportFlags.OwnedByApp
g.Viewports:push_back(viewport)
g.ViewportCreatedCount = g.ViewportCreatedCount + 1
g.PlatformIO.Viewports:push_back(g.Viewports.Data[1])
local atlas = g.IO.Fonts
g.DrawListSharedData.Context = g
ImGui.RegisterFontAtlas(atlas)
g.Initialized = true
end
--- @param shared_font_atlas? ImFontAtlas
function ImGui.CreateContext(shared_font_atlas)
local prev_ctx = ImGui.GetCurrentContext()
local ctx = ImGuiContext(shared_font_atlas)
ImGui.SetCurrentContext(ctx)
ImGui.Initialize()
if prev_ctx ~= nil then
ImGui.SetCurrentContext(prev_ctx) -- Restore previous context if any, else keep new one
end
return ctx
end
--- @param ctx? ImGuiContext
function ImGui.DestroyContext(ctx)
local prev_ctx = ImGui.GetCurrentContext()
if ctx == nil then
ctx = prev_ctx
end
ImGui.SetCurrentContext(ctx)
-- TODO:
end
--- @param window ImGuiWindow
--- @return any
function ImGui.FindWindowSettingsByWindow(window)
local g = GImGui
if window.SettingsOffset ~= -1 then
return g.SettingsWindows[window.SettingsOffset + 1]
end
return ImGui.FindWindowSettingsByID(window.ID)
end
--- @param id ImGuiID
--- @return any
function ImGui.FindWindowSettingsByID(id)
local g = GImGui
for _, settings in g.SettingsWindows:iter() do
if settings.ID == id and not settings.WantDelete then
return settings
end
end
return nil
end
--- @param window ImGuiWindow
--- @return ImGuiWindow?
function ImGui.FindFrontMostVisibleChildWindow(window)
for n = window.DC.ChildWindows.Size, 1, -1 do
if ImGui.IsWindowActiveAndVisible(window.DC.ChildWindows.Data[n]) then
return ImGui.FindFrontMostVisibleChildWindow(window.DC.ChildWindows.Data[n])
end
end
return window
end
--- @param window ImGuiWindow
--- @param cond ImGuiCond
--- @param allow bool
local function SetWindowConditionAllowFlags(window, cond, allow)
if allow then
window.SetWindowPosAllowFlags = bit.bor(window.SetWindowPosAllowFlags, cond)
window.SetWindowSizeAllowFlags = bit.bor(window.SetWindowSizeAllowFlags, cond)
window.SetWindowCollapsedAllowFlags = bit.bor(window.SetWindowCollapsedAllowFlags, cond)
else
window.SetWindowPosAllowFlags = bit.band(window.SetWindowPosAllowFlags, bit.bnot(cond))
window.SetWindowSizeAllowFlags = bit.band(window.SetWindowSizeAllowFlags, bit.bnot(cond))
window.SetWindowCollapsedAllowFlags = bit.band(window.SetWindowCollapsedAllowFlags, bit.bnot(cond))
end
end
--- @param window ImGuiWindow
--- @param settings ImGuiWindowSettings
local function ApplyWindowSettings(window, settings)
local main_viewport = ImGui.GetMainViewport()
ImVec2_Copy(window.ViewportPos, main_viewport.Pos)
if (settings.ViewportId ~= 0) then
window.ViewportId = settings.ViewportId
ImVec2_Copy(window.ViewportPos, settings.ViewportPos)
end
window.Pos = ImTruncV2(ImVec2(settings.Pos.x + window.ViewportPos.x, settings.Pos.y + window.ViewportPos.y))
if settings.Size.x > 0 and settings.Size.y > 0 then
local size = ImVec2(ImTrunc(settings.Size.x), ImTrunc(settings.Size.y))
ImVec2_Copy(window.Size, size)
ImVec2_Copy(window.SizeFull, size)
end
window.Collapsed = settings.Collapsed
end
--- @param window ImGuiWindow
--- @param settings ImGuiWindowSettings
local function InitOrLoadWindowSettings(window, settings)
-- Initial window state with e.g. default/arbitrary window position
-- Use SetNextWindowPos() with the appropriate condition flag to change the initial position of a window.
local main_viewport = ImGui.GetMainViewport()
ImVec2_Copy(window.Pos, main_viewport.Pos + ImVec2(60, 60))
ImVec2_Copy(window.Size, ImVec2(0, 0))
ImVec2_Copy(window.SizeFull, ImVec2(0, 0))
ImVec2_Copy(window.ViewportPos, main_viewport.Pos)
window.SetWindowPosAllowFlags = bit.bor(ImGuiCond.Always, ImGuiCond.Once, ImGuiCond.FirstUseEver, ImGuiCond.Appearing)
window.SetWindowSizeAllowFlags = window.SetWindowPosAllowFlags
window.SetWindowCollapsedAllowFlags = window.SetWindowPosAllowFlags