bevy_lunex 0.3.2

Blazingly fast retained UI layout engine for Bevy ECS
Documentation
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
#![doc = include_str!("../README.md")]
#![allow(clippy::type_complexity)]

// Crate import only
pub(crate) use std::any::TypeId;
use bevy::app::PluginGroupBuilder;
pub(crate) use bevy::prelude::*;
pub(crate) use bevy::sprite::SpriteSource;
pub(crate) use bevy::text::TextLayoutInfo;
pub(crate) use bevy::utils::HashMap;
pub(crate) use bevy::render::view::RenderLayers;
pub(crate) use colored::Colorize;

// Re-export
pub use bevy::sprite::Anchor;
#[cfg(feature = "text3d")]
pub use bevy_rich_text3d::*;
#[cfg(feature = "text3d")]
pub use bevy::text::cosmic_text::Weight;

mod cursor;
pub use cursor::*;

mod layouts;
pub use layouts::*;

mod picking;
pub use picking::*;

mod states;
pub use states::*;

mod units;
pub use units::*;


// #===============================#
// #=== MULTIPURPOSE COMPONENTS ===#

/// **Dimension** - This component holds width and height used for different Ui components
#[derive(Component, Reflect, Deref, DerefMut, Default, Clone, PartialEq, Debug)]
pub struct Dimension(pub Vec2);
/// Conversion implementations
impl <T: Into<Vec2>> From<T> for Dimension {
    fn from(value: T) -> Self {
        Dimension(value.into())
    }
}

/// This system takes [`Dimension`] data and pipes them into querried [`Sprite`].
pub fn system_pipe_sprite_size_from_dimension(
    mut query: Query<(&mut Sprite, &Dimension), Changed<Dimension>>,
) {
    for (mut sprite, dimension) in &mut query {
        sprite.custom_size = Some(**dimension)
    }
}


// #=========================#
// #=== TEXTURE EMBEDDING ===#

/// **Ui Embedding** - Use this component to mark entities that their texture handles are embeddings instead of regular assets.
#[derive(Component, Reflect, Clone, PartialEq, Debug)]
pub struct UiEmbedding;

/// This system takes [`Dimension`] data and pipes them into querried [`Handle<Image>`] data to fit.
/// This will resize the original image texture.
pub fn system_embedd_resize(
    query: Query<(&Sprite, &Dimension), (With<UiEmbedding>, Changed<Dimension>)>,
    mut images: ResMut<Assets<Image>>,
) {
    for (sprite, dimension) in &query {
        if let Some(image) = images.get_mut(&sprite.image) {
            if **dimension != Vec2::ZERO {
                image.resize(bevy::render::render_resource::Extent3d { width: dimension.x as u32, height: dimension.y as u32, ..default() });
            }
        }
    }
}

/// Provides utility constructor methods for [`Image`]
pub trait ImageTextureConstructor {
    /// Just a utility constructor hiding the necessary texture initialization
    fn clear_render_texture() -> Image {
        use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages};
        use bevy::asset::RenderAssetUsages;

        let mut image = Image::new_fill(
            Extent3d {
                width: 512,
                height: 512,
                ..default()
            },
            TextureDimension::D2,
            &[0, 0, 0, 0],
            TextureFormat::Bgra8UnormSrgb,
            RenderAssetUsages::default(),
        );
        image.texture_descriptor.usage = TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST | TextureUsages::RENDER_ATTACHMENT;
        image
    }
}
impl ImageTextureConstructor for Image {}

/// Provides utility costructor methods for [`Camera`]
pub trait CameraTextureRenderConstructor {
    /// Just a utility constructor for camera that renders to a transparent texture
    fn clear_render_to(handle: Handle<Image>) -> Camera {
        use bevy::render::camera::RenderTarget;
        Camera {
            target: RenderTarget::Image(handle),
            clear_color: ClearColorConfig::Custom(Color::srgba(0.0, 0.0, 0.0, 0.0)),
            ..default()
        }
    }
    /// Modify the camera render order
    fn with_order(self, order: isize) -> Self;
}
impl CameraTextureRenderConstructor for Camera {
    fn with_order(mut self, order: isize) -> Self {
        self.order = order;
        self
    }
}


// #===========================#
// #=== LAYOUT ROOT CONTROL ===#

/// **Ui Layout Root** - This component marks the start of a worldspace Ui-Tree. Spawn this standalone for worldspace 3D UI
/// or spawn this as a child of camera for a HUD. For 2D UI, if your camera does not move you can spawn it standalone too.
///
/// Important components:
/// - [`Transform`] - Set the position of the Ui-Tree
/// - [`Dimension`] - Set the size of the Ui-Tree
/// ## 🛠️ Example
/// ```
/// # use bevy::prelude::*;
/// # use bevy_lunex::*;
/// # fn spawn_main_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
///     commands.spawn((
///         UiLayoutRoot::new_2d(),
///         UiFetchFromCamera::<0>, // Pipe the size from Camera
///     )).with_children(|ui| {
///         // ... spawn your Ui Here
///     });
/// # }
/// ```
#[derive(Component, Reflect, Clone, PartialEq, Debug)]
#[require(Visibility, Transform, Dimension)]
pub struct UiLayoutRoot {
    abs_scale: f32,
}
impl UiLayoutRoot {
    pub fn new_2d() -> Self {
        Self { abs_scale: 1.0 }
    }
    pub fn new_3d() -> Self {
        Self { abs_scale: 0.001 }
    }
}


/// Marker component for all entities that you can use to check if it is used for 3D UI.
#[derive(Component, Reflect, Clone, PartialEq, Debug)]
pub struct UiRoot3d;

/// This system traverses the hierarchy and adds [`UiRoot3d`] component to children.
pub fn system_mark_3d(
    root_query: Query<(Has<UiRoot3d>, &Children), (With<UiLayoutRoot>, Without<UiLayout>, Changed<UiLayoutRoot>)>,
    node_query: Query<(Entity, Has<UiRoot3d>, Option<&Children>), (With<UiLayout>, Without<UiLayoutRoot>)>,
    mut commands: Commands,
) {
    for (is_root_3d, root_children) in &root_query {

        // Stack-based traversal
        let mut stack: Vec<(Entity, usize)> = root_children.iter().map(|&child| (child, 1)).rev().collect();

        // Loop over the stack
        while let Some((current_entity, depth)) = stack.pop() {
            if let Ok((node, is_node_3d, node_children_option)) = node_query.get(current_entity) {


                if is_root_3d != is_node_3d {
                    if is_root_3d {
                        commands.entity(node).insert(UiRoot3d);
                    } else {
                        commands.entity(node).remove::<UiRoot3d>();
                    }
                }


                // Push children to the stack
                if let Some(node_children) = node_children_option {
                    for &child in node_children.iter().rev() {
                        stack.push((child, depth + 1));
                    }
                }
            }
        }
    }
}



/// Trigger this event to recompute all [`UiLayoutRoot`] entities.
#[derive(Event)]
pub struct RecomputeUiLayout;

/// This observer will mutably touch [`UiLayoutRoot`] which will trigger [`system_layout_compute`].
pub fn observer_touch_layout_root(
    _trigger: Trigger<RecomputeUiLayout>,
    mut query: Query<&mut UiLayoutRoot>,
){
    for mut root in &mut query {
        root.as_mut();
    }
}

/// This system draws the outlines of [`UiLayout`] and [`UiLayoutRoot`] as gizmos.
pub fn system_debug_draw_gizmo_2d(
    query: Query<(&GlobalTransform, &Dimension), (Or<(With<UiLayout>, With<UiLayoutRoot>)>, Without<UiRoot3d>)>,
    mut gizmos: Gizmos<LunexGizmoGroup2d>
) {
    for (transform, dimension) in &query {
        // Draw the gizmo outline
        gizmos.rect(
            Isometry3d::from(transform.translation()),
            **dimension,
            Color::linear_rgb(0.0, 1.0, 0.0),
        );
    }
}

/// This system draws the outlines of [`UiLayout`] and [`UiLayoutRoot`] as gizmos.
pub fn system_debug_draw_gizmo_3d(
    query: Query<(&GlobalTransform, &Dimension), (Or<(With<UiLayout>, With<UiLayoutRoot>)>, With<UiRoot3d>)>,
    mut gizmos: Gizmos<LunexGizmoGroup3d>
) {
    for (transform, dimension) in &query {
        // Draw the gizmo outline
        gizmos.rect(
            Isometry3d::from(transform.translation()),
            **dimension,
            Color::linear_rgb(0.0, 1.0, 0.0),
        );
    }
}

/// This system traverses the hierarchy and prints the debug information.
pub fn system_debug_print_data(
    root_query: Query<(&UiLayoutRoot, NameOrEntity, &Dimension, &Children), (Without<UiLayout>, Or<(Changed<UiLayoutRoot>, Changed<Dimension>)>)>,
    node_query: Query<(&UiLayout, &UiState, NameOrEntity, &Dimension, &Transform, Option<&Children>), Without<UiLayoutRoot>>,
) {
    for (_, root_name, root_dimension, root_children) in &root_query {
        // Create output string
        let mut output_string = format!("{}", format!("{root_name}").bold().underline().magenta());

        output_string += "";
        output_string += &format!("[w: {}, h: {}]", format!("{:.02}", root_dimension.x).green(), format!("{:.02}", root_dimension.y).green());

        output_string += "\n";

        // Stack-based traversal
        let mut stack: Vec<(Entity, usize, bool)> = root_children
            .iter()
            .enumerate()
            .map(|(i, &child)| (child, 1, i == root_children.len() - 1)) // Track last-child flag
            .rev()
            .collect();

        // Tracks whether previous levels had last children (for vertical bars)
        let mut last_child_levels: Vec<bool> = Vec::new();

        while let Some((current_entity, depth, is_last)) = stack.pop() {
            if let Ok((node_layout, _node_state, node_name, node_dimension, node_transform, node_children_option)) = node_query.get(current_entity) {

                // Adjust last_child_levels size
                if last_child_levels.len() < depth {
                    last_child_levels.push(is_last);
                } else {
                    last_child_levels[depth - 1] = is_last;
                }

                // Create the tab level offset
                for &last in &last_child_levels[..depth - 1] {
                    output_string += &if last { format!("{}", "".black()) } else { "".to_string() };
                }

                // Add the name
                output_string += if is_last { "" } else { "" };
                if node_name.name.is_some() {
                    output_string += &format!("{}", format!("{node_name}").bold().yellow());
                } else {
                    output_string += &format!("{}", format!("{node_name}").yellow());
                }

                output_string += "";

                output_string += &format!("[w: {}, h: {}, d: {}]",
                    format!("{:.02}", node_dimension.x).green(),
                    format!("{:.02}", node_dimension.y).green(),
                    format!("{:.00}", node_transform.translation.z).green(),
                );

                match node_layout.layouts.get(&UiBase::id()).unwrap() {
                    UiLayoutType::Boundary(boundary) => {
                        output_string += &format!("{} {} p1: {}, p2: {} {}",
                            "Boundary".bold(),
                            "{",
                            boundary.pos1.to_nicestr(),
                            boundary.pos2.to_nicestr(),
                            "}",
                        );
                    },
                    UiLayoutType::Window(window) => {
                        output_string += &format!("{} {} p: {}, s: {}, a: {} {}",
                            "Window".bold(),
                            "{",
                            window.pos.to_nicestr(),
                            window.size.to_nicestr(),
                            window.anchor.to_nicestr(),
                            "}",
                        );
                    },
                    UiLayoutType::Solid(solid) => {
                        output_string += &format!("{} {} s: {}, ax: {}, ay: {}, scl: {} {}",
                            "Solid".bold(),
                            "{",
                            solid.size.to_nicestr(),
                            format!("{:.02}", solid.align_x.0).green(),
                            format!("{:.02}", solid.align_y.0).green(),
                            format!("{:?}", solid.scaling).green(),
                            "}",
                        );
                    },
                }

                output_string += "\n";

                if let Some(node_children) = node_children_option {
                    let child_count = node_children.len();
                    for (i, &child) in node_children.iter().enumerate().rev() {
                        stack.push((child, depth + 1, i == child_count - 1));
                    }
                }
            }
        }

        // Print to console
        info!("UiLayout change detected:\n{}", output_string);
    }
}


// #======================#
// #=== LAYOUT CONTROL ===#

/// **Ui Layout** - This component specifies the layout of a Ui-Node, which must be spawned as a child
/// of either [`UiLayoutRoot`] or [`UiLayout`] to work. Based on the provided layout other attached
/// components on this entity are overwritten to match the computed structure.
///
/// Direct output components:
/// - [`Transform`] - The computed position of the Ui-Node _(Read-only)_
/// - [`Dimension`] - The computed size of the Ui-Node _(Read-only)_
///
/// Indirectly affected components:
/// - [`Sprite`] - `custom_size` to match [`Dimension`]
///
/// ## 🛠️ Example
/// ```
/// # use bevy::prelude::*;
/// # use bevy_lunex::*;
/// # fn spawn_main_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
/// # commands.spawn((
/// #     UiLayoutRoot::new_2d(),
/// # )).with_children(|ui| {
///       // Must be spawned as a child
///       ui.spawn((
///           // Use 1 of the 3 available layout types
///           UiLayout::solid().size((1920.0, 1080.0)).scaling(Scaling::Fill).pack(),
///           // Attach image to the node
///           Sprite::from_image(asset_server.load("images/ui/background.png")),
///       ));
/// # });
/// # }
/// ```
#[derive(Component, Reflect, Clone, PartialEq, Debug)]
#[require(Visibility, SpriteSource, Transform, Dimension, UiState, UiDepth)]
pub struct UiLayout {
    /// Stored layout per state
    pub layouts: HashMap<TypeId, UiLayoutType>
}
/// Constructors
impl UiLayout {
    /// **Boundary** - Declarative layout type that is defined by its top-left corner and bottom-right corner.
    /// Nodes with this layout are not included in the ui flow.
    /// ## 🛠️ Example
    /// ```
    /// # use bevy_lunex::{UiLayout, Rl};
    /// let layout: UiLayout = UiLayout::boundary().pos1(Rl(20.0)).pos2(Rl(80.0)).pack();
    /// ```
    pub fn boundary() -> UiLayoutTypeBoundary {
        UiLayoutTypeBoundary::new()
    }
    /// **Window** - Declarative layout type that is defined by its size and position.
    /// Nodes with this layout are not included in the ui flow.
    /// ## 🛠️ Example
    /// ```
    /// # use bevy_lunex::{UiLayout, Ab, Rl};
    /// let layout: UiLayout = UiLayout::window().pos(Ab(100.0)).size(Rl(50.0)).pack();
    /// ```
    pub fn window() -> UiLayoutTypeWindow {
        UiLayoutTypeWindow::new()
    }
    /// **Solid** - Declarative layout type that is defined by its width and height ratio.
    /// Scales in a way to fit itself inside parent container. It never deforms.
    /// Nodes with this layout are not included in the ui flow.
    /// ## 🛠️ Example
    /// ```
    /// # use bevy_lunex::UiLayout;
    /// let layout: UiLayout = UiLayout::solid().size((4.0, 3.0)).align_x(-0.8).pack();
    /// ```
    pub fn solid() -> UiLayoutTypeSolid {
        UiLayoutTypeSolid::new()
    }
    /// Create multiple layouts for a different states at once.
    pub fn new(value: Vec<(TypeId, impl Into<UiLayoutType>)>) -> Self {
        let mut map = HashMap::new();
        for (state, layout) in value {
            map.insert(state, layout.into());
        }
        Self { layouts: map }
    }
}
/// Conversion implementations
impl From<UiLayoutType> for UiLayout {
    fn from(value: UiLayoutType) -> Self {
        let mut map = HashMap::new();
        map.insert(UiBase::id(), value);
        Self {
            layouts: map,
        }
    }
}
impl From<UiLayoutTypeBoundary> for UiLayout {
    fn from(value: UiLayoutTypeBoundary) -> Self {
        let value: UiLayoutType = value.into();
        UiLayout::from(value)
    }
}
impl From<UiLayoutTypeWindow> for UiLayout {
    fn from(value: UiLayoutTypeWindow) -> Self {
        let value: UiLayoutType = value.into();
        UiLayout::from(value)
    }
}
impl From<UiLayoutTypeSolid> for UiLayout {
    fn from(value: UiLayoutTypeSolid) -> Self {
        let value: UiLayoutType = value.into();
        UiLayout::from(value)
    }
}

#[derive(Component, Reflect, Clone, PartialEq, Debug)]
pub enum UiDepth {
    Add(f32),
    Set(f32),
}
impl Default for UiDepth {
    fn default() -> Self {
        UiDepth::Add(1.0)
    }
}


/// This system traverses the hierarchy and computes all nodes.
pub fn system_layout_compute(
    root_query: Query<(&UiLayoutRoot, &Transform, &Dimension, &Children), (Without<UiLayout>, Or<(Changed<UiLayoutRoot>, Changed<Dimension>)>)>,
    mut node_query: Query<(&UiLayout, &UiDepth, &UiState, &mut Transform, &mut Dimension, Option<&Children>), Without<UiLayoutRoot>>,
) {
    for (root, root_transform, root_dimension, root_children) in &root_query {
        // Size of the viewport
        let root_rectangle = Rectangle2D {
            pos: root_transform.translation.xy(),
            size: **root_dimension,
        };

        // Stack-based traversal
        let mut stack: Vec<(Entity, Rectangle2D, f32)> = root_children.iter().map(|&child| (child, root_rectangle, 0.0)).rev().collect();

        while let Some((current_entity, parent_rectangle, depth)) = stack.pop() {
            if let Ok((node_layout, node_depth, node_state, mut node_transform, mut node_dimension, node_children_option)) = node_query.get_mut(current_entity) {
                // Compute all layouts for the node
                let mut computed_rectangles = Vec::with_capacity(node_layout.layouts.len());
                for (state, layout) in &node_layout.layouts {
                    computed_rectangles.push((state, layout.compute(&parent_rectangle, root.abs_scale, root_rectangle.size, 16.0)));
                }

                // Normalize the active state weights
                let mut total_weight = 0.0;
                for (state, _) in &node_layout.layouts {
                    if let Some(weight) = node_state.states.get(state) {
                        total_weight += weight;
                    }
                }

                // Combine the state rectangles into one normalized
                let mut node_rectangle = Rectangle2D::EMPTY;

                // Use base if no active state
                if total_weight == 0.0 {
                    node_rectangle.pos += computed_rectangles[0].1.pos;
                    node_rectangle.size += computed_rectangles[0].1.size;

                // Combine the active states into one rectangle
                } else {
                    for (state, rectangle) in computed_rectangles {
                        if let Some(weight) = node_state.states.get(state) {
                            node_rectangle.pos += rectangle.pos * (weight / total_weight);
                            node_rectangle.size += rectangle.size * (weight / total_weight);
                        }
                    }
                }

                // Save the computed layout
                node_transform.translation.x = node_rectangle.pos.x;
                node_transform.translation.y = -node_rectangle.pos.y;
                let depth = match node_depth {
                    UiDepth::Add(v) => {depth + v},
                    UiDepth::Set(v) => {*v},
                };
                node_transform.translation.z = depth * root.abs_scale;
                **node_dimension = node_rectangle.size;

                if let Some(node_children) = node_children_option {
                    // Add children to the stack
                    stack.extend(node_children.iter().map(|&child| (child, node_rectangle, depth)));
                }
            }
        }
    }
}


// #=====================#
// #=== STATE CONTROL ===#

/// **Ui State** - This component aggrages state transition values for later reference
/// by other components. You don't directly control or spawn this component, but use an abstraction
/// instead. You can use the prebuilt state components or create a custom ones with a completely
/// unique transition logic. You just have to provide transition value to this component later.
/// - [`UiBase`] _(Type only, not a component)_
/// - [`UiHover`]
/// - [`UiSelected`]
/// - [`UiClicked`]
/// - [`UiIntro`]
/// - [`UiOutro`]
///
/// Dependant components:
/// - [`UiLayout`]
/// - [`UiColor`]
///
/// ## 🛠️ Example
/// ```
/// # use bevy::prelude::*;
/// # use bevy::color::palettes::basic::*;
/// # use bevy_lunex::*;
/// # fn spawn_main_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
/// # commands.spawn((
/// #     UiLayoutRoot::new_2d(),
/// # )).with_children(|ui| {
///       ui.spawn((
///           // Like this you can enable a state
///           UiHover::new().forward_speed(20.0).backward_speed(4.0),
///           // You can define layouts per state
///           UiLayout::new(vec![
///               (UiBase::id(), UiLayout::window().full()),
///               (UiHover::id(), UiLayout::window().x(Rl(10.0)).full())
///           ]),
///           // You can define colors per state
///           UiColor::new(vec![
///               (UiBase::id(), Color::Srgba(RED).with_alpha(0.8)),
///               (UiHover::id(), Color::Srgba(YELLOW).with_alpha(1.2))
///           ]),
///           // ... Sprite, Text, etc.
///
///       // Add observers that enable/disable the hover state component
///       )).observe(hover_set::<Pointer<Over>, true>)
///         .observe(hover_set::<Pointer<Out>, false>);
/// # });
/// # }
/// ```
#[derive(Component, Reflect, Clone, PartialEq, Debug)]
pub struct UiState {
    /// Stored transition per state
    states: HashMap<TypeId, f32>,
}
/// Default constructor
impl Default for UiState {
    fn default() -> Self {
        let mut map = HashMap::new();
        map.insert(UiBase::id(), 1.0);
        Self {
            states: map,
        }
    }
}

/// This system controls the [`UiBase`] state. This state is decreased based on total sum of all other active states.
pub fn system_state_base_balancer(
    mut query: Query<&mut UiState, Changed<UiState>>,
) {
    for mut manager in &mut query {
        // Normalize the active nobase state weights
        let mut total_nonbase_weight = 0.0;
        for (state, value) in &manager.states {
            if *state == UiBase::id() { continue; }
            total_nonbase_weight += value;
        }

        // Decrease base transition based on other states
        if let Some(value) = manager.states.get_mut(&UiBase::id()) {
            *value = (1.0 - total_nonbase_weight).clamp(0.0, 1.0);
        }
    }
}
/// This system pipes the attached state component data to the [`UiState`] component.
pub fn system_state_pipe_into_manager<S: UiStateTrait + Component>(
    mut commads: Commands,
    mut query: Query<(&mut UiState, &S), Changed<S>>,
) {
    for (mut manager, state) in &mut query {
        // Send the value to the manager
        if let Some(value) = manager.states.get_mut(&S::id()) {
            *value = state.value();

        // Insert the value if it does not exist
        } else {
            manager.states.insert(S::id(), state.value());
        }
        // Recompute layout
        commads.trigger(RecomputeUiLayout);
    }
}

/// Trait that all states must implement before being integrated into the state machine.
pub trait UiStateTrait: Send + Sync + 'static {
    /// This is used as a key to identify a Ui-Node state.
    fn id() -> TypeId {
        TypeId::of::<Self>()
    }
    /// This must return a value between `0.0 - 1.0`. It is used as transition value
    /// for a state, with `0.0` being off and `1.0` being on. Any smoothing should happen
    /// inside this function.
    fn value(&self) -> f32;
}

/// **Ui Base** - The default state for a Ui-Node, used only for the [`UiBase::id`] key. It is not a component that you can control.
#[derive(Clone, PartialEq, Debug)]
pub struct UiBase;
impl UiStateTrait for UiBase {
    fn value(&self) -> f32 {
        1.0
    }
}


// #====================#
// #=== TEXT CONTROL ===#

/// **Ui Text Size** - This component is used to control the size of a text compared
/// to other Ui-Nodes. It works by overwritting the attached [`UiLayout`] window
/// size parameter to match the text bounds. The value provided is used as a _scale_
/// to adjust this size, specificaly it's height. It is recommended to use `non-relative`
/// units such as [`Ab`], [`Rw`], [`Rh`], [`Vh`], [`Vw`] and [`Em`] for even values.
///
/// Affected components:
/// - [`UiLayout`] - **MUST BE WINDOW TYPE** for this to work
///
/// ## 🛠️ Example
/// ```
/// # use bevy::prelude::*;
/// # use bevy_lunex::*;
/// # fn spawn_main_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
/// # commands.spawn((
/// #     UiLayoutRoot::new_2d(),
/// # )).with_children(|ui| {
///       ui.spawn((
///           // Position the text using the window layout's position and anchor
///           UiLayout::window().pos((Rh(40.0), Rl(50.0))).anchor(Anchor::CenterLeft).pack(),
///           // This controls the height of the text, so 60% of the parent's node height
///           UiTextSize::from(Rh(60.0)),
///           // You can attach text like this
///           Text2d::new("Button"),
///           // Font size now works as "text resolution"
///           TextFont {
///               font: asset_server.load("fonts/Rajdhani.ttf"),
///               font_size: 64.0,
///               ..default()
///           },
///       ));
/// # });
/// # }
/// ```
#[derive(Component, Reflect, Deref, DerefMut, Default, Clone, PartialEq, Debug)]
pub struct UiTextSize (pub UiValue<f32>);
/// Constructors
impl <T: Into<UiValue<f32>>> From<T> for UiTextSize {
    fn from(value: T) -> Self {
        UiTextSize(value.into())
    }
}

/// This system takes [`TextLayoutInfo`] data and pipes them into querried [`Transform`] scale.
pub fn system_text_size_from_dimension(
    mut commands: Commands,
    mut query: Query<(&mut Transform, &Dimension, &TextLayoutInfo), Changed<Dimension>>,
) {
    for (mut transform, dimension, text_info) in &mut query {
        // Wait for text to render
        if text_info.size.y == 0.0 {
            commands.trigger(RecomputeUiLayout);
        }

        // Scale the text
        let scale = **dimension / text_info.size;
        transform.scale.x = scale.x;
        transform.scale.y = scale.x;
    }
}

/// This system takes updated [`TextLayoutInfo`] data and overwrites coresponding [`UiLayout`] data to match the text size.
pub fn system_text_size_to_layout(
    mut commands: Commands,
    mut query: Query<(&mut UiLayout, &TextLayoutInfo, &UiTextSize), Changed<TextLayoutInfo>>,
) {
    for (mut layout, text_info, text_size) in &mut query {
        // Wait for text to render
        if text_info.size.y == 0.0 {
            commands.trigger(RecomputeUiLayout);
        }

        // Create the text layout
        match layout.layouts.get_mut(&UiBase::id()).expect("UiBase state not found for Text") {
            UiLayoutType::Window(window) => {
                window.set_height(**text_size);
                window.set_width(**text_size * (text_info.size.x / text_info.size.y));
            },
            UiLayoutType::Solid(solid) => {
                solid.set_size(Ab(text_info.size));
            },
            _ => {},
        }
    }
}

// # TEXT 3D

/// This system takes updated [`Text3dDimensionOut`] data and overwrites coresponding [`UiLayout`] data to match the text size.
#[cfg(feature = "text3d")]
pub fn system_text_3d_size_to_layout(
    mut commands: Commands,
    mut query: Query<(&mut UiLayout, &Text3dDimensionOut, &UiTextSize), Changed<Text3dDimensionOut>>,
) {
    for (mut layout, text_info, text_size) in &mut query {
        // Wait for text to render
        if text_info.dimension.y == 0.0 {
            commands.trigger(RecomputeUiLayout);
            continue;
        }

        // Create the text layout
        match layout.layouts.get_mut(&UiBase::id()).expect("UiBase state not found for Text") {
            UiLayoutType::Window(window) => {
                window.set_height(**text_size);
                window.set_width(**text_size * (text_info.dimension.x / text_info.dimension.y));
            },
            UiLayoutType::Solid(solid) => {
                solid.set_size(Ab(text_info.dimension));
            },
            _ => {},
        }
    }
}

/// This system takes [`Text3dDimensionOut`] data and pipes them into querried [`Transform`] scale.
#[cfg(feature = "text3d")]
pub fn system_text_3d_size_from_dimension(
    mut commands: Commands,
    mut query: Query<(&mut Transform, &Dimension, &Text3dDimensionOut), Changed<Dimension>>,
) {
    for (mut transform, dimension, text_info) in &mut query {
        // Wait for text to render
        if text_info.dimension.y == 0.0 {
            commands.trigger(RecomputeUiLayout);
            continue;
        }

        // Scale the text
        let scale = dimension.x / text_info.dimension.x;
        transform.scale.x = scale;
        transform.scale.y = scale;
    }
}


// #=====================#
// #=== STATE CONTROL ===#

/// **Ui Mesh Plane 3d** - This component is used to mark mesh entities that can be freely replaced with quad mesh on demand.
#[derive(Component, Reflect, Default, Clone, PartialEq, Debug)]
#[require(Mesh3d)]
pub struct UiMeshPlane3d;

/// **Ui Mesh Plane 2d** - This component is used to mark mesh entities that can be freely replaced with quad mesh on demand.
#[derive(Component, Reflect, Default, Clone, PartialEq, Debug)]
#[require(Mesh2d)]
pub struct UiMeshPlane2d;

/// This system takes [`Dimension`] data and constructs a plane mesh.
pub fn system_mesh_3d_reconstruct_from_dimension(
    mut query: Query<(&Dimension, &mut Mesh3d), (With<UiMeshPlane3d>, Changed<Dimension>)>,
    mut meshes: ResMut<Assets<Mesh>>,
) {
    for (dimension, mut mesh) in &mut query {
        let plane_mesh = meshes.add(Rectangle::new(dimension.x, dimension.y));
        mesh.0 = plane_mesh;
    }
}

/// This system takes [`Dimension`] data and constructs a plane mesh.
pub fn system_mesh_2d_reconstruct_from_dimension(
    mut query: Query<(&Dimension, &mut Mesh2d), (With<UiMeshPlane2d>, Changed<Dimension>)>,
    mut meshes: ResMut<Assets<Mesh>>,
) {
    for (dimension, mut mesh) in &mut query {
        let plane_mesh = meshes.add(Rectangle::new(dimension.x, dimension.y));
        mesh.0 = plane_mesh;
    }
}



// #=======================#
// #=== CAMERA FETCHING ===#

/// **Ui Fetch From Camera** - Attaching this component to [`UiLayoutRoot`] will make the [`Dimension`]
/// component pull data from a [`Camera`] with attached [`UiSourceCamera`] that has the same index.
#[derive(Component, Reflect, Clone, PartialEq, Debug)]
pub struct UiFetchFromCamera<const INDEX: usize>;

/// **Ui Source Camera** - Marks a [`Camera`] as a source for [`UiLayoutRoot`] with [`UiFetchFromCamera`].
/// They must have the same index and only one [`UiSourceCamera`] can exist for a single index.
#[derive(Component, Reflect, Clone, PartialEq, Debug)]
pub struct UiSourceCamera<const INDEX: usize>;

/// This system takes [`Camera`] viewport data and pipes them into querried [`Dimension`] + [`UiLayoutRoot`] + [`UiFetchFromCamera`].
pub fn system_fetch_dimension_from_camera<const INDEX: usize>(
    src_query: Query<(&Camera, Option<&OrthographicProjection>), (With<UiSourceCamera<INDEX>>, Changed<Camera>)>,
    mut dst_query: Query<&mut Dimension, (With<UiLayoutRoot>, With<UiFetchFromCamera<INDEX>>)>,
) {
    // Check if we have a camera dimension input
    if src_query.is_empty() { return; }
    let Ok((camera, projection_option)) = src_query.get_single() else {
        warn_once!("Multiple UiSourceCamera<{INDEX}> exist at once! Ignoring all camera inputs to avoid unexpected behavior!");
        return;
    };

    // Pipe the camera viewport size
    if let Some(cam_size) = camera.logical_viewport_size() {
        for mut size in &mut dst_query {
            **size = Vec2::from((cam_size.x, cam_size.y)) * if let Some(p) = projection_option { p.scale } else { 1.0 };
        }
    }
}

/// This system listens for added [`UiFetchFromCamera`] components and if it finds one, mutable accesses all [`Camera`]s to trigger fetching systems.
pub fn system_touch_camera_if_fetch_added<const INDEX: usize>(
    query: Query<Entity, Added<UiFetchFromCamera<INDEX>>>,
    mut cameras: Query<&mut Camera, With<UiSourceCamera<INDEX>>>,
){
    if !query.is_empty() {
        for mut camera in &mut cameras {
            camera.as_mut();
        }
    }
}


// #===================#
// #=== STYLE COLOR ===#

/// **Ui Color** - This component is used to control the color of the Ui-Node.
/// It is synchronized with a state machine and allows for specifying unique
/// colors for each state.
///
/// Affected components:
/// - [`Sprite`]
/// - [`TextColor`]
///
/// ## 🛠️ Example
/// ```
/// # use bevy::prelude::*;
/// # use bevy::color::palettes::basic::*;
/// # use bevy_lunex::*;
/// # fn spawn_main_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
/// # commands.spawn((
/// #     UiLayoutRoot::new_2d(),
/// # )).with_children(|ui| {
///       // Spawn as a single color
///       ui.spawn((
///           // ... Layout, etc.
///           UiColor::from(Color::Srgba(RED).with_alpha(0.8)),
///           // ... Sprite, Text, etc.
///       ));
///
///       // Spawn as a collection for different states
///       ui.spawn((
///           // ... Layout, etc.
///           UiColor::new(vec![
///               (UiBase::id(), Color::Srgba(RED).with_alpha(0.8)),
///               (UiHover::id(), Color::Srgba(YELLOW).with_alpha(1.2))
///           ]),
///           // ... Sprite, Text, etc.
///       ));
/// # });
/// # }
/// ```
#[derive(Component, Reflect, Deref, DerefMut, Default, Clone, PartialEq, Debug)]
pub struct UiColor {
    colors: HashMap<TypeId, Color>
}
/// Constructors
impl UiColor {
    /// Define multiple states at once using a vec.
    pub fn new(value: Vec<(TypeId, impl Into<Color>)>) -> Self {
        let mut map = HashMap::new();
        for (state, layout) in value {
            map.insert(state, layout.into());
        }
        Self { colors: map }
    }
}
/// Conversion implementations
impl <T: Into<Color>> From<T> for UiColor {
    fn from(value: T) -> Self {
        let mut map = HashMap::new();
        map.insert(UiBase::id(), value.into());
        Self {
            colors: map,
        }
    }
}

/// This system takes care of [`UiColor`] data and updates querried [`Sprite`] and [`TextColor`] components.
pub fn system_color(
    mut query: Query<(Option<&mut Sprite>, Option<&mut TextColor>, &UiColor, &UiState), Or<(Changed<UiColor>, Changed<UiState>)>>,
) {
    for (node_sprite_option, node_text_option, node_color, node_state) in &mut query {

        // Normalize the active state weights
        let mut total_weight = 0.0;
        for (state, _) in &node_color.colors {
            if let Some(weight) = node_state.states.get(state) {
                total_weight += weight;
            }
        }

        // Combine the color into one normalized
        let mut blend_color = Hsla::new(0.0, 0.0, 0.0, 0.0);

        // If no state active just try to use base color
        if total_weight == 0.0 {
            if let Some(color) = node_color.colors.get(&UiBase::id()) {
                blend_color = (*color).into();
            }

        // Blend colors from active states
        } else {
            for (state, color) in &node_color.colors {
                if let Some(weight) = node_state.states.get(state) {
                    let converted: Hsla = (*color).into();

                    if blend_color.alpha == 0.0 {
                        blend_color.hue = converted.hue;
                    } else {
                        blend_color.hue = lerp_hue(blend_color.hue, converted.hue, weight / total_weight);
                    }

                    //blend_color.hue += converted.hue * (weight / total_weight);
                    blend_color.saturation += converted.saturation * (weight / total_weight);
                    blend_color.lightness += converted.lightness * (weight / total_weight);
                    blend_color.alpha += converted.alpha * (weight / total_weight);
                }
            }
        }

        // Apply the color to attached components
        if let Some(mut sprite) = node_sprite_option {
            sprite.color = blend_color.into();
        }
        if let Some(mut text) = node_text_option {
            **text = blend_color.into();
        }
    }
}
fn lerp_hue(h1: f32, h2: f32, t: f32) -> f32 {
    let diff = (h2 - h1 + 540.0) % 360.0 - 180.0; // Ensure shortest direction
    (h1 + diff * t + 360.0) % 360.0
}


// #===============================#
// #=== THE LUNEX SETS & GROUPS ===#

/// System set for [`UiLunexPlugins`]
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
pub enum UiSystems {
    /// Systems that modify data pre-computation
    PreCompute,
    /// The computation
    Compute,
    /// Systems that modify data post-computation
    PostCompute,
}

/// Gizmo group for UI 2D node debug outlines
#[derive(GizmoConfigGroup, Default, Reflect, Clone, Debug)]
pub struct LunexGizmoGroup2d;

/// Gizmo group for UI 3D node debug outlines
#[derive(GizmoConfigGroup, Default, Reflect, Clone, Debug)]
pub struct LunexGizmoGroup3d;


// #=========================#
// #=== THE LUNEX PLUGINS ===#

/// This plugin is used for the main logic.
#[derive(Debug, Default, Clone)]
pub struct UiLunexPlugin;
impl Plugin for UiLunexPlugin {
    fn build(&self, app: &mut App) {

        // Configure the system set
        app.configure_sets(PostUpdate, (
            UiSystems::PreCompute.before(UiSystems::Compute),
            UiSystems::PostCompute.after(UiSystems::Compute).before(bevy::transform::TransformSystem::TransformPropagate),
        ));

        // Add observers
        app.add_observer(observer_touch_layout_root);

        // PRE-COMPUTE SYSTEMS
        app.add_systems(PostUpdate, (

            system_state_base_balancer,
            system_text_size_to_layout.after(bevy::text::update_text2d_layout),

        ).in_set(UiSystems::PreCompute));

        #[cfg(feature = "text3d")]
        app.add_systems(PostUpdate,
            system_text_3d_size_to_layout
                .after(bevy_rich_text3d::Text3dSet)
                .in_set(UiSystems::PreCompute)
        );


        // COMPUTE SYSTEMS
        app.add_systems(PostUpdate, (

            system_layout_compute,

        ).in_set(UiSystems::Compute));


        // POST-COMPUTE SYSTEMS
        app.add_systems(PostUpdate, (

            system_color,
            system_mark_3d,
            system_pipe_sprite_size_from_dimension.before(bevy::sprite::SpriteSystem::ComputeSlices),
            system_text_size_from_dimension,
            system_mesh_3d_reconstruct_from_dimension,
            system_mesh_2d_reconstruct_from_dimension,
            system_embedd_resize,

        ).in_set(UiSystems::PostCompute));

        #[cfg(feature = "text3d")]
        app.add_systems(PostUpdate,
            system_text_3d_size_from_dimension
                .in_set(UiSystems::PostCompute)
        );


        // Add index plugins
        app.add_plugins((
            CursorPlugin,
            UiLunexStatePlugin,
            UiLunexPickingPlugin,
            UiLunexIndexPlugin::<0>,
            UiLunexIndexPlugin::<1>,
            UiLunexIndexPlugin::<2>,
            UiLunexIndexPlugin::<3>,
        ));
    }
}


/// This plugin is used to enable debug functionality.
#[derive(Debug, Default, Clone)]
pub struct UiLunexDebugPlugin<const GIZMO_2D_LAYER: usize = 0, const GIZMO_3D_LAYER: usize = 0>;
impl <const GIZMO_2D_LAYER: usize, const GIZMO_3D_LAYER: usize> Plugin for UiLunexDebugPlugin<GIZMO_2D_LAYER, GIZMO_3D_LAYER> {
    fn build(&self, app: &mut App) {

        // Configure the Gizmo render groups
        app .init_gizmo_group::<LunexGizmoGroup2d>()
            .init_gizmo_group::<LunexGizmoGroup3d>()
            .add_systems(Startup, |mut config_store: ResMut<GizmoConfigStore>| {
                let (my_config, _) = config_store.config_mut::<LunexGizmoGroup2d>();
                my_config.render_layers = RenderLayers::layer(GIZMO_2D_LAYER);

                let (my_config, _) = config_store.config_mut::<LunexGizmoGroup3d>();
                my_config.render_layers = RenderLayers::layer(GIZMO_3D_LAYER);
            });

        // Add the 2d and 3d gizmo outlines
        app.add_systems(PostUpdate, (
            system_debug_draw_gizmo_2d,
            system_debug_draw_gizmo_3d,
        ));

        // Add the debug tree printing
        app.add_systems(PostUpdate, (
            system_debug_print_data,
        ).in_set(UiSystems::PostCompute));
    }
}


/// This plugin is used to register index components.
#[derive(Debug, Default, Clone)]
pub struct UiLunexIndexPlugin<const INDEX: usize>;
impl <const INDEX: usize> Plugin for UiLunexIndexPlugin<INDEX> {
    fn build(&self, app: &mut App) {
        app.add_systems(PostUpdate, (
            system_fetch_dimension_from_camera::<INDEX>,
            system_touch_camera_if_fetch_added::<INDEX>,
        ).in_set(UiSystems::PreCompute));
    }
}


/// Plugin group adding all necessary plugins for Lunex
pub struct UiLunexPlugins;
impl PluginGroup for UiLunexPlugins {
    fn build(self) -> PluginGroupBuilder {
        let mut builder = PluginGroupBuilder::start::<Self>();

        // Add text 3d plugin
        #[cfg(feature = "text3d")] {
            builder = builder.add(Text3dPlugin {
                load_system_fonts: true,
                ..Default::default()
            });
        }

        // Add Lunex plugin
        builder = builder.add(UiLunexPlugin);

        // Return the plugin group
        builder
    }
}