Skip to main content

beuvy_runtime/
button.rs

1mod build;
2mod state;
3
4use crate::text::{LocalizedTextFormat, TypographyStyle};
5use bevy::{input_focus::InputFocus, prelude::*};
6use bevy_localization::TextKey;
7
8pub use self::state::sync_button_active_state;
9pub use build::default_button_node;
10
11#[derive(SystemSet, Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum ButtonSet {
13    Build,
14}
15
16#[derive(Component, Default, Debug, Clone)]
17pub struct Button {
18    pub name: String,
19    pub button_type: ButtonType,
20}
21
22#[derive(Component, Default, Debug, Clone)]
23pub struct ButtonInner;
24
25#[derive(Component, Debug, Clone, Copy)]
26pub struct ButtonLabel {
27    pub entity: Entity,
28}
29
30#[derive(Component, Default, Debug, Clone)]
31pub struct DisabledButton;
32
33#[derive(Component, Default, Debug, Clone)]
34pub struct ActiveButton;
35
36#[derive(Default)]
37pub struct ButtonPlugin;
38
39impl Plugin for ButtonPlugin {
40    fn build(&self, app: &mut App) {
41        app.init_resource::<InputFocus>()
42            .add_message::<ButtonClickMessage>()
43            .add_systems(Update, build::add_button.in_set(ButtonSet::Build));
44    }
45}
46
47/// Declarative request to materialize a themed button.
48#[derive(Component, Debug, Clone)]
49pub struct AddButton {
50    pub name: String,
51    pub button_type: ButtonType,
52    pub text: String,
53    pub localized_text: Option<TextKey>,
54    pub localized_text_format: Option<LocalizedTextFormat>,
55    pub class: Option<String>,
56    pub label_class: Option<String>,
57    pub label_typography: TypographyStyle,
58    pub visible: bool,
59    pub disabled: bool,
60}
61
62impl Default for AddButton {
63    fn default() -> Self {
64        Self {
65            name: String::new(),
66            button_type: ButtonType::Button,
67            text: String::new(),
68            localized_text: None,
69            localized_text_format: None,
70            class: None,
71            label_class: None,
72            label_typography: TypographyStyle::default(),
73            visible: true,
74            disabled: false,
75        }
76    }
77}
78
79#[derive(Message, Debug, Clone)]
80pub struct ButtonClickMessage {
81    pub button: Button,
82    pub entity: Entity,
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
86pub enum ButtonType {
87    #[default]
88    Button,
89    Submit,
90    Reset,
91}