swoop_ui/button/
text_button.rs

1use bevy_color::prelude::*;
2use bevy_ecs::prelude::*;
3use bevy_math::prelude::*;
4use bevy_ui::prelude::*;
5
6use crate::background::BackgroundStyle;
7use crate::border::{BorderStyle, BorderView};
8use crate::prelude::{BackgroundView, PositionView, StackView};
9use crate::shadow::{BoxShadowView, TextShadowView};
10use crate::text::{TextStyle, TextView};
11use crate::{View, ViewToBundle};
12
13/// A horizontally laid-out button view that includes styling for border, background,
14/// box shadow, and text shadow.
15///
16/// `TextButton` is a Bevy UI bundle suited for building rich, styled buttons with optional
17/// text shadow effects. It uses `FlexDirection::Row` to horizontally align its children,
18/// and implements positioning and style-related traits such as `BorderView`, `BackgroundView`,
19/// `BoxShadowView`, `TextShadowView`, and `PositionView` (if implemented).
20///
21/// # Default Layout
22/// - `display`: `Flex`
23/// - `flex_direction`: `Row`
24/// - `justify_content`: `Center`
25/// - `align_items`: `Center`
26/// - `column_gap`: `0.0`
27///
28/// # Example
29/// ```ignore
30/// commands.spawn(TextButton::default()
31///     .text("OK")
32///     .text_color(Color::WHITE)
33///     .font(my_font)
34///     .background_color(Color::rgb(0.2, 0.6, 0.8)));
35/// ```
36#[derive(Debug, Clone)]
37pub struct TextButton {
38    /// Name tag for debugging or entity inspection.
39    name: Name,
40
41    /// Layout node controlling size, flex behavior, and spacing.
42    node: Node,
43
44    /// Bevy's built-in Button marker for interaction detection.
45    botton: Button,
46
47    /// Visual styling for borders (widths, colors, radius).
48    border: BorderStyle,
49
50    /// Visual background style (fill, gradient, texture).
51    background: BackgroundStyle,
52
53    /// Optional outer shadow to simulate depth or elevation.
54    box_shadow: BoxShadow,
55
56    /// Text content, color, font, size
57    text: TextStyle,
58
59    /// Inner shadow applied to the text content.
60    text_shadow: TextShadow,
61}
62
63impl Default for TextButton {
64    fn default() -> Self {
65        Self {
66            name: Name::new("TextButton"),
67            node: Node {
68                display: Display::Flex,
69                flex_direction: FlexDirection::Row,
70                justify_content: JustifyContent::Center,
71                align_items: AlignItems::Center,
72                column_gap: Val::Px(0.0),
73                ..Default::default()
74            },
75            botton: Button,
76            border: BorderStyle::button(),
77            background: BackgroundStyle::button(),
78            box_shadow: BoxShadow::default(),
79            text: TextStyle::button(),
80            text_shadow: TextShadow {
81                offset: Vec2::ZERO,
82                color: Srgba::NONE.into(),
83            },
84        }
85    }
86}
87
88impl View for TextButton {
89    fn name_node(&mut self) -> &mut Name {
90        &mut self.name
91    }
92
93    fn node_node(&mut self) -> &mut Node {
94        &mut self.node
95    }
96}
97
98impl ViewToBundle for TextButton {
99    fn pack(self) -> impl Bundle {
100        (
101            self.name,
102            self.node,
103            self.botton,
104            self.border,
105            self.background,
106            self.box_shadow,
107            children![self.text, self.text_shadow],
108        )
109    }
110}
111
112impl StackView for TextButton {}
113
114impl BackgroundView for TextButton {
115    fn background_node(&mut self) -> &mut BackgroundStyle {
116        &mut self.background
117    }
118}
119
120impl BorderView for TextButton {
121    fn border_node(&mut self) -> &mut BorderStyle {
122        &mut self.border
123    }
124}
125
126impl BoxShadowView for TextButton {
127    fn box_shadow_node(&mut self) -> &mut BoxShadow {
128        &mut self.box_shadow
129    }
130}
131
132impl TextView for TextButton {
133    fn text_node(&mut self) -> &mut crate::text::TextStyle {
134        &mut self.text
135    }
136}
137
138impl TextShadowView for TextButton {
139    fn text_shadow_node(&mut self) -> &mut TextShadow {
140        &mut self.text_shadow
141    }
142}
143
144impl PositionView for TextButton {}