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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
|
/*
* Copyright 2011-2016 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
// This code is based on:
//
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
// Source altered and distributed from https://github.com/AdrienHerubel/imgui
#include <stdio.h>
#include <bx/string.h>
#include <bx/uint32_t.h>
#include <bx/fpumath.h>
#include <bx/handlealloc.h>
#include "imgui.h"
#include "ocornut_imgui.h"
#include "../nanovg/nanovg.h"
// embedded shaders
#include "vs_imgui_color.bin.h"
#include "fs_imgui_color.bin.h"
#include "vs_imgui_texture.bin.h"
#include "fs_imgui_texture.bin.h"
#include "vs_imgui_cubemap.bin.h"
#include "fs_imgui_cubemap.bin.h"
#include "vs_imgui_latlong.bin.h"
#include "fs_imgui_latlong.bin.h"
#include "vs_imgui_image.bin.h"
#include "fs_imgui_image.bin.h"
#include "fs_imgui_image_swizz.bin.h"
// embedded font
#include "droidsans.ttf.h"
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4244); // warning C4244: '=' : conversion from '' to '', possible loss of data
#define USE_NANOVG_FONT 0
#define IMGUI_CONFIG_MAX_FONTS 20
#define MAX_TEMP_COORDS 100
#define NUM_CIRCLE_VERTS (8 * 4)
static const int32_t BUTTON_HEIGHT = 20;
static const int32_t SLIDER_HEIGHT = 20;
static const int32_t SLIDER_MARKER_WIDTH = 10;
static const int32_t CHECK_SIZE = 8;
static const int32_t DEFAULT_SPACING = 4;
static const int32_t TEXT_HEIGHT = 8;
static const int32_t SCROLL_AREA_PADDING = 6;
static const int32_t AREA_HEADER = 20;
static const float s_tabStops[4] = {150, 210, 270, 330};
void* imguiMalloc(size_t _size, void*);
void imguiFree(void* _ptr, void*);
#define IMGUI_MIN(_a, _b) (_a)<(_b)?(_a):(_b)
#define IMGUI_MAX(_a, _b) (_a)>(_b)?(_a):(_b)
#define IMGUI_CLAMP(_a, _min, _max) IMGUI_MIN(IMGUI_MAX(_a, _min), _max)
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4505); // error C4505: '' : unreferenced local function has been removed
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-function"); // warning: ‘int rect_width_compare(const void*, const void*)’ defined but not used
BX_PRAGMA_DIAGNOSTIC_PUSH();
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wtype-limits"); // warning: comparison is always true due to limited range of data type
#define STBTT_malloc(_size, _userData) imguiMalloc(_size, _userData)
#define STBTT_free(_ptr, _userData) imguiFree(_ptr, _userData)
#define STB_RECT_PACK_IMPLEMENTATION
#include <stb/stb_rect_pack.h>
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb/stb_truetype.h>
BX_PRAGMA_DIAGNOSTIC_POP();
namespace
{
static uint32_t addQuad(uint16_t* _indices, uint16_t _idx0, uint16_t _idx1, uint16_t _idx2, uint16_t _idx3)
{
_indices[0] = _idx0;
_indices[1] = _idx3;
_indices[2] = _idx1;
_indices[3] = _idx1;
_indices[4] = _idx3;
_indices[5] = _idx2;
return 6;
}
float sign(float px, float py, float ax, float ay, float bx, float by)
{
return (px - bx) * (ay - by) - (ax - bx) * (py - by);
}
bool pointInTriangle(float px, float py, float ax, float ay, float bx, float by, float cx, float cy)
{
const bool b1 = sign(px, py, ax, ay, bx, by) < 0.0f;
const bool b2 = sign(px, py, bx, by, cx, cy) < 0.0f;
const bool b3 = sign(px, py, cx, cy, ax, ay) < 0.0f;
return ( (b1 == b2) && (b2 == b3) );
}
void closestPointOnLine(float& ox, float &oy, float px, float py, float ax, float ay, float bx, float by)
{
float dx = px - ax;
float dy = py - ay;
float lx = bx - ax;
float ly = by - ay;
float len = sqrtf(lx*lx+ly*ly);
// Normalize.
float invLen = 1.0f/len;
lx*=invLen;
ly*=invLen;
float dot = (dx*lx + dy*ly);
if (dot < 0.0f)
{
ox = ax;
oy = ay;
}
else if (dot > len)
{
ox = bx;
oy = by;
}
else
{
ox = ax + lx*dot;
oy = ay + ly*dot;
}
}
void closestPointOnTriangle(float& ox, float &oy, float px, float py, float ax, float ay, float bx, float by, float cx, float cy)
{
float abx, aby;
float bcx, bcy;
float cax, cay;
closestPointOnLine(abx, aby, px, py, ax, ay, bx, by);
closestPointOnLine(bcx, bcy, px, py, bx, by, cx, cy);
closestPointOnLine(cax, cay, px, py, cx, cy, ax, ay);
const float pabx = px - abx;
const float paby = py - aby;
const float pbcx = px - bcx;
const float pbcy = py - bcy;
const float pcax = px - cax;
const float pcay = py - cay;
const float lab = sqrtf(pabx*pabx+paby*paby);
const float lbc = sqrtf(pbcx*pbcx+pbcy*pbcy);
const float lca = sqrtf(pcax*pcax+pcay*pcay);
const float m = bx::fmin3(lab, lbc, lca);
if (m == lab)
{
ox = abx;
oy = aby;
}
else if (m == lbc)
{
ox = bcx;
oy = bcy;
}
else// if (m == lca).
{
ox = cax;
oy = cay;
}
}
inline float vec2Dot(const float* __restrict _a, const float* __restrict _b)
{
return _a[0]*_b[0] + _a[1]*_b[1];
}
void barycentric(float& _u, float& _v, float& _w
, float _ax, float _ay
, float _bx, float _by
, float _cx, float _cy
, float _px, float _py
)
{
const float v0[2] = { _bx - _ax, _by - _ay };
const float v1[2] = { _cx - _ax, _cy - _ay };
const float v2[2] = { _px - _ax, _py - _ay };
const float d00 = vec2Dot(v0, v0);
const float d01 = vec2Dot(v0, v1);
const float d11 = vec2Dot(v1, v1);
const float d20 = vec2Dot(v2, v0);
const float d21 = vec2Dot(v2, v1);
const float denom = d00 * d11 - d01 * d01;
_v = (d11 * d20 - d01 * d21) / denom;
_w = (d00 * d21 - d01 * d20) / denom;
_u = 1.0f - _v - _w;
}
struct PosColorVertex
{
float m_x;
float m_y;
uint32_t m_abgr;
static void init()
{
ms_decl
.begin()
.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.end();
}
static bgfx::VertexDecl ms_decl;
};
bgfx::VertexDecl PosColorVertex::ms_decl;
struct PosColorUvVertex
{
float m_x;
float m_y;
float m_u;
float m_v;
uint32_t m_abgr;
static void init()
{
ms_decl
.begin()
.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.end();
}
static bgfx::VertexDecl ms_decl;
};
bgfx::VertexDecl PosColorUvVertex::ms_decl;
struct PosUvVertex
{
float m_x;
float m_y;
float m_u;
float m_v;
static void init()
{
ms_decl
.begin()
.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
.end();
}
static bgfx::VertexDecl ms_decl;
};
bgfx::VertexDecl PosUvVertex::ms_decl;
struct PosNormalVertex
{
float m_x;
float m_y;
float m_z;
float m_nx;
float m_ny;
float m_nz;
static void init()
{
ms_decl.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Normal, 3, bgfx::AttribType::Float)
.end();
}
void set(float _x, float _y, float _z, float _nx, float _ny, float _nz)
{
m_x = _x;
m_y = _y;
m_z = _z;
m_nx = _nx;
m_ny = _ny;
m_nz = _nz;
}
static bgfx::VertexDecl ms_decl;
};
bgfx::VertexDecl PosNormalVertex::ms_decl;
} // namespace
#if !USE_NANOVG_FONT
static float getTextLength(stbtt_bakedchar* _chardata, const char* _text, uint32_t& _numVertices)
{
float xpos = 0;
float len = 0;
uint32_t numVertices = 0;
while (*_text)
{
int32_t ch = (uint8_t)*_text;
if (ch == '\t')
{
for (int32_t ii = 0; ii < 4; ++ii)
{
if (xpos < s_tabStops[ii])
{
xpos = s_tabStops[ii];
break;
}
}
}
else if (ch >= ' '
&& ch < 128)
{
stbtt_bakedchar* b = _chardata + ch - ' ';
int32_t round_x = STBTT_ifloor( (xpos + b->xoff) + 0.5);
len = round_x + b->x1 - b->x0 + 0.5f;
xpos += b->xadvance;
numVertices += 6;
}
++_text;
}
_numVertices = numVertices;
return len;
}
#endif // !USE_NANOVG_FONT
struct Imgui
{
Imgui()
: m_mx(-1)
, m_my(-1)
, m_scroll(0)
, m_active(0)
, m_hot(0)
, m_hotToBe(0)
, m_dragX(0)
, m_dragY(0)
, m_dragOrig(0)
, m_left(false)
, m_leftPressed(false)
, m_leftReleased(false)
, m_isHot(false)
, m_wentActive(false)
, m_insideArea(false)
, m_isActivePresent(false)
, m_checkActivePresence(false)
, m_widgetId(0)
, m_enabledAreaIds(0)
, m_textureWidth(512)
, m_textureHeight(512)
, m_halfTexel(0.0f)
, m_nvg(NULL)
, m_view(255)
, m_surfaceWidth(0)
, m_surfaceHeight(0)
, m_viewWidth(0)
, m_viewHeight(0)
, m_currentFontIdx(0)
{
m_areaId.reset();
m_invTextureWidth = 1.0f/m_textureWidth;
m_invTextureHeight = 1.0f/m_textureHeight;
u_imageLodEnabled.idx = bgfx::invalidHandle;
u_imageSwizzle.idx = bgfx::invalidHandle;
s_texColor.idx = bgfx::invalidHandle;
m_missingTexture.idx = bgfx::invalidHandle;
m_colorProgram.idx = bgfx::invalidHandle;
m_textureProgram.idx = bgfx::invalidHandle;
m_cubeMapProgram.idx = bgfx::invalidHandle;
m_latlongProgram.idx = bgfx::invalidHandle;
m_imageProgram.idx = bgfx::invalidHandle;
m_imageSwizzProgram.idx = bgfx::invalidHandle;
}
ImguiFontHandle createFont(const void* _data, float _fontSize)
{
#if !USE_NANOVG_FONT
const ImguiFontHandle handle = { m_fontHandle.alloc() };
const bgfx::Memory* mem = bgfx::alloc(m_textureWidth * m_textureHeight);
stbtt_BakeFontBitmap( (uint8_t*)_data, 0, _fontSize, mem->data, m_textureWidth, m_textureHeight, 32, 96, m_fonts[handle.idx].m_cdata);
m_fonts[handle.idx].m_texture = bgfx::createTexture2D(m_textureWidth, m_textureHeight, 1, bgfx::TextureFormat::R8, BGFX_TEXTURE_NONE, mem);
m_fonts[handle.idx].m_size = _fontSize;
#else
const ImguiFontHandle handle = { bgfx::invalidHandle };
#endif // !USE_NANOVG_FONT
return handle;
}
void setFont(ImguiFontHandle _handle)
{
if (isValid(_handle) )
{
m_currentFontIdx = _handle.idx;
}
}
bgfx::TextureHandle genMissingTexture(uint32_t _width, uint32_t _height, float _lineWidth = 0.02f)
{
const bgfx::Memory* mem = bgfx::alloc(_width*_height*4);
uint32_t* bgra8 = (uint32_t*)mem->data;
const float sx = 0.70710677f;
const float cx = 0.70710677f;
for (uint32_t yy = 0; yy < _height; ++yy)
{
for (uint32_t xx = 0; xx < _width; ++xx)
{
float px = xx / float(_width) * 2.0f - 1.0f;
float py = yy / float(_height) * 2.0f - 1.0f;
float sum = bx::fpulse(px * cx - py * sx, _lineWidth, -_lineWidth)
+ bx::fpulse(px * sx + py * cx, _lineWidth, -_lineWidth)
;
*bgra8++ = sum >= 1.0f ? 0xffff0000 : 0xffffffff;
}
}
return bgfx::createTexture2D(uint16_t(_width), uint16_t(_height), 0, bgfx::TextureFormat::BGRA8, 0, mem);
}
ImguiFontHandle create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
{
m_allocator = _allocator;
if (NULL == m_allocator)
{
static bx::CrtAllocator allocator;
m_allocator = &allocator;
}
if (NULL == _data)
{
_data = s_droidSansTtf;
_size = sizeof(s_droidSansTtf);
}
IMGUI_create(_data, _size, _fontSize, m_allocator);
m_nvg = nvgCreate(1, m_view, m_allocator);
nvgCreateFontMem(m_nvg, "default", (unsigned char*)_data, INT32_MAX, 0);
nvgFontSize(m_nvg, _fontSize);
nvgFontFace(m_nvg, "default");
for (int32_t ii = 0; ii < NUM_CIRCLE_VERTS; ++ii)
{
float a = (float)ii / (float)NUM_CIRCLE_VERTS * (float)(bx::pi * 2.0);
m_circleVerts[ii * 2 + 0] = cosf(a);
m_circleVerts[ii * 2 + 1] = sinf(a);
}
PosColorVertex::init();
PosColorUvVertex::init();
PosUvVertex::init();
PosNormalVertex::init();
u_imageLodEnabled = bgfx::createUniform("u_imageLodEnabled", bgfx::UniformType::Vec4);
u_imageSwizzle = bgfx::createUniform("u_swizzle", bgfx::UniformType::Vec4);
s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
const bgfx::Memory* vs_imgui_color;
const bgfx::Memory* fs_imgui_color;
const bgfx::Memory* vs_imgui_texture;
const bgfx::Memory* fs_imgui_texture;
const bgfx::Memory* vs_imgui_cubemap;
const bgfx::Memory* fs_imgui_cubemap;
const bgfx::Memory* vs_imgui_latlong;
const bgfx::Memory* fs_imgui_latlong;
const bgfx::Memory* vs_imgui_image;
const bgfx::Memory* fs_imgui_image;
const bgfx::Memory* fs_imgui_image_swizz;
switch (bgfx::getRendererType() )
{
case bgfx::RendererType::Direct3D9:
vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx9, sizeof(vs_imgui_color_dx9) );
fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx9, sizeof(fs_imgui_color_dx9) );
vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx9, sizeof(vs_imgui_texture_dx9) );
fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx9, sizeof(fs_imgui_texture_dx9) );
vs_imgui_cubemap = bgfx::makeRef(vs_imgui_cubemap_dx9, sizeof(vs_imgui_cubemap_dx9) );
fs_imgui_cubemap = bgfx::makeRef(fs_imgui_cubemap_dx9, sizeof(fs_imgui_cubemap_dx9) );
vs_imgui_latlong = bgfx::makeRef(vs_imgui_latlong_dx9, sizeof(vs_imgui_latlong_dx9) );
fs_imgui_latlong = bgfx::makeRef(fs_imgui_latlong_dx9, sizeof(fs_imgui_latlong_dx9) );
vs_imgui_image = bgfx::makeRef(vs_imgui_image_dx9, sizeof(vs_imgui_image_dx9) );
fs_imgui_image = bgfx::makeRef(fs_imgui_image_dx9, sizeof(fs_imgui_image_dx9) );
fs_imgui_image_swizz = bgfx::makeRef(fs_imgui_image_swizz_dx9, sizeof(fs_imgui_image_swizz_dx9) );
m_halfTexel = 0.5f;
break;
case bgfx::RendererType::Direct3D11:
case bgfx::RendererType::Direct3D12:
vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx11, sizeof(vs_imgui_color_dx11) );
fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx11, sizeof(fs_imgui_color_dx11) );
vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx11, sizeof(vs_imgui_texture_dx11) );
fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx11, sizeof(fs_imgui_texture_dx11) );
vs_imgui_cubemap = bgfx::makeRef(vs_imgui_cubemap_dx11, sizeof(vs_imgui_cubemap_dx11) );
fs_imgui_cubemap = bgfx::makeRef(fs_imgui_cubemap_dx11, sizeof(fs_imgui_cubemap_dx11) );
vs_imgui_latlong = bgfx::makeRef(vs_imgui_latlong_dx11, sizeof(vs_imgui_latlong_dx11) );
fs_imgui_latlong = bgfx::makeRef(fs_imgui_latlong_dx11, sizeof(fs_imgui_latlong_dx11) );
vs_imgui_image = bgfx::makeRef(vs_imgui_image_dx11, sizeof(vs_imgui_image_dx11) );
fs_imgui_image = bgfx::makeRef(fs_imgui_image_dx11, sizeof(fs_imgui_image_dx11) );
fs_imgui_image_swizz = bgfx::makeRef(fs_imgui_image_swizz_dx11, sizeof(fs_imgui_image_swizz_dx11) );
break;
case bgfx::RendererType::Metal:
vs_imgui_color = bgfx::makeRef(vs_imgui_color_mtl, sizeof(vs_imgui_color_mtl) );
fs_imgui_color = bgfx::makeRef(fs_imgui_color_mtl, sizeof(fs_imgui_color_mtl) );
vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_mtl, sizeof(vs_imgui_texture_mtl) );
fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_mtl, sizeof(fs_imgui_texture_mtl) );
vs_imgui_cubemap = bgfx::makeRef(vs_imgui_cubemap_mtl, sizeof(vs_imgui_cubemap_mtl) );
fs_imgui_cubemap = bgfx::makeRef(fs_imgui_cubemap_mtl, sizeof(fs_imgui_cubemap_mtl) );
vs_imgui_latlong = bgfx::makeRef(vs_imgui_latlong_mtl, sizeof(vs_imgui_latlong_mtl) );
fs_imgui_latlong = bgfx::makeRef(fs_imgui_latlong_mtl, sizeof(fs_imgui_latlong_mtl) );
vs_imgui_image = bgfx::makeRef(vs_imgui_image_mtl, sizeof(vs_imgui_image_mtl) );
fs_imgui_image = bgfx::makeRef(fs_imgui_image_mtl, sizeof(fs_imgui_image_mtl) );
fs_imgui_image_swizz = bgfx::makeRef(fs_imgui_image_swizz_mtl, sizeof(fs_imgui_image_swizz_mtl) );
break;
default:
vs_imgui_color = bgfx::makeRef(vs_imgui_color_glsl, sizeof(vs_imgui_color_glsl) );
fs_imgui_color = bgfx::makeRef(fs_imgui_color_glsl, sizeof(fs_imgui_color_glsl) );
vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_glsl, sizeof(vs_imgui_texture_glsl) );
fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_glsl, sizeof(fs_imgui_texture_glsl) );
vs_imgui_cubemap = bgfx::makeRef(vs_imgui_cubemap_glsl, sizeof(vs_imgui_cubemap_glsl) );
fs_imgui_cubemap = bgfx::makeRef(fs_imgui_cubemap_glsl, sizeof(fs_imgui_cubemap_glsl) );
vs_imgui_latlong = bgfx::makeRef(vs_imgui_latlong_glsl, sizeof(vs_imgui_latlong_glsl) );
fs_imgui_latlong = bgfx::makeRef(fs_imgui_latlong_glsl, sizeof(fs_imgui_latlong_glsl) );
vs_imgui_image = bgfx::makeRef(vs_imgui_image_glsl, sizeof(vs_imgui_image_glsl) );
fs_imgui_image = bgfx::makeRef(fs_imgui_image_glsl, sizeof(fs_imgui_image_glsl) );
fs_imgui_image_swizz = bgfx::makeRef(fs_imgui_image_swizz_glsl, sizeof(fs_imgui_image_swizz_glsl) );
break;
}
bgfx::ShaderHandle vsh;
bgfx::ShaderHandle fsh;
vsh = bgfx::createShader(vs_imgui_color);
fsh = bgfx::createShader(fs_imgui_color);
m_colorProgram = bgfx::createProgram(vsh, fsh);
bgfx::destroyShader(vsh);
bgfx::destroyShader(fsh);
vsh = bgfx::createShader(vs_imgui_texture);
fsh = bgfx::createShader(fs_imgui_texture);
m_textureProgram = bgfx::createProgram(vsh, fsh);
bgfx::destroyShader(vsh);
bgfx::destroyShader(fsh);
vsh = bgfx::createShader(vs_imgui_cubemap);
fsh = bgfx::createShader(fs_imgui_cubemap);
m_cubeMapProgram = bgfx::createProgram(vsh, fsh);
bgfx::destroyShader(vsh);
bgfx::destroyShader(fsh);
vsh = bgfx::createShader(vs_imgui_latlong);
fsh = bgfx::createShader(fs_imgui_latlong);
m_latlongProgram = bgfx::createProgram(vsh, fsh);
bgfx::destroyShader(vsh);
bgfx::destroyShader(fsh);
vsh = bgfx::createShader(vs_imgui_image);
fsh = bgfx::createShader(fs_imgui_image);
m_imageProgram = bgfx::createProgram(vsh, fsh);
bgfx::destroyShader(fsh);
// Notice: using the same vsh.
fsh = bgfx::createShader(fs_imgui_image_swizz);
m_imageSwizzProgram = bgfx::createProgram(vsh, fsh);
bgfx::destroyShader(fsh);
bgfx::destroyShader(vsh);
m_missingTexture = genMissingTexture(256, 256, 0.04f);
#if !USE_NANOVG_FONT
const ImguiFontHandle handle = createFont(_data, _fontSize);
m_currentFontIdx = handle.idx;
#else
const ImguiFontHandle handle = { bgfx::invalidHandle };
#endif // !USE_NANOVG_FONT
return handle;
}
void destroy()
{
bgfx::destroyUniform(u_imageLodEnabled);
bgfx::destroyUniform(u_imageSwizzle);
bgfx::destroyUniform(s_texColor);
#if !USE_NANOVG_FONT
for (uint16_t ii = 0, num = m_fontHandle.getNumHandles(); ii < num; ++ii)
{
uint16_t idx = m_fontHandle.getHandleAt(0);
bgfx::destroyTexture(m_fonts[idx].m_texture);
m_fontHandle.free(idx);
}
#endif // !USE_NANOVG_FONT
bgfx::destroyTexture(m_missingTexture);
bgfx::destroyProgram(m_colorProgram);
bgfx::destroyProgram(m_textureProgram);
bgfx::destroyProgram(m_cubeMapProgram);
bgfx::destroyProgram(m_latlongProgram);
bgfx::destroyProgram(m_imageProgram);
bgfx::destroyProgram(m_imageSwizzProgram);
nvgDelete(m_nvg);
IMGUI_destroy();
}
bool anyActive() const
{
return m_active != 0;
}
inline void updatePresence(uint32_t _id)
{
if (m_checkActivePresence && m_active == _id)
{
m_isActivePresent = true;
}
}
uint32_t getId()
{
const uint32_t id = (m_areaId << 16) | m_widgetId++;
updatePresence(id);
return id;
}
bool isActive(uint32_t _id) const
{
return m_active == _id;
}
bool isActiveInputField(uint32_t _id) const
{
return m_inputField == _id;
}
bool isHot(uint32_t _id) const
{
return m_hot == _id;
}
bool inRect(int32_t _x, int32_t _y, int32_t _width, int32_t _height, bool _checkScroll = true) const
{
return (!_checkScroll || m_areas[m_areaId].m_inside)
&& m_mx >= _x
&& m_mx <= _x + _width
&& m_my >= _y
&& m_my <= _y + _height;
}
bool isEnabled(uint16_t _areaId)
{
return (m_enabledAreaIds>>_areaId)&0x1;
}
void setEnabled(uint16_t _areaId)
{
m_enabledAreaIds |= (UINT64_C(1)<<_areaId);
}
void clearInput()
{
m_leftPressed = false;
m_leftReleased = false;
m_scroll = 0;
}
void clearActive()
{
m_active = 0;
// mark all UI for this frame as processed
clearInput();
}
void clearActiveInputField()
{
m_inputField = 0;
}
void setActive(uint32_t _id)
{
m_active = _id;
m_wentActive = true;
m_inputField = 0;
}
void setActiveInputField(uint32_t _id)
{
m_inputField = _id;
}
void setHot(uint32_t _id)
{
m_hotToBe = _id;
}
bool buttonLogic(uint32_t _id, bool _over)
{
bool res = false;
// process down
if (!anyActive() )
{
if (_over)
{
setHot(_id);
}
if (isHot(_id)
&& m_leftPressed)
{
setActive(_id);
}
}
// if button is active, then react on left up
if (isActive(_id) )
{
if (_over)
{
setHot(_id);
}
if (m_leftReleased)
{
if (isHot(_id) )
{
res = true;
}
clearActive();
}
}
if (isHot(_id) )
{
m_isHot = true;
}
return res;
}
void inputLogic(uint32_t _id, bool _over)
{
if (!anyActive() )
{
if (_over)
{
setHot(_id);
}
if (isHot(_id)
&& m_leftPressed)
{
// Toggle active input.
if (isActiveInputField(_id) )
{
clearActiveInputField();
}
else
{
setActiveInputField(_id);
}
}
}
if (isHot(_id) )
{
m_isHot = true;
}
if (m_leftPressed
&& !m_isHot
&& m_inputField != 0)
{
clearActiveInputField();
}
}
void updateInput(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, char _inputChar)
{
bool left = (_button & IMGUI_MBUT_LEFT) != 0;
m_mx = _mx;
m_my = _my;
m_leftPressed = !m_left && left;
m_leftReleased = m_left && !left;
m_left = left;
m_scroll = _scroll;
_inputChar = _inputChar & 0x7f; // ASCII or GTFO! :)
m_lastChar = m_char;
m_char = _inputChar;
}
void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint16_t _surfaceWidth, uint16_t _surfaceHeight, char _inputChar, uint8_t _view)
{
m_view = _view;
m_viewWidth = _width;
m_viewHeight = _height;
m_surfaceWidth = _surfaceWidth;
m_surfaceHeight = _surfaceHeight;
const float xscale = float(m_surfaceWidth) /float(m_viewWidth);
const float yscale = float(m_surfaceHeight)/float(m_viewHeight);
const int32_t mx = int32_t(float(_mx)*xscale);
const int32_t my = int32_t(float(_my)*yscale);
IMGUI_beginFrame(mx, my, _button, _scroll, _width, _height, _inputChar, _view);
nvgBeginFrame(m_nvg, m_viewWidth, m_viewHeight, 1.0f);
nvgViewId(m_nvg, _view);
bgfx::setViewName(_view, "IMGUI");
bgfx::setViewSeq(_view, true);
const bgfx::HMD* hmd = bgfx::getHMD();
if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
{
m_viewWidth = _width / 2;
m_surfaceWidth = _surfaceWidth / 2;
float proj[16];
bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
static float time = 0.0f;
time += 0.05f;
const float dist = 10.0f;
const float offset0 = -proj[8] + (hmd->eye[0].viewOffset[0] / dist * proj[0]);
const float offset1 = -proj[8] + (hmd->eye[1].viewOffset[0] / dist * proj[0]);
float ortho[2][16];
const float viewOffset = _surfaceWidth/4.0f;
const float viewWidth = _surfaceWidth/2.0f;
bx::mtxOrtho(ortho[0], viewOffset, viewOffset + viewWidth, (float)m_surfaceHeight, 0.0f, 0.0f, 1000.0f, offset0);
bx::mtxOrtho(ortho[1], viewOffset, viewOffset + viewWidth, (float)m_surfaceHeight, 0.0f, 0.0f, 1000.0f, offset1);
bgfx::setViewTransform(_view, NULL, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
bgfx::setViewRect(_view, 0, 0, hmd->width, hmd->height);
}
else
{
float ortho[16];
bx::mtxOrtho(ortho, 0.0f, (float)m_surfaceWidth, (float)m_surfaceHeight, 0.0f, 0.0f, 1000.0f);
bgfx::setViewTransform(_view, NULL, ortho);
bgfx::setViewRect(_view, 0, 0, _width, _height);
}
updateInput(mx, my, _button, _scroll, _inputChar);
m_hot = m_hotToBe;
m_hotToBe = 0;
m_wentActive = false;
m_isHot = false;
Area& area = getCurrentArea();
area.m_widgetX = 0;
area.m_widgetY = 0;
area.m_widgetW = 0;
m_areaId.reset();
m_widgetId = 0;
m_enabledAreaIds = 0;
m_insideArea = false;
m_isActivePresent = false;
}
void endFrame()
{
if (m_checkActivePresence && !m_isActivePresent)
{
// The ui element is not present any more, reset active field.
m_active = 0;
}
m_checkActivePresence = (0 != m_active);
clearInput();
nvgEndFrame(m_nvg);
IMGUI_endFrame();
}
bool beginScroll(int32_t _height, int32_t* _scroll, bool _enabled)
{
Area& parentArea = getCurrentArea();
m_areaId.next();
const uint32_t scrollId = getId();
Area& area = getCurrentArea();
const uint16_t parentBottom = parentArea.m_scissorY + parentArea.m_scissorHeight;
const uint16_t childBottom = parentArea.m_widgetY + _height;
const uint16_t bottom = IMGUI_MIN(childBottom, parentBottom);
const uint16_t top = IMGUI_MAX(parentArea.m_widgetY, parentArea.m_scissorY);
area.m_contentX = parentArea.m_contentX;
area.m_contentY = parentArea.m_widgetY;
area.m_contentWidth = parentArea.m_contentWidth - (SCROLL_AREA_PADDING*3);
area.m_contentHeight = _height;
area.m_widgetX = parentArea.m_widgetX;
area.m_widgetY = parentArea.m_widgetY + (*_scroll);
area.m_widgetW = parentArea.m_widgetW - (SCROLL_AREA_PADDING*3);
area.m_scissorX = area.m_contentX;
area.m_scissorWidth = area.m_contentWidth;
area.m_scissorY = top - 1;
area.m_scissorHeight = bottom - top;
area.m_scissorEnabled = true;
area.m_height = _height;
area.m_scrollVal = _scroll;
area.m_scrollId = scrollId;
area.m_inside = inRect(parentArea.m_scissorX
, area.m_scissorY
, parentArea.m_scissorWidth
, area.m_scissorHeight
, false
);
area.m_didScroll = false;
parentArea.m_widgetY += (_height + DEFAULT_SPACING);
if (_enabled)
{
setEnabled(m_areaId);
}
nvgScissor(m_nvg, area);
m_insideArea |= area.m_inside;
return area.m_inside;
}
void endScroll(int32_t _r)
{
Area& area = getCurrentArea();
area.m_scissorEnabled = false;
const int32_t xx = area.m_contentX + area.m_contentWidth - 1;
const int32_t yy = area.m_contentY;
const int32_t width = SCROLL_AREA_PADDING * 2;
const int32_t height = area.m_height;
const int32_t aa = area.m_contentY+area.m_height;
const int32_t bb = area.m_widgetY-DEFAULT_SPACING;
const int32_t sbot = IMGUI_MAX(aa, bb);
const int32_t stop = area.m_contentY + (*area.m_scrollVal);
const int32_t sh = IMGUI_MAX(1, sbot - stop); // The scrollable area height.
const uint32_t hid = area.m_scrollId;
const float barHeight = (float)height / (float)sh;
const bool hasScrollBar = (barHeight < 1.0f);
// Handle mouse scrolling.
if (area.m_inside && !area.m_didScroll && !anyActive() )
{
if (m_scroll)
{
const int32_t diff = height - sh;
const int32_t val = *area.m_scrollVal + 20*m_scroll;
const int32_t min = (diff < 0) ? diff : *area.m_scrollVal;
const int32_t max = 0;
const int32_t newVal = IMGUI_CLAMP(val, min, max);
*area.m_scrollVal = newVal;
if (hasScrollBar)
{
area.m_didScroll = true;
}
}
}
area.m_inside = false;
int32_t* scroll = area.m_scrollVal;
// This must be called here before drawing scroll bars
// so that scissor of parrent area applies.
m_areaId.pop();
// Propagate 'didScroll' to parrent area to avoid scrolling multiple areas at once.
Area& parentArea = getCurrentArea();
parentArea.m_didScroll = (parentArea.m_didScroll || area.m_didScroll);
// Draw and handle scroll click.
if (hasScrollBar)
{
const float barY = bx::fsaturate( (float)(-(*scroll) ) / (float)sh);
// Handle scroll bar logic.
const int32_t hx = xx;
const int32_t hy = yy + (int)(barY * height);
const int32_t hw = width;
const int32_t hh = (int)(barHeight * height);
const int32_t range = height - (hh - 1);
const bool over = inRect(hx, hy, hw, hh);
buttonLogic(hid, over);
if (isActive(hid) )
{
float uu = (float)(hy - yy) / (float)range;
if (m_wentActive)
{
m_dragY = m_my;
m_dragOrig = uu;
}
if (m_dragY != m_my)
{
const int32_t diff = height - sh;
const int32_t drag = m_my - m_dragY;
const float dragFactor = float(sh)/float(height);
const int32_t val = *scroll - int32_t(drag*dragFactor);
const int32_t min = (diff < 0) ? diff : *scroll;
const int32_t max = 0;
*scroll = IMGUI_CLAMP(val, min, max);
m_dragY = m_my;
}
}
// BG
drawRoundedRect( (float)xx
, (float)yy
, (float)width
, (float)height
, (float)_r
, imguiRGBA(0, 0, 0, 196)
);
// Bar
if (isActive(hid) )
{
drawRoundedRect( (float)hx
, (float)hy
, (float)hw
, (float)hh
, (float)_r
, imguiRGBA(255, 196, 0, 196)
);
}
else
{
drawRoundedRect( (float)hx
, (float)hy
, (float)hw
, (float)hh
, (float)_r
, isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64)
);
}
}
else
{
// Clear active if scroll is selected but not visible any more.
if (isActive(hid) )
{
clearActive();
}
}
nvgScissor(m_nvg, parentArea);
}
bool beginArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, bool _enabled, int32_t _r)
{
m_areaId.next();
const uint32_t scrollId = getId();
const bool hasTitle = (NULL != _name && '\0' != _name[0]);
const int32_t header = hasTitle ? AREA_HEADER : 0;
Area& area = getCurrentArea();
area.m_x = _x;
area.m_y = _y;
area.m_width = _width;
area.m_height = _height;
area.m_contentX = area.m_x + SCROLL_AREA_PADDING;
area.m_contentY = area.m_y + SCROLL_AREA_PADDING + header;
area.m_contentWidth = area.m_width - SCROLL_AREA_PADDING;
area.m_contentHeight = area.m_height - SCROLL_AREA_PADDING*2 - header;
area.m_scissorX = area.m_contentX;
area.m_scissorY = area.m_y + SCROLL_AREA_PADDING + header;
area.m_scissorHeight = area.m_height - SCROLL_AREA_PADDING*2 - header;
area.m_scissorWidth = area.m_contentWidth;
area.m_scissorEnabled = false;
area.m_widgetX = area.m_contentX;
area.m_widgetY = area.m_contentY;
area.m_widgetW = area.m_width - SCROLL_AREA_PADDING*2;
static int32_t s_zeroScroll = 0;
area.m_scrollVal = &s_zeroScroll;
area.m_scrollId = scrollId;
area.m_inside = inRect(area.m_scissorX, area.m_scissorY, area.m_scissorWidth, area.m_scissorHeight, false);
area.m_didScroll = false;
if (_enabled)
{
setEnabled(m_areaId);
}
if (0 == _r)
{
drawRect( (float)_x
, (float)_y
, (float)_width + 0.3f /*border fix for seamlessly joining two scroll areas*/
, (float)_height + 0.3f /*border fix for seamlessly joining two scroll areas*/
, imguiRGBA(0, 0, 0, 192)
);
}
else
{
drawRoundedRect( (float)_x
, (float)_y
, (float)_width
, (float)_height
, (float)_r
, imguiRGBA(0, 0, 0, 192)
);
}
if (hasTitle)
{
drawText(_x + 10
, _y + 18
, ImguiTextAlign::Left
, _name
, imguiRGBA(255, 255, 255, 128)
);
}
area.m_scissorEnabled = true;
nvgScissor(m_nvg, area);
m_insideArea |= area.m_inside;
return area.m_inside;
}
void endArea()
{
m_areaId.pop();
nvgResetScissor(m_nvg);
}
bool button(const char* _text, bool _enabled, ImguiAlign::Enum _align, uint32_t _rgb0, int32_t _r)
{
const uint32_t id = getId();
Area& area = getCurrentArea();
const int32_t yy = area.m_widgetY;
const int32_t height = BUTTON_HEIGHT;
area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
int32_t xx;
int32_t width;
if (ImguiAlign::Left == _align)
{
xx = area.m_contentX + SCROLL_AREA_PADDING;
width = area.m_widgetW;
}
else if (ImguiAlign::LeftIndented == _align
|| ImguiAlign::Right == _align)
{
xx = area.m_widgetX;
width = area.m_widgetW-1; //TODO: -1 !
}
else //if (ImguiAlign::Center == _align
//|| ImguiAlign::CenterIndented == _align).
{
xx = area.m_widgetX;
width = area.m_widgetW - (area.m_widgetX-area.m_contentX);
}
const bool enabled = _enabled && isEnabled(m_areaId);
const bool over = enabled && inRect(xx, yy, width, height);
const bool res = buttonLogic(id, over);
const uint32_t rgb0 = _rgb0&0x00ffffff;
if (!visible(yy, height, area.m_scissorY, area.m_scissorHeight))
{
return false;
}
drawRoundedRect( (float)xx
, (float)yy
, (float)width
, (float)height
, (float)_r
, rgb0 | imguiRGBA(0, 0, 0, isActive(id) ? 196 : 96)
);
if (enabled)
{
drawText(xx + BUTTON_HEIGHT / 2
, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Left
, _text
, isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
);
}
else
{
drawText(xx + BUTTON_HEIGHT / 2
, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Left
, _text
, imguiRGBA(128, 128, 128, 200)
);
}
return res;
}
bool item(const char* _text, bool _enabled)
{
const uint32_t id = getId();
Area& area = getCurrentArea();
const int32_t xx = area.m_widgetX;
const int32_t yy = area.m_widgetY;
const int32_t width = area.m_widgetW;
const int32_t height = BUTTON_HEIGHT;
area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
const bool enabled = _enabled && isEnabled(m_areaId);
const bool over = enabled && inRect(xx, yy, width, height);
const bool res = buttonLogic(id, over);
if (!visible(yy, height, area.m_scissorY, area.m_scissorHeight))
{
return false;
}
if (isHot(id) )
{
drawRoundedRect( (float)xx
, (float)yy
, (float)width
, (float)height
, 2.0f
, imguiRGBA(255, 196, 0, isActive(id) ? 196 : 96)
);
}
if (enabled)
{
drawText(xx + BUTTON_HEIGHT / 2
, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Left
, _text
, imguiRGBA(255, 255, 255, 200)
);
}
else
{
drawText(xx + BUTTON_HEIGHT / 2
, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Left
, _text
, imguiRGBA(128, 128, 128, 200)
);
}
return res;
}
bool check(const char* _text, bool _checked, bool _enabled)
{
const uint32_t id = getId();
Area& area = getCurrentArea();
const int32_t xx = area.m_widgetX;
const int32_t yy = area.m_widgetY;
const int32_t width = area.m_widgetW;
const int32_t height = BUTTON_HEIGHT;
area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
const bool enabled = _enabled && isEnabled(m_areaId);
const bool over = enabled && inRect(xx, yy, width, height);
const bool res = buttonLogic(id, over);
const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
if (!visible(cy, CHECK_SIZE+6, area.m_scissorY, area.m_scissorHeight))
{
return false;
}
drawRoundedRect( (float)cx - 3
, (float)cy - 3
, (float)CHECK_SIZE + 6
, (float)CHECK_SIZE + 6
, 4
, imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
);
if (_checked)
{
if (enabled)
{
drawRoundedRect( (float)cx
, (float)cy
, (float)CHECK_SIZE
, (float)CHECK_SIZE
, (float)CHECK_SIZE / 2 - 1
, imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
);
}
else
{
drawRoundedRect( (float)cx
, (float)cy
, (float)CHECK_SIZE
, (float)CHECK_SIZE
, (float)CHECK_SIZE / 2 - 1
, imguiRGBA(128, 128, 128, 200)
);
}
}
if (enabled)
{
drawText(xx + BUTTON_HEIGHT
, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Left
, _text
, isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
);
}
else
{
drawText(xx + BUTTON_HEIGHT
, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Left
, _text
, imguiRGBA(128, 128, 128, 200)
);
}
return res;
}
void input(const char* _label, char* _str, uint32_t _len, bool _enabled, ImguiAlign::Enum _align, int32_t _r)
{
const uint32_t id = getId();
Area& area = getCurrentArea();
const int32_t yy = area.m_widgetY;
area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
int32_t xx;
int32_t width;
if (ImguiAlign::Left == _align)
{
xx = area.m_contentX + SCROLL_AREA_PADDING;
width = area.m_widgetW;
}
else if (ImguiAlign::LeftIndented == _align
|| ImguiAlign::Right == _align)
{
xx = area.m_widgetX;
width = area.m_widgetW-1; //TODO: -1 !
}
else //if (ImguiAlign::Center == _align
//|| ImguiAlign::CenterIndented == _align).
{
xx = area.m_widgetX;
width = area.m_widgetW - (area.m_widgetX-area.m_contentX);
}
const bool drawLabel = (NULL != _label && _label[0] != '\0');
if (drawLabel)
{
drawText(xx
, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Left
, _label
, imguiRGBA(255, 255, 255, 200)
);
}
// Handle input.
if (isActiveInputField(id) )
{
const size_t cursor = size_t(strlen(_str) );
if (m_char == 0x08 || m_char == 0x7f) //backspace or delete
{
_str[cursor-1] = '\0';
}
else if (m_char == 0x0d || m_char == 0x1b) //enter or escape
{
clearActiveInputField();
}
else if (cursor < _len-1
&& 0 != m_char)
{
_str[cursor] = m_char;
_str[cursor+1] = '\0';
}
}
// Draw input area.
const int32_t height = BUTTON_HEIGHT;
if (drawLabel)
{
uint32_t numVertices = 0; //unused
const int32_t labelWidth = int32_t(getTextLength(m_fonts[m_currentFontIdx].m_cdata, _label, numVertices) );
xx += (labelWidth + 6);
width -= (labelWidth + 6);
}
const bool enabled = _enabled && isEnabled(m_areaId);
const bool over = enabled && inRect(xx, yy, width, height);
inputLogic(id, over);
drawRoundedRect( (float)xx
, (float)yy
, (float)width
, (float)height
, (float)_r
, isActiveInputField(id)?imguiRGBA(255,196,0,255):imguiRGBA(128,128,128,96)
);
if (isActiveInputField(id) )
{
drawText(xx + 6
, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Left
, _str
, imguiRGBA(0, 0, 0, 255)
);
}
else
{
drawText(xx + 6
, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Left
, _str
, isHot(id) ? imguiRGBA(255,196,0,255) : imguiRGBA(255,255,255,255)
);
}
}
uint8_t tabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, uint8_t _nTabs, uint8_t _nEnabled, va_list _argList)
{
const char* titles[16];
bool tabEnabled[16];
const uint8_t tabCount = IMGUI_MIN(_nTabs, 16);
const uint8_t enabledCount = IMGUI_MIN(_nEnabled, 16);
// Read titles.
{
uint8_t ii = 0;
for (; ii < tabCount; ++ii)
{
const char* str = va_arg(_argList, const char*);
titles[ii] = str;
}
for (; ii < _nTabs; ++ii)
{
const char* str = va_arg(_argList, const char*);
BX_UNUSED(str);
}
}
// Read enabled tabs.
{
uint8_t ii = 0;
for (; ii < enabledCount; ++ii)
{
const bool enabled = (0 != va_arg(_argList, int) );
tabEnabled[ii] = enabled;
}
for (; ii < _nEnabled; ++ii)
{
const int enabled = va_arg(_argList, int);
BX_UNUSED(enabled);
}
for (; ii < _nTabs; ++ii)
{
tabEnabled[ii] = true;
}
}
Area& area = getCurrentArea();
const int32_t yy = area.m_widgetY;
area.m_widgetY += _height + DEFAULT_SPACING;
int32_t xx;
int32_t width;
if (ImguiAlign::Left == _align)
{
xx = area.m_contentX + SCROLL_AREA_PADDING;
width = area.m_widgetW;
}
else if (ImguiAlign::LeftIndented == _align
|| ImguiAlign::Right == _align)
{
xx = area.m_widgetX;
width = area.m_widgetW-1; //TODO: -1 !
}
else //if (ImguiAlign::Center == _align
//|| ImguiAlign::CenterIndented == _align).
{
xx = area.m_widgetX;
width = area.m_widgetW - (area.m_widgetX-area.m_contentX);
}
uint8_t selected = _selected;
const int32_t tabWidth = width / tabCount;
const int32_t tabWidthHalf = width / (tabCount*2);
const int32_t textY = yy + _height/2 + int32_t(m_fonts[m_currentFontIdx].m_size)/2 - 2;
drawRoundedRect( (float)xx
, (float)yy
, (float)width
, (float)_height
, (float)_r
, _enabled?imguiRGBA(128,128,128,96):imguiRGBA(128,128,128,64)
);
for (uint8_t ii = 0; ii < tabCount; ++ii)
{
const uint32_t id = getId();
int32_t buttonX = xx + ii*width/tabCount;
int32_t textX = buttonX + tabWidthHalf;
const bool enabled = _enabled && tabEnabled[ii] && isEnabled(m_areaId);
const bool over = enabled && inRect(buttonX, yy, tabWidth, _height);
const bool res = buttonLogic(id, over);
const uint32_t textColor = (ii == selected)
? (enabled ? imguiRGBA(0,0,0,255) : imguiRGBA(255,255,255,100) )
: (isHot(id) ? imguiRGBA(255,196,0,enabled?255:100) : imguiRGBA(255,255,255,enabled?200:100) )
;
if (ii == selected)
{
drawRoundedRect( (float)buttonX
, (float)yy
, (float)tabWidth
, (float)_height
, (float)_r
, enabled?imguiRGBA(255,196,0,200):imguiRGBA(128,128,128,32)
);
}
else if (isActive(id) )
{
drawRoundedRect( (float)buttonX
, (float)yy
, (float)tabWidth
, (float)_height
, (float)_r
, imguiRGBA(128,128,128,196)
);
}
drawText(textX
, textY
, ImguiTextAlign::Center
, titles[ii]
, textColor
);
if (res)
{
selected = ii;
}
}
return selected;
}
bool image(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _enabled, bool _originBottomLeft)
{
const uint32_t id = getId();
Area& area = getCurrentArea();
int32_t xx;
if (ImguiAlign::Left == _align)
{
xx = area.m_contentX + SCROLL_AREA_PADDING;
}
else if (ImguiAlign::LeftIndented == _align)
{
xx = area.m_widgetX;
}
else if (ImguiAlign::Center == _align)
{
xx = area.m_contentX + (area.m_widgetW-_width)/2;
}
else if (ImguiAlign::CenterIndented == _align)
{
xx = (area.m_widgetX + area.m_widgetW + area.m_contentX - _width)/2;
}
else //if (ImguiAlign::Right == _align).
{
xx = area.m_contentX + area.m_widgetW - _width;
}
const int32_t yy = area.m_widgetY;
area.m_widgetY += _height + DEFAULT_SPACING;
if (screenQuad(xx, yy, _width, _height, _originBottomLeft) )
{
const bool enabled = _enabled && isEnabled(m_areaId);
const bool over = enabled && inRect(xx, yy, _width, _height);
const bool res = buttonLogic(id, over);
const float lodEnabled[4] = { _lod, float(enabled), 0.0f, 0.0f };
bgfx::setUniform(u_imageLodEnabled, lodEnabled);
bgfx::setTexture(0, s_texColor, bgfx::isValid(_image) ? _image : m_missingTexture);
bgfx::setState(BGFX_STATE_RGB_WRITE
|BGFX_STATE_ALPHA_WRITE
|BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
);
setCurrentScissor();
bgfx::submit(m_view, m_imageProgram);
return res;
}
return false;
}
bool image(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _enabled, bool _originBottomLeft)
{
const float width = _width*float(getCurrentArea().m_widgetW);
const float height = width/_aspect;
return image(_image, _lod, int32_t(width), int32_t(height), _align, _enabled, _originBottomLeft);
}
bool imageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _enabled)
{
BX_CHECK(_channel < 4, "Channel param must be from 0 to 3!");
const uint32_t id = getId();
Area& area = getCurrentArea();
int32_t xx;
if (ImguiAlign::Left == _align)
{
xx = area.m_contentX + SCROLL_AREA_PADDING;
}
else if (ImguiAlign::LeftIndented == _align)
{
xx = area.m_widgetX;
}
else if (ImguiAlign::Center == _align)
{
xx = area.m_contentX + (area.m_widgetW-_width)/2;
}
else if (ImguiAlign::CenterIndented == _align)
{
xx = (area.m_widgetX + area.m_widgetW + area.m_contentX - _width)/2;
}
else //if (ImguiAlign::Right == _align).
{
xx = area.m_contentX + area.m_widgetW - _width;
}
const int32_t yy = area.m_widgetY;
area.m_widgetY += _height + DEFAULT_SPACING;
if (screenQuad(xx, yy, _width, _height) )
{
const bool enabled = _enabled && isEnabled(m_areaId);
const bool over = enabled && inRect(xx, yy, _width, _height);
const bool res = buttonLogic(id, over);
const float lodEnabled[4] = { _lod, float(enabled), 0.0f, 0.0f };
bgfx::setUniform(u_imageLodEnabled, lodEnabled);
float swizz[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
swizz[_channel] = 1.0f;
bgfx::setUniform(u_imageSwizzle, swizz);
bgfx::setTexture(0, s_texColor, bgfx::isValid(_image) ? _image : m_missingTexture);
bgfx::setState(BGFX_STATE_RGB_WRITE
|BGFX_STATE_ALPHA_WRITE
|BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
);
setCurrentScissor();
bgfx::submit(m_view, m_imageSwizzProgram);
return res;
}
return false;
}
bool imageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _enabled)
{
const float width = _width*float(getCurrentArea().m_widgetW);
const float height = width/_aspect;
return imageChannel(_image, _channel, _lod, int32_t(width), int32_t(height), _align, _enabled);
}
bool latlong(bgfx::TextureHandle _cubemap, float _lod, ImguiAlign::Enum _align, bool _enabled)
{
const uint32_t id = getId();
Area& area = getCurrentArea();
int32_t xx;
int32_t width;
if (ImguiAlign::Left == _align)
{
xx = area.m_contentX + SCROLL_AREA_PADDING;
width = area.m_widgetW;
}
else if (ImguiAlign::LeftIndented == _align
|| ImguiAlign::Right == _align)
{
xx = area.m_widgetX;
width = area.m_widgetW;
}
else //if (ImguiAlign::Center == _align
//|| ImguiAlign::CenterIndented == _align).
{
xx = area.m_widgetX;
width = area.m_widgetW - (area.m_widgetX-area.m_contentX);
}
const int32_t height = width/2;
const int32_t yy = area.m_widgetY;
area.m_widgetY += height + DEFAULT_SPACING;
if (screenQuad(xx, yy, width, height, false) )
{
const bool enabled = _enabled && isEnabled(m_areaId);
const bool over = enabled && inRect(xx, yy, width, height);
const bool res = buttonLogic(id, over);
const float lodEnabled[4] = { _lod, float(enabled), 0.0f, 0.0f };
bgfx::setUniform(u_imageLodEnabled, lodEnabled);
bgfx::setTexture(0, s_texColor, _cubemap);
bgfx::setState(BGFX_STATE_RGB_WRITE
|BGFX_STATE_ALPHA_WRITE
|BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
);
setCurrentScissor();
bgfx::submit(m_view, m_latlongProgram);
return res;
}
return false;
}
bool cubeMap(bgfx::TextureHandle _cubemap, float _lod, bool _cross, bool _sameHeight, ImguiAlign::Enum _align, bool _enabled)
{
const uint32_t id = getId();
Area& area = getCurrentArea();
int32_t xx;
int32_t width;
if (ImguiAlign::Left == _align)
{
xx = area.m_contentX + SCROLL_AREA_PADDING;
width = area.m_widgetW;
}
else if (ImguiAlign::LeftIndented == _align
|| ImguiAlign::Right == _align)
{
xx = area.m_widgetX;
width = area.m_widgetW;
}
else //if (ImguiAlign::Center == _align
//|| ImguiAlign::CenterIndented == _align).
{
xx = area.m_widgetX;
width = area.m_widgetW - (area.m_widgetX-area.m_contentX);
}
const bool adjustHeight = (_cross && _sameHeight);
const bool fullHeight = (_cross && !_sameHeight);
if (adjustHeight)
{
xx += width/6;
}
const int32_t height = fullHeight ? (width*3)/4 : (width/2);
const int32_t yy = area.m_widgetY;
area.m_widgetY += height + DEFAULT_SPACING;
const uint32_t numVertices = 14;
const uint32_t numIndices = 36;
if (bgfx::checkAvailTransientBuffers(numVertices, PosNormalVertex::ms_decl, numIndices) )
{
bgfx::TransientVertexBuffer tvb;
bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosNormalVertex::ms_decl);
bgfx::TransientIndexBuffer tib;
bgfx::allocTransientIndexBuffer(&tib, numIndices);
PosNormalVertex* vertex = (PosNormalVertex*)tvb.data;
uint16_t* indices = (uint16_t*)tib.data;
if (_cross)
{
vertex->set(0.0f, 0.5f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
vertex->set(0.0f, 1.0f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
vertex->set(0.5f, 0.0f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
vertex->set(0.5f, 0.5f, 0.0f, -1.0f, 1.0f, 1.0f); ++vertex;
vertex->set(0.5f, 1.0f, 0.0f, -1.0f, -1.0f, 1.0f); ++vertex;
vertex->set(0.5f, 1.5f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
vertex->set(1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f); ++vertex;
vertex->set(1.0f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f); ++vertex;
vertex->set(1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); ++vertex;
vertex->set(1.0f, 1.5f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
vertex->set(1.5f, 0.5f, 0.0f, 1.0f, 1.0f, -1.0f); ++vertex;
vertex->set(1.5f, 1.0f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
vertex->set(2.0f, 0.5f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
vertex->set(2.0f, 1.0f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
indices += addQuad(indices, 0, 3, 4, 1);
indices += addQuad(indices, 2, 6, 7, 3);
indices += addQuad(indices, 3, 7, 8, 4);
indices += addQuad(indices, 4, 8, 9, 5);
indices += addQuad(indices, 7, 10, 11, 8);
indices += addQuad(indices, 10, 12, 13, 11);
}
else
{
vertex->set(0.0f, 0.25f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
vertex->set(0.0f, 0.75f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
vertex->set(0.5f, 0.00f, 0.0f, -1.0f, 1.0f, 1.0f); ++vertex;
vertex->set(0.5f, 0.50f, 0.0f, -1.0f, -1.0f, 1.0f); ++vertex;
vertex->set(0.5f, 1.00f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
vertex->set(1.0f, 0.25f, 0.0f, 1.0f, 1.0f, 1.0f); ++vertex;
vertex->set(1.0f, 0.75f, 0.0f, 1.0f, -1.0f, 1.0f); ++vertex;
vertex->set(1.0f, 0.25f, 0.0f, 1.0f, 1.0f, 1.0f); ++vertex;
vertex->set(1.0f, 0.75f, 0.0f, 1.0f, -1.0f, 1.0f); ++vertex;
vertex->set(1.5f, 0.00f, 0.0f, -1.0f, 1.0f, 1.0f); ++vertex;
vertex->set(1.5f, 0.50f, 0.0f, 1.0f, 1.0f, -1.0f); ++vertex;
vertex->set(1.5f, 1.00f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
vertex->set(2.0f, 0.25f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
vertex->set(2.0f, 0.75f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
indices += addQuad(indices, 0, 2, 3, 1);
indices += addQuad(indices, 1, 3, 6, 4);
indices += addQuad(indices, 2, 5, 6, 3);
indices += addQuad(indices, 7, 9, 12, 10);
indices += addQuad(indices, 7, 10, 11, 8);
indices += addQuad(indices, 10, 12, 13, 11);
}
const bool enabled = _enabled && isEnabled(m_areaId);
const bool over = enabled && inRect(xx, yy, width, height);
const bool res = buttonLogic(id, over);
const float widthf = float(width);
const float scale = adjustHeight ? (widthf+0.5f)/3.0f : (widthf*0.5f + 0.25f);
float mtx[16];
bx::mtxSRT(mtx, scale, scale, 1.0f, 0.0f, 0.0f, 0.0f, float(xx), float(yy), 0.0f);
const float lodEnabled[4] = { _lod, float(enabled), 0.0f, 0.0f };
bgfx::setUniform(u_imageLodEnabled, lodEnabled);
bgfx::setTransform(mtx);
bgfx::setTexture(0, s_texColor, _cubemap);
bgfx::setVertexBuffer(&tvb);
bgfx::setIndexBuffer(&tib);
bgfx::setState(BGFX_STATE_RGB_WRITE
|BGFX_STATE_ALPHA_WRITE
|BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
);
setCurrentScissor();
bgfx::submit(m_view, m_cubeMapProgram);
return res;
}
return false;
}
bool cubeMap(bgfx::TextureHandle _cubemap, float _lod, ImguiCubemap::Enum _display, bool _sameHeight, ImguiAlign::Enum _align, bool _enabled)
{
if (ImguiCubemap::Cross == _display
|| ImguiCubemap::Hex == _display)
{
return cubeMap(_cubemap, _lod, (ImguiCubemap::Cross == _display), _sameHeight, _align, _enabled);
}
else //(ImguiCubemap::Latlong == _display).
{
return latlong(_cubemap, _lod, _align, _enabled);
}
}
bool collapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
{
const uint32_t id = getId();
Area& area = getCurrentArea();
const int32_t xx = area.m_widgetX;
const int32_t yy = area.m_widgetY;
const int32_t width = area.m_widgetW;
const int32_t height = BUTTON_HEIGHT;
area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
const int32_t cx = xx + BUTTON_HEIGHT/2 - CHECK_SIZE/2;
const int32_t cy = yy + BUTTON_HEIGHT/2 - CHECK_SIZE/2 + DEFAULT_SPACING/2;
const int32_t textY = yy + BUTTON_HEIGHT/2 + TEXT_HEIGHT/2 + DEFAULT_SPACING/2;
const bool enabled = _enabled && isEnabled(m_areaId);
const bool over = enabled && inRect(xx, yy, width, height);
const bool res = buttonLogic(id, over);
if (_checked)
{
drawTriangle(cx
, cy
, CHECK_SIZE
, CHECK_SIZE
, TriangleOrientation::Up
, imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
);
}
else
{
drawTriangle(cx-1 // With -1 is more aesthetically pleasing.
, cy
, CHECK_SIZE
, CHECK_SIZE
, TriangleOrientation::Right
, imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
);
}
if (enabled)
{
drawText(xx + BUTTON_HEIGHT
, textY
, ImguiTextAlign::Left
, _text
, isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
);
}
else
{
drawText(xx + BUTTON_HEIGHT
, textY
, ImguiTextAlign::Left
, _text
, imguiRGBA(128, 128, 128, 200)
);
}
if (_subtext)
{
drawText(xx + width - BUTTON_HEIGHT / 2
, textY
, ImguiTextAlign::Right
, _subtext
, imguiRGBA(255, 255, 255, 128)
);
}
return res;
}
bool borderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled)
{
// Since border button isn't part of any area, just use this custom/unique areaId.
const uint16_t areaId = UINT16_MAX-1;
const uint32_t id = (areaId << 16) | m_widgetId++;
updatePresence(id);
const int32_t triSize = 12;
const int32_t borderSize = 15;
int32_t xx;
int32_t yy;
int32_t width;
int32_t height;
int32_t triX;
int32_t triY;
TriangleOrientation::Enum orientation;
if (ImguiBorder::Left == _border)
{
xx = -borderSize;
yy = -1;
width = 2*borderSize+1;
height = m_surfaceHeight+1;
triX = 0;
triY = (m_surfaceHeight-triSize)/2;
orientation = _checked ? TriangleOrientation::Left : TriangleOrientation::Right;
}
else if (ImguiBorder::Right == _border)
{
xx = m_surfaceWidth - borderSize;
yy = -1;
width = 2*borderSize+1;
height = m_surfaceHeight+1;
triX = m_surfaceWidth - triSize - 2;
triY = (m_surfaceHeight-width)/2;
orientation = _checked ? TriangleOrientation::Right : TriangleOrientation::Left;
}
else if (ImguiBorder::Top == _border)
{
xx = 0;
yy = -borderSize;
width = m_surfaceWidth;
height = 2*borderSize;
triX = (m_surfaceWidth-triSize)/2;
triY = 0;
orientation = _checked ? TriangleOrientation::Up : TriangleOrientation::Down;
}
else //if (ImguiBorder::Bottom == _border).
{
xx = 0;
yy = m_surfaceHeight - borderSize;
width = m_surfaceWidth;
height = 2*borderSize;
triX = (m_surfaceWidth-triSize)/2;
triY = m_surfaceHeight-triSize;
orientation = _checked ? TriangleOrientation::Down : TriangleOrientation::Up;
}
const bool over = _enabled && inRect(xx, yy, width, height, false);
const bool res = buttonLogic(id, over);
drawRect( (float)xx
, (float)yy
, (float)width
, (float)height
, isActive(id) ? imguiRGBA(23, 23, 23, 192) : imguiRGBA(0, 0, 0, 222)
);
drawTriangle( triX
, triY
, triSize
, triSize
, orientation
, isHot(id) ? imguiRGBA(255, 196, 0, 222) : imguiRGBA(255, 255, 255, 192)
);
return res;
}
void labelVargs(const char* _format, va_list _argList, uint32_t _rgba)
{
char temp[8192];
char* out = temp;
int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
if ( (int32_t)sizeof(temp) < len)
{
out = (char*)alloca(len+1);
len = bx::vsnprintf(out, len, _format, _argList);
}
out[len] = '\0';
Area& area = getCurrentArea();
const int32_t xx = area.m_widgetX;
const int32_t yy = area.m_widgetY;
area.m_widgetY += BUTTON_HEIGHT;
drawText(xx
, yy + BUTTON_HEIGHT/2 + TEXT_HEIGHT/2
, ImguiTextAlign::Left
, out
, _rgba
);
}
void value(const char* _text)
{
Area& area = getCurrentArea();
const int32_t xx = area.m_widgetX;
const int32_t yy = area.m_widgetY;
const int32_t ww = area.m_widgetW;
area.m_widgetY += BUTTON_HEIGHT;
drawText(xx + ww - BUTTON_HEIGHT / 2
, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Right
, _text
, imguiRGBA(255, 255, 255, 200)
);
}
bool slider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled, ImguiAlign::Enum _align)
{
const uint32_t id = getId();
Area& area = getCurrentArea();
const int32_t yy = area.m_widgetY;
const int32_t height = SLIDER_HEIGHT;
area.m_widgetY += SLIDER_HEIGHT + DEFAULT_SPACING;
int32_t xx;
int32_t width;
if (ImguiAlign::Left == _align)
{
xx = area.m_contentX + SCROLL_AREA_PADDING;
width = area.m_widgetW;
}
else if (ImguiAlign::LeftIndented == _align
|| ImguiAlign::Right == _align)
{
xx = area.m_widgetX;
width = area.m_widgetW;
}
else //if (ImguiAlign::Center == _align
//|| ImguiAlign::CenterIndented == _align).
{
xx = area.m_widgetX;
width = area.m_widgetW - (area.m_widgetX-area.m_contentX);
}
drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, 4.0f, imguiRGBA(0, 0, 0, 128) );
const int32_t range = width - SLIDER_MARKER_WIDTH;
float uu = bx::fsaturate( (_val - _vmin) / (_vmax - _vmin) );
int32_t m = (int)(uu * range);
bool valChanged = false;
const bool enabled = _enabled && isEnabled(m_areaId);
const bool over = enabled && inRect(xx + m, yy, SLIDER_MARKER_WIDTH, SLIDER_HEIGHT);
const bool res = buttonLogic(id, over);
if (isActive(id) )
{
if (m_wentActive)
{
m_dragX = m_mx;
m_dragOrig = uu;
}
if (m_dragX != m_mx)
{
uu = bx::fsaturate(m_dragOrig + (float)(m_mx - m_dragX) / (float)range);
_val = _vmin + uu * (_vmax - _vmin);
_val = floorf(_val / _vinc + 0.5f) * _vinc; // Snap to vinc
m = (int)(uu * range);
valChanged = true;
}
}
if (isActive(id) )
{
drawRoundedRect( (float)(xx + m)
, (float)yy
, (float)SLIDER_MARKER_WIDTH
, (float)SLIDER_HEIGHT
, 4.0f
, imguiRGBA(255, 255, 255, 255)
);
}
else
{
drawRoundedRect( (float)(xx + m)
, (float)yy
, (float)SLIDER_MARKER_WIDTH
, (float)SLIDER_HEIGHT
, 4.0f
, isHot(id) ? imguiRGBA(255, 196, 0, 128) : imguiRGBA(255, 255, 255, 64)
);
}
// TODO: fix this, take a look at 'nicenum'.
int32_t digits = (int)(ceilf(log10f(_vinc) ) );
char fmt[16];
bx::snprintf(fmt, 16, "%%.%df", digits >= 0 ? 0 : -digits);
char msg[128];
bx::snprintf(msg, 128, fmt, _val);
if (enabled)
{
drawText(xx + SLIDER_HEIGHT / 2
, yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Left
, _text
, isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
);
drawText(xx + width - SLIDER_HEIGHT / 2
, yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Right
, msg
, isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
);
}
else
{
drawText(xx + SLIDER_HEIGHT / 2
, yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Left
, _text
, imguiRGBA(128, 128, 128, 200)
);
drawText(xx + width - SLIDER_HEIGHT / 2
, yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
, ImguiTextAlign::Right
, msg
, imguiRGBA(128, 128, 128, 200)
);
}
return res || valChanged;
}
void indent(uint16_t _width)
{
Area& area = getCurrentArea();
area.m_widgetX += _width;
area.m_widgetW -= _width;
}
void unindent(uint16_t _width)
{
Area& area = getCurrentArea();
area.m_widgetX -= _width;
area.m_widgetW += _width;
}
void separator(uint16_t _height)
{
Area& area = getCurrentArea();
area.m_widgetY += _height;
}
void separatorLine(uint16_t _height, ImguiAlign::Enum _align)
{
Area& area = getCurrentArea();
//const int32_t width = area.m_widgetW;
const int32_t height = 1;
//const int32_t xx = area.m_widgetX;
const int32_t yy = area.m_widgetY + _height/2 - height;
int32_t xx;
int32_t width;
if (ImguiAlign::Left == _align)
{
xx = area.m_contentX + SCROLL_AREA_PADDING;
width = area.m_widgetW;
}
else if (ImguiAlign::LeftIndented == _align
|| ImguiAlign::Right == _align)
{
xx = area.m_widgetX;
width = area.m_widgetW;
}
else //if (ImguiAlign::Center == _align
//|| ImguiAlign::CenterIndented == _align).
{
xx = area.m_widgetX;
width = area.m_widgetW - (area.m_widgetX-area.m_contentX) + 1;
}
area.m_widgetY += _height;
drawRect( (float)xx
, (float)yy
, (float)width
, (float)height
, imguiRGBA(255, 255, 255, 32)
);
}
void drawPolygon(const float* _coords, uint32_t _numCoords, float _r, uint32_t _abgr)
{
_numCoords = bx::uint32_min(_numCoords, MAX_TEMP_COORDS);
for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
{
const float* v0 = &_coords[jj * 2];
const float* v1 = &_coords[ii * 2];
float dx = v1[0] - v0[0];
float dy = v1[1] - v0[1];
float d = sqrtf(dx * dx + dy * dy);
if (d > 0)
{
d = 1.0f / d;
dx *= d;
dy *= d;
}
m_tempNormals[jj * 2 + 0] = dy;
m_tempNormals[jj * 2 + 1] = -dx;
}
for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
{
float dlx0 = m_tempNormals[jj * 2 + 0];
float dly0 = m_tempNormals[jj * 2 + 1];
float dlx1 = m_tempNormals[ii * 2 + 0];
float dly1 = m_tempNormals[ii * 2 + 1];
float dmx = (dlx0 + dlx1) * 0.5f;
float dmy = (dly0 + dly1) * 0.5f;
float dmr2 = dmx * dmx + dmy * dmy;
if (dmr2 > 0.000001f)
{
float scale = 1.0f / dmr2;
if (scale > 10.0f)
{
scale = 10.0f;
}
dmx *= scale;
dmy *= scale;
}
m_tempCoords[ii * 2 + 0] = _coords[ii * 2 + 0] + dmx * _r;
m_tempCoords[ii * 2 + 1] = _coords[ii * 2 + 1] + dmy * _r;
}
uint32_t numVertices = _numCoords*6 + (_numCoords-2)*3;
if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorVertex::ms_decl) )
{
bgfx::TransientVertexBuffer tvb;
bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorVertex::ms_decl);
uint32_t trans = _abgr&0xffffff;
PosColorVertex* vertex = (PosColorVertex*)tvb.data;
for (uint32_t ii = 0, jj = _numCoords-1; ii < _numCoords; jj = ii++)
{
vertex->m_x = _coords[ii*2+0];
vertex->m_y = _coords[ii*2+1];
vertex->m_abgr = _abgr;
++vertex;
vertex->m_x = _coords[jj*2+0];
vertex->m_y = _coords[jj*2+1];
vertex->m_abgr = _abgr;
++vertex;
vertex->m_x = m_tempCoords[jj*2+0];
vertex->m_y = m_tempCoords[jj*2+1];
vertex->m_abgr = trans;
++vertex;
vertex->m_x = m_tempCoords[jj*2+0];
vertex->m_y = m_tempCoords[jj*2+1];
vertex->m_abgr = trans;
++vertex;
vertex->m_x = m_tempCoords[ii*2+0];
vertex->m_y = m_tempCoords[ii*2+1];
vertex->m_abgr = trans;
++vertex;
vertex->m_x = _coords[ii*2+0];
vertex->m_y = _coords[ii*2+1];
vertex->m_abgr = _abgr;
++vertex;
}
for (uint32_t ii = 2; ii < _numCoords; ++ii)
{
vertex->m_x = _coords[0];
vertex->m_y = _coords[1];
vertex->m_abgr = _abgr;
++vertex;
vertex->m_x = _coords[(ii-1)*2+0];
vertex->m_y = _coords[(ii-1)*2+1];
vertex->m_abgr = _abgr;
++vertex;
vertex->m_x = _coords[ii*2+0];
vertex->m_y = _coords[ii*2+1];
vertex->m_abgr = _abgr;
++vertex;
}
bgfx::setVertexBuffer(&tvb);
bgfx::setState(0
| BGFX_STATE_RGB_WRITE
| BGFX_STATE_ALPHA_WRITE
| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
);
setCurrentScissor();
bgfx::submit(m_view, m_colorProgram);
}
}
void drawRect(float _x, float _y, float _w, float _h, uint32_t _argb, float _fth = 1.0f)
{
float verts[4 * 2] =
{
_x + 0.5f, _y + 0.5f,
_x + _w - 0.5f, _y + 0.5f,
_x + _w - 0.5f, _y + _h - 0.5f,
_x + 0.5f, _y + _h - 0.5f,
};
drawPolygon(verts, 4, _fth, _argb);
}
void drawRoundedRect(float _x, float _y, float _w, float _h, float _r, uint32_t _argb, float _fth = 1.0f)
{
if (0.0f == _r)
{
return drawRect(_x, _y, _w, _h, _argb, _fth);
}
const uint32_t num = NUM_CIRCLE_VERTS / 4;
const float* cverts = m_circleVerts;
float verts[(num + 1) * 4 * 2];
float* vv = verts;
for (uint32_t ii = 0; ii <= num; ++ii)
{
*vv++ = _x + _w - _r + cverts[ii * 2] * _r;
*vv++ = _y + _h - _r + cverts[ii * 2 + 1] * _r;
}
for (uint32_t ii = num; ii <= num * 2; ++ii)
{
*vv++ = _x + _r + cverts[ii * 2] * _r;
*vv++ = _y + _h - _r + cverts[ii * 2 + 1] * _r;
}
for (uint32_t ii = num * 2; ii <= num * 3; ++ii)
{
*vv++ = _x + _r + cverts[ii * 2] * _r;
*vv++ = _y + _r + cverts[ii * 2 + 1] * _r;
}
for (uint32_t ii = num * 3; ii < num * 4; ++ii)
{
*vv++ = _x + _w - _r + cverts[ii * 2] * _r;
*vv++ = _y + _r + cverts[ii * 2 + 1] * _r;
}
*vv++ = _x + _w - _r + cverts[0] * _r;
*vv++ = _y + _r + cverts[1] * _r;
drawPolygon(verts, (num + 1) * 4, _fth, _argb);
}
void drawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _abgr, float _fth = 1.0f)
{
float dx = _x1 - _x0;
float dy = _y1 - _y0;
float d = sqrtf(dx * dx + dy * dy);
if (d > 0.0001f)
{
d = 1.0f / d;
dx *= d;
dy *= d;
}
float nx = dy;
float ny = -dx;
float verts[4 * 2];
_r -= _fth;
_r *= 0.5f;
if (_r < 0.01f)
{
_r = 0.01f;
}
dx *= _r;
dy *= _r;
nx *= _r;
ny *= _r;
verts[0] = _x0 - dx - nx;
verts[1] = _y0 - dy - ny;
verts[2] = _x0 - dx + nx;
verts[3] = _y0 - dy + ny;
verts[4] = _x1 + dx + nx;
verts[5] = _y1 + dy + ny;
verts[6] = _x1 + dx - nx;
verts[7] = _y1 + dy - ny;
drawPolygon(verts, 4, _fth, _abgr);
}
struct TriangleOrientation
{
enum Enum
{
Left,
Right,
Up,
Down,
};
};
void drawTriangle(int32_t _x, int32_t _y, int32_t _width, int32_t _height, TriangleOrientation::Enum _orientation, uint32_t _abgr)
{
if (TriangleOrientation::Left == _orientation)
{
const float verts[3 * 2] =
{
(float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f,
(float)_x + 0.5f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
(float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
};
drawPolygon(verts, 3, 1.0f, _abgr);
}
else if (TriangleOrientation::Right == _orientation)
{
const float verts[3 * 2] =
{
(float)_x + 0.5f, (float)_y + 0.5f,
(float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
(float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
};
drawPolygon(verts, 3, 1.0f, _abgr);
}
else if (TriangleOrientation::Up == _orientation)
{
const float verts[3 * 2] =
{
(float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
(float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f,
(float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
};
drawPolygon(verts, 3, 1.0f, _abgr);
}
else //if (TriangleOrientation::Down == _orientation).
{
const float verts[3 * 2] =
{
(float)_x + 0.5f, (float)_y + 0.5f,
(float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
(float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f,
};
drawPolygon(verts, 3, 1.0f, _abgr);
}
}
#if !USE_NANOVG_FONT
void getBakedQuad(stbtt_bakedchar* _chardata, int32_t char_index, float* _xpos, float* _ypos, stbtt_aligned_quad* _quad)
{
stbtt_bakedchar* b = _chardata + char_index;
int32_t round_x = STBTT_ifloor(*_xpos + b->xoff);
int32_t round_y = STBTT_ifloor(*_ypos + b->yoff);
_quad->x0 = (float)round_x;
_quad->y0 = (float)round_y;
_quad->x1 = (float)round_x + b->x1 - b->x0;
_quad->y1 = (float)round_y + b->y1 - b->y0;
_quad->s0 = (b->x0 + m_halfTexel) * m_invTextureWidth;
_quad->t0 = (b->y0 + m_halfTexel) * m_invTextureWidth;
_quad->s1 = (b->x1 + m_halfTexel) * m_invTextureHeight;
_quad->t1 = (b->y1 + m_halfTexel) * m_invTextureHeight;
*_xpos += b->xadvance;
}
#endif // !USE_NANOVG_FONT
void drawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _abgr)
{
drawText( (float)_x, (float)_y, _text, _align, _abgr);
}
void drawText(float _x, float _y, const char* _text, ImguiTextAlign::Enum _align, uint32_t _abgr)
{
if (NULL == _text
|| '\0' == _text[0])
{
return;
}
#if USE_NANOVG_FONT
static uint32_t textAlign[ImguiTextAlign::Count] =
{
NVG_ALIGN_LEFT,
NVG_ALIGN_CENTER,
NVG_ALIGN_RIGHT,
};
nvgTextAlign(m_nvg, textAlign[_align]);
nvgFontBlur(m_nvg, 0.0f);
nvgFillColor(m_nvg, nvgRGBAu(_abgr) );
nvgText(m_nvg, _x, _y, _text, NULL);
#else
uint32_t numVertices = 0;
if (_align == ImguiTextAlign::Center)
{
_x -= getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices) / 2;
}
else if (_align == ImguiTextAlign::Right)
{
_x -= getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices);
}
else // just count vertices
{
getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices);
}
if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorUvVertex::ms_decl) )
{
bgfx::TransientVertexBuffer tvb;
bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorUvVertex::ms_decl);
PosColorUvVertex* vertex = (PosColorUvVertex*)tvb.data;
const float ox = _x;
while (*_text)
{
int32_t ch = (uint8_t)*_text;
if (ch == '\t')
{
for (int32_t i = 0; i < 4; ++i)
{
if (_x < s_tabStops[i] + ox)
{
_x = s_tabStops[i] + ox;
break;
}
}
}
else if (ch >= ' '
&& ch < 128)
{
stbtt_aligned_quad quad;
getBakedQuad(m_fonts[m_currentFontIdx].m_cdata, ch - 32, &_x, &_y, &quad);
vertex->m_x = quad.x0;
vertex->m_y = quad.y0;
vertex->m_u = quad.s0;
vertex->m_v = quad.t0;
vertex->m_abgr = _abgr;
++vertex;
vertex->m_x = quad.x1;
vertex->m_y = quad.y1;
vertex->m_u = quad.s1;
vertex->m_v = quad.t1;
vertex->m_abgr = _abgr;
++vertex;
vertex->m_x = quad.x1;
vertex->m_y = quad.y0;
vertex->m_u = quad.s1;
vertex->m_v = quad.t0;
vertex->m_abgr = _abgr;
++vertex;
vertex->m_x = quad.x0;
vertex->m_y = quad.y0;
vertex->m_u = quad.s0;
vertex->m_v = quad.t0;
vertex->m_abgr = _abgr;
++vertex;
vertex->m_x = quad.x0;
vertex->m_y = quad.y1;
vertex->m_u = quad.s0;
vertex->m_v = quad.t1;
vertex->m_abgr = _abgr;
++vertex;
vertex->m_x = quad.x1;
vertex->m_y = quad.y1;
vertex->m_u = quad.s1;
vertex->m_v = quad.t1;
vertex->m_abgr = _abgr;
++vertex;
}
++_text;
}
bgfx::setTexture(0, s_texColor, m_fonts[m_currentFontIdx].m_texture);
bgfx::setVertexBuffer(&tvb);
bgfx::setState(0
| BGFX_STATE_RGB_WRITE
| BGFX_STATE_ALPHA_WRITE
| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
);
setCurrentScissor();
bgfx::submit(m_view, m_textureProgram);
}
#endif // USE_NANOVG_FONT
}
bool screenQuad(int32_t _x, int32_t _y, int32_t _width, uint32_t _height, bool _originBottomLeft = false)
{
if (bgfx::checkAvailTransientVertexBuffer(6, PosUvVertex::ms_decl) )
{
bgfx::TransientVertexBuffer vb;
bgfx::allocTransientVertexBuffer(&vb, 6, PosUvVertex::ms_decl);
PosUvVertex* vertex = (PosUvVertex*)vb.data;
const float widthf = float(_width);
const float heightf = float(_height);
const float minx = float(_x);
const float miny = float(_y);
const float maxx = minx+widthf;
const float maxy = miny+heightf;
const float texelHalfW = m_halfTexel/widthf;
const float texelHalfH = m_halfTexel/heightf;
const float minu = texelHalfW;
const float maxu = 1.0f - texelHalfW;
const float minv = _originBottomLeft ? texelHalfH+1.0f : texelHalfH ;
const float maxv = _originBottomLeft ? texelHalfH : texelHalfH+1.0f;
vertex[0].m_x = minx;
vertex[0].m_y = miny;
vertex[0].m_u = minu;
vertex[0].m_v = minv;
vertex[1].m_x = maxx;
vertex[1].m_y = miny;
vertex[1].m_u = maxu;
vertex[1].m_v = minv;
vertex[2].m_x = maxx;
vertex[2].m_y = maxy;
vertex[2].m_u = maxu;
vertex[2].m_v = maxv;
vertex[3].m_x = maxx;
vertex[3].m_y = maxy;
vertex[3].m_u = maxu;
vertex[3].m_v = maxv;
vertex[4].m_x = minx;
vertex[4].m_y = maxy;
vertex[4].m_u = minu;
vertex[4].m_v = maxv;
vertex[5].m_x = minx;
vertex[5].m_y = miny;
vertex[5].m_u = minu;
vertex[5].m_v = minv;
bgfx::setVertexBuffer(&vb);
return true;
}
return false;
}
void colorWheelWidget(float _rgb[3], bool _respectIndentation, float _size, bool _enabled)
{
const uint32_t wheelId = getId();
const uint32_t triangleId = getId();
Area& area = getCurrentArea();
const float areaX = float(_respectIndentation ? area.m_widgetX : area.m_contentX);
const float areaW = float(_respectIndentation ? area.m_widgetW : area.m_contentWidth);
const float width = areaW*_size;
const float xx = areaX + areaW*0.5f;
const float yy = float(area.m_widgetY) + width*0.5f;
const float center[2] = { xx, yy };
area.m_widgetY += int32_t(width) + DEFAULT_SPACING;
const float ro = width*0.5f - 5.0f; // radiusOuter.
const float rd = _size*25.0f; // radiusDelta.
const float ri = ro - rd; // radiusInner.
const float aeps = 0.5f / ro; // Half a pixel arc length in radians (2pi cancels out).
const float cmx = float(m_mx) - center[0];
const float cmy = float(m_my) - center[1];
const float aa[2] = { ri - 6.0f, 0.0f }; // Hue point.
const float bb[2] = { cosf(-120.0f/180.0f*NVG_PI) * aa[0], sinf(-120.0f/180.0f*NVG_PI) * aa[0] }; // Black point.
const float cc[2] = { cosf( 120.0f/180.0f*NVG_PI) * aa[0], sinf( 120.0f/180.0f*NVG_PI) * aa[0] }; // White point.
const float ca[2] = { aa[0] - cc[0], aa[1] - cc[1] };
const float lenCa = sqrtf(ca[0]*ca[0]+ca[1]*ca[1]);
const float invLenCa = 1.0f/lenCa;
const float dirCa[2] = { ca[0]*invLenCa, ca[1]*invLenCa };
float sel[2];
float hsv[3];
bx::rgbToHsv(hsv, _rgb);
const bool enabled = _enabled && isEnabled(m_areaId);
if (enabled)
{
if (m_leftPressed)
{
const float len = sqrtf(cmx*cmx + cmy*cmy);
if (len > ri)
{
if (len < ro)
{
setActive(wheelId);
}
}
else
{
setActive(triangleId);
}
}
if (m_leftReleased
&& (isActive(wheelId) || isActive(triangleId) ) )
{
clearActive();
}
// Set hue.
if (m_left
&& isActive(wheelId) )
{
hsv[0] = atan2f(cmy, cmx)/NVG_PI*0.5f;
if (hsv[0] < 0.0f)
{
hsv[0]+=1.0f;
}
}
}
if (enabled
&& m_left
&& isActive(triangleId) )
{
float an = -hsv[0]*NVG_PI*2.0f;
float tmx = (cmx*cosf(an)-cmy*sinf(an) );
float tmy = (cmx*sinf(an)+cmy*cosf(an) );
if (pointInTriangle(tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]) )
{
sel[0] = tmx;
sel[1] = tmy;
}
else
{
closestPointOnTriangle(sel[0], sel[1], tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]);
}
}
else
{
/*
* bb (black)
* /\
* / \
* / \
* / \
* / \
* / .sel \
* / \
* cc(white)/____.ss_______\aa (hue)
*/
const float ss[2] =
{
cc[0] + dirCa[0]*lenCa*hsv[1],
cc[1] + dirCa[1]*lenCa*hsv[1],
};
const float sb[2] = { bb[0]-ss[0], bb[1]-ss[1] };
const float lenSb = sqrtf(sb[0]*sb[0]+sb[1]*sb[1]);
const float invLenSb = 1.0f/lenSb;
const float dirSb[2] = { sb[0]*invLenSb, sb[1]*invLenSb };
sel[0] = cc[0] + dirCa[0]*lenCa*hsv[1] + dirSb[0]*lenSb*(1.0f - hsv[2]);
sel[1] = cc[1] + dirCa[1]*lenCa*hsv[1] + dirSb[1]*lenSb*(1.0f - hsv[2]);
}
float uu, vv, ww;
barycentric(uu, vv, ww
, aa[0], aa[1]
, bb[0], bb[1]
, cc[0], cc[1]
, sel[0], sel[1]
);
const float val = bx::fclamp(1.0f-vv, 0.0001f, 1.0f);
const float sat = bx::fclamp(uu/val, 0.0001f, 1.0f);
const float out[3] = { hsv[0], sat, val };
bx::hsvToRgb(_rgb, out);
// Draw widget.
nvgSave(m_nvg);
{
float saturation;
uint8_t alpha0;
uint8_t alpha1;
if (enabled)
{
saturation = 1.0f;
alpha0 = 255;
alpha1 = 192;
}
else
{
saturation = 0.0f;
alpha0 = 10;
alpha1 = 10;
}
// Circle.
for (uint8_t ii = 0; ii < 6; ii++)
{
const float a0 = float(ii)/6.0f * 2.0f*NVG_PI - aeps;
const float a1 = float(ii+1.0f)/6.0f * 2.0f*NVG_PI + aeps;
nvgBeginPath(m_nvg);
nvgArc(m_nvg, center[0], center[1], ri, a0, a1, NVG_CW);
nvgArc(m_nvg, center[0], center[1], ro, a1, a0, NVG_CCW);
nvgClosePath(m_nvg);
const float ax = center[0] + cosf(a0) * (ri+ro)*0.5f;
const float ay = center[1] + sinf(a0) * (ri+ro)*0.5f;
const float bx = center[0] + cosf(a1) * (ri+ro)*0.5f;
const float by = center[1] + sinf(a1) * (ri+ro)*0.5f;
NVGpaint paint = nvgLinearGradient(m_nvg
, ax, ay
, bx, by
, nvgHSLA(a0/NVG_PI*0.5f,saturation,0.55f,alpha0)
, nvgHSLA(a1/NVG_PI*0.5f,saturation,0.55f,alpha0)
);
nvgFillPaint(m_nvg, paint);
nvgFill(m_nvg);
}
// Circle stroke.
nvgBeginPath(m_nvg);
nvgCircle(m_nvg, center[0], center[1], ri-0.5f);
nvgCircle(m_nvg, center[0], center[1], ro+0.5f);
nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
nvgStrokeWidth(m_nvg, 1.0f);
nvgStroke(m_nvg);
nvgSave(m_nvg);
{
// Hue selector.
nvgTranslate(m_nvg, center[0], center[1]);
nvgRotate(m_nvg, hsv[0]*NVG_PI*2.0f);
nvgStrokeWidth(m_nvg, 2.0f);
nvgBeginPath(m_nvg);
nvgRect(m_nvg, ri-1.0f,-3.0f,rd+2.0f,6.0f);
nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
nvgStroke(m_nvg);
// Hue selector drop shadow.
NVGpaint paint = nvgBoxGradient(m_nvg, ri-3.0f,-5.0f,ro-ri+6.0f,10.0f, 2.0f,4.0f, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0) );
nvgBeginPath(m_nvg);
nvgRect(m_nvg, ri-2.0f-10.0f,-4.0f-10.0f,ro-ri+4.0f+20.0f,8.0f+20.0f);
nvgRect(m_nvg, ri-2.0f,-4.0f,ro-ri+4.0f,8.0f);
nvgPathWinding(m_nvg, NVG_HOLE);
nvgFillPaint(m_nvg, paint);
nvgFill(m_nvg);
// Center triangle stroke.
nvgBeginPath(m_nvg);
nvgMoveTo(m_nvg, aa[0], aa[1]);
nvgLineTo(m_nvg, bb[0], bb[1]);
nvgLineTo(m_nvg, cc[0], cc[1]);
nvgClosePath(m_nvg);
nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
nvgStroke(m_nvg);
// Center triangle fill.
paint = nvgLinearGradient(m_nvg, aa[0], aa[1], bb[0], bb[1], nvgHSL(hsv[0],saturation,0.5f), nvgRGBA(0,0,0,alpha0) );
nvgFillPaint(m_nvg, paint);
nvgFill(m_nvg);
paint = nvgLinearGradient(m_nvg, (aa[0]+bb[0])*0.5f, (aa[1]+bb[1])*0.5f, cc[0], cc[1], nvgRGBA(0,0,0,0), nvgRGBA(255,255,255,alpha0) );
nvgFillPaint(m_nvg, paint);
nvgFill(m_nvg);
// Color selector.
nvgStrokeWidth(m_nvg, 2.0f);
nvgBeginPath(m_nvg);
nvgCircle(m_nvg, sel[0], sel[1], 5);
nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
nvgStroke(m_nvg);
// Color selector stroke.
paint = nvgRadialGradient(m_nvg, sel[0], sel[1], 7.0f, 9.0f, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0) );
nvgBeginPath(m_nvg);
nvgRect(m_nvg, sel[0]-20.0f, sel[1]-20.0f, 40.0f, 40.0f);
nvgCircle(m_nvg, sel[0], sel[1], 7.0f);
nvgPathWinding(m_nvg, NVG_HOLE);
nvgFillPaint(m_nvg, paint);
nvgFill(m_nvg);
}
nvgRestore(m_nvg);
}
nvgRestore(m_nvg);
}
struct Area
{
int32_t m_x;
int32_t m_y;
int32_t m_width;
int32_t m_height;
int16_t m_contentX;
int16_t m_contentY;
int16_t m_contentWidth;
int16_t m_contentHeight;
int16_t m_scissorX;
int16_t m_scissorY;
int16_t m_scissorHeight;
int16_t m_scissorWidth;
int32_t m_widgetX;
int32_t m_widgetY;
int32_t m_widgetW;
int32_t* m_scrollVal;
uint32_t m_scrollId;
bool m_inside;
bool m_didScroll;
bool m_scissorEnabled;
};
bool visible(int32_t _elemY, int32_t _elemHeight, int32_t _scissorY, int32_t _scissorHeight)
{
return (_elemY+_elemHeight) > _scissorY
&& (_elemY) < (_scissorY+_scissorHeight);
}
inline Area& getCurrentArea()
{
return m_areas[m_areaId];
}
inline void setCurrentScissor()
{
const Area& area = getCurrentArea();
if (area.m_scissorEnabled)
{
const float xscale = float(m_viewWidth) /float(m_surfaceWidth);
const float yscale = float(m_viewHeight)/float(m_surfaceHeight);
const int16_t scissorX = int16_t(float(area.m_scissorX)*xscale);
const int16_t scissorY = int16_t(float(area.m_scissorY)*yscale);
const int16_t scissorWidth = int16_t(float(area.m_scissorWidth)*xscale);
const int16_t scissorHeight = int16_t(float(area.m_scissorHeight)*yscale);
bgfx::setScissor(uint16_t(IMGUI_MAX(0, scissorX) )
, uint16_t(IMGUI_MAX(0, scissorY-1) )
, scissorWidth
, scissorHeight+1
);
}
else
{
bgfx::setScissor(UINT16_MAX);
}
}
inline void nvgScissor(NVGcontext* _ctx, const Area& _area)
{
if (_area.m_scissorEnabled)
{
::nvgScissor(_ctx
, float(IMGUI_MAX(0, _area.m_scissorX) )
, float(IMGUI_MAX(0, _area.m_scissorY-1) )
, float(_area.m_scissorWidth)
, float(_area.m_scissorHeight+1)
);
}
else
{
nvgResetScissor(_ctx);
}
}
template <typename Ty, uint16_t Max=64>
struct IdStack
{
IdStack()
{
reset();
}
void reset()
{
m_current = 0;
m_idGen = 0;
m_ids[0] = 0;
}
void next()
{
BX_CHECK(Max > (m_current+1), "Param out of bounds!");
m_ids[++m_current] = ++m_idGen;
}
void pop()
{
m_current = m_current > 0 ? m_current-1 : 0;
}
Ty current() const
{
BX_CHECK(Max > (m_current), "Param out of bounds!");
return m_ids[m_current];
}
operator Ty() const
{
BX_CHECK(Max > (m_current), "Param out of bounds!");
return m_ids[m_current];
}
private:
uint16_t m_current;
Ty m_idGen;
Ty m_ids[Max];
};
bx::AllocatorI* m_allocator;
int32_t m_mx;
int32_t m_my;
int32_t m_scroll;
uint32_t m_active;
uint32_t m_hot;
uint32_t m_hotToBe;
char m_char;
char m_lastChar;
uint32_t m_inputField;
int32_t m_dragX;
int32_t m_dragY;
float m_dragOrig;
bool m_left;
bool m_leftPressed;
bool m_leftReleased;
bool m_isHot;
bool m_wentActive;
bool m_insideArea;
bool m_isActivePresent;
bool m_checkActivePresence;
IdStack<uint16_t> m_areaId;
uint16_t m_widgetId;
uint64_t m_enabledAreaIds;
Area m_areas[64];
float m_tempCoords[MAX_TEMP_COORDS * 2];
float m_tempNormals[MAX_TEMP_COORDS * 2];
float m_circleVerts[NUM_CIRCLE_VERTS * 2];
uint16_t m_textureWidth;
uint16_t m_textureHeight;
float m_invTextureWidth;
float m_invTextureHeight;
float m_halfTexel;
NVGcontext* m_nvg;
uint8_t m_view;
uint16_t m_surfaceWidth;
uint16_t m_surfaceHeight;
uint16_t m_viewWidth;
uint16_t m_viewHeight;
#if !USE_NANOVG_FONT
struct Font
{
stbtt_bakedchar m_cdata[96]; // ASCII 32..126 is 95 glyphs
bgfx::TextureHandle m_texture;
float m_size;
};
uint16_t m_currentFontIdx;
bx::HandleAllocT<IMGUI_CONFIG_MAX_FONTS> m_fontHandle;
Font m_fonts[IMGUI_CONFIG_MAX_FONTS];
#endif // !USE_NANOVG_FONT
bgfx::UniformHandle u_imageLodEnabled;
bgfx::UniformHandle u_imageSwizzle;
bgfx::UniformHandle s_texColor;
bgfx::ProgramHandle m_colorProgram;
bgfx::ProgramHandle m_textureProgram;
bgfx::ProgramHandle m_cubeMapProgram;
bgfx::ProgramHandle m_latlongProgram;
bgfx::ProgramHandle m_imageProgram;
bgfx::ProgramHandle m_imageSwizzProgram;
bgfx::TextureHandle m_missingTexture;
};
static Imgui s_imgui;
void* imguiMalloc(size_t _size, void*)
{
return BX_ALLOC(s_imgui.m_allocator, _size);
}
void imguiFree(void* _ptr, void*)
{
BX_FREE(s_imgui.m_allocator, _ptr);
}
ImguiFontHandle imguiCreate(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
{
return s_imgui.create(_data, _size, _fontSize, _allocator);
}
void imguiDestroy()
{
s_imgui.destroy();
}
ImguiFontHandle imguiCreateFont(const void* _data, float _fontSize)
{
return s_imgui.createFont(_data, _fontSize);
}
void imguiSetFont(ImguiFontHandle _handle)
{
s_imgui.setFont(_handle);
}
ImguiFontHandle imguiGetCurrentFont()
{
const ImguiFontHandle handle = { s_imgui.m_currentFontIdx };
return handle;
}
void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint16_t _surfaceWidth, uint16_t _surfaceHeight, char _inputChar, uint8_t _view)
{
s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _surfaceWidth, _surfaceHeight, _inputChar, _view);
}
void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, char _inputChar, uint8_t _view)
{
s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _width, _height, _inputChar, _view);
}
void imguiEndFrame()
{
s_imgui.endFrame();
}
void imguiDrawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _argb)
{
s_imgui.drawText(_x, _y, _align, _text, _argb);
}
void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb)
{
s_imgui.drawLine(_x0, _y0, _x1, _y1, _r, _argb);
}
void imguiDrawRoundedRect(float _x, float _y, float _width, float _height, float _r, uint32_t _argb)
{
s_imgui.drawRoundedRect(_x, _y, _width, _height, _r, _argb);
}
void imguiDrawRect(float _x, float _y, float _width, float _height, uint32_t _argb)
{
s_imgui.drawRect(_x, _y, _width, _height, _argb);
}
bool imguiBorderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled)
{
return s_imgui.borderButton(_border, _checked, _enabled);
}
bool imguiBeginArea(const char* _name, int _x, int _y, int _width, int _height, bool _enabled, int32_t _r)
{
return s_imgui.beginArea(_name, _x, _y, _width, _height, _enabled, _r);
}
void imguiEndArea()
{
return s_imgui.endArea();
}
bool imguiBeginScroll(int32_t _height, int32_t* _scroll, bool _enabled)
{
return s_imgui.beginScroll(_height, _scroll, _enabled);
}
void imguiEndScroll(int32_t _r)
{
s_imgui.endScroll(_r);
}
bool imguiBeginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll, bool _enabled, int32_t _r)
{
const bool result = s_imgui.beginArea(_name, _x, _y, _width, _height, _enabled, _r);
const bool hasTitle = (NULL != _name && '\0' != _name[0]);
const int32_t margins = int32_t(hasTitle)*(AREA_HEADER+2*SCROLL_AREA_PADDING-1);
s_imgui.beginScroll(_height - margins, _scroll, _enabled);
return result;
}
void imguiEndScrollArea(int32_t _r)
{
s_imgui.endScroll(_r);
s_imgui.endArea();
}
void imguiIndent(uint16_t _width)
{
s_imgui.indent(_width);
}
void imguiUnindent(uint16_t _width)
{
s_imgui.unindent(_width);
}
void imguiSeparator(uint16_t _height)
{
s_imgui.separator(_height);
}
void imguiSeparatorLine(uint16_t _height, ImguiAlign::Enum _align)
{
s_imgui.separatorLine(_height, _align);
}
int32_t imguiGetWidgetX()
{
return s_imgui.getCurrentArea().m_widgetX;
}
int32_t imguiGetWidgetY()
{
return s_imgui.getCurrentArea().m_widgetY;
}
int32_t imguiGetWidgetW()
{
return s_imgui.getCurrentArea().m_widgetW;
}
void imguiSetCurrentScissor()
{
return s_imgui.setCurrentScissor();
}
bool imguiButton(const char* _text, bool _enabled, ImguiAlign::Enum _align, uint32_t _rgb0, int32_t _r)
{
return s_imgui.button(_text, _enabled, _align, _rgb0, _r);
}
bool imguiItem(const char* _text, bool _enabled)
{
return s_imgui.item(_text, _enabled);
}
bool imguiCheck(const char* _text, bool _checked, bool _enabled)
{
return s_imgui.check(_text, _checked, _enabled);
}
bool imguiBool(const char* _text, bool& _flag, bool _enabled)
{
bool result = imguiCheck(_text, _flag, _enabled);
if (result)
{
_flag = !_flag;
}
return result;
}
bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
{
return s_imgui.collapse(_text, _subtext, _checked, _enabled);
}
void imguiLabel(const char* _format, ...)
{
va_list argList;
va_start(argList, _format);
s_imgui.labelVargs(_format, argList, imguiRGBA(255, 255, 255, 255) );
va_end(argList);
}
void imguiLabel(uint32_t _rgba, const char* _format, ...)
{
va_list argList;
va_start(argList, _format);
s_imgui.labelVargs(_format, argList, _rgba);
va_end(argList);
}
void imguiValue(const char* _text)
{
s_imgui.value(_text);
}
bool imguiSlider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled, ImguiAlign::Enum _align)
{
return s_imgui.slider(_text, _val, _vmin, _vmax, _vinc, _enabled, _align);
}
bool imguiSlider(const char* _text, int32_t& _val, int32_t _vmin, int32_t _vmax, bool _enabled, ImguiAlign::Enum _align)
{
float val = (float)_val;
bool result = s_imgui.slider(_text, val, (float)_vmin, (float)_vmax, 1.0f, _enabled, _align);
_val = (int32_t)val;
return result;
}
void imguiInput(const char* _label, char* _str, uint32_t _len, bool _enabled, ImguiAlign::Enum _align, int32_t _r)
{
s_imgui.input(_label, _str, _len, _enabled, _align, _r);
}
uint8_t imguiTabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, uint8_t _nTabs, uint8_t _nEnabled, ...)
{
va_list argList;
va_start(argList, _nEnabled);
const uint8_t result = s_imgui.tabs(_selected, _enabled, _align, _height, _r, _nTabs, _nEnabled, argList);
va_end(argList);
return result;
}
uint8_t imguiTabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, uint8_t _nTabs, ...)
{
va_list argList;
va_start(argList, _nTabs);
const uint8_t result = s_imgui.tabs(_selected, _enabled, _align, _height, _r, _nTabs, 0, argList);
va_end(argList);
return result;
}
uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...)
{
va_list argList;
va_start(argList, _selected);
const char* str = va_arg(argList, const char*);
for (uint32_t ii = 0; str != NULL; ++ii, str = va_arg(argList, const char*) )
{
if (imguiCheck(str, ii == _selected) )
{
_selected = ii;
}
}
va_end(argList);
return _selected;
}
void imguiColorWheel(float _rgb[3], bool _respectIndentation, float _size, bool _enabled)
{
s_imgui.colorWheelWidget(_rgb, _respectIndentation, _size, _enabled);
}
void imguiColorWheel(const char* _text, float _rgb[3], bool& _activated, float _size, bool _enabled)
{
char buf[128];
bx::snprintf(buf, sizeof(buf), "[RGB %-2.2f %-2.2f %-2.2f]"
, _rgb[0]
, _rgb[1]
, _rgb[2]
);
if (imguiCollapse(_text, buf, _activated) )
{
_activated = !_activated;
}
if (_activated)
{
imguiColorWheel(_rgb, false, _size, _enabled);
}
}
bool imguiImage(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _enabled, bool _originBottomLeft)
{
return s_imgui.image(_image, _lod, _width, _height, _align, _enabled, _originBottomLeft);
}
bool imguiImage(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _enabled, bool _originBottomLeft)
{
return s_imgui.image(_image, _lod, _width, _aspect, _align, _enabled, _originBottomLeft);
}
bool imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _enabled)
{
return s_imgui.imageChannel(_image, _channel, _lod, _width, _height, _align, _enabled);
}
bool imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _enabled)
{
return s_imgui.imageChannel(_image, _channel, _lod, _width, _aspect, _align, _enabled);
}
bool imguiCube(bgfx::TextureHandle _cubemap, float _lod, ImguiCubemap::Enum _display, bool _sameHeight, ImguiAlign::Enum _align, bool _enabled)
{
return s_imgui.cubeMap(_cubemap, _lod, _display, _sameHeight, _align, _enabled);
}
float imguiGetTextLength(const char* _text, ImguiFontHandle _handle)
{
#if !USE_NANOVG_FONT
uint32_t numVertices = 0; //unused
return getTextLength(s_imgui.m_fonts[_handle.idx].m_cdata, _text, numVertices);
#else
return 0.0f;
#endif
}
bool imguiMouseOverArea()
{
return s_imgui.m_insideArea;
}
bgfx::ProgramHandle imguiGetImageProgram(uint8_t _mip)
{
const float lodEnabled[4] = { float(_mip), 1.0f, 0.0f, 0.0f };
bgfx::setUniform(s_imgui.u_imageLodEnabled, lodEnabled);
return s_imgui.m_imageProgram;
}
|