From 63dae75761b0500950bec27229dd7ea16708106a Mon Sep 17 00:00:00 2001 From: demenik Date: Fri, 28 Nov 2025 12:21:34 +0100 Subject: [PATCH] feat: Add button element (#56) --- src/features/ui/components.rs | 40 +++++++++++++++++++++++++++++++++++ src/features/ui/mod.rs | 5 ++++- src/features/ui/ui/button.rs | 39 ++++++++++++++++++++++++++++++++++ src/features/ui/ui/mod.rs | 3 +++ src/prelude.rs | 2 +- 5 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/features/ui/ui/button.rs create mode 100644 src/features/ui/ui/mod.rs diff --git a/src/features/ui/components.rs b/src/features/ui/components.rs index 57950c9..b20eb12 100644 --- a/src/features/ui/components.rs +++ b/src/features/ui/components.rs @@ -6,3 +6,43 @@ pub struct Scroll { pub entity: Entity, pub delta: Vec2, } + +#[derive(Component, Clone)] +pub enum ButtonVariant { + Primary, + Secondary, + Destructive, +} + +impl ButtonVariant { + pub fn normal_background(&self) -> Color { + match self { + ButtonVariant::Primary => Color::srgb(0.35, 0.17, 0.78), + ButtonVariant::Secondary => Color::srgb(0.15, 0.15, 0.15), + ButtonVariant::Destructive => Color::srgb(0.79, 0.17, 0.20), + } + } + + pub fn hover_background(&self) -> Color { + match self { + ButtonVariant::Primary => Color::srgb(0.45, 0.27, 0.88), + ButtonVariant::Secondary => Color::srgb(0.25, 0.25, 0.25), + ButtonVariant::Destructive => Color::srgb(0.89, 0.27, 0.30), + } + } + + pub fn pressed_background(&self) -> Color { + match self { + ButtonVariant::Primary => Color::srgb(0.55, 0.37, 0.98), + ButtonVariant::Secondary => Color::srgb(0.35, 0.35, 0.35), + ButtonVariant::Destructive => Color::srgb(0.99, 0.37, 0.40), + } + } + + pub fn text_color(&self) -> Color { + match self { + ButtonVariant::Primary | ButtonVariant::Destructive => Color::srgb(0.1, 0.1, 0.1), + ButtonVariant::Secondary => Color::WHITE, + } + } +} diff --git a/src/features/ui/mod.rs b/src/features/ui/mod.rs index 999ca55..6d42304 100644 --- a/src/features/ui/mod.rs +++ b/src/features/ui/mod.rs @@ -1,8 +1,9 @@ -use crate::prelude::*; +use crate::prelude::{button::update_buttons, *}; use bevy::{input::mouse::*, picking::hover::HoverMap}; pub mod components; pub mod consts; +pub mod ui; pub struct UiPlugin; @@ -10,6 +11,8 @@ impl Plugin for UiPlugin { fn build(&self, app: &mut App) { app.add_systems(Update, scroll_events); app.add_observer(on_scroll_handler); + + app.add_systems(Update, update_buttons); } } diff --git a/src/features/ui/ui/button.rs b/src/features/ui/ui/button.rs new file mode 100644 index 0000000..bae76a7 --- /dev/null +++ b/src/features/ui/ui/button.rs @@ -0,0 +1,39 @@ +use crate::prelude::*; + +pub fn button( + button_type: impl Component, + variant: ButtonVariant, + node: Node, + title: impl Into, + font_size: f32, +) -> impl Bundle { + ( + Button, + button_type, + variant.clone(), + node, + BackgroundColor(variant.normal_background()), + BorderRadius::all(px(10)), + children![( + Text::new(title), + TextFont::from_font_size(font_size), + TextColor(variant.text_color()) + )], + ) +} + +pub fn update_buttons( + mut interaction_query: Query< + (&Interaction, &ButtonVariant, &mut BackgroundColor), + (Changed, With