drag and drop

This commit is contained in:
Rendo 2026-01-25 18:21:01 +05:00
commit 28b6b040b7
4 changed files with 88 additions and 14 deletions

View file

@ -1,4 +1,68 @@
use bevy::prelude::*;
use crate::{animation::transform::AnimatedTransform, card::Hand};
#[derive(Component)]
pub struct DraggableCard;
#[derive(Component)]
pub struct DraggedCard;
#[derive(Resource)]
pub struct MousePosition(pub Vec3);
pub fn mouse_position_system(
mut mouse: ResMut<MousePosition>,
window: Single<&Window>,
camera: Single<(&Camera, &GlobalTransform)>,
) {
if let Some(Ok(mouse_pos)) = window
.cursor_position()
.and_then(|viewport_pos| Some(camera.0.viewport_to_world_2d(camera.1, viewport_pos)))
{
mouse.0 = Vec3 {
x: mouse_pos.x,
y: mouse_pos.y,
z: 0.,
}
}
}
pub fn card_drag_start(
event: On<Pointer<DragStart>>,
mut commands: Commands,
card_query: Query<(), With<DraggableCard>>,
dragged_option: Option<Single<&DraggedCard>>,
) {
if dragged_option.is_some() {
return;
}
if let Err(_) = card_query.get(event.entity) {
return;
}
commands
.entity(event.entity)
.insert(DraggedCard)
.remove_parent_in_place();
}
pub fn card_drag(
_: On<Pointer<Drag>>,
mut card_transform: Single<&mut AnimatedTransform, With<DraggedCard>>,
mouse_pos: Res<MousePosition>,
) {
card_transform.translation = mouse_pos.0;
}
pub fn card_drag_end(
_: On<Pointer<DragEnd>>,
mut commands: Commands,
dragged: Single<Entity, With<DraggedCard>>,
hand: Single<Entity, With<Hand>>,
) {
commands
.entity(dragged.entity())
.remove::<DraggedCard>()
.set_parent_in_place(hand.entity());
}