-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimgui_draw.lua
More file actions
4712 lines (4005 loc) · 186 KB
/
Copy pathimgui_draw.lua
File metadata and controls
4712 lines (4005 loc) · 186 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
-- (Draw Code)
FONT_ATLAS_DEFAULT_TEX_DATA_W = 122
FONT_ATLAS_DEFAULT_TEX_DATA_H = 27
local IM_FONTGLYPH_INDEX_UNUSED = 0xFFFF
local IM_FONTGLYPH_INDEX_NOT_FOUND = 0xFFFE
--- @param str string
--- @return table
--- @nodiscard
--- @package
local function str_to_table(str)
local t = {} for i = 1, #str do t[i] = string.sub(str, i, i) end return t
end
--- @param buf char[] # 1-based
--- @param base int # 1-based start index
--- @param u32 ImU32
local function write_u32(buf, base, u32)
buf[base + 0] = bit.band(u32, 0xFF)
buf[base + 1] = bit.band(bit.rshift(u32, 8), 0xFF)
buf[base + 2] = bit.band(bit.rshift(u32, 16), 0xFF)
buf[base + 3] = bit.band(bit.rshift(u32, 24), 0xFF)
end
--- Original ImGui pixel art!
local FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS = str_to_table(
"..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX- XX - XX XX " ..
"..- -X.....X- X.X - X.X -X.....X - X.....X- X..X -X..X X..X" ..
"--- -XXX.XXX- X...X - X...X -X....X - X....X- X..X -X...X X...X" ..
"X - X.X - X.....X - X.....X -X...X - X...X- X..X - X...X X...X " ..
"XX - X.X -X.......X- X.......X -X..X.X - X.X..X- X..X - X...X...X " ..
"X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X- X..XXX - X.....X " ..
"X..X - X.X - X.X - X.X -XX X.X - X.X XX- X..X..XXX - X...X " ..
"X...X - X.X - X.X - XX X.X XX - X.X - X.X - X..X..X..XX - X.X " ..
"X....X - X.X - X.X - X.X X.X X.X - X.X - X.X - X..X..X..X.X - X...X " ..
"X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X -XXX X..X..X..X..X- X.....X " ..
"X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X -X..XX........X..X- X...X...X " ..
"X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X -X...X...........X- X...X X...X " ..
"X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X - X..............X-X...X X...X" ..
"X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X - X.............X-X..X X..X" ..
"X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X - X.............X- XX XX " ..
"X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X - X............X--------------" ..
"X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX - X...........X - " ..
"X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------- X..........X - " ..
"X.X X..X - -X.......X- X.......X - XX XX - - X..........X - " ..
"XX X..X - - X.....X - X.....X - X.X X.X - - X........X - " ..
" X..X - - X...X - X...X - X..X X..X - - X........X - " ..
" XX - - X.X - X.X - X...XXXXXXXXXXXXX...X - - XXXXXXXXXX - " ..
"------------- - X - X -X.....................X- ------------------- " ..
" ----------------------------------- X...XXXXXXXXXXXXX...X - " ..
" - X..X X..X - " ..
" - X.X X.X - " ..
" - XX XX - "
)
local FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA = {
-- Pos .......... Size ......... Offset .......
{ ImVec2( 0, 3), ImVec2(12, 19), ImVec2( 0, 0) }, -- ImGuiMouseCursor.Arrow
{ ImVec2( 13, 0), ImVec2( 7, 16), ImVec2( 1, 8) }, -- ImGuiMouseCursor.TextInput
{ ImVec2( 31, 0), ImVec2(23, 23), ImVec2(11, 11) }, -- ImGuiMouseCursor.ResizeAll
{ ImVec2( 21, 0), ImVec2( 9, 23), ImVec2( 4, 11) }, -- ImGuiMouseCursor.ResizeNS
{ ImVec2( 55, 18), ImVec2(23, 9), ImVec2(11, 4) }, -- ImGuiMouseCursor.ResizeEW
{ ImVec2( 73, 0), ImVec2(17, 17), ImVec2( 8, 8) }, -- ImGuiMouseCursor.ResizeNESW
{ ImVec2( 55, 0), ImVec2(17, 17), ImVec2( 8, 8) }, -- ImGuiMouseCursor.ResizeNWSE
{ ImVec2( 91, 0), ImVec2(17, 22), ImVec2( 5, 0) }, -- ImGuiMouseCursor.Hand
{ ImVec2( 0, 3), ImVec2(12, 19), ImVec2( 0, 0) }, -- ImGuiMouseCursor.Wait -- Arrow + custom code in ImGui.RenderMouseCursor()
{ ImVec2( 0, 3), ImVec2(12, 19), ImVec2( 0, 0) }, -- ImGuiMouseCursor.Progress -- Arrow + custom code in ImGui.RenderMouseCursor()
{ ImVec2(109, 0), ImVec2(13, 15), ImVec2( 6, 7) }, -- ImGuiMouseCursor.NotAllowed
}
local MT = ImGui.GetMetatables()
--- @module "imstb_rectpack"
local stbrp = IM_INCLUDE("imstb_rectpack.lua")
--- @module "imstb_truetype"
local stbtt = IM_INCLUDE("imstb_truetype.lua")
---------------------------------------------------------------------------------------
-- [SECTION] STYLE FUNCTIONS
---------------------------------------------------------------------------------------
--- @param dst? ImGuiStyle
function ImGui.StyleColorsDark(dst)
local style = dst and dst or ImGui.GetStyle()
local colors = style.Colors
colors[ImGuiCol.Text] = ImVec4(1.00, 1.00, 1.00, 1.00)
colors[ImGuiCol.TextDisabled] = ImVec4(0.50, 0.50, 0.50, 1.00)
colors[ImGuiCol.WindowBg] = ImVec4(0.06, 0.06, 0.06, 0.94)
colors[ImGuiCol.ChildBg] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[ImGuiCol.PopupBg] = ImVec4(0.08, 0.08, 0.08, 0.94)
colors[ImGuiCol.Border] = ImVec4(0.43, 0.43, 0.50, 0.50)
colors[ImGuiCol.BorderShadow] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[ImGuiCol.FrameBg] = ImVec4(0.16, 0.29, 0.48, 0.54)
colors[ImGuiCol.FrameBgHovered] = ImVec4(0.26, 0.59, 0.98, 0.40)
colors[ImGuiCol.FrameBgActive] = ImVec4(0.26, 0.59, 0.98, 0.67)
colors[ImGuiCol.TitleBg] = ImVec4(0.04, 0.04, 0.04, 1.00)
colors[ImGuiCol.TitleBgActive] = ImVec4(0.16, 0.29, 0.48, 1.00)
colors[ImGuiCol.TitleBgCollapsed] = ImVec4(0.00, 0.00, 0.00, 0.51)
colors[ImGuiCol.MenuBarBg] = ImVec4(0.14, 0.14, 0.14, 1.00)
colors[ImGuiCol.ScrollbarBg] = ImVec4(0.02, 0.02, 0.02, 0.53)
colors[ImGuiCol.ScrollbarGrab] = ImVec4(0.31, 0.31, 0.31, 1.00)
colors[ImGuiCol.ScrollbarGrabHovered] = ImVec4(0.41, 0.41, 0.41, 1.00)
colors[ImGuiCol.ScrollbarGrabActive] = ImVec4(0.51, 0.51, 0.51, 1.00)
colors[ImGuiCol.CheckMark] = ImVec4(0.26, 0.59, 0.98, 1.00)
colors[ImGuiCol.CheckboxSelectedBg] = ImLerpV4V4(colors[ImGuiCol.FrameBg], colors[ImGuiCol.FrameBgHovered], 0.65)
colors[ImGuiCol.SliderGrab] = ImVec4(0.24, 0.52, 0.88, 1.00)
colors[ImGuiCol.SliderGrabActive] = ImVec4(0.26, 0.59, 0.98, 1.00)
colors[ImGuiCol.Button] = ImVec4(0.26, 0.59, 0.98, 0.40)
colors[ImGuiCol.ButtonHovered] = ImVec4(0.26, 0.59, 0.98, 1.00)
colors[ImGuiCol.ButtonActive] = ImVec4(0.06, 0.53, 0.98, 1.00)
colors[ImGuiCol.Header] = ImVec4(0.26, 0.59, 0.98, 0.31)
colors[ImGuiCol.HeaderHovered] = ImVec4(0.26, 0.59, 0.98, 0.80)
colors[ImGuiCol.HeaderActive] = ImVec4(0.26, 0.59, 0.98, 1.00)
colors[ImGuiCol.Separator] = colors[ImGuiCol.Border]
colors[ImGuiCol.SeparatorHovered] = ImVec4(0.10, 0.40, 0.75, 0.78)
colors[ImGuiCol.SeparatorActive] = ImVec4(0.10, 0.40, 0.75, 1.00)
colors[ImGuiCol.ResizeGrip] = ImVec4(0.26, 0.59, 0.98, 0.20)
colors[ImGuiCol.ResizeGripHovered] = ImVec4(0.26, 0.59, 0.98, 0.67)
colors[ImGuiCol.ResizeGripActive] = ImVec4(0.26, 0.59, 0.98, 0.95)
colors[ImGuiCol.InputTextCursor] = colors[ImGuiCol.Text]
colors[ImGuiCol.TabHovered] = colors[ImGuiCol.HeaderHovered]
colors[ImGuiCol.Tab] = ImLerpV4V4(colors[ImGuiCol.Header], colors[ImGuiCol.TitleBgActive], 0.80)
colors[ImGuiCol.TabSelected] = ImLerpV4V4(colors[ImGuiCol.HeaderActive], colors[ImGuiCol.TitleBgActive], 0.60)
colors[ImGuiCol.TabSelectedOverline] = colors[ImGuiCol.HeaderActive]
colors[ImGuiCol.TabDimmed] = ImLerpV4V4(colors[ImGuiCol.Tab], colors[ImGuiCol.TitleBg], 0.80)
colors[ImGuiCol.TabDimmedSelected] = ImLerpV4V4(colors[ImGuiCol.TabSelected], colors[ImGuiCol.TitleBg], 0.40)
colors[ImGuiCol.TabDimmedSelectedOverline] = ImVec4(0.50, 0.50, 0.50, 0.00)
colors[ImGuiCol.PlotLines] = ImVec4(0.61, 0.61, 0.61, 1.00)
colors[ImGuiCol.PlotLinesHovered] = ImVec4(1.00, 0.43, 0.35, 1.00)
colors[ImGuiCol.PlotHistogram] = ImVec4(0.90, 0.70, 0.00, 1.00)
colors[ImGuiCol.PlotHistogramHovered] = ImVec4(1.00, 0.60, 0.00, 1.00)
colors[ImGuiCol.TableHeaderBg] = ImVec4(0.19, 0.19, 0.20, 1.00)
colors[ImGuiCol.TableBorderStrong] = ImVec4(0.31, 0.31, 0.35, 1.00)
colors[ImGuiCol.TableBorderLight] = ImVec4(0.23, 0.23, 0.25, 1.00)
colors[ImGuiCol.TableRowBg] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[ImGuiCol.TableRowBgAlt] = ImVec4(1.00, 1.00, 1.00, 0.06)
colors[ImGuiCol.TextLink] = colors[ImGuiCol.HeaderActive]
colors[ImGuiCol.TextSelectedBg] = ImVec4(0.26, 0.59, 0.98, 0.35)
colors[ImGuiCol.TreeLines] = colors[ImGuiCol.Border]
colors[ImGuiCol.DragDropTarget] = ImVec4(1.00, 1.00, 0.00, 0.90)
colors[ImGuiCol.DragDropTargetBg] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[ImGuiCol.UnsavedMarker] = ImVec4(1.00, 1.00, 1.00, 1.00)
colors[ImGuiCol.NavCursor] = ImVec4(0.26, 0.59, 0.98, 1.00)
colors[ImGuiCol.NavWindowingHighlight] = ImVec4(1.00, 1.00, 1.00, 0.70)
colors[ImGuiCol.NavWindowingDimBg] = ImVec4(0.80, 0.80, 0.80, 0.20)
colors[ImGuiCol.ModalWindowDimBg] = ImVec4(0.80, 0.80, 0.80, 0.35)
end
--- @param dst? ImGuiStyle
function ImGui.StyleColorsClassic(dst)
local style = dst and dst or ImGui.GetStyle()
local colors = style.Colors
colors[ImGuiCol.Text] = ImVec4(0.90, 0.90, 0.90, 1.00)
colors[ImGuiCol.TextDisabled] = ImVec4(0.60, 0.60, 0.60, 1.00)
colors[ImGuiCol.WindowBg] = ImVec4(0.00, 0.00, 0.00, 0.85)
colors[ImGuiCol.ChildBg] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[ImGuiCol.PopupBg] = ImVec4(0.11, 0.11, 0.14, 0.92)
colors[ImGuiCol.Border] = ImVec4(0.50, 0.50, 0.50, 0.50)
colors[ImGuiCol.BorderShadow] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[ImGuiCol.FrameBg] = ImVec4(0.43, 0.43, 0.43, 0.39)
colors[ImGuiCol.FrameBgHovered] = ImVec4(0.47, 0.47, 0.69, 0.40)
colors[ImGuiCol.FrameBgActive] = ImVec4(0.42, 0.41, 0.64, 0.69)
colors[ImGuiCol.TitleBg] = ImVec4(0.27, 0.27, 0.54, 0.83)
colors[ImGuiCol.TitleBgActive] = ImVec4(0.32, 0.32, 0.63, 0.87)
colors[ImGuiCol.TitleBgCollapsed] = ImVec4(0.40, 0.40, 0.80, 0.20)
colors[ImGuiCol.MenuBarBg] = ImVec4(0.40, 0.40, 0.55, 0.80)
colors[ImGuiCol.ScrollbarBg] = ImVec4(0.20, 0.25, 0.30, 0.60)
colors[ImGuiCol.ScrollbarGrab] = ImVec4(0.40, 0.40, 0.80, 0.30)
colors[ImGuiCol.ScrollbarGrabHovered] = ImVec4(0.40, 0.40, 0.80, 0.40)
colors[ImGuiCol.ScrollbarGrabActive] = ImVec4(0.41, 0.39, 0.80, 0.60)
colors[ImGuiCol.CheckMark] = ImVec4(0.90, 0.90, 0.90, 0.50)
colors[ImGuiCol.CheckboxSelectedBg] = ImLerpV4V4(colors[ImGuiCol.FrameBg], colors[ImGuiCol.FrameBgActive], 0.65)
colors[ImGuiCol.SliderGrab] = ImVec4(1.00, 1.00, 1.00, 0.30)
colors[ImGuiCol.SliderGrabActive] = ImVec4(0.41, 0.39, 0.80, 0.60)
colors[ImGuiCol.Button] = ImVec4(0.35, 0.40, 0.61, 0.62)
colors[ImGuiCol.ButtonHovered] = ImVec4(0.40, 0.48, 0.71, 0.79)
colors[ImGuiCol.ButtonActive] = ImVec4(0.46, 0.54, 0.80, 1.00)
colors[ImGuiCol.Header] = ImVec4(0.40, 0.40, 0.90, 0.45)
colors[ImGuiCol.HeaderHovered] = ImVec4(0.45, 0.45, 0.90, 0.80)
colors[ImGuiCol.HeaderActive] = ImVec4(0.53, 0.53, 0.87, 0.80)
colors[ImGuiCol.Separator] = ImVec4(0.50, 0.50, 0.50, 0.60)
colors[ImGuiCol.SeparatorHovered] = ImVec4(0.60, 0.60, 0.70, 1.00)
colors[ImGuiCol.SeparatorActive] = ImVec4(0.70, 0.70, 0.90, 1.00)
colors[ImGuiCol.ResizeGrip] = ImVec4(1.00, 1.00, 1.00, 0.10)
colors[ImGuiCol.ResizeGripHovered] = ImVec4(0.78, 0.82, 1.00, 0.60)
colors[ImGuiCol.ResizeGripActive] = ImVec4(0.78, 0.82, 1.00, 0.90)
colors[ImGuiCol.InputTextCursor] = colors[ImGuiCol.Text]
colors[ImGuiCol.TabHovered] = colors[ImGuiCol.HeaderHovered]
colors[ImGuiCol.Tab] = ImLerpV4V4(colors[ImGuiCol.Header], colors[ImGuiCol.TitleBgActive], 0.80)
colors[ImGuiCol.TabSelected] = ImLerpV4V4(colors[ImGuiCol.HeaderActive], colors[ImGuiCol.TitleBgActive], 0.60)
colors[ImGuiCol.TabSelectedOverline] = colors[ImGuiCol.HeaderActive]
colors[ImGuiCol.TabDimmed] = ImLerpV4V4(colors[ImGuiCol.Tab], colors[ImGuiCol.TitleBg], 0.80)
colors[ImGuiCol.TabDimmedSelected] = ImLerpV4V4(colors[ImGuiCol.TabSelected], colors[ImGuiCol.TitleBg], 0.40)
colors[ImGuiCol.TabDimmedSelectedOverline] = ImVec4(0.53, 0.53, 0.87, 0.00)
colors[ImGuiCol.PlotLines] = ImVec4(1.00, 1.00, 1.00, 1.00)
colors[ImGuiCol.PlotLinesHovered] = ImVec4(0.90, 0.70, 0.00, 1.00)
colors[ImGuiCol.PlotHistogram] = ImVec4(0.90, 0.70, 0.00, 1.00)
colors[ImGuiCol.PlotHistogramHovered] = ImVec4(1.00, 0.60, 0.00, 1.00)
colors[ImGuiCol.TableHeaderBg] = ImVec4(0.27, 0.27, 0.38, 1.00)
colors[ImGuiCol.TableBorderStrong] = ImVec4(0.31, 0.31, 0.45, 1.00)
colors[ImGuiCol.TableBorderLight] = ImVec4(0.26, 0.26, 0.28, 1.00)
colors[ImGuiCol.TableRowBg] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[ImGuiCol.TableRowBgAlt] = ImVec4(1.00, 1.00, 1.00, 0.07)
colors[ImGuiCol.TextLink] = colors[ImGuiCol.HeaderActive]
colors[ImGuiCol.TextSelectedBg] = ImVec4(0.00, 0.00, 1.00, 0.35)
colors[ImGuiCol.TreeLines] = colors[ImGuiCol.Border]
colors[ImGuiCol.DragDropTarget] = ImVec4(1.00, 1.00, 0.00, 0.90)
colors[ImGuiCol.DragDropTargetBg] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[ImGuiCol.UnsavedMarker] = ImVec4(0.90, 0.90, 0.90, 1.00)
colors[ImGuiCol.NavCursor] = colors[ImGuiCol.HeaderHovered]
colors[ImGuiCol.NavWindowingHighlight] = ImVec4(1.00, 1.00, 1.00, 0.70)
colors[ImGuiCol.NavWindowingDimBg] = ImVec4(0.80, 0.80, 0.80, 0.20)
colors[ImGuiCol.ModalWindowDimBg] = ImVec4(0.20, 0.20, 0.20, 0.35)
end
-- Those light colors are better suited with a thicker font than the default one + FrameBorder
--- @param dst? ImGuiStyle
function ImGui.StyleColorsLight(dst)
local style = dst and dst or ImGui.GetStyle()
local colors = style.Colors
colors[ImGuiCol.Text] = ImVec4(0.00, 0.00, 0.00, 1.00)
colors[ImGuiCol.TextDisabled] = ImVec4(0.60, 0.60, 0.60, 1.00)
colors[ImGuiCol.WindowBg] = ImVec4(0.94, 0.94, 0.94, 1.00)
colors[ImGuiCol.ChildBg] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[ImGuiCol.PopupBg] = ImVec4(1.00, 1.00, 1.00, 0.98)
colors[ImGuiCol.Border] = ImVec4(0.00, 0.00, 0.00, 0.30)
colors[ImGuiCol.BorderShadow] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[ImGuiCol.FrameBg] = ImVec4(1.00, 1.00, 1.00, 1.00)
colors[ImGuiCol.FrameBgHovered] = ImVec4(0.26, 0.59, 0.98, 0.40)
colors[ImGuiCol.FrameBgActive] = ImVec4(0.26, 0.59, 0.98, 0.67)
colors[ImGuiCol.TitleBg] = ImVec4(0.96, 0.96, 0.96, 1.00)
colors[ImGuiCol.TitleBgActive] = ImVec4(0.82, 0.82, 0.82, 1.00)
colors[ImGuiCol.TitleBgCollapsed] = ImVec4(1.00, 1.00, 1.00, 0.51)
colors[ImGuiCol.MenuBarBg] = ImVec4(0.86, 0.86, 0.86, 1.00)
colors[ImGuiCol.ScrollbarBg] = ImVec4(0.98, 0.98, 0.98, 0.53)
colors[ImGuiCol.ScrollbarGrab] = ImVec4(0.69, 0.69, 0.69, 0.80)
colors[ImGuiCol.ScrollbarGrabHovered] = ImVec4(0.49, 0.49, 0.49, 0.80)
colors[ImGuiCol.ScrollbarGrabActive] = ImVec4(0.49, 0.49, 0.49, 1.00)
colors[ImGuiCol.CheckMark] = ImVec4(0.26, 0.59, 0.98, 1.00)
colors[ImGuiCol.CheckboxSelectedBg] = ImVec4(0.95, 0.97, 1.00, 1.00)
colors[ImGuiCol.SliderGrab] = ImVec4(0.26, 0.59, 0.98, 0.78)
colors[ImGuiCol.SliderGrabActive] = ImVec4(0.46, 0.54, 0.80, 0.60)
colors[ImGuiCol.Button] = ImVec4(0.26, 0.59, 0.98, 0.40)
colors[ImGuiCol.ButtonHovered] = ImVec4(0.26, 0.59, 0.98, 1.00)
colors[ImGuiCol.ButtonActive] = ImVec4(0.06, 0.53, 0.98, 1.00)
colors[ImGuiCol.Header] = ImVec4(0.26, 0.59, 0.98, 0.31)
colors[ImGuiCol.HeaderHovered] = ImVec4(0.26, 0.59, 0.98, 0.80)
colors[ImGuiCol.HeaderActive] = ImVec4(0.26, 0.59, 0.98, 1.00)
colors[ImGuiCol.Separator] = ImVec4(0.39, 0.39, 0.39, 0.62)
colors[ImGuiCol.SeparatorHovered] = ImVec4(0.14, 0.44, 0.80, 0.78)
colors[ImGuiCol.SeparatorActive] = ImVec4(0.14, 0.44, 0.80, 1.00)
colors[ImGuiCol.ResizeGrip] = ImVec4(0.35, 0.35, 0.35, 0.17)
colors[ImGuiCol.ResizeGripHovered] = ImVec4(0.26, 0.59, 0.98, 0.67)
colors[ImGuiCol.ResizeGripActive] = ImVec4(0.26, 0.59, 0.98, 0.95)
colors[ImGuiCol.InputTextCursor] = colors[ImGuiCol.Text]
colors[ImGuiCol.TabHovered] = colors[ImGuiCol.HeaderHovered]
colors[ImGuiCol.Tab] = ImLerpV4V4(colors[ImGuiCol.Header], colors[ImGuiCol.TitleBgActive], 0.90)
colors[ImGuiCol.TabSelected] = ImLerpV4V4(colors[ImGuiCol.HeaderActive], colors[ImGuiCol.TitleBgActive], 0.60)
colors[ImGuiCol.TabSelectedOverline] = colors[ImGuiCol.HeaderActive]
colors[ImGuiCol.TabDimmed] = ImLerpV4V4(colors[ImGuiCol.Tab], colors[ImGuiCol.TitleBg], 0.80)
colors[ImGuiCol.TabDimmedSelected] = ImLerpV4V4(colors[ImGuiCol.TabSelected], colors[ImGuiCol.TitleBg], 0.40)
colors[ImGuiCol.TabDimmedSelectedOverline] = ImVec4(0.26, 0.59, 1.00, 0.00)
colors[ImGuiCol.PlotLines] = ImVec4(0.39, 0.39, 0.39, 1.00)
colors[ImGuiCol.PlotLinesHovered] = ImVec4(1.00, 0.43, 0.35, 1.00)
colors[ImGuiCol.PlotHistogram] = ImVec4(0.90, 0.70, 0.00, 1.00)
colors[ImGuiCol.PlotHistogramHovered] = ImVec4(1.00, 0.45, 0.00, 1.00)
colors[ImGuiCol.TableHeaderBg] = ImVec4(0.78, 0.87, 0.98, 1.00)
colors[ImGuiCol.TableBorderStrong] = ImVec4(0.57, 0.57, 0.64, 1.00)
colors[ImGuiCol.TableBorderLight] = ImVec4(0.68, 0.68, 0.74, 1.00)
colors[ImGuiCol.TableRowBg] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[ImGuiCol.TableRowBgAlt] = ImVec4(0.30, 0.30, 0.30, 0.09)
colors[ImGuiCol.TextLink] = colors[ImGuiCol.HeaderActive]
colors[ImGuiCol.TextSelectedBg] = ImVec4(0.26, 0.59, 0.98, 0.35)
colors[ImGuiCol.TreeLines] = colors[ImGuiCol.Border]
colors[ImGuiCol.DragDropTarget] = ImVec4(0.26, 0.59, 0.98, 0.95)
colors[ImGuiCol.DragDropTargetBg] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[ImGuiCol.UnsavedMarker] = ImVec4(0.00, 0.00, 0.00, 1.00)
colors[ImGuiCol.NavCursor] = colors[ImGuiCol.HeaderHovered]
colors[ImGuiCol.NavWindowingHighlight] = ImVec4(0.70, 0.70, 0.70, 0.70)
colors[ImGuiCol.NavWindowingDimBg] = ImVec4(0.20, 0.20, 0.20, 0.20)
colors[ImGuiCol.ModalWindowDimBg] = ImVec4(0.20, 0.20, 0.20, 0.35)
end
--- @param draw_list ImDrawList
--- @param vert_start_idx int
--- @param vert_end_idx int
--- @param gradient_p0 ImVec2
--- @param gradient_p1 ImVec2
--- @param col0 ImU32
--- @param col1 ImU32
function ImGui.ShadeVertsLinearColorGradientKeepAlpha(draw_list, vert_start_idx, vert_end_idx, gradient_p0, gradient_p1, col0, col1)
local gradient_extent = gradient_p1 - gradient_p0
local gradient_inv_length2 = 1.0 / ImLengthSqr(gradient_extent)
local col0_r = bit.band(bit.rshift(col0, IM_COL32_R_SHIFT), 0xFF)
local col0_g = bit.band(bit.rshift(col0, IM_COL32_G_SHIFT), 0xFF)
local col0_b = bit.band(bit.rshift(col0, IM_COL32_B_SHIFT), 0xFF)
local col_delta_r = bit.band(bit.rshift(col1, IM_COL32_R_SHIFT), 0xFF) - col0_r
local col_delta_g = bit.band(bit.rshift(col1, IM_COL32_G_SHIFT), 0xFF) - col0_g
local col_delta_b = bit.band(bit.rshift(col1, IM_COL32_B_SHIFT), 0xFF) - col0_b
local a = ImVec2()
for vert_idx = vert_start_idx, vert_end_idx - 1 do
local vert = draw_list.VtxBuffer.Data[vert_idx]
ImVec2_CopyV(a, ImVec2_SubV(vert[1], gradient_p0))
local d = ImDot(a, gradient_extent)
local t = ImClamp(d * gradient_inv_length2, 0.0, 1.0)
local r = math.floor(col0_r + col_delta_r * t)
local g = math.floor(col0_g + col_delta_g * t)
local b = math.floor(col0_b + col_delta_b * t)
vert[3] = bit.bor(bit.lshift(r, IM_COL32_R_SHIFT), bit.lshift(g, IM_COL32_G_SHIFT), bit.lshift(b, IM_COL32_B_SHIFT), bit.band(vert[3], IM_COL32_A_MASK))
end
end
-- Distribute UV over (a, b) rectangle
--- @param draw_list ImDrawList
--- @param vert_start_idx int
--- @param vert_end_idx int
--- @param a ImVec2
--- @param b ImVec2
--- @param uv_a ImVec2
--- @param uv_b ImVec2
--- @param clamp bool
function ImGui.ShadeVertsLinearUV(draw_list, vert_start_idx, vert_end_idx, a, b, uv_a, uv_b, clamp)
local size = b - a
local uv_size = uv_b - uv_a
local scale = ImVec2(
(size.x ~= 0.0) and (uv_size.x / size.x) or 0.0,
(size.y ~= 0.0) and (uv_size.y / size.y) or 0.0
)
local vertex
local verts = draw_list.VtxBuffer.Data
if clamp then
local min = ImMinVec2(uv_a, uv_b)
local max = ImMaxVec2(uv_a, uv_b)
for vert_idx = vert_start_idx, vert_end_idx - 1 do
vertex = verts[vert_idx]
ImVec2_Copy(vertex[2], ImClampV2(uv_a + ImMul(ImVec2(vertex[1].x, vertex[1].y) - a, scale), min, max))
end
else
for vert_idx = vert_start_idx, vert_end_idx - 1 do
vertex = verts[vert_idx]
ImVec2_Copy(vertex[2], uv_a + ImMul(ImVec2(vertex[1].x, vertex[1].y) - a, scale))
end
end
end
--- Forward Declarations
local ImFontAtlasTextureAdd
local ImFontAtlasTextureGrow
local ImFontAtlasTextureGetSizeEstimate
local ImFontAtlasTextureBlockCopy
local ImFontAtlasTextureBlockConvert
local ImFontAtlasTextureBlockPostProcess
local ImFontAtlasTextureBlockPostProcessMultiply
local ImFontAtlasTextureBlockQueueUpload
local ImFontAtlasPackInit
local ImFontAtlasPackAddRect
local ImFontAtlasPackGetRect
local ImFontAtlasPackGetRectSafe
local ImFontAtlasBakedAdd
local ImFontAtlasBakedAddFontGlyph
local ImFontAtlasBakedAddFontGlyphAdvancedX
local ImFontAtlasBakedDiscard
local ImFontAtlasBakedGetOrAdd
local ImFontAtlasBakedGetId
local ImFontAtlasBakedGetClosestMatch
local ImFontAtlasBuildInit
local ImFontAtlasBuildSetTexture
local ImFontAtlasBuildUpdateTexDataLines
local ImFontAtlasBuildUpdateTexDataBasic
local ImFontAtlasBuildSetupFontLoader
local ImFontAtlasBuildSetupFontSpecialGlyphs
local ImFontAtlasBuildSetupFontBakedBlanks
local ImFontAtlasBuildSetupFontBakedEllipsis
local ImFontAtlasBuildSetupFontBakedFallback
local ImFontAtlasBuildDestroy
local ImFontAtlasBuildAcceptCodepointForSource
local ImFontAtlasBuildNotifySetFont
local ImFontAtlasBuildRenderBitmapFromString
local ImFontAtlasFontSourceInit
local ImFontAtlasFontSourceAddToFont
local ImFontAtlasFontDestroySourceData
local ImFontAtlasFontDestroyOutput
local ImFontBaked_BuildGrowIndex
local ImFontBaked_BuildLoadGlyph
local ImFontBaked_BuildLoadGlyphAdvanceX
local ImTextInitClassifiers
local ImTextClassifierGet
local ImTextClassifierClear
local ImTextClassifierSetCharClass
local ImTextClassifierSetCharClassFromStr
local ImTextureDataGetFormatBytesPerPixel
function MT.ImFontAtlas:Clear()
IMGUI_DEBUG_LOG_FONT("[font] ImFontAtlas:Clear()")
local backup_renderer_has_textures = self.RendererHasTextures
self.RendererHasTextures = false
self:ClearFonts()
self:ClearTexData()
self.RendererHasTextures = backup_renderer_has_textures
end
function MT.ImFontAtlas:SetFontLoader(font_loader)
ImFontAtlasBuildSetupFontLoader(self, font_loader)
end
function MT.ImFontAtlas:ClearInputData()
IM_ASSERT(not self.Locked, "Cannot modify a locked ImFontAtlas!")
for _, font in self.Fonts:iter() do
ImFontAtlasFontDestroyOutput(self, font)
end
for _, font_cfg in self.Sources:iter() do
ImFontAtlasFontDestroySourceData(self, font_cfg)
end
for _, font in self.Fonts:iter() do
font.Sources:clear()
font.Flags = bit.bor(font.Flags, ImFontFlags.NoLoadGlyphs)
end
self.Sources:clear()
end
function MT.ImFontAtlas:ClearTexData()
IM_ASSERT(not self.Locked, "Cannot modify a locked ImFontAtlas!")
IM_ASSERT(self.RendererHasTextures == false, "Not supported for dynamic atlases, but you may call Clear().")
for _, tex in self.TexList:iter() do
tex:DestroyPixels()
end
end
function MT.ImFontAtlas:ClearFonts()
IMGUI_DEBUG_LOG_FONT("[font] ImFontAtlas:ClearFonts()")
IM_ASSERT(not self.Locked, "Cannot modify a locked ImFontAtlas!")
for _, font in self.Fonts:iter() do
ImFontAtlasBuildNotifySetFont(self, font, nil)
end
ImFontAtlasBuildDestroy(self)
self:ClearInputData()
self.Fonts:clear_delete()
self.TexIsBuilt = false
for _, shared_data in self.DrawListSharedDatas:iter() do
if shared_data.FontAtlas == self then
shared_data.Font = nil
shared_data.FontSize = 0.0
shared_data.FontScale = 0.0
end
end
end
--- @param tex ImTextureData
function ImTextureDataUpdateNewFrame(tex)
local remove_from_list = false
if tex.Status == ImTextureStatus.OK then
tex.Updates:resize(0)
tex.UpdateRect.x = 65535 tex.UpdateRect.y = 65535
tex.UpdateRect.w = 0 tex.UpdateRect.h = 0
end
if (tex.WantDestroyNextFrame and tex.Status ~= ImTextureStatus.Destroyed and tex.Status ~= ImTextureStatus.WantDestroy) then
IM_ASSERT(tex.Status == ImTextureStatus.OK or tex.Status == ImTextureStatus.WantCreate or tex.Status == ImTextureStatus.WantUpdates)
tex.Status = ImTextureStatus.WantDestroy
end
if (tex.Status == ImTextureStatus.WantDestroy and tex.TexID == ImTextureID_Invalid and tex.BackendUserData == nil) then
tex.Status = ImTextureStatus.Destroyed
end
if (tex.Status == ImTextureStatus.Destroyed) then
IM_ASSERT(tex.TexID == ImTextureID_Invalid and tex.BackendUserData == nil, "Backend set texture Status to Destroyed but did not clear TexID/BackendUserData!")
if (tex.WantDestroyNextFrame) then
remove_from_list = true
else
tex.Status = ImTextureStatus.WantCreate
end
end
if (tex.Status == ImTextureStatus.WantDestroy) then
tex.UnusedFrames = tex.UnusedFrames + 1
end
return remove_from_list
end
--- @param atlas ImFontAtlas
--- @param frame_count int
--- @param renderer_has_textures any
function ImFontAtlasUpdateNewFrame(atlas, frame_count, renderer_has_textures)
IM_ASSERT(atlas.Builder == nil or atlas.Builder.FrameCount < frame_count)
atlas.RendererHasTextures = renderer_has_textures
local tex_n = 1
while tex_n <= atlas.TexList.Size do
local tex = atlas.TexList.Data[tex_n]
if (tex.Status == ImTextureStatus.WantCreate and atlas.RendererHasTextures) then
IM_ASSERT(tex.TexID == ImTextureID_Invalid and tex.BackendUserData == nil, "Backend set texture's TexID/BackendUserData but did not update Status to OK.")
end
local remove_from_list = ImTextureDataUpdateNewFrame(tex)
if remove_from_list then
IM_ASSERT(atlas.TexData ~= tex)
tex:DestroyPixels()
atlas.TexList:erase(tex_n)
tex_n = tex_n - 1
end
tex_n = tex_n + 1
end
if (atlas.RendererHasTextures) then
atlas.TexIsBuilt = true
if (atlas.Builder == nil) then
ImFontAtlasBuildMain(atlas)
end
end
if (not atlas.RendererHasTextures) then
IM_ASSERT_USER_ERROR(atlas.TexIsBuilt, "Backend does not support ImGuiBackendFlags.RendererHasTextures, and font atlas is not built! Update backend OR make sure you called ImGui_ImplXXXX_NewFrame() function for renderer backend, which should call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8().")
end
if (atlas.TexIsBuilt and atlas.Builder.PreloadedAllGlyphsRanges) then
IM_ASSERT_USER_ERROR(atlas.RendererHasTextures == false, "Called ImFontAtlas::Build() before ImGuiBackendFlags.RendererHasTextures got set! With new backends: you don't need to call Build().")
end
local builder = atlas.Builder
builder.FrameCount = frame_count
for _, font in atlas.Fonts:iter() do
font.LastBaked = nil
end
if builder.BakedDiscardedCount > 0 then
local dst_n = 1
for src_n = 1, builder.BakedPool.Size do
local p_src = builder.BakedPool.Data[src_n]
if p_src.WantDestroy then
goto __CONTINUE__
end
local p_dst = builder.BakedPool.Data[dst_n]
dst_n = dst_n + 1
if p_dst == p_src then
goto __CONTINUE__
end
builder.BakedPool.Data[dst_n - 1] = p_src
builder.BakedMap[p_src.BakedId] = p_src
:: __CONTINUE__ ::
end
IM_ASSERT(dst_n - 1 + builder.BakedDiscardedCount == builder.BakedPool.Size)
builder.BakedPool.Size = builder.BakedPool.Size - builder.BakedDiscardedCount
builder.BakedDiscardedCount = 0
end
end
--- @param src_pixels unsigned_char[]
--- @param src_fmt ImTextureFormat
--- @param src_pitch int
--- @param dst_pixels unsigned_char[]
--- @param dst_pixels_base int
--- @param dst_fmt ImTextureFormat
--- @param dst_pitch int
--- @param w int
--- @param h int
function ImFontAtlasTextureBlockConvert(src_pixels, src_fmt, src_pitch, dst_pixels, dst_pixels_base, dst_fmt, dst_pitch, w, h)
IM_ASSERT(src_pixels ~= nil and dst_pixels ~= nil)
local src_pixels_base = 1
if src_fmt == dst_fmt then
local line_sz = w * ImTextureDataGetFormatBytesPerPixel(src_fmt)
for ny = h, 1, -1 do
ImStd.memmove(dst_pixels, dst_pixels_base, src_pixels, src_pixels_base, line_sz)
src_pixels_base = src_pixels_base + src_pitch
dst_pixels_base = dst_pixels_base + dst_pitch
end
elseif src_fmt == ImTextureFormat.Alpha8 and dst_fmt == ImTextureFormat.RGBA32 then
for ny = h, 1, -1 do
for nx = w, 1, -1 do
local alpha = src_pixels[src_pixels_base + 0]
src_pixels_base = src_pixels_base + 1
write_u32(dst_pixels, dst_pixels_base, IM_COL32(255, 255, 255, alpha))
dst_pixels_base = dst_pixels_base + 4
end
src_pixels_base = src_pixels_base + (src_pitch - w)
dst_pixels_base = dst_pixels_base + (dst_pitch - w * 4)
end
elseif src_fmt == ImTextureFormat.RGBA32 and dst_fmt == ImTextureFormat.Alpha8 then
for ny = h, 1, -1 do
for nx = w, 1, -1 do
local alpha = src_pixels[src_pixels_base + 3]
src_pixels_base = src_pixels_base + 4
dst_pixels[dst_pixels_base + 0] = alpha
dst_pixels_base = dst_pixels_base + 1
end
src_pixels_base = src_pixels_base + (src_pitch - w * 4)
dst_pixels_base = dst_pixels_base + (dst_pitch - w)
end
else
IM_ASSERT(false)
end
end
--- @param data ImFontAtlasPostProcessData
function ImFontAtlasTextureBlockPostProcess(data)
if data.FontSrc.RasterizerMultiply ~= 1.0 then
ImFontAtlasTextureBlockPostProcessMultiply(data, data.FontSrc.RasterizerMultiply)
end
end
--- @param data ImFontAtlasPostProcessData
--- @param multiply_factor float
function ImFontAtlasTextureBlockPostProcessMultiply(data, multiply_factor)
local pixels, pixels_base = data.Pixels, data._PixelsBase
local pitch = data.Pitch
if data.Format == ImTextureFormat.Alpha8 then
for ny = data.Height, 1, -1 do
local p = pixels_base
for nx = data.Width, 1, -1 do
local v = ImMin(ImFloor(pixels[p + 0] * multiply_factor), 255)
pixels[p + 0] = v
p = p + 1
end
pixels_base = pixels_base + pitch
end
elseif data.Format == ImTextureFormat.RGBA32 then
for ny = data.Height, 1, -1 do
local p = pixels_base
for nx = data.Width, 1, -1 do
local a = ImMin(ImFloor(pixels[p + 3] * multiply_factor), 255)
write_u32(pixels, p, IM_COL32(pixels[p + 0], pixels[p + 1], pixels[p + 2], a))
p = p + 4
end
pixels_base = pixels_base + pitch
end
else
IM_ASSERT(false)
end
end
--- @param src_tex ImTextureData
--- @param src_x int
--- @param src_y int
--- @param dst_tex ImTextureData
--- @param dst_x int
--- @param dst_y int
--- @param w int
--- @param h int
function ImFontAtlasTextureBlockCopy(src_tex, src_x, src_y, dst_tex, dst_x, dst_y, w, h)
IM_ASSERT(src_tex.Pixels ~= nil and dst_tex.Pixels ~= nil)
IM_ASSERT(src_tex.Format == dst_tex.Format)
IM_ASSERT(src_x >= 0 and src_x + w <= src_tex.Width)
IM_ASSERT(src_y >= 0 and src_y + h <= src_tex.Height)
IM_ASSERT(dst_x >= 0 and dst_x + w <= dst_tex.Width)
IM_ASSERT(dst_y >= 0 and dst_y + h <= dst_tex.Height)
for y = 0, h - 1 do
local dst, dst_base = dst_tex:GetPixelsAt(dst_x, dst_y + y)
local src, src_base = src_tex:GetPixelsAt(src_x, src_y + y)
ImStd.memmove(dst, dst_base, src, src_base, w * dst_tex.BytesPerPixel)
end
end
--- @param tex ImTextureData
--- @param x int
--- @param y int
--- @param w int
--- @param h int
local function ImTextureDataQueueUpload(tex, x, y, w, h)
IM_ASSERT(tex.Status ~= ImTextureStatus.WantDestroy and tex.Status ~= ImTextureStatus.Destroyed)
IM_ASSERT(x >= 0 and x <= 0xFFFF and y >= 0 and y <= 0xFFFF and w >= 0 and x + w <= 0x10000 and h >= 0 and y + h <= 0x10000)
local req = ImTextureRect(x, y, w, h) -- (unsigned short)
local new_x1 = ImMax(tex.UpdateRect.w == 0 and 0 or tex.UpdateRect.x + tex.UpdateRect.w, req.x + req.w)
local new_y1 = ImMax(tex.UpdateRect.h == 0 and 0 or tex.UpdateRect.y + tex.UpdateRect.h, req.y + req.h)
tex.UpdateRect.x = ImMin(tex.UpdateRect.x, req.x)
tex.UpdateRect.y = ImMin(tex.UpdateRect.y, req.y)
tex.UpdateRect.w = (new_x1 - tex.UpdateRect.x) -- (unsigned short)
tex.UpdateRect.h = (new_y1 - tex.UpdateRect.y) -- (unsigned short)
tex.UsedRect.x = ImMin(tex.UsedRect.x, req.x)
tex.UsedRect.y = ImMin(tex.UsedRect.y, req.y)
tex.UsedRect.w = (ImMax(tex.UsedRect.x + tex.UsedRect.w, req.x + req.w) - tex.UsedRect.x) -- (unsigned short)
tex.UsedRect.h = (ImMax(tex.UsedRect.y + tex.UsedRect.h, req.y + req.h) - tex.UsedRect.y) -- (unsigned short)
if (tex.Status == ImTextureStatus.OK or tex.Status == ImTextureStatus.WantUpdates) then
tex.Status = ImTextureStatus.WantUpdates
tex.Updates:push_back(req)
end
end
--- @param atlas ImFontAtlas
--- @param tex ImTextureData
--- @param x int
--- @param y int
--- @param w int
--- @param h int
function ImFontAtlasTextureBlockQueueUpload(atlas, tex, x, y, w, h)
ImTextureDataQueueUpload(tex, x, y, w, h)
atlas.TexIsBuilt = false
end
--- @param atlas ImFontAtlas
--- @param baked ImFontBaked
--- @param src ImFontConfig
--- @param glyph ImFontGlyph
--- @param r ImTextureRect
--- @param src_pixels unsigned_char[]
--- @param src_fmt ImTextureFormat
--- @param src_pitch int
local function ImFontAtlasBakedSetFontGlyphBitmap(atlas, baked, src, glyph, r, src_pixels, src_fmt, src_pitch)
local tex = atlas.TexData
IM_ASSERT(r.x + r.w <= tex.Width and r.y + r.h <= tex.Height)
local out_p, out_p_base = tex:GetPixelsAt(r.x, r.y)
IM_ASSERT(out_p ~= nil)
ImFontAtlasTextureBlockConvert(src_pixels, src_fmt, src_pitch, out_p, out_p_base, tex.Format, tex:GetPitch(), r.w, r.h)
local pp_data = ImFontAtlasPostProcessData(atlas, baked.OwnerFont, src, baked, glyph, out_p, out_p_base, tex.Format, tex:GetPitch(), r.w, r.h)
ImFontAtlasTextureBlockPostProcess(pp_data)
ImFontAtlasTextureBlockQueueUpload(atlas, tex, r.x, r.y, r.w, r.h)
end
local function ImFontAtlasBuildGetOversampleFactors(src, baked)
local raster_size = baked.Size * baked.RasterizerDensity * src.RasterizerDensity
local out_oversample_h
if src.OversampleH ~= 0 then
out_oversample_h = src.OversampleH
elseif raster_size > 36.0 or src.PixelSnapH then
out_oversample_h = 1
else
out_oversample_h = 2
end
local out_oversample_v = (src.OversampleV ~= 0) and src.OversampleV or 1
return out_oversample_h, out_oversample_v
end
local function ImFontAtlasBuildDiscardBakes(atlas, unused_frames)
local builder = atlas.Builder
for baked_n = 1, builder.BakedPool.Size do
local baked = builder.BakedPool[baked_n]
if (baked.LastUsedFrame + unused_frames > atlas.Builder.FrameCount) then
goto __CONTINUE__
end
if (baked.WantDestroy or (bit.band(baked.OwnerFont.Flags, ImFontFlags.LockBakedSizes) ~= 0)) then
goto __CONTINUE__
end
ImFontAtlasBakedDiscard(atlas, baked.OwnerFont, baked)
:: __CONTINUE__ ::
end
end
--- @param atlas ImFontAtlas
--- @param data ImDrawListSharedData
function ImFontAtlasAddDrawListSharedData(atlas, data)
IM_ASSERT(not atlas.DrawListSharedDatas:contains(data))
atlas.DrawListSharedDatas:push_back(data)
end
--- @param atlas ImFontAtlas
--- @param data ImDrawListSharedData
function ImFontAtlasRemoveDrawListSharedData(atlas, data)
IM_ASSERT(not atlas.DrawListSharedDatas:contains(data))
atlas.DrawListSharedDatas:find_erase(data)
end
--- @param atlas ImFontAtlas
--- @param w int
--- @param h int
local function ImFontAtlasTextureRepack(atlas, w, h)
local builder = atlas.Builder
builder.LockDisableResize = true
local old_tex = atlas.TexData
local new_tex = ImFontAtlasTextureAdd(atlas, w, h)
new_tex.UseColors = old_tex.UseColors
IMGUI_DEBUG_LOG_FONT("[font] Texture #%03d: resize+repack %dx%d => Texture #%03d: %dx%d", old_tex.UniqueID, old_tex.Width, old_tex.Height, new_tex.UniqueID, new_tex.Width, new_tex.Height)
ImFontAtlasPackInit(atlas)
local old_rects = ImVector()
local old_index = builder.RectsIndex:copy()
old_rects:swap(builder.Rects)
for _, index_entry in builder.RectsIndex:iter() do
if index_entry.IsUsed == false then
goto __CONTINUE__
end
local old_r = old_rects[index_entry.TargetIndex + 1]
if (old_r.w == 0 and old_r.h == 0) then
goto __CONTINUE__
end
local new_r_id = ImFontAtlasPackAddRect(atlas, old_r.w, old_r.h, index_entry)
if new_r_id == ImFontAtlasRectId_Invalid then
IMGUI_DEBUG_LOG_FONT("[font] Texture #%03d: resize failed. Will grow.", new_tex.UniqueID)
new_tex.WantDestroyNextFrame = true
builder.Rects:swap(old_rects)
builder.RectsIndex = old_index
ImFontAtlasBuildSetTexture(atlas, old_tex)
ImFontAtlasTextureGrow(atlas, w, h)
return
end
IM_ASSERT(ImFontAtlasRectId_GetIndex(new_r_id) == builder.RectsIndex:index_from_ptr(index_entry))
local new_r = ImFontAtlasPackGetRect(atlas, new_r_id)
ImFontAtlasTextureBlockCopy(old_tex, old_r.x, old_r.y, new_tex, new_r.x, new_r.y, new_r.w, new_r.h)
:: __CONTINUE__ ::
end
IM_ASSERT(old_rects.Size == builder.Rects.Size + builder.RectsDiscardedCount)
builder.RectsDiscardedCount = 0
builder.RectsDiscardedSurface = 0
for baked_n = 1, builder.BakedPool.Size do
for _, glyph in builder.BakedPool[baked_n].Glyphs:iter() do
if (glyph.PackId ~= ImFontAtlasRectId_Invalid) then
local r = ImFontAtlasPackGetRect(atlas, glyph.PackId)
glyph.U0 = (r.x) * atlas.TexUvScale.x
glyph.V0 = (r.y) * atlas.TexUvScale.y
glyph.U1 = (r.x + r.w) * atlas.TexUvScale.x
glyph.V1 = (r.y + r.h) * atlas.TexUvScale.y
end
end
end
ImFontAtlasBuildUpdateTexData(atlas)
builder.LockDisableResize = false
ImFontAtlasUpdateDrawListsSharedData(atlas)
-- ImFontAtlasDebugWriteTexToDisk(new_tex, "After Pack");
end
function ImFontAtlasTextureGrow(atlas, old_tex_w, old_tex_h)
local builder = atlas.Builder
old_tex_w = old_tex_w or -1
old_tex_h = old_tex_h or -1
if (old_tex_w == -1) then
old_tex_w = atlas.TexData.Width
end
if (old_tex_h == -1) then
old_tex_h = atlas.TexData.Height
end
IM_ASSERT(ImIsPowerOfTwo(old_tex_w) and ImIsPowerOfTwo(old_tex_h))
IM_ASSERT(ImIsPowerOfTwo(atlas.TexMinWidth) and ImIsPowerOfTwo(atlas.TexMaxWidth) and ImIsPowerOfTwo(atlas.TexMinHeight) and ImIsPowerOfTwo(atlas.TexMaxHeight))
local new_tex_w = (old_tex_h <= old_tex_w) and old_tex_w or old_tex_w * 2
local new_tex_h = (old_tex_h <= old_tex_w) and old_tex_h * 2 or old_tex_h
local pack_padding = atlas.TexGlyphPadding
new_tex_w = ImMax(new_tex_w, ImUpperPowerOfTwo(builder.MaxRectSize.x + pack_padding))
new_tex_h = ImMax(new_tex_h, ImUpperPowerOfTwo(builder.MaxRectSize.y + pack_padding))
new_tex_w = ImClamp(new_tex_w, atlas.TexMinWidth, atlas.TexMaxWidth)
new_tex_h = ImClamp(new_tex_h, atlas.TexMinHeight, atlas.TexMaxHeight)
if (new_tex_w == old_tex_w and new_tex_h == old_tex_h) then
return
end
ImFontAtlasTextureRepack(atlas, new_tex_w, new_tex_h)
end
--- @param atlas ImFontAtlas
local function ImFontAtlasTextureMakeSpace(atlas)
local builder = atlas.Builder
ImFontAtlasBuildDiscardBakes(atlas, 2)
if (builder.RectsDiscardedSurface < builder.RectsPackedSurface * 0.20) then
ImFontAtlasTextureGrow(atlas)
else
ImFontAtlasTextureRepack(atlas, atlas.TexData.Width, atlas.TexData.Height)
end
end
--- @param atlas ImFontAtlas
--- @param rect_idx ImFontAtlasRectId
local function ImFontAtlasPackAllocRectEntry(atlas, rect_idx)
local builder = atlas.Builder
local index_idx, index_entry
if builder.RectsIndexFreeListStart < 0 then
builder.RectsIndex:resize(builder.RectsIndex.Size + 1)
index_idx = builder.RectsIndex.Size - 1
index_entry = ImFontAtlasRectEntry()
builder.RectsIndex.Data[builder.RectsIndex.Size] = index_entry
else
index_idx = builder.RectsIndexFreeListStart
index_entry = builder.RectsIndex[index_idx + 1]
IM_ASSERT(index_entry.IsUsed == false and index_entry.Generation > 0)
builder.RectsIndexFreeListStart = index_entry.TargetIndex
end
index_entry.TargetIndex = rect_idx
index_entry.IsUsed = true
return ImFontAtlasRectId_Make(index_idx, index_entry.Generation)
end
--- @param atlas ImFontAtlas
--- @param index_entry ImFontAtlasRectEntry
local function ImFontAtlasPackReuseRectEntry(atlas, index_entry)
IM_ASSERT(index_entry.IsUsed)
index_entry.TargetIndex = atlas.Builder.Rects.Size - 1
local index_idx = atlas.Builder.RectsIndex:index_from_ptr(index_entry)
return ImFontAtlasRectId_Make(index_idx, index_entry.Generation)
end
--- @param atlas ImFontAtlas
--- @param id ImFontAtlasRectId
function ImFontAtlasPackDiscardRect(atlas, id)
IM_ASSERT(id ~= ImFontAtlasRectId_Invalid)
local rect = ImFontAtlasPackGetRect(atlas, id)
if (rect == nil) then
return
end
local builder = atlas.Builder
local index_idx = ImFontAtlasRectId_GetIndex(id)
local index_entry = builder.RectsIndex.Data[index_idx + 1]
IM_ASSERT(index_entry.IsUsed and index_entry.TargetIndex >= 0)
index_entry.IsUsed = false
index_entry.TargetIndex = builder.RectsIndexFreeListStart
index_entry.Generation = index_entry.Generation + 1
if (index_entry.Generation == 0) then
index_entry.Generation = index_entry.Generation + 1
end
local pack_padding = atlas.TexGlyphPadding
builder.RectsIndexFreeListStart = index_idx
builder.RectsDiscardedCount = builder.RectsDiscardedCount + 1
builder.RectsDiscardedSurface = builder.RectsDiscardedSurface + ((rect.w + pack_padding) * (rect.h + pack_padding))
rect.w = 0 rect.h = 0
end
function ImFontAtlasPackAddRect(atlas, w, h, overwrite_entry)
IM_ASSERT(w > 0 and w <= 0xFFFF)
IM_ASSERT(h > 0 and h <= 0xFFFF)
local builder = atlas.Builder
local pack_padding = atlas.TexGlyphPadding
builder.MaxRectSize.x = ImMax(builder.MaxRectSize.x, w)
builder.MaxRectSize.y = ImMax(builder.MaxRectSize.y, h)
local r = ImTextureRect(0, 0, w, h)
for attempts_remaining = 3, 0, -1 do
local pack_r = stbrp.rect()
pack_r.w = w + pack_padding
pack_r.h = h + pack_padding
stbrp.pack_rects(builder.PackContext, {pack_r}, 1)
r.x = pack_r.x -- (unsigned short)
r.y = pack_r.y -- (unsigned short)
if pack_r.was_packed == 1 then
break
end
if (attempts_remaining == 0 or builder.LockDisableResize) then
IMGUI_DEBUG_LOG_FONT("[font] Failed packing %dx%d rectangle. Returning fallback.", w, h)
return ImFontAtlasRectId_Invalid
end
ImFontAtlasTextureMakeSpace(atlas)
end