feat: Add button element (#56)

This commit is contained in:
demenik
2025-11-28 12:21:34 +01:00
parent 971cf54fa0
commit 63dae75761
5 changed files with 87 additions and 2 deletions

View File

@@ -6,3 +6,43 @@ pub struct Scroll {
pub entity: Entity, pub entity: Entity,
pub delta: Vec2, 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,
}
}
}

View File

@@ -1,8 +1,9 @@
use crate::prelude::*; use crate::prelude::{button::update_buttons, *};
use bevy::{input::mouse::*, picking::hover::HoverMap}; use bevy::{input::mouse::*, picking::hover::HoverMap};
pub mod components; pub mod components;
pub mod consts; pub mod consts;
pub mod ui;
pub struct UiPlugin; pub struct UiPlugin;
@@ -10,6 +11,8 @@ impl Plugin for UiPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Update, scroll_events); app.add_systems(Update, scroll_events);
app.add_observer(on_scroll_handler); app.add_observer(on_scroll_handler);
app.add_systems(Update, update_buttons);
} }
} }

View File

@@ -0,0 +1,39 @@
use crate::prelude::*;
pub fn button(
button_type: impl Component,
variant: ButtonVariant,
node: Node,
title: impl Into<String>,
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<Interaction>, With<Button>),
>,
) {
for (interaction, variant, mut color) in &mut interaction_query {
*color = match *interaction {
Interaction::None => variant.normal_background(),
Interaction::Hovered => variant.hover_background(),
Interaction::Pressed => variant.pressed_background(),
}
.into()
}
}

View File

@@ -0,0 +1,3 @@
pub mod button;
pub use button::button;

View File

@@ -14,7 +14,7 @@ pub use crate::features::{
messages::{InteractStartMessage, MoveMessage}, messages::{InteractStartMessage, MoveMessage},
}, },
savegame::components::SavegamePath, savegame::components::SavegamePath,
ui::consts::*, ui::{components::ButtonVariant, consts::*, ui::*},
}; };
pub use crate::utils::path::get_internal_path; pub use crate::utils::path::get_internal_path;
pub use bevy::prelude::*; pub use bevy::prelude::*;