button system
This commit is contained in:
parent
950451938f
commit
4ba69a6834
5 changed files with 62 additions and 1 deletions
53
src/buttons.rs
Normal file
53
src/buttons.rs
Normal 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
4
src/card/drag.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct DraggableCard;
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
pub mod drag;
|
||||
|
||||
pub struct CardPlugin;
|
||||
|
||||
#[derive(Component)]
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue