swoop_ui/button/h_button.rs
1use bevy_ecs::prelude::*;
2use bevy_ui::prelude::*;
3
4use crate::View;
5use crate::background::BackgroundStyle;
6use crate::border::{BorderStyle, BorderView};
7use crate::prelude::{BackgroundView, PositionView, StackView};
8use crate::shadow::BoxShadowView;
9
10/// A horizontally laid-out button view with customizable border, background, and shadow.
11///
12/// `HButton` is a styled UI component composed using Bevy's ECS `Bundle`.
13/// By default, it uses a horizontal `FlexDirection::Row` layout with centered alignment,
14/// and includes styling traits such as `BorderView`, `BackgroundView`, `BoxShadowView`, and `PositionView`.
15///
16/// This component is ideal for creating rich, composable buttons in declarative UI.
17///
18/// # Default Layout
19/// - `display`: `Flex`
20/// - `flex_direction`: `Row`
21/// - `justify_content`: `Center`
22/// - `align_items`: `Center`
23/// - `column_gap`: `0.0`
24///
25/// # Example
26/// ```ignore
27/// commands.spawn(HButton::default()
28/// .text("Click me")
29/// .font(my_font)
30/// .border_width(Val::Px(2.0))
31/// .background_color(Color::BLUE));
32/// ```
33#[derive(Bundle, Debug, Clone)]
34pub struct HButton {
35 /// Identifier for debugging and inspection.
36 name: Name,
37
38 /// Layout node defining size, flex behavior, and spacing.
39 node: Node,
40
41 /// Button marker component used by Bevy's UI interaction system.
42 botton: Button,
43
44 /// Style information for borders.
45 border: BorderStyle,
46
47 /// Background styling (color, gradient, image, etc.).
48 background: BackgroundStyle,
49
50 /// Optional box shadow styling for depth and elevation.
51 shadow: BoxShadow,
52}
53
54impl Default for HButton {
55 /// Creates a default `HButton` with centered horizontal layout and themed styling.
56 fn default() -> Self {
57 Self {
58 name: Name::new("HButton"),
59 node: Node {
60 display: Display::Flex,
61 flex_direction: FlexDirection::Row,
62 justify_content: JustifyContent::Center,
63 align_items: AlignItems::Center,
64 column_gap: Val::Px(0.0),
65 ..Default::default()
66 },
67 botton: Button,
68 border: BorderStyle::button(),
69 background: BackgroundStyle::button(),
70 shadow: BoxShadow::default(),
71 }
72 }
73}
74
75impl View for HButton {
76 fn name_node(&mut self) -> &mut Name {
77 &mut self.name
78 }
79
80 fn node_node(&mut self) -> &mut Node {
81 &mut self.node
82 }
83}
84
85impl StackView for HButton {}
86
87impl BackgroundView for HButton {
88 fn background_node(&mut self) -> &mut BackgroundStyle {
89 &mut self.background
90 }
91}
92
93impl BorderView for HButton {
94 fn border_node(&mut self) -> &mut BorderStyle {
95 &mut self.border
96 }
97}
98
99impl BoxShadowView for HButton {
100 fn box_shadow_node(&mut self) -> &mut BoxShadow {
101 &mut self.shadow
102 }
103}
104
105impl PositionView for HButton {}