diff --git a/Cargo.toml b/Cargo.toml index dc19326..4c09dd2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/buttons.rs b/src/buttons.rs new file mode 100644 index 0000000..0382aa4 --- /dev/null +++ b/src/buttons.rs @@ -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::(); +} + +fn button_system( + mut input_focus: ResMut, + interaction_query: Query<(Entity, &Interaction), Changed>, + 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::(); + commands.trigger(ButtonReleasedEvent(entity)); + } + } + Interaction::None => { + if let Ok(_) = pressed_query.get(entity) { + commands.entity(entity).remove::(); + commands.trigger(ButtonReleasedEvent(entity)); + } + } + } + } +} diff --git a/src/card/drag.rs b/src/card/drag.rs new file mode 100644 index 0000000..adfa2ee --- /dev/null +++ b/src/card/drag.rs @@ -0,0 +1,4 @@ +use bevy::prelude::*; + +#[derive(Component)] +pub struct DraggableCard; diff --git a/src/card.rs b/src/card/mod.rs similarity index 99% rename from src/card.rs rename to src/card/mod.rs index 3382135..b84a60c 100644 --- a/src/card.rs +++ b/src/card/mod.rs @@ -1,5 +1,7 @@ use bevy::prelude::*; +pub mod drag; + pub struct CardPlugin; #[derive(Component)] diff --git a/src/main.rs b/src/main.rs index 6ff8f32..3a3eea7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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();