gloss-renderer 0.9.0

Core renderer for gloss
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
use crate::{
    components::{
        Colors, DiffuseImg, DiffuseTex, Faces, ImgConfig, LightEmit, MeshColorType, ModelMatrix, Name, NormalImg, NormalTex, Normals, OnGui,
        PointColorType, PosLookat, Projection, Renderable, RoughnessImg, RoughnessTex, ShadowCaster, ShadowMapDirty, UVs, Verts, VisLines, VisMesh,
        VisNormals, VisOutline, VisPoints, VisWireframe,
    },
    config::Config,
    selector::Selector,
    viewer::Runner,
};

use crate::plugin_manager::gui::window::{GuiWindowType, WindowPivot, WindowPositionType};

use crate::{
    builders,
    forward_renderer::Renderer,
    plugin_manager::plugins::Plugins,
    scene::{Scene, GLOSS_FLOOR_NAME},
};

use egui::{style::TextCursorStyle, CornerRadius, TextStyle};
use gloss_geometry::geom;
use gloss_utils::string::float2string;
use log::debug;

use easy_wgpu::gpu::Gpu;
use egui_wgpu::ScreenDescriptor;

use egui::style::{HandleShape, NumericColorSpace, ScrollStyle};

use gloss_utils::{
    abi_stable_aliases::std_types::{ROption::RSome, RString, RVec},
    memory::get_last_relevant_func_name,
};

use re_memory::{accounting_allocator, CallstackStatistics, MemoryUse};

use log::error;
use winit::window::Window;

use crate::plugin_manager::gui::widgets::Widgets as WidgetsFFI;
use egui::{
    epaint,
    epaint::AlphaFromCoverage,
    epaint::Shadow,
    scroll_area,
    style::{Interaction, Selection, Spacing, WidgetVisuals, Widgets},
    Align, Align2, Color32, FontId, Layout, RichText, ScrollArea, Slider, Stroke, Style, Ui, Vec2, Visuals,
};
use egui_winit::{self, EventResponse};
use epaint::Margin;

use gloss_hecs::{CommandBuffer, Entity};

use std::{collections::HashMap, path::Path};

use nalgebra as na;

// check the integration example here: https://docs.rs/egui/latest/egui/
// info of other people trying to do custom stuff: https://users.rust-lang.org/t/egui-is-it-possible-to-avoid-using-eframe/70470/22
// some other example of a large codebase using egui: https://github.com/parasyte/cartunes
// example of egui-winit and egui-wgpu: https://github.com/hasenbanck/egui_example/blob/master/src/main.rs
// official example of egui-wgpu: https://github.com/emilk/egui/blob/master/crates/egui_demo_app/src/apps/custom3d_wgpu.rs

// integration
const SIDE_PANEL_WIDTH: f32 = 250.0;
const SPACING_1: f32 = 10.0;

/// Separate the egui ctx from the rest of the gui because borrow checker
/// complains when we modify state mutably of the gui and also have immutable
/// reference to `egui_ctx`. having a mutable widget the deal only with state
/// solved this
pub struct GuiMainWidget {
    //contains the gui state
    pub selected_mesh_name: String,
    pub selected_entity: Option<Entity>,
    pub selected_light_name: String,
    pub selected_light_entity: Option<Entity>,
    pub wgputex_2_eguitex: HashMap<wgpu::Texture, epaint::TextureId>,
    pub hovered_diffuse_tex: bool,
    pub hovered_normal_tex: bool,
    pub hovered_roughness_tex: bool,
    default_texture: Option<easy_wgpu::texture::Texture>,
    //gizmo stuff
    // gizmo_mode: GizmoMode,
    // gizmo_orientation: GizmoOrientation,
}
impl Default for GuiMainWidget {
    #[allow(clippy::derivable_impls)]
    fn default() -> GuiMainWidget {
        GuiMainWidget {
            selected_mesh_name: String::new(),
            selected_light_name: String::new(),
            selected_entity: None,
            selected_light_entity: None,
            wgputex_2_eguitex: HashMap::new(),
            hovered_diffuse_tex: false,
            hovered_normal_tex: false,
            hovered_roughness_tex: false,
            default_texture: None,
            // gizmo_mode: GizmoMode::Translate,
            // gizmo_orientation: GizmoOrientation::Local,
        }
    }
}
impl GuiMainWidget {
    pub fn new(gpu: &Gpu) -> Self {
        let path_tex = concat!(env!("CARGO_MANIFEST_DIR"), "/../../data/uv_checker.png");
        debug!("path_tex {path_tex}");
        let default_texture = easy_wgpu::texture::Texture::create_default_texture(gpu.device(), gpu.queue());

        Self {
            default_texture: Some(default_texture),
            ..Default::default()
        }
    }
}

type CbFnType = fn(&mut GuiMainWidget, ctx: &egui::Context, ui: &mut Ui, renderer: &Renderer, scene: &mut Scene);
pub struct Gui {
    egui_ctx: egui::Context,           //we do all the gui rendering inside this context
    pub egui_state: egui_winit::State, //integrator with winit https://github.com/emilk/egui/blob/master/crates/egui-winit/src/lib.rs#L55
    //similar to voiding on github
    egui_renderer: egui_wgpu::Renderer,
    width: u32,
    height: u32,
    pub hidden: bool,
    gui_main_widget: GuiMainWidget,
    command_buffer: CommandBuffer, //defer insertions and deletion of scene entities for whenever we apply this command buffer
    //callbacks
    //https://users.rust-lang.org/t/callback-with-generic/52426/5
    //https://stackoverflow.com/questions/66832392/sending-method-as-callback-function-to-field-object-in-rust
    //https://stackoverflow.com/questions/41081240/idiomatic-callbacks-in-rust
    //https://www.reddit.com/r/rust/comments/gi2pld/callback_functions_the_right_way/
    //https://github.com/rhaiscript/rhai/issues/178
    //https://www.reddit.com/r/rust/comments/ymingb/what_is_the_idiomatic_approach_to_eventscallbacks/iv5pgz9/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button

    //callback function that can add gui elements either inside the sidebar or outside the sidebar
    callbacks: Vec<CbFnType>,
    callbacks_for_selected_mesh: Vec<CbFnType>,
}

impl Gui {
    pub fn new(window: &winit::window::Window, gpu: &Gpu, surface_format: wgpu::TextureFormat) -> Self {
        #[allow(clippy::cast_possible_truncation)] //it's ok, we don't have very big numbers
        let native_pixels_per_point = window.scale_factor() as f32;

        let egui_renderer = egui_wgpu::Renderer::new(gpu.device(), surface_format, None, 1, false);

        let egui_ctx = egui::Context::default();
        let egui_state = egui_winit::State::new(
            egui_ctx.clone(), //just a shallow clone since it's behind an Arc
            egui::ViewportId::default(),
            window,
            Some(native_pixels_per_point),
            None,
            Some(2048), //TODO maybe find the concrete value, for now we leave 2048 because wasm
        ); //state that gets all the events from the window and gather them
           //https://github.com/emilk/egui/blob/bdc8795b0476c25faab927fc3c731f2d79f2098f/crates/eframe/src/native/epi_integration.rs#L361

        //size of the gui window. Will get resized automatically
        let width = 100;
        let height = 100;

        //Gui state based on what the user does
        let gui_main_widget = GuiMainWidget::new(gpu);

        // Mutate global style with above changes
        #[allow(unused_mut)]
        let mut style = style();

        //on web the view only renders when there is a mouse being dragged or when
        // there is an input so animations are choppy if you don't move the mouse.
        // Therefore we disable them
        cfg_if::cfg_if! {
            if #[cfg(target_arch = "wasm32")] {
                style.animation_time = 0.0;
            }
        }
        egui_ctx.set_style(style);

        let command_buffer = CommandBuffer::new();

        Self {
            egui_ctx,
            egui_state,
            egui_renderer,
            width,
            height,
            hidden: false,
            gui_main_widget,
            command_buffer,
            callbacks: Vec::new(),
            callbacks_for_selected_mesh: Vec::new(),
        }
    }

    pub fn resize(&mut self, width: u32, height: u32) {
        self.width = width;
        self.height = height;
    }

    //TODO rename to "process gui event" for coherency with the other event
    // processing things
    pub fn on_event(&mut self, window: &Window, event: &winit::event::WindowEvent) -> EventResponse {
        self.egui_state.on_window_event(window, event)
    }

    /// # Panics
    /// Will panic is the path is not valid unicode
    pub fn on_drop(&mut self, path_buf: &Path, scene: &mut Scene) {
        self.gui_main_widget.set_default_selected_entity(scene);

        let path = path_buf.to_str().unwrap();
        let entity = self.gui_main_widget.selected_entity.unwrap();
        if self.gui_main_widget.hovered_diffuse_tex {
            scene
                .world_mut()
                .insert_one(entity, DiffuseImg::new_from_path(path, &ImgConfig::default()))
                .ok();
        }
        if self.gui_main_widget.hovered_normal_tex {
            scene
                .world_mut()
                .insert_one(entity, NormalImg::new_from_path(path, &ImgConfig::default()))
                .ok();
        }
        if self.gui_main_widget.hovered_roughness_tex {
            scene
                .world_mut()
                .insert_one(entity, RoughnessImg::new_from_path(path, &ImgConfig::default()))
                .ok();
        }
    }

    pub fn wants_pointer_input(&self) -> bool {
        //tryng to solve https://github.com/urholaukkarinen/egui-gizmo/issues/19
        self.egui_ctx.wants_pointer_input()
    }

    pub fn is_hovering(&self) -> bool {
        self.egui_ctx.is_pointer_over_area()
    }

    //inspiration from voidin renderer on github
    //https://github.com/pudnax/voidin/blob/91e6b564008879388f3777bcb6154c656bfc533c/crates/app/src/app.rs#L643
    #[allow(clippy::too_many_arguments)]
    pub fn render(
        &mut self,
        window: &winit::window::Window,
        gpu: &Gpu,
        renderer: &Renderer,
        runner: &Runner,
        scene: &mut Scene,
        plugins: &Plugins,
        config: &mut Config,
        out_view: &wgpu::TextureView,
    ) {
        if self.hidden {
            return;
        }
        self.begin_frame();

        let screen_descriptor = ScreenDescriptor {
            size_in_pixels: [self.width, self.height],
            pixels_per_point: self.egui_ctx.pixels_per_point(),
        };

        let full_output = self.egui_ctx.run(self.egui_state.take_egui_input(window), |ctx: &egui::Context| {
            // ui_builder(ctx) // THIS ACTUALLY RENDERS GUI
            self.gui_main_widget.build_gui(
                self.width,
                self.height,
                ctx,
                renderer,
                &mut self.egui_renderer,
                gpu,
                scene,
                config,
                runner,
                &mut self.command_buffer,
                &mut self.callbacks,
                &mut self.callbacks_for_selected_mesh,
                plugins,
            );
        });
        let paint_jobs = self.egui_ctx.tessellate(full_output.shapes, self.egui_ctx.pixels_per_point());
        let textures_delta = full_output.textures_delta;

        let mut encoder = gpu
            .device()
            .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("Gui") });
        {
            for (texture_id, image_delta) in &textures_delta.set {
                self.egui_renderer.update_texture(gpu.device(), gpu.queue(), *texture_id, image_delta);
            }
            for texture_id in &textures_delta.free {
                self.egui_renderer.free_texture(texture_id);
            }
            self.egui_renderer
                .update_buffers(gpu.device(), gpu.queue(), &mut encoder, &paint_jobs, &screen_descriptor);

            let render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("UI Pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: out_view,
                    resolve_target: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Load,
                        store: wgpu::StoreOp::Store,
                    },
                })],
                depth_stencil_attachment: None,
                timestamp_writes: None,
                occlusion_query_set: None,
            });
            self.egui_renderer
                .render(&mut render_pass.forget_lifetime(), paint_jobs.as_slice(), &screen_descriptor);
        }
        gpu.queue().submit(Some(encoder.finish()));
        self.end_frame(scene);
    }

    fn begin_frame(&self) {}

    fn end_frame(&mut self, scene: &mut Scene) {
        scene.world_mut().run_command_buffer(&mut self.command_buffer);
    }

    pub fn add_callback(
        &mut self,
        f: fn(&mut GuiMainWidget, ctx: &egui::Context, ui: &mut Ui, renderer: &Renderer, scene: &mut Scene),
        draw_in_global_panel: bool,
    ) {
        if draw_in_global_panel {
            self.callbacks.push(f);
        } else {
            self.callbacks_for_selected_mesh.push(f);
        }
    }
}

impl GuiMainWidget {
    #[allow(clippy::too_many_arguments)]
    #[allow(clippy::too_many_lines)]
    fn build_gui(
        &mut self,
        screen_width: u32,
        screen_height: u32,
        ctx: &egui::Context,
        renderer: &Renderer,
        egui_renderer: &mut egui_wgpu::Renderer,
        gpu: &Gpu,
        scene: &mut Scene,
        config: &mut Config,
        runner: &Runner,
        command_buffer: &mut CommandBuffer,
        callbacks: &mut [CbFnType],
        callbacks_for_selected_mesh: &mut [CbFnType],
        plugins: &Plugins,
    ) {
        self.set_default_selected_entity(scene);

        //draw point indices for the selected entity
        if let Some(ent) = self.selected_entity {
            if let Ok(mut c) = scene.get_comp::<&mut VisPoints>(&ent) {
                self.draw_verts_indices(ctx, scene, screen_width, screen_height, &mut c);
            }
        }

        egui::SidePanel::left("my_left_panel").default_width(SIDE_PANEL_WIDTH).show(ctx, |ui| {
            egui::ScrollArea::vertical().show(ui, |ui| {
                // Scene
                // We use this most if not all of the time, might as well leave it open by default
                egui::CollapsingHeader::new("Scene").default_open(true).show(ui, |ui| {
                    ui.group(|ui| {
                        ScrollArea::vertical()
                            .max_height(200.0)
                            .scroll_bar_visibility(scroll_area::ScrollBarVisibility::AlwaysVisible)
                            .auto_shrink([false, false])
                            .show(ui, |ui| {
                                ui.with_layout(Layout::top_down_justified(Align::LEFT), |ui| {
                                    ui.spacing_mut().item_spacing.y = 0.0;
                                    ui.spacing_mut().button_padding.y = 4.0;

                                    //get all entities that are renderable and sort by name
                                    let entities = scene.get_renderables(true);
                                    /* ---------------------------------- NOTE ---------------------------------- */
                                    // We have 2 ways to select an entity - click2select and through this GUI
                                    // Both of them should work together and using one should update the state of the other.
                                    // Both selection methods are managed by the Selector resource.
                                    // Since the click2select supports the idea of deselection, so should the GUI.
                                    /* -------------------------------------------------------------------------- */
                                    // The selector may have been set by click2select, respect that selection instead of starting afresh
                                    let mut nothing_selected = true;
                                    if let Ok(selector) = scene.get_resource::<&mut Selector>() {
                                        if let Some(current_selected) = &selector.current_selected {
                                            nothing_selected = false;
                                            let name = current_selected.clone();
                                            if self.selected_mesh_name != name.clone() {
                                                let entity = scene.get_entity_with_name(&name);
                                                self.selected_entity = entity;
                                            }
                                            self.selected_mesh_name = name;
                                        }
                                    }
                                    if nothing_selected {
                                        // If we dont have a selector resource OR if selection is None, leave the selection GUI in a deselected state
                                        self.selected_mesh_name = String::new();
                                        self.selected_entity = None;
                                    }

                                    // Manage selection via the GUI
                                    // Go through all visible meshes and show their names as selectable options
                                    for entity in entities {
                                        let e_ref = scene.world().entity(entity).unwrap();
                                        // get the name of the mesh which acts like a unique id
                                        let name = e_ref.get::<&Name>().expect("The entity has no name").0.clone();
                                        // GUI for this concrete mesh
                                        // if we click we can see options for vis
                                        let _res = ui.selectable_value(&mut self.selected_mesh_name, name.clone(), &name);

                                        if name == self.selected_mesh_name {
                                            // First, turn off outline for previous selection
                                            if let Some(prev_entity) = self.selected_entity {
                                                if let Ok(mut vis_outline) = scene.world().get::<&mut VisOutline>(prev_entity) {
                                                    vis_outline.show_outline = false;
                                                }
                                            }

                                            // Set new selection
                                            self.selected_entity = Some(entity);

                                            // Set selector and update outline for new selection
                                            let selector = Selector {
                                                current_selected: Some(name.clone()),
                                            };
                                            scene.add_resource(selector);
                                            if let Ok(mut vis_outline) = scene.world().get::<&mut VisOutline>(entity) {
                                                vis_outline.show_outline = true;
                                            }
                                            //make a side window
                                            self.draw_vis(ctx, renderer, scene, entity, command_buffer, callbacks_for_selected_mesh);
                                        }
                                    }
                                });
                            });
                    });
                });

                // Resources in the scene (global components)
                egui::CollapsingHeader::new("Resources").show(ui, |ui| {
                    self.draw_comps(ui, scene, scene.get_entity_resource(), command_buffer);
                });

                // Params
                egui::CollapsingHeader::new("Textures").show(ui, |ui| {
                    self.draw_textures(ui, scene, egui_renderer, gpu, command_buffer);
                });

                //Move
                // egui::CollapsingHeader::new("Move")
                // .show(ui, |ui| self.draw_move(ui, scene, command_buffer));

                // Params
                egui::CollapsingHeader::new("Params").show(ui, |ui| {
                    self.draw_params(ui, scene, config, command_buffer);
                });

                // Lights
                egui::CollapsingHeader::new("Lights").show(ui, |ui| self.draw_lights(ui, scene, command_buffer));

                // Cam
                egui::CollapsingHeader::new("Camera").show(ui, |ui| self.draw_cam(ui, scene, command_buffer));

                // Io
                egui::CollapsingHeader::new("Io").show(ui, |ui| {
                    self.draw_io(ui, scene, command_buffer, self.selected_entity);
                });

                // profiling
                egui::CollapsingHeader::new("Profiling").show(ui, |ui| self.draw_profiling(ui, scene, command_buffer));

                // Plugins
                egui::CollapsingHeader::new("Plugins").show(ui, |ui| {
                    self.draw_plugins(ui, scene, plugins, command_buffer);
                });

                //fps
                ui.separator();
                let dt = runner.dt();
                let fps = 1.0 / dt.as_secs_f32();
                let ms = dt.as_millis();
                let fps_string = format!("{fps:.0}");
                let ms_string = format!("{ms:.2}");
                ui.label(egui::RichText::new("FPS: ".to_owned() + &fps_string));
                ui.label(egui::RichText::new("dt(ms): ".to_owned() + &ms_string));

                // A `scope` creates a temporary [`Ui`] in which you can change settings:
                // TODO: Change wrap mode?
                ui.scope(|ui| {
                    ui.visuals_mut().override_text_color = Some(egui::Color32::RED);
                    ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
                    ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Wrap);
                });

                ui.add(egui::Separator::default());
                for f in callbacks.iter() {
                    f(self, ctx, ui, renderer, scene);
                }

                for system_and_metadata in plugins.gui_systems.iter() {
                    let sys = &system_and_metadata.0;
                    let func = sys.f;
                    let gui_window = func(&self.selected_entity.into(), scene);
                    let window_name = gui_window.window_name;
                    let widgets = gui_window.widgets;
                    let window_type = gui_window.window_type;
                    if widgets.is_empty() {
                        continue; //there's no widgets so there's nothing to
                                  // draw
                    }

                    //recursivelly draw all widgets
                    // https://stackoverflow.com/a/72862424
                    let mut draw_widgets = |ui: &mut Ui| {
                        //we make a helper so that we can call recursivelly
                        fn helper(ui: &mut Ui, widgets: &RVec<WidgetsFFI>, selected_entity: Entity, scene: &mut Scene) {
                            for widget in widgets.iter() {
                                match widget {
                                    WidgetsFFI::Slider(slider) => {
                                        let mut val = slider.init_val;
                                        if let RSome(slider_width) = slider.width {
                                            ui.spacing_mut().slider_width = slider_width;
                                            //changes size od slider2
                                        }
                                        let res = ui.add(
                                            Slider::new(&mut val, slider.min..=slider.max)
                                                .fixed_decimals(3)
                                                .text(slider.name.as_str()),
                                        );
                                        if res.dragged() {
                                            (slider.f_change)(val, &slider.name, &selected_entity, scene);
                                        }
                                        // } else {
                                        if res.drag_stopped() {
                                            // Updated method here
                                            if let RSome(func) = slider.f_no_change {
                                                func(&slider.name.clone(), &selected_entity, scene);
                                            }
                                        }
                                        // if res.drag_released() {
                                        //     if let RSome(func) =
                                        // slider.f_no_change {
                                        //         func(slider.name.clone(),
                                        // selected_entity, scene);
                                        //     }
                                        // }
                                    }
                                    WidgetsFFI::Checkbox(checkbox) => {
                                        let mut val = checkbox.init_val;
                                        let res = ui.add(egui::Checkbox::new(&mut val, checkbox.name.as_str()));
                                        if res.clicked() {
                                            (checkbox.f_clicked)(val, &checkbox.name, &selected_entity, scene);
                                        }
                                    }
                                    WidgetsFFI::Button(button) => {
                                        if ui.add(egui::Button::new(button.name.as_str())).clicked() {
                                            (button.f_clicked)(&button.name, &selected_entity, scene);
                                        }
                                    }
                                    WidgetsFFI::SelectableList(selectable_list) => {
                                        let mut draw_selectables = |ui: &mut Ui| {
                                            for item in selectable_list.items.iter() {
                                                if ui.add(egui::Button::selectable(item.is_selected, item.name.to_string())).clicked() {
                                                    (item.f_clicked)(&item.name, &selected_entity, scene);
                                                }
                                            }
                                        };

                                        if selectable_list.is_horizontal {
                                            ui.horizontal(draw_selectables);
                                        } else {
                                            draw_selectables(ui);
                                        }
                                    }
                                    WidgetsFFI::Horizontal(widgets) => {
                                        ui.horizontal(|ui| {
                                            helper(ui, widgets, selected_entity, scene);
                                        });
                                    }
                                }
                            }
                        }
                        if let Some(selected_entity) = self.selected_entity {
                            //finally call the helper function so that we start the recursion
                            helper(ui, &widgets, selected_entity, scene);
                        } else {
                            // If we don't have a selected entity, we create a dummy entity and pass it to the helper function
                            // This is to make sure that the gui is still drawn even for a GuiSystem that doesn't need an entity (SceneAnimation in smpl-rs)
                            // GuiSystems that do need an entity will not run since the dummy entity has no components
                            let dummy_entity = scene.get_or_create_hidden_entity("DummyEntity").entity();
                            helper(ui, &widgets, dummy_entity, scene);
                        }
                    };

                    match window_type {
                        #[allow(clippy::cast_precision_loss)]
                        GuiWindowType::FloatWindow(pivot, position, position_type) => {
                            // egui::Window::new(window_name.to_string()).show(ctx, &mut draw_widgets);
                            let pos_x = (screen_width as f32 - SIDE_PANEL_WIDTH) * position.0[0];
                            let pos_y = (screen_height as f32) * position.0[1];
                            let pivot = match pivot {
                                WindowPivot::LeftBottom => Align2::LEFT_BOTTOM,
                                WindowPivot::LeftCenter => Align2::LEFT_CENTER,
                                WindowPivot::LeftTop => Align2::LEFT_TOP,
                                WindowPivot::CenterBottom => Align2::CENTER_BOTTOM,
                                WindowPivot::CenterCenter => Align2::CENTER_CENTER,
                                WindowPivot::CenterTop => Align2::CENTER_TOP,
                                WindowPivot::RightBottom => Align2::RIGHT_BOTTOM,
                                WindowPivot::RightCenter => Align2::RIGHT_CENTER,
                                WindowPivot::RightTop => Align2::RIGHT_TOP,
                            };

                            let mut win = egui::Window::new(window_name.to_string()).pivot(pivot);
                            match position_type {
                                WindowPositionType::Fixed => {
                                    win = win.fixed_pos([pos_x, pos_y]);
                                }
                                WindowPositionType::Initial => {
                                    win = win.default_pos([pos_x, pos_y]);
                                }
                            }
                            // win.show(ctx, &mut draw_widgets);
                            win.show(ctx, &mut draw_widgets);
                        }
                        GuiWindowType::Sidebar => {
                            egui::CollapsingHeader::new(window_name.to_string()).show(ui, &mut draw_widgets);
                        }
                    };
                }
            });
        });

        //draw floating windows for any entities that have the OnGui component
        let mut query = scene.world().query::<(&Name, &DiffuseTex)>().with::<&OnGui>();
        for (_ent, (name, texture)) in query.iter() {
            //get the size of the texture and calculate an initial size of the window
            let tex_w = texture.0.width();
            let tex_h = texture.0.height();
            let maximum_size_in_one_axis = 500;
            //calculate an initial size of the window
            let tex_max_dim = tex_w.max(tex_h);
            #[allow(clippy::cast_precision_loss)]
            let scale = if tex_max_dim > maximum_size_in_one_axis {
                maximum_size_in_one_axis as f32 / tex_max_dim as f32
            } else {
                1.0
            };
            #[allow(clippy::cast_precision_loss)]
            let initial_size = Vec2::new(tex_w as f32 * scale, tex_h as f32 * scale);

            //we need to create a new texture view because egui_renderer expects.register_native_texture expects rgba8unorm but my diffuse img is rgba8unormsgb
            #[cfg(not(target_arch = "wasm32"))]
            let new_view = texture.0.texture.create_view(&wgpu::TextureViewDescriptor {
                mip_level_count: Some(texture.0.texture.mip_level_count()),
                format: Some(wgpu::TextureFormat::Rgba8Unorm),
                ..Default::default()
            });

            #[cfg(target_arch = "wasm32")]
            let new_view = texture.0.view.clone();

            egui::Window::new(name.0.clone())
                .resizable(true)
                .vscroll(true)
                .hscroll(true)
                .default_size(initial_size)
                .show(ctx, |ui| {
                    //get egui textureid
                    let diffuse_egui_tex_id = self
                        .wgputex_2_eguitex
                        .entry(texture.0.texture.clone())
                        .or_insert_with(|| egui_renderer.register_native_texture(gpu.device(), &new_view, wgpu::FilterMode::Linear));
                    //show img
                    ui.add(egui::Image::from_texture((*diffuse_egui_tex_id, ui.available_size())));
                });
        }
    }

    // if no selected mesh is set yet, any query with the name will fail.
    // we can use this function to set it to some default value(usually the first
    // mesh in my list)
    fn set_default_selected_entity(&mut self, scene: &Scene) {
        for e_ref in scene.world().iter() {
            // let entity = e_ref.entity();
            let is_renderable = e_ref.has::<Renderable>();
            if is_renderable {
                //get the name of the mesh which acts like a unique id
                let name = e_ref.get::<&Name>().expect("The entity has no name").0.clone();

                //if it's the first time we encounter a renderable mesh,  we set the selected
                // name to this one
                if self.selected_mesh_name.is_empty() && name != GLOSS_FLOOR_NAME {
                    self.selected_mesh_name.clone_from(&name);
                    self.selected_entity = Some(e_ref.entity());
                }
            }
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn draw_vis(
        &mut self,
        ctx: &egui::Context,
        renderer: &Renderer,
        scene: &mut Scene,
        entity: Entity,
        command_buffer: &mut CommandBuffer,
        callbacks_for_selected_mesh: &mut [CbFnType],
    ) {
        let e_ref = scene.world().entity(entity).unwrap();
        let has_vis_points = e_ref.has::<VisPoints>();
        let has_vis_lines = e_ref.has::<VisLines>();
        let has_vis_outline = e_ref.has::<VisOutline>();
        let _has_vis_wireframe = e_ref.has::<VisWireframe>();
        let has_vis_mesh = e_ref.has::<VisMesh>();
        let _has_vis_normals = e_ref.has::<VisNormals>();
        let mut _window = egui::Window::new("vis_points")
            // .auto_sized()
            .default_width(100.0)
            // .min_height(600.0)
            .resizable(false)
            // .collapsible(true)
            .title_bar(false)
            .scroll([false, false])
            .anchor(Align2::LEFT_TOP, Vec2::new(SIDE_PANEL_WIDTH + 12.0, 0.0))
            .show(ctx, |ui| {
                //dummy vis options that we use to draw invisible widgets
                //we need this because when we don't have a component we still want to draw an
                // empty space for it and we use this dummy widget to figure out how much space
                // we need points
                if has_vis_points {
                    ui.add_space(SPACING_1);
                    let mut c = scene.get_comp::<&mut VisPoints>(&entity).unwrap();
                    self.draw_vis_points(ui, scene, entity, command_buffer, has_vis_points, &mut c);
                }
                //mesh
                if has_vis_mesh {
                    ui.add_space(SPACING_1);
                    // let mut c = scene.get_comp::<&mut VisMesh>(&entity);
                    self.draw_vis_mesh(ui, scene, entity, command_buffer, has_vis_mesh);
                }
                //lines
                if has_vis_lines {
                    ui.add_space(SPACING_1);
                    let mut c = scene.get_comp::<&mut VisLines>(&entity).unwrap();
                    self.draw_vis_lines(ui, scene, entity, command_buffer, has_vis_lines, &mut c);
                }
                //outline
                if has_vis_outline {
                    ui.add_space(SPACING_1);
                    let mut c = scene.get_comp::<&mut VisOutline>(&entity).unwrap();
                    self.draw_vis_outline(ui, scene, entity, command_buffer, has_vis_outline, &mut c);
                }
                // TODO: Keep this?
                //wireframe
                // if has_vis_wireframe {
                //     ui.add_space(SPACING_1);
                //     let mut c = scene.get_comp::<&mut VisWireframe>(&entity);
                //     self.draw_vis_wireframe(
                //         // ctx,
                //         ui,
                //         // renderer,
                //         scene,
                //         entity,
                //         command_buffer,
                //         has_vis_wireframe,
                //         &mut c,
                //     );
                // }
                //normals
                // if has_vis_normals {
                //     ui.add_space(SPACING_1);
                //     let mut c = scene.get_comp::<&mut VisNormals>(&entity);
                //     self.draw_vis_normals(
                //         // ctx,
                //         ui,
                //         // renderer,
                //         scene,
                //         entity,
                //         command_buffer,
                //         has_vis_wireframe,
                //         &mut c,
                //     );
                // }

                ui.label("Components");
                ui.separator();
                self.draw_comps(ui, scene, entity, command_buffer);

                for f in callbacks_for_selected_mesh.iter() {
                    f(self, ctx, ui, renderer, scene);
                }
            });
    }

    #[allow(clippy::cast_precision_loss)]
    fn draw_verts_indices(&self, ctx: &egui::Context, scene: &Scene, screen_width: u32, screen_height: u32, c: &mut VisPoints) {
        if !c.show_points_indices {
            return;
        }
        //TODO remove all these unwraps
        egui::Area::new(egui::Id::new("verts_indices"))
            .interactable(false)
            .anchor(Align2::LEFT_TOP, egui::vec2(0.0, 0.0))
            .show(ctx, |ui| {
                if let Some(ent) = self.selected_entity {
                    let verts = scene.get_comp::<&Verts>(&ent).unwrap();
                    let model_matrix = scene.get_comp::<&ModelMatrix>(&ent).unwrap();
                    let cam = scene.get_current_cam().unwrap();
                    let view = cam.view_matrix(scene);
                    let proj = cam.proj_matrix(scene);
                    for (idx, vert) in verts.0.row_iter().enumerate() {
                        let point_world = model_matrix.0 * na::Point3::from(vert.fixed_columns::<3>(0).transpose());
                        let point_screen = cam.project(
                            point_world,
                            view,
                            proj,
                            na::Vector2::<f32>::new(screen_width as f32, screen_height as f32),
                        );

                        let widget_max_size = egui::vec2(35.0, 35.0);
                        let widget_rect = egui::Rect::from_min_size(
                            egui::pos2(
                                point_screen.x / ctx.pixels_per_point() - widget_max_size.x / 2.0,
                                (screen_height as f32 - point_screen.y) / ctx.pixels_per_point(),
                            ),
                            widget_max_size,
                        );
                        ui.put(widget_rect, egui::Label::new(idx.to_string()));
                    }
                };
            });
    }

    fn draw_vis_points(&self, ui: &mut Ui, _scene: &Scene, entity: Entity, command_buffer: &mut CommandBuffer, is_visible: bool, c: &mut VisPoints) {
        // VIS POINTS
        ui.label("Points");
        ui.separator();
        ui.add_enabled_ui(is_visible, |ui| {
            let res = ui.checkbox(&mut c.show_points, "Show points");
            ui.checkbox(&mut c.show_points_indices, "Show point indices");
            if res.clicked() {
                command_buffer.insert_one(entity, ShadowMapDirty);
            }
            //all the other guis are disabled if we don't show points
            if c.show_points {
                //point_color
                ui.horizontal(|ui| {
                    ui.color_edit_button_rgba_premultiplied(&mut c.point_color.data.0.as_mut_slice()[0]);
                    ui.label("Point color");
                });
                //point_size
                ui.horizontal(|ui| {
                    ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
                    ui.add(Slider::new(&mut c.point_size, 0.0..=30.0).text("Size"))
                });
                ui.checkbox(&mut c.is_point_size_in_world_space, "isSizeInWorld");
                // zbuffer
                ui.checkbox(&mut c.zbuffer, "Use Z-Buffer");
                //colortype
                egui::ComboBox::new(0, "Color") //the id has to be unique to other comboboxes
                    .selected_text(format!("{:?}", c.color_type))
                    .show_ui(ui, |ui| {
                        // ui.style_mut().wrap = Some(false);
                        ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Wrap);
                        ui.set_min_width(60.0);
                        ui.selectable_value(&mut c.color_type, PointColorType::Solid, "Solid");
                        ui.selectable_value(&mut c.color_type, PointColorType::PerVert, "PerVert");
                    });
            }
        });
    }

    fn draw_vis_mesh(&mut self, ui: &mut Ui, scene: &mut Scene, entity: Entity, command_buffer: &mut CommandBuffer, is_visible: bool) {
        let mut c = scene.get_comp::<&mut VisMesh>(&entity).unwrap();

        // VIS MESH
        ui.label("Mesh");
        ui.separator();

        // Use `add_enabled_ui` to conditionally enable UI elements
        ui.add_enabled_ui(is_visible, |ui| {
            let res = ui.checkbox(&mut c.show_mesh, "Show mesh");
            if res.clicked() {
                command_buffer.insert_one(entity, ShadowMapDirty);
            }

            let _name = scene.get_comp::<&Name>(&entity).unwrap().0.clone();

            // The following settings are only enabled if `show_mesh` is true
            if c.show_mesh {
                // Solid color editor
                ui.horizontal(|ui| {
                    ui.color_edit_button_rgba_unmultiplied(&mut c.solid_color.data.0.as_mut_slice()[0]);
                    ui.label("Solid color");
                });

                // Metalness slider
                ui.horizontal(|ui| {
                    ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0;
                    ui.add(Slider::new(&mut c.metalness, 0.0..=1.0).text("Metal"));
                });

                // Roughness slider
                ui.horizontal(|ui| {
                    ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0;
                    ui.add(Slider::new(&mut c.perceptual_roughness, 0.0..=1.0).text("Rough"));
                });

                // Roughness black level slider
                ui.horizontal(|ui| {
                    ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0;
                    ui.add(Slider::new(&mut c.roughness_black_lvl, 0.0..=1.0).text("RoughBlackLvl"));
                });

                // Opacity slider
                ui.horizontal(|ui| {
                    ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0;
                    ui.add(Slider::new(&mut c.opacity, 0.0..=1.0).text("Opacity"));
                });

                // SSS checkbox
                ui.checkbox(&mut c.needs_sss, "Needs SSS");

                // Color type selection combo box
                egui::ComboBox::new(1, "Color")
                    .selected_text(format!("{:?}", c.color_type))
                    .show_ui(ui, |ui| {
                        ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Wrap);
                        ui.set_min_width(60.0);
                        ui.selectable_value(&mut c.color_type, MeshColorType::Solid, "Solid");
                        ui.selectable_value(&mut c.color_type, MeshColorType::PerVert, "PerVert");
                        ui.selectable_value(&mut c.color_type, MeshColorType::Texture, "Texture");
                        ui.selectable_value(&mut c.color_type, MeshColorType::UV, "UV");
                        ui.selectable_value(&mut c.color_type, MeshColorType::Normal, "Normal");
                        ui.selectable_value(&mut c.color_type, MeshColorType::NormalViewCoords, "NormalViewCoords");
                    });

                // UV scale slider
                ui.horizontal(|ui| {
                    ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0;
                    ui.add(Slider::new(&mut c.uv_scale, 0.0..=3000.0).text("UVScale"));
                });
            }
        });
    }
    fn draw_vis_outline(
        &mut self,
        ui: &mut Ui,
        _scene: &Scene,
        _entity: Entity,
        _command_buffer: &mut CommandBuffer,
        is_visible: bool,
        c: &mut VisOutline,
    ) {
        ui.label("Outline");
        ui.separator();

        // Use `add_enabled_ui` to conditionally enable UI elements
        ui.add_enabled_ui(is_visible, |ui| {
            // Do not expose this to the user, we will use this internally to toggle the outline
            // let res = ui.checkbox(&mut c.show_outline, "Show outline");
            // if res.clicked() {
            //     command_buffer.insert_one(entity, ShadowMapDirty);
            // }

            if c.show_outline {
                // Outline color editor
                ui.horizontal(|ui| {
                    ui.color_edit_button_rgba_unmultiplied(&mut c.outline_color.data.0.as_mut_slice()[0]);
                    ui.label("Outline color");
                });

                // Outline width slider
                ui.horizontal(|ui| {
                    ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; // Adjust slider width
                    ui.add(Slider::new(&mut c.outline_width, 0.0..=25.0).text("Width"));
                });
            }
        });
    }
    fn draw_vis_lines(
        &mut self,
        ui: &mut Ui,
        _scene: &Scene,
        entity: Entity,
        command_buffer: &mut CommandBuffer,
        is_visible: bool,
        c: &mut VisLines,
    ) {
        ui.label("Lines");
        ui.separator();

        // Use `add_enabled_ui` to conditionally enable UI elements
        ui.add_enabled_ui(is_visible, |ui| {
            let res = ui.checkbox(&mut c.show_lines, "Show lines");
            if res.clicked() {
                command_buffer.insert_one(entity, ShadowMapDirty);
            }

            if c.show_lines {
                // Solid color editor
                ui.horizontal(|ui| {
                    ui.color_edit_button_rgba_unmultiplied(&mut c.line_color.data.0.as_mut_slice()[0]);
                    ui.label("Solid color");
                });

                // Line width slider
                ui.horizontal(|ui| {
                    ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; // Adjust slider width
                    ui.add(Slider::new(&mut c.line_width, 0.0..=30.0).text("Width"));
                });

                // Z-buffer and antialiasing options
                ui.checkbox(&mut c.zbuffer, "Use Z-Buffer");
                ui.checkbox(&mut c.antialias_edges, "Antialias");
            }
        });
    }

    // TODO: Probably remove this, can always add back later if needed
    // fn draw_vis_wireframe(
    //     &mut self,
    //     // ctx: &egui::Context,
    //     ui: &mut Ui,
    //     // renderer: &Renderer,
    //     _scene: &Scene,
    //     entity: Entity,
    //     command_buffer: &mut CommandBuffer,
    //     is_visible: bool,
    //     c: &mut VisWireframe,
    // ) {
    //     //VIS Wireframe
    //     ui.label("Wireframe");
    //     ui.separator();
    //     ui.add_visible_ui(is_visible, |ui| {
    //         let res = ui.checkbox(&mut c.show_wireframe, "Show wireframe");
    //         if res.clicked() {
    //             command_buffer.insert_one(entity, ShadowMapDirty);
    //         }
    //         if c.show_wireframe {
    //             // ui.add_enabled_ui(c.show_wireframe, |ui| {
    //             //solid_color
    //             ui.horizontal(|ui| {
    //                 ui.color_edit_button_rgba_unmultiplied(
    //                     &mut c.wire_color.data.0.as_mut_slice()[0],
    //                 );
    //                 ui.label("Wire color");
    //             });
    //             //width
    //             ui.horizontal(|ui| {
    //                 ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0;
    // //changes size od slider                 ui.add(Slider::new(&mut
    // c.wire_width, 0.0..=8.0).text("Width"))             });
    //             // });
    //         }
    //     });
    // }

    // fn draw_vis_normals(
    //     &mut self,
    //     // ctx: &egui::Context,
    //     ui: &mut Ui,
    //     // renderer: &Renderer,
    //     _scene: &Scene,
    //     entity: Entity,
    //     command_buffer: &mut CommandBuffer,
    //     is_visible: bool,
    //     c: &mut VisNormals,
    // ) {
    //     //VIS Normals
    //     ui.label("Normals");
    //     ui.separator();
    //     ui.add_visible_ui(is_visible, |ui| {
    //         let res = ui.checkbox(&mut c.show_normals, "Show Normals");
    //         if res.clicked() {
    //             command_buffer.insert_one(entity, ShadowMapDirty);
    //         }
    //         if c.show_normals {
    //             // ui.add_enabled_ui(c.show_wireframe, |ui| {
    //             //solid_color
    //             ui.horizontal(|ui| {
    //                 ui.color_edit_button_rgba_unmultiplied(
    //                     &mut c.normals_color.data.0.as_mut_slice()[0],
    //                 );
    //                 ui.label("Normal color");
    //             });
    //             //width
    //             ui.horizontal(|ui| {
    //                 ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0;
    // //changes size od slider                 ui.add(Slider::new(&mut
    // c.normals_width, 0.0..=8.0).text("Width"))             });
    //             //scale
    //             ui.horizontal(|ui| {
    //                 ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0;
    // //changes size od slider                 ui.add(Slider::new(&mut
    // c.normals_scale, 0.0..=1.0).text("Scale"))             });
    //         }
    //     });
    // }

    fn draw_comps(&mut self, ui: &mut Ui, scene: &Scene, entity: Entity, _command_buffer: &mut CommandBuffer) {
        let e_ref = scene.world().entity(entity).unwrap();
        //print component names
        let comp_infos: Vec<gloss_hecs::TypeInfo> = e_ref.component_infos().collect();
        let comp_full_names: Vec<String> = comp_infos.iter().map(gloss_hecs::TypeInfo::name).collect();
        // let mut comp_names: Vec<String> = comp_full_names
        //     .iter()
        //     .map(|n| n.split("::").last().unwrap().to_string())
        //     .collect();
        // Some of our comps are now generic so they need to be handled
        let mut comp_names: Vec<String> = comp_full_names
            .iter()
            .map(|n| {
                // Split at the '<' character if it exists, otherwise split at "::" and take the
                // last part
                if let Some(pos) = n.find('<') {
                    n[..pos].split("::").last().unwrap().to_string()
                } else {
                    n.split("::").last().unwrap().to_string()
                }
            })
            .collect();

        //concat also the type id
        let mut comp_names_and_type = Vec::new();
        for (comp_name, comp_info) in comp_names.iter().zip(comp_infos.iter()) {
            let comp_info_str = format!("{:?}", comp_info.id());
            comp_names_and_type.push(comp_name.to_owned() + &comp_info_str);
        }
        comp_names.sort();
        comp_names_and_type.sort();

        for name in comp_names {
            ui.label(RichText::new(name).font(FontId::proportional(10.0)));
        }
    }

    fn draw_textures(
        &mut self,
        ui: &mut Ui,
        scene: &mut Scene,
        egui_renderer: &mut egui_wgpu::Renderer,
        gpu: &Gpu,
        _command_buffer: &mut CommandBuffer,
    ) {
        if scene.nr_renderables() == 0 {
            return;
        }
        let Some(entity) = scene.get_entity_with_name(&self.selected_mesh_name) else {
            error!("Selected mesh does not exist with name {}", self.selected_mesh_name);
            return;
        };

        //get diffuse tex
        ui.label("Diffuse");
        ui.separator();
        let diffuse_tex = scene.get_comp::<&DiffuseTex>(&entity).unwrap();
        //if we have a CPU texture, we get the corresponding GPU texture, if not, we get a default GPU tex
        let tex: &easy_wgpu::texture::Texture = scene
            .world()
            .get::<&DiffuseImg>(entity)
            .map_or_else(|_| self.default_texture.as_ref().unwrap(), |_| &diffuse_tex.0);
        //we need to create a new texture view because egui_renderer expects.register_native_texture expects rgba8unorm but my diffuse img is rgba8unormsgb
        let nr_mips = diffuse_tex.0.texture.mip_level_count();
        let new_view = diffuse_tex.0.texture.create_view(&wgpu::TextureViewDescriptor {
            mip_level_count: Some(nr_mips),
            format: Some(wgpu::TextureFormat::Rgba8Unorm),
            ..Default::default()
        });
        //get egui textureid
        let diffuse_egui_tex_id = self
            .wgputex_2_eguitex
            .entry(tex.texture.clone())
            .or_insert_with(|| egui_renderer.register_native_texture(gpu.device(), &new_view, wgpu::FilterMode::Linear));
        //show img
        let res = ui.add(egui::Image::from_texture((*diffuse_egui_tex_id, Vec2::new(120.0, 120.0))));
        self.hovered_diffuse_tex = res.hovered();

        //get normal tex
        ui.label("Normal");
        ui.separator();
        let normal_tex = scene.get_comp::<&NormalTex>(&entity).unwrap();
        //if we have a CPU texture, we get the corresponding GPU texture, if not, we get a default GPU tex
        let tex: &easy_wgpu::texture::Texture = scene
            .world()
            .get::<&NormalImg>(entity)
            .map_or_else(|_| self.default_texture.as_ref().unwrap(), |_| &normal_tex.0);
        //get egui textureid
        let normal_egui_tex_id = self
            .wgputex_2_eguitex
            .entry(tex.texture.clone())
            .or_insert_with(|| egui_renderer.register_native_texture(gpu.device(), &tex.view, wgpu::FilterMode::Linear));
        //show img
        let res = ui.add(egui::Image::from_texture((*normal_egui_tex_id, Vec2::new(120.0, 120.0))));
        self.hovered_normal_tex = res.hovered();

        //get roughness tex
        ui.label("Roughness");
        ui.separator();
        let roughness_tex = scene.get_comp::<&RoughnessTex>(&entity).unwrap();
        //if we have a CPU texture, we get the corresponding GPU texture, if not, we get a default GPU tex
        let tex: &easy_wgpu::texture::Texture = scene
            .world()
            .get::<&RoughnessImg>(entity)
            .map_or_else(|_| self.default_texture.as_ref().unwrap(), |_| &roughness_tex.0);
        //get egui textureid
        let roughness_egui_tex_id = self
            .wgputex_2_eguitex
            .entry(tex.texture.clone())
            .or_insert_with(|| egui_renderer.register_native_texture(gpu.device(), &tex.view, wgpu::FilterMode::Linear));
        //show img
        let res = ui.add(egui::Image::from_texture((*roughness_egui_tex_id, Vec2::new(120.0, 120.0))));
        self.hovered_roughness_tex = res.hovered();
    }

    // TODO: Keep or remove?
    // #[allow(clippy::similar_names)]
    // fn draw_move(&mut self, ui: &mut Ui, scene: &Scene, command_buffer: &mut
    // CommandBuffer) {     egui::ComboBox::from_label("Mode")
    //         .selected_text(format!("{:?}", self.gizmo_mode))
    //         .show_ui(ui, |ui| {
    //             ui.selectable_value(&mut self.gizmo_mode, GizmoMode::Rotate,
    // "Rotate");             ui.selectable_value(&mut self.gizmo_mode,
    // GizmoMode::Translate, "Translate");             ui.selectable_value(&mut
    // self.gizmo_mode, GizmoMode::Scale, "Scale");         });

    //     egui::ComboBox::from_label("Orientation")
    //         .selected_text(format!("{:?}", self.gizmo_orientation))
    //         .show_ui(ui, |ui| {
    //             ui.selectable_value(
    //                 &mut self.gizmo_orientation,
    //                 GizmoOrientation::Global,
    //                 "Global",
    //             );
    //             ui.selectable_value(
    //                 &mut self.gizmo_orientation,
    //                 GizmoOrientation::Local,
    //                 "Local",
    //             );
    //         });

    //     //get camera
    //     let cam = scene.get_current_cam().unwrap();
    //     if !cam.is_initialized(scene) {
    //         return;
    //     }
    //     let view = cam.view_matrix(scene);
    //     let proj = cam.proj_matrix(scene);
    //     let v: [[f32; 4]; 4] = view.into();
    //     let p: [[f32; 4]; 4] = proj.into();

    //     //model matrix
    //     // let entity = scene.get_entity_with_name(&self.selected_mesh_name);
    //     if let Some(entity) = self.selected_entity {
    //         let model_matrix = scene.get_comp::<&ModelMatrix>(&entity).unwrap();
    //         let mm: [[f32; 4]; 4] = model_matrix.0.to_homogeneous().into();

    //         let gizmo = Gizmo::new("My gizmo")
    //             .view_matrix(v.into())
    //             .projection_matrix(p.into())
    //             .model_matrix(mm.into())
    //             .mode(self.gizmo_mode)
    //             .orientation(self.gizmo_orientation);

    //         let possible_gizmo_response = gizmo.interact(ui);

    //         if let Some(gizmo_response) = possible_gizmo_response {
    //             let _new_model_matrix = gizmo_response.transform();

    //             //TODO you can actually do gizmo_response.translation and
    // gizmo_reponse.quat directly...

    //             //get T
    //             let new_t_mint = gizmo_response.translation;
    //             let mut new_t = na::Translation3::<f32>::identity();
    //             new_t.x = new_t_mint.x;
    //             new_t.y = new_t_mint.y;
    //             new_t.z = new_t_mint.z;
    //             //get R
    //             let new_q_mint = gizmo_response.rotation;
    //             let mut new_quat = na::Quaternion::<f32>::identity();
    //             new_quat.i = new_q_mint.v.x;
    //             new_quat.j = new_q_mint.v.y;
    //             new_quat.k = new_q_mint.v.z;
    //             new_quat.w = new_q_mint.s;
    //             let new_quat_unit =
    // na::UnitQuaternion::from_quaternion(new_quat);             let new_rot =
    // new_quat_unit.into();             //get scale
    //             let new_scale_mint = gizmo_response.scale;
    //             let new_scale =
    // new_scale_mint.x.max(new_scale_mint.y).max(new_scale_mint.z); //TODO For now
    // we only get one scale value

    //             // let new_scale = gizmo_response.scale.max_element(); //TODO For
    // now we only get one scale value             //
    // // println!("gizmo scale is {:?}", gizmo_response.scale);             //
    // // println!("new_scale {:?}", new_scale);             //combine
    //             let mut new_model_mat = na::SimilarityMatrix3::<f32>::identity();
    //             new_model_mat.append_rotation_mut(&new_rot);
    //             new_model_mat.append_translation_mut(&new_t);
    //             new_model_mat.set_scaling(new_scale);

    //             //set
    //             // mesh.set_model_matrix(scene, new_isometry);
    //             let new_model_matrix = ModelMatrix(new_model_mat);
    //             command_buffer.insert_one(entity, new_model_matrix);

    //             //if it has pos lookat we also modify that
    //             if scene.world.has::<PosLookat>(entity).unwrap() {
    //                 let pos_lookat =
    // scene.get_comp::<&PosLookat>(&entity).unwrap();                 // TODO
    // make poslookat clone                 let pos_lookat =
    //                     PosLookat::new_from_model_matrix(new_model_mat,
    // pos_lookat.dist_lookat());
    // command_buffer.insert_one(entity, pos_lookat);             }
    //         }
    //     }
    // }

    fn draw_params(&mut self, ui: &mut Ui, _scene: &mut Scene, config: &mut Config, _command_buffer: &mut CommandBuffer) {
        //TODO get all things from config

        //ambient factor
        ui.horizontal(|ui| {
            ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
            ui.add(Slider::new(&mut config.render.ambient_factor, 0.0..=1.0).text("AmbientFactor"))
        });
        //environment_factor
        ui.horizontal(|ui| {
            ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
            ui.add(Slider::new(&mut config.render.environment_factor, 0.0..=5.0).text("EnvFactor"))
        });
        //bg_color
        ui.horizontal(|ui| {
            ui.color_edit_button_rgba_premultiplied(&mut config.render.bg_color.data.0.as_mut_slice()[0]);
            ui.label("bg_color");
        });
        //distance fade
        ui.checkbox(config.render.enable_distance_fade.as_mut().unwrap_or(&mut false), "DistanceFade");
        //distance fade start
        ui.horizontal(|ui| {
            ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
            ui.add(Slider::new(config.render.distance_fade_start.as_mut().unwrap_or(&mut 0.0), 1.0..=100.0).text("FadeStart"))
        });
        //distance fade end
        ui.horizontal(|ui| {
            ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
            ui.add(Slider::new(config.render.distance_fade_end.as_mut().unwrap_or(&mut 0.0), 1.0..=100.0).text("FadeEnd"))
        });
        //saturation
        ui.horizontal(|ui| {
            ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
            ui.add(Slider::new(&mut config.render.saturation, 0.0..=2.0).text("Saturation"))
        });
        //gamma
        ui.horizontal(|ui| {
            ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
            ui.add(Slider::new(&mut config.render.gamma, 0.5..=1.5).text("Gamma"))
        });
        //exposure
        ui.horizontal(|ui| {
            ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
            ui.add(Slider::new(&mut config.render.exposure, -5.0..=5.0).text("Exposure"))
        });
    }

    #[allow(clippy::too_many_lines)]
    fn draw_lights(&mut self, ui: &mut Ui, scene: &mut Scene, command_buffer: &mut CommandBuffer) {
        // //get all entities that are renderable and sort by name
        let entities = scene.get_lights(true);

        //go through all the lights and show their name
        ui.group(|ui| {
            ScrollArea::vertical()
                .max_height(200.0)
                .scroll_bar_visibility(scroll_area::ScrollBarVisibility::AlwaysVisible)
                .auto_shrink([false, false])
                .show(ui, |ui| {
                    ui.with_layout(Layout::top_down_justified(Align::LEFT), |ui| {
                        ui.spacing_mut().item_spacing.y = 0.0;
                        ui.spacing_mut().button_padding.y = 4.0;
                        for entity in entities {
                            let e_ref = scene.world().entity(entity).unwrap();

                            //get the name of the mesh which acts like a unique id
                            let name = e_ref.get::<&Name>().expect("The entity has no name").0.clone();

                            //if it's the first time we encounter a renderable mesh,  we set the selected
                            // name to this one
                            if self.selected_light_name.is_empty() {
                                self.selected_light_name.clone_from(&name);
                            }

                            //GUI for this concrete mesh
                            //if we click we can see options for vis
                            let _res = ui.selectable_value(&mut self.selected_light_name, name.clone(), &name);

                            if name == self.selected_light_name {
                                self.selected_light_entity = Some(entity);
                            }
                        }
                    });
                });
        });

        if let Some(entity) = self.selected_light_entity {
            ui.label("LightEmit");
            ui.separator();
            //color
            ui.horizontal(|ui| {
                let mut comp_light_emit = scene.get_comp::<&mut LightEmit>(&entity).unwrap();
                ui.color_edit_button_rgb(&mut comp_light_emit.color.data.0.as_mut_slice()[0]);
                ui.label("color");
            });
            //intensity
            ui.horizontal(|ui| {
                let mut comp_light_emit = scene.get_comp::<&mut LightEmit>(&entity).unwrap();
                ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
                ui.add(Slider::new(&mut comp_light_emit.intensity, 0.0..=1_000_000.0).text("intensity"))
            });
            //cast shadows
            let mut is_shadow_casting: bool = scene.world().has::<ShadowCaster>(entity).unwrap();
            let res = ui.checkbox(&mut is_shadow_casting, "CastShadows");
            if res.changed() {
                if is_shadow_casting {
                    //TODO adding this parameter just adds it with these daault. Maybe we need
                    // another way to temporarily disable shadows
                    command_buffer.insert_one(
                        entity,
                        ShadowCaster {
                            shadow_res: 2048,
                            shadow_bias_fixed: 2e-5,
                            shadow_bias: 0.15,
                            shadow_bias_normal: 1.5,
                        },
                    );
                } else {
                    command_buffer.remove_one::<ShadowCaster>(entity);
                }
            }
            // shadow_res
            if scene.world().has::<ShadowCaster>(entity).unwrap() {
                //we only mutate the component if we modify the slider because we want the
                // change detection of hecs to only pick-up and detect when we actually change
                // the value
                let shadow_caster_read = scene.get_comp::<&ShadowCaster>(&entity).unwrap();
                let mut shadow_res = shadow_caster_read.shadow_res;
                let mut shadow_bias_fixed = shadow_caster_read.shadow_bias_fixed;
                let mut shadow_bias = shadow_caster_read.shadow_bias;
                let mut shadow_bias_normal = shadow_caster_read.shadow_bias_normal;
                drop(shadow_caster_read);
                // ui.horizontal(|ui| {
                ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
                let res = ui.add(Slider::new(&mut shadow_res, 128..=4096).text("shadow_res"));
                if res.changed() {
                    //now we actually mutate the component
                    let mut shadow_caster = scene.get_comp::<&mut ShadowCaster>(&entity).unwrap();
                    shadow_caster.shadow_res = shadow_res;
                }
                //shadow bias fixed
                let res = ui.add(Slider::new(&mut shadow_bias_fixed, 0.0..=0.5).text("shadow_bias_fixed"));
                if res.changed() {
                    //now we actually mutate the component
                    let mut shadow_caster = scene.get_comp::<&mut ShadowCaster>(&entity).unwrap();
                    shadow_caster.shadow_bias_fixed = shadow_bias_fixed;
                }

                //shadow bias light
                let res = ui.add(Slider::new(&mut shadow_bias, 0.0..=0.4).text("shadow_bias"));
                if res.changed() {
                    //now we actually mutate the component
                    let mut shadow_caster = scene.get_comp::<&mut ShadowCaster>(&entity).unwrap();
                    shadow_caster.shadow_bias = shadow_bias;
                }
                //shadow bias normal
                let res = ui.add(Slider::new(&mut shadow_bias_normal, 0.0..=50.0).text("shadow_bias_normal"));
                if res.changed() {
                    //now we actually mutate the component
                    let mut shadow_caster = scene.get_comp::<&mut ShadowCaster>(&entity).unwrap();
                    shadow_caster.shadow_bias_normal = shadow_bias_normal;
                }
                // });
            }
            //range
            ui.horizontal(|ui| {
                let mut comp_light_emit = scene.get_comp::<&mut LightEmit>(&entity).unwrap();
                ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
                ui.add(Slider::new(&mut comp_light_emit.range, 0.0..=10000.0).text("range"))
            });
            //radius
            ui.horizontal(|ui| {
                let mut comp_light_emit = scene.get_comp::<&mut LightEmit>(&entity).unwrap();
                ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
                ui.add(Slider::new(&mut comp_light_emit.radius, 0.0..=10.0).text("radius"))
            });
            //inner_angle
            ui.horizontal(|ui| {
                let mut comp_light_emit = scene.get_comp::<&mut LightEmit>(&entity).unwrap();
                let outer_angle = comp_light_emit.outer_angle;
                ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
                ui.add(Slider::new(&mut comp_light_emit.inner_angle, 0.0..=outer_angle).text("inner_angle"))
            });
            //outer_angle
            ui.horizontal(|ui| {
                let mut comp_light_emit = scene.get_comp::<&mut LightEmit>(&entity).unwrap();
                ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
                ui.add(Slider::new(&mut comp_light_emit.outer_angle, 0.0..=std::f32::consts::PI / 2.0).text("outer_angle"))
            });
            ui.horizontal(|ui| {
                let mut comp_proj = scene.get_comp::<&mut Projection>(&entity).unwrap();
                let (mut near, _) = comp_proj.near_far();
                ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
                let res = ui.add(Slider::new(&mut near, 0.0..=20.0).text("near"));
                if res.changed() {
                    comp_proj.set_near(near);
                }
            });
            //far
            ui.horizontal(|ui| {
                let mut comp_proj = scene.get_comp::<&mut Projection>(&entity).unwrap();
                let (_, mut far) = comp_proj.near_far();
                ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
                let res = ui.add(Slider::new(&mut far, 0.0..=2000.0).text("far"));
                if res.changed() {
                    comp_proj.set_far(far);
                }
            });
            {
                let mut poslookat = scene.get_comp::<&mut PosLookat>(&entity).unwrap();
                ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
                ui.add(Slider::new(&mut poslookat.position.x, -40.0..=40.0).text("x"));
                ui.add(Slider::new(&mut poslookat.position.y, -40.0..=40.0).text("y"));
                ui.add(Slider::new(&mut poslookat.position.z, -40.0..=40.0).text("z"));
            }
            //view plane that cna be used to manipulate the light
            let mut has_mesh = scene.world().has::<Renderable>(entity).unwrap();
            let res = ui.checkbox(&mut has_mesh, "ShowLight");
            if res.changed() {
                if has_mesh {
                    //add plane
                    let center = scene.get_comp::<&PosLookat>(&entity).unwrap().position;
                    let normal = scene.get_comp::<&PosLookat>(&entity).unwrap().direction();
                    //move the plane a bit behind the light so it doesn't cast a shadow
                    let center = center - normal * 0.03;
                    let mut builder = builders::build_plane(center, normal, 0.3, 0.3, false);
                    let _ = scene.world_mut().insert(entity, builder.build());
                    let _ = scene.world_mut().insert_one(
                        entity,
                        VisMesh {
                            solid_color: na::Vector4::<f32>::new(1.0, 1.0, 1.0, 1.0),
                            ..Default::default()
                        },
                    );
                    let _ = scene.world_mut().insert_one(entity, Renderable);
                } else {
                    command_buffer.remove_one::<Renderable>(entity);
                }
            }
        }
    }

    #[allow(clippy::too_many_lines)]
    fn draw_cam(&mut self, ui: &mut Ui, scene: &mut Scene, _command_buffer: &mut CommandBuffer) {
        // get all entities that are renderable and sort by name
        let _entities = scene.get_lights(true);
        let cam = scene.get_current_cam().unwrap();

        if let Ok(mut projection) = scene.world().get::<&mut Projection>(cam.entity) {
            let (mut near, mut far) = projection.near_far();
            ui.label("Projection");
            ui.separator();
            ui.spacing_mut().slider_width = SIDE_PANEL_WIDTH / 3.0; //changes size od slider
            let res = ui.add(Slider::new(&mut near, 1e-5..=1.0).text("near"));
            if res.changed() {
                projection.set_near(near);
            }
            let res = ui.add(Slider::new(&mut far, near..=10000.0).text("far"));
            if res.changed() {
                projection.set_far(far);
            }
        }
    }

    #[allow(clippy::too_many_lines)]
    #[allow(clippy::cast_precision_loss)]
    fn draw_plugins(&mut self, ui: &mut Ui, _scene: &mut Scene, plugins: &Plugins, _command_buffer: &mut CommandBuffer) {
        // //get all entities that are renderable and sort by name
        // let entities = scene.get_lights(true);
        egui::Grid::new("grid_plugins").show(ui, |ui| {
            for system_and_metadata in plugins.logic_systems.iter() {
                let metadata = &system_and_metadata.1;
                let sys = &system_and_metadata.0;
                let name = sys.name.clone().unwrap_or(RString::from("unknown_name"));
                let ms = metadata.execution_time.as_nanos() as f32 / 1_000_000.0;
                let ms_string = format!("{ms:.2}");
                //draw in green if the system took more than 0.01ms (todo we need a better way
                // to check if the system was ran)
                let was_ran = ms > 0.07;
                // ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| {
                if was_ran {
                    ui.label(egui::RichText::new(name).strong());
                    ui.label(egui::RichText::new(ms_string).strong());
                } else {
                    ui.label(egui::RichText::new(name).weak());
                    ui.label(egui::RichText::new(ms_string).weak());
                }
                ui.end_row();
            }
        });

        // //go through all the lights and show their name
        // TODO: Keep or remove?
        // ui.group(|ui| {
        //     ScrollArea::vertical()
        //         .max_height(200.0)
        //         .scroll_bar_visibility(scroll_area::ScrollBarVisibility::AlwaysVisible)
        //         .auto_shrink([false, false])
        //         .show(ui, |ui| {
        //             ui.with_layout(Layout::top_down_justified(Align::LEFT),
        // |ui| {                 ui.spacing_mut().item_spacing.y = 0.0;
        //                 ui.spacing_mut().button_padding.y = 4.0;
        //                 for entity in entities {
        //                     let e_ref = scene.world.entity(entity).unwrap();

        //                     //get the name of the mesh which acts like a
        // unique id                     let name = e_ref
        //                         .get::<&Name>()
        //                         .expect("The entity has no name")
        //                         .0
        //                         .clone();

        //                     //if it's the first time we encounter a
        // renderable mesh,  we set the selected name to this one
        //                     if self.selected_light_name.is_empty() {
        //                         self.selected_light_name = name.clone();
        //                     }

        //                     //GUI for this concrete mesh
        //                     //if we click we can see options for vis
        //                     let _res = ui.selectable_value(
        //                         &mut self.selected_light_name,
        //                         name.clone(),
        //                         &name,
        //                     );

        //                     if name == self.selected_light_name {
        //                         self.selected_light_entity = Some(entity);
        //                     }
        //                 }
        //             });
        //         });
        // });
    }

    #[allow(clippy::too_many_lines)]
    fn draw_io(&mut self, ui: &mut Ui, scene: &mut Scene, _command_buffer: &mut CommandBuffer, selected_entity: Option<Entity>) {
        //save obj
        if let Some(selected_entity) = selected_entity {
            if ui.add(egui::Button::new("Save Obj")).clicked() {
                //get v, f and possibly uv from the entity
                if scene.world().has::<Verts>(selected_entity).unwrap() && scene.world().has::<Faces>(selected_entity).unwrap() {
                    let v = scene.get_comp::<&Verts>(&selected_entity).unwrap();
                    // let uv = scene.get_comp::<&UVs>(&selected_entity);

                    //transform vertices
                    let mm = scene.get_comp::<&ModelMatrix>(&selected_entity).unwrap();
                    let v = geom::transform_verts(&v.0, &mm.0);

                    let f = scene.get_comp::<&Faces>(&selected_entity).ok().map(|f| f.0.clone());

                    let uv = scene.get_comp::<&UVs>(&selected_entity).ok().map(|uv| uv.0.clone());

                    let normals = scene
                        .get_comp::<&Normals>(&selected_entity)
                        .ok()
                        .map(|normals| geom::transform_vectors(&normals.0, &mm.0));

                    //TODO make the path parametrizable
                    geom::save_obj(
                        &v,
                        f.as_ref(),
                        // None,
                        uv.as_ref(),
                        normals.as_ref(),
                        "./saved_obj.obj",
                    );
                }
            }
        }

        //save ply
        if let Some(selected_entity) = selected_entity {
            if ui.add(egui::Button::new("Save Ply")).clicked() {
                //get v, f and possibly uv from the entity
                if scene.world().has::<Verts>(selected_entity).unwrap() && scene.world().has::<Faces>(selected_entity).unwrap() {
                    let v = scene.get_comp::<&Verts>(&selected_entity).unwrap();

                    //transform vertices
                    let mm = scene.get_comp::<&ModelMatrix>(&selected_entity).unwrap();
                    let v = geom::transform_verts(&v.0, &mm.0);

                    let f = scene.get_comp::<&Faces>(&selected_entity).ok().map(|f| f.0.clone());

                    let uv = scene.get_comp::<&UVs>(&selected_entity).ok().map(|uv| uv.0.clone());

                    let normals = scene
                        .get_comp::<&Normals>(&selected_entity)
                        .ok()
                        .map(|normals| geom::transform_vectors(&normals.0, &mm.0));

                    let colors = scene.get_comp::<&Colors>(&selected_entity).ok().map(|colors| colors.0.clone());

                    //TODO make the path parametrizable
                    geom::save_ply(
                        &v,
                        f.as_ref(),
                        // None,
                        uv.as_ref(),
                        normals.as_ref(),
                        colors.as_ref(),
                        "./saved_ply.ply",
                    );
                }
            }
        }
    }

    #[allow(clippy::too_many_lines)]
    #[allow(unused_variables)]
    fn draw_profiling(&mut self, ui: &mut Ui, _scene: &mut Scene, _command_buffer: &mut CommandBuffer) {
        // TODO: Keep or remove?
        // cfg_if::cfg_if! {
        //     if #[cfg(feature = "peak-alloc")] {
        //         let current_mem = PEAK_ALLOC.current_usage_as_mb() as u32;
        //         let peak_mem = PEAK_ALLOC.peak_usage_as_mb() as u32;
        //         ui.label("current_mem (MB): ".to_owned() + &current_mem.to_string());
        //         ui.label("peak_mem(MB): ".to_owned() + &peak_mem.to_string());
        //     }
        // }

        // cfg_if::cfg_if! {
        //     if #[cfg(feature = "talc")] {

        //         cfg_if::cfg_if! {
        //         if #[cfg(target_arch = "wasm32")] {
        //             use crate::ALLOCATOR;
        //             let talc = ALLOCATOR.lock();
        //             let counters = talc.get_counters();
        //             ui.label("available to claim MB: ".to_owned() +
        // &(counters.available_bytes/(1024*1024)).to_string());
        // ui.label("nr gaps: ".to_owned() + &counters.fragment_count.to_string());
        //             ui.label("claimed MB: ".to_owned() +
        // &(counters.claimed_bytes/(1024*1024)).to_string());
        // ui.label("unavailable MB: ".to_owned() +
        // &(counters.overhead_bytes()/(1024*1024)).to_string());
        // ui.label("total_freed MB: ".to_owned() +
        // &(counters.total_freed_bytes()/(1024*1024)).to_string());         }}
        //     }
        // }

        // cfg_if::cfg_if! {
        //     if #[cfg(feature = "jemallocator")] {

        //         ui.label("current_mem (MB): ".to_owned());
        //     }
        // }

        // cfg_if::cfg_if! {
        //     if #[cfg(feature = "mimalloc")] {
        //         ui.label("mimalloc");

        //         ui.label("current_mem (MB): ".to_owned());
        //     }
        // }

        //for accounting allocator
        let memory_usage = MemoryUse::capture();
        if let Some(mem_resident) = memory_usage.resident {
            ui.label(egui::RichText::new(
                "MB resident: ".to_owned() + &(mem_resident / (1024 * 1024)).to_string(),
            ));
        }
        if let Some(mem_counted) = memory_usage.counted {
            ui.label(egui::RichText::new(
                "MB counted: ".to_owned() + &(mem_counted / (1024 * 1024)).to_string(),
            ));
        }

        //are we tracking callstacks
        let mut is_tracking = re_memory::accounting_allocator::is_tracking_callstacks();
        let res = ui.checkbox(&mut is_tracking, "Track memory callstacks");
        if res.clicked() {
            re_memory::accounting_allocator::set_tracking_callstacks(is_tracking);
        }

        // //make a memory bar where we show all the memory and the parts that are free in
        // // red, parts that are allocated in red only works in wasm because it
        // // uses a linear memory model
        // let size_per_mb = 1.0;
        // // #[cfg(target_arch = "wasm32")]
        // {
        //     use egui::{epaint::RectShape, Sense, Shape};
        //     let unknown_mem = Color32::from_rgb(150, 150, 150);
        //     let allocated_mem = Color32::from_rgb(255, 10, 10);

        //     //make per mb colors
        //     let mut mb2color: Vec<egui::Color32> = Vec::new();
        //     if let Some(mem_resident) = memory_usage.resident {
        //         let mb_resident = mem_resident / (1024 * 1024);
        //         mb2color.resize(mb_resident.try_into().unwrap(), unknown_mem);

        //         // Older usage was
        //         // let live_allocs = accounting_allocator::live_allocs_list();
        //         // re_memory does not seem to expose this the same way as before. We can't get the memory addresses.
        //         // Using backtrace string as a unique identifier since re_memory doesn't expose memory addresses anymore.
        //         // This maintains similar functionality while working with the available API.
        //         // let tracking_stats = accounting_allocator::tracking_stats().unwrap();
        //         // let top_callstacks = tracking_stats.top_callstacks;
        //         // let live_allocs = top_callstacks.;
        //         // let live_allocs = GLOBAL.tracking_stats().unwrap().top_callstacks;

        //         // use re_memory::allocation
        //         // tracking_stats.
        //         // let live_allocs_big = accounting_allocator::BIG_ALL &BIG_ALLOCATION_TRACKER.lock();
        //         // use re_memory::accounting_allocator::;
        //         let live_allocs: Vec<(usize, usize)> = accounting_allocator::tracking_stats()
        //             .map(|stats| {
        //                 stats
        //                     .top_callstacks
        //                     .iter()
        //                     .map(|cs| (cs.readable_backtrace.to_string().len(), cs.extant.size))
        //                     .collect()
        //             })
        //             .unwrap_or_default();

        //         // Older usage was
        //         // let min_ptr = accounting_allocator::min_ptr_alloc_memory();
        //         // We dont have this fn anymore, so we do something similar to above
        //         // Using first tracked allocation as minimum pointer since re_memory doesn't expose global min ptr.
        //         #[allow(clippy::get_first)] // getting borrow issues when using .first()
        //         let min_ptr = accounting_allocator::tracking_stats().map_or(0, |stats| {
        //             stats.top_callstacks.get(0).map_or(0, |cs| cs.readable_backtrace.to_string().len())
        //         });

        //         //for each live allow, paint the value with Red( allocated )
        //         for (ptr, size) in live_allocs {
        //             let ptr_mb = (ptr - min_ptr) / (1024 * 1024);
        //             let size_mb = size / (1024 * 1024);
        //             //for each mb paint it allocated
        //             for local_mb_idx in 0..size_mb {
        //                 let idx = ptr_mb + local_mb_idx;
        //                 if idx < mb2color.len() {
        //                     mb2color[idx] = allocated_mem;
        //                 }
        //             }
        //         }
        //     }

        //     //draw bar
        //     if let Some(mem_resident) = memory_usage.resident {
        //         let mb_resident = mem_resident / (1024 * 1024);
        //         ui.horizontal(|ui| {
        //             ui.spacing_mut().item_spacing = egui::vec2(0.5, 0.0);
        //             // for i in 0..mb_resident {
        //             #[allow(clippy::cast_sign_loss)]
        //             #[allow(clippy::cast_possible_truncation)]
        //             for i in 0..(mb_resident as usize) {
        //                 // let cursor = Ui::cursor(ui);
        //                 let rect_size = egui::Vec2::new(size_per_mb, 10.0);
        //                 let rect = ui.allocate_exact_size(rect_size, Sense::click()).0;
        //                 let rounding = Rounding::default();
        //                 #[allow(arithmetic_overflow)]
        //                 // let fill_color = egui::Color32::from_rgb(50 * 3, 0, 0);
        //                 // let fill_color =
        //                 let fill_color = if i < mb2color.len() {
        //                     mb2color[i]
        //                 } else {
        //                     egui::Color32::from_rgb(50, 0, 0)
        //                 };
        //                 let stroke = Stroke::default();
        //                 let rect_shape = RectShape::new(rect, rounding, fill_color, stroke);
        //                 ui.painter().add(Shape::Rect(rect_shape));
        //             }
        //         });
        //     }
        // }

        // TODO: Find where and how in the re_memory API to get the memory addresses
        // TODO: Should we factor in sampling rate here? should be a reasonable estimate regardless?

        //make a memory bar showing proportion of allocated vs free memory
        let size_per_mb = 1.0;
        #[cfg(target_arch = "wasm32")]
        {
            use egui::{epaint::RectShape, Sense, Shape, StrokeKind};
            let unknown_mem = Color32::from_rgb(150, 150, 150);
            let allocated_mem = Color32::from_rgb(255, 10, 10);

            //make per mb colors
            let mut mb2color: Vec<egui::Color32> = Vec::new();
            if let Some(mem_resident) = memory_usage.resident {
                let mb_resident = mem_resident / (1024 * 1024);
                mb2color.resize(mb_resident.try_into().unwrap(), unknown_mem);

                // Calculate total allocated memory from tracking stats
                let total_allocated: usize =
                    accounting_allocator::tracking_stats().map_or(0, |stats| stats.top_callstacks.iter().map(|cs| cs.extant.size).sum());

                // Convert to MB and fill that proportion of the bar with red
                let allocated_mb = total_allocated / (1024 * 1024);
                for i in 0..allocated_mb {
                    if i < mb2color.len() {
                        mb2color[i] = allocated_mem;
                    }
                }
            }

            //draw bar
            if let Some(mem_resident) = memory_usage.resident {
                let mb_resident = mem_resident / (1024 * 1024);
                ui.horizontal(|ui| {
                    ui.spacing_mut().item_spacing = egui::vec2(0.5, 0.0);
                    #[allow(clippy::cast_sign_loss)]
                    #[allow(clippy::cast_possible_truncation)]
                    for i in 0..(mb_resident as usize) {
                        let rect_size = egui::Vec2::new(size_per_mb, 10.0);
                        let rect = ui.allocate_exact_size(rect_size, Sense::click()).0;
                        let rounding = CornerRadius::default();
                        let fill_color = if i < mb2color.len() {
                            mb2color[i]
                        } else {
                            egui::Color32::from_rgb(50, 0, 0)
                        };
                        let stroke = Stroke::default();
                        let stroke_kind = StrokeKind::Inside;
                        let rect_shape = RectShape::new(rect, rounding, fill_color, stroke, stroke_kind);
                        ui.painter().add(Shape::Rect(rect_shape));
                    }
                });
            }
        }

        ui.add_space(15.0);
        if let Some(tracks) = accounting_allocator::tracking_stats() {
            #[allow(clippy::cast_precision_loss)]
            for (i, cb) in tracks.top_callstacks.iter().enumerate() {
                //callstack name will be the nr mb
                let mb_total = cb.extant.size as f32 / (1024.0 * 1024.0);

                let cb_stack = cb.readable_backtrace.to_string();
                let last_relevant_func_name = get_last_relevant_func_name(&cb_stack);

                let text_header = egui::RichText::new(float2string(mb_total, 1) + " MB: " + &last_relevant_func_name).size(12.0);

                ui.push_id(i, |ui| {
                    egui::CollapsingHeader::new(text_header).show(ui, |ui| self.draw_callstack_profiling(ui, cb));
                });
            }
        }
    }

    #[allow(clippy::too_many_lines)]
    #[allow(unused_variables)]
    fn draw_callstack_profiling(&mut self, ui: &mut Ui, cb: &CallstackStatistics) {
        let text = "cb: ".to_owned() + &cb.readable_backtrace.to_string();
        ui.label(egui::RichText::new(text).size(11.0));
        ui.label("count".to_owned() + &(cb.extant.count).to_string());
        ui.label("size".to_owned() + &(cb.extant.size / (1024 * 1024)).to_string());
        ui.label("stochastic_rate".to_owned() + &(cb.stochastic_rate).to_string());
    }
}

// Generated by egui-themer (https://github.com/grantshandy/egui-themer).
#[allow(clippy::too_many_lines)]
pub fn style() -> Style {
    Style {
        spacing: Spacing {
            item_spacing: Vec2 { x: 8.0, y: 3.0 },
            window_margin: Margin {
                left: 6,
                right: 6,
                top: 6,
                bottom: 6,
            },
            button_padding: Vec2 { x: 4.0, y: 1.0 },
            menu_margin: Margin {
                left: 6,
                right: 6,
                top: 6,
                bottom: 6,
            },
            indent: 18.0,
            interact_size: Vec2 { x: 40.0, y: 18.0 },
            slider_width: 100.0,
            combo_width: 100.0,
            text_edit_width: 280.0,
            icon_width: 14.0,
            icon_width_inner: 8.0,
            icon_spacing: 4.0,
            tooltip_width: 600.0,
            indent_ends_with_horizontal_line: false,
            combo_height: 200.0,
            scroll: ScrollStyle {
                bar_width: 6.0,
                handle_min_length: 12.0,
                bar_inner_margin: 4.0,
                bar_outer_margin: 0.0,
                ..Default::default()
            },
            ..Default::default()
        },
        interaction: Interaction {
            resize_grab_radius_side: 5.0,
            resize_grab_radius_corner: 10.0,
            show_tooltips_only_when_still: true,
            tooltip_delay: 0.5,
            ..Default::default()
        },
        visuals: Visuals {
            dark_mode: true,
            override_text_color: None,
            widgets: Widgets {
                noninteractive: WidgetVisuals {
                    bg_fill: Color32::from_rgba_premultiplied(27, 27, 27, 255),
                    weak_bg_fill: Color32::from_rgba_premultiplied(27, 27, 27, 255),
                    bg_stroke: Stroke {
                        width: 1.0,
                        color: Color32::from_rgba_premultiplied(60, 60, 60, 255),
                    },
                    corner_radius: CornerRadius::ZERO,
                    fg_stroke: Stroke {
                        width: 1.0,
                        color: Color32::from_rgba_premultiplied(140, 140, 140, 255),
                    },
                    expansion: 0.0,
                },
                inactive: WidgetVisuals {
                    bg_fill: Color32::from_rgba_premultiplied(60, 60, 60, 255),
                    weak_bg_fill: Color32::from_rgba_premultiplied(60, 60, 60, 255),
                    bg_stroke: Stroke {
                        width: 0.0,
                        color: Color32::from_rgba_premultiplied(0, 0, 0, 0),
                    },
                    corner_radius: CornerRadius::ZERO,
                    fg_stroke: Stroke {
                        width: 1.0,
                        color: Color32::from_rgba_premultiplied(180, 180, 180, 255),
                    },
                    expansion: 0.0,
                },
                hovered: WidgetVisuals {
                    bg_fill: Color32::from_rgba_premultiplied(70, 70, 70, 255),
                    weak_bg_fill: Color32::from_rgba_premultiplied(70, 70, 70, 255),
                    bg_stroke: Stroke {
                        width: 1.0,
                        color: Color32::from_rgba_premultiplied(150, 150, 150, 255),
                    },
                    corner_radius: CornerRadius::ZERO,
                    fg_stroke: Stroke {
                        width: 1.5,
                        color: Color32::from_rgba_premultiplied(240, 240, 240, 255),
                    },
                    expansion: 1.0,
                },
                active: WidgetVisuals {
                    bg_fill: Color32::from_rgba_premultiplied(55, 55, 55, 255),
                    weak_bg_fill: Color32::from_rgba_premultiplied(55, 55, 55, 255),
                    bg_stroke: Stroke {
                        width: 1.0,
                        color: Color32::from_rgba_premultiplied(255, 255, 255, 255),
                    },
                    corner_radius: CornerRadius::ZERO,
                    fg_stroke: Stroke {
                        width: 2.0,
                        color: Color32::from_rgba_premultiplied(255, 255, 255, 255),
                    },
                    expansion: 1.0,
                },
                open: WidgetVisuals {
                    bg_fill: Color32::from_rgba_premultiplied(27, 27, 27, 255),
                    weak_bg_fill: Color32::from_rgba_premultiplied(27, 27, 27, 255),
                    bg_stroke: Stroke {
                        width: 1.0,
                        color: Color32::from_rgba_premultiplied(60, 60, 60, 255),
                    },
                    corner_radius: CornerRadius::ZERO,
                    fg_stroke: Stroke {
                        width: 1.0,
                        color: Color32::from_rgba_premultiplied(210, 210, 210, 255),
                    },
                    expansion: 0.0,
                },
            },
            selection: Selection {
                bg_fill: Color32::from_rgba_premultiplied(0, 92, 128, 255),
                stroke: Stroke {
                    width: 1.0,
                    color: Color32::from_rgba_premultiplied(192, 222, 255, 255),
                },
            },
            hyperlink_color: Color32::from_rgba_premultiplied(90, 170, 255, 255),
            faint_bg_color: Color32::from_rgba_premultiplied(0, 0, 0, 0),
            extreme_bg_color: Color32::from_rgba_premultiplied(17, 17, 17, 255),
            code_bg_color: Color32::from_rgba_premultiplied(64, 64, 64, 255),
            warn_fg_color: Color32::from_rgba_premultiplied(255, 143, 0, 255),
            error_fg_color: Color32::from_rgba_premultiplied(255, 0, 0, 255),
            window_corner_radius: CornerRadius::ZERO,
            window_shadow: Shadow {
                // extrusion: 5.0,
                color: Color32::from_rgba_premultiplied(0, 0, 0, 96),
                ..Default::default()
            },
            window_fill: Color32::from_rgba_premultiplied(27, 27, 27, 255),
            window_stroke: Stroke {
                width: 0.0,
                color: Color32::from_rgba_premultiplied(71, 71, 71, 255),
            },
            menu_corner_radius: CornerRadius::ZERO,
            panel_fill: Color32::from_rgba_premultiplied(20, 20, 20, 255),
            popup_shadow: Shadow {
                // extrusion: 16.0,
                color: Color32::from_rgba_premultiplied(0, 0, 0, 96),
                ..Default::default()
            },
            resize_corner_size: 12.0,
            // text_cursor_width: 2.0,
            text_cursor: TextCursorStyle::default(),
            // text_cursor_preview: false,
            clip_rect_margin: 3.0,
            button_frame: true,
            collapsing_header_frame: false,
            indent_has_left_vline: true,
            striped: false,
            slider_trailing_fill: true,
            window_highlight_topmost: true,
            handle_shape: HandleShape::Circle,
            interact_cursor: None,
            image_loading_spinners: true,
            numeric_color_space: NumericColorSpace::GammaByte,
            text_alpha_from_coverage: AlphaFromCoverage::DARK_MODE_DEFAULT,
            weak_text_alpha: 0.6,
            weak_text_color: None,
            text_edit_bg_color: None, // use `extreme_bg_color` by default
            disabled_alpha: 0.5,
        },
        animation_time: 0.08333,
        explanation_tooltips: false,
        text_styles: [
            (TextStyle::Small, FontId::new(9.0, egui::FontFamily::Proportional)),
            (TextStyle::Body, FontId::new(12.5, egui::FontFamily::Proportional)),
            (TextStyle::Button, FontId::new(12.5, egui::FontFamily::Proportional)),
            (TextStyle::Heading, FontId::new(14.0, egui::FontFamily::Proportional)),
            (TextStyle::Monospace, FontId::new(12.0, egui::FontFamily::Monospace)),
        ]
        .into(),
        ..Default::default()
    }
}