button system

This commit is contained in:
Rendo 2026-01-25 00:15:46 +05:00
commit 4ba69a6834
5 changed files with 62 additions and 1 deletions

View file

@ -4,4 +4,4 @@ version = "0.1.0"
edition = "2024"
[dependencies]
bevy = { version = "0.18.0", features = ["wav"] }
bevy = { version = "0.18.0", features = ["wav","debug"] }

53
src/buttons.rs Normal file
View file

@ -0,0 +1,53 @@
use bevy::{input_focus::InputFocus, prelude::*, sprite::Anchor};
pub struct ButtonPlugin;
#[derive(EntityEvent)]
pub struct ButtonPressedEvent(Entity);
#[derive(EntityEvent)]
pub struct ButtonReleasedEvent(Entity);
#[derive(Component)]
struct ButtonPressed;
impl Plugin for ButtonPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, button_setup)
.add_systems(Update, button_system);
}
}
fn button_setup(mut commands: Commands) {
commands.init_resource::<InputFocus>();
}
fn button_system(
mut input_focus: ResMut<InputFocus>,
interaction_query: Query<(Entity, &Interaction), Changed<Interaction>>,
pressed_query: Query<&ButtonPressed>,
mut commands: Commands,
) {
for (entity, interaction) in interaction_query {
match *interaction {
Interaction::Pressed => {
input_focus.set(entity);
commands.entity(entity).insert(ButtonPressed);
commands.trigger(ButtonPressedEvent(entity));
}
Interaction::Hovered => {
input_focus.set(entity);
if let Ok(_) = pressed_query.get(entity) {
commands.entity(entity).remove::<ButtonPressed>();
commands.trigger(ButtonReleasedEvent(entity));
}
}
Interaction::None => {
if let Ok(_) = pressed_query.get(entity) {
commands.entity(entity).remove::<ButtonPressed>();
commands.trigger(ButtonReleasedEvent(entity));
}
}
}
}
}

4
src/card/drag.rs Normal file
View file

@ -0,0 +1,4 @@
use bevy::prelude::*;
#[derive(Component)]
pub struct DraggableCard;

View file

@ -1,5 +1,7 @@
use bevy::prelude::*;
pub mod drag;
pub struct CardPlugin;
#[derive(Component)]

View file

@ -1,5 +1,6 @@
use bevy::{audio::Volume, prelude::*};
mod buttons;
mod card;
mod dev_tools;
mod turns;
@ -11,6 +12,7 @@ fn main() {
turns::TurnSystemPlugin,
card::CardPlugin,
dev_tools::DevToolsPlugin,
buttons::ButtonPlugin,
))
.add_systems(Startup, setup)
.run();