[no description]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use bevy::camera::RenderTarget;
use bevy::picking::PickingSystems;
use bevy::picking::backend::HitData;
use bevy::picking::backend::PointerHits;
use bevy::picking::pointer::PointerId;
use bevy::picking::pointer::PointerLocation;
use bevy::prelude::*;
use bevy::window::PrimaryWindow;
use bevy_ecs_tilemap::prelude::*;

pub struct TilemapPickingPlugin;

impl Plugin for TilemapPickingPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(PreUpdate, tile_picking.in_set(PickingSystems::Backend));
    }
}

fn tile_picking(
    pointers_q: Query<(&PointerId, &PointerLocation)>,
    cameras_q: Query<(
        Entity,
        &Camera,
        &RenderTarget,
        &GlobalTransform,
        &Projection,
    )>,
    primary_window_q: Query<Entity, With<PrimaryWindow>>,
    tilemap_q: Query<(
        &TilemapSize,
        &TilemapGridSize,
        &TilemapTileSize,
        &TilemapAnchor,
        &TilemapType,
        &TileStorage,
        &GlobalTransform,
        &ViewVisibility,
    )>,
    mut output: MessageWriter<PointerHits>,
) {
    for (p_id, p_loc) in pointers_q
        .iter()
        .filter_map(|(p_id, p_loc)| p_loc.location().map(|l| (p_id, l)))
    {
        let Some((cam_entity, camera, _cam_target, cam_transform, _cam_ortho)) = cameras_q
            .iter()
            .filter(|(_, camera, ..)| camera.is_active)
            .find(|(_, _, cam_target, ..)| {
                cam_target
                    .normalize(match primary_window_q.single() {
                        Ok(w) => Some(w),
                        Err(_) => return false,
                    })
                    .as_ref()
                    == Some(&p_loc.target)
            })
        else {
            continue;
        };

        let Ok(cursor_pos_world) = camera.viewport_to_world_2d(cam_transform, p_loc.position)
        else {
            continue;
        };

        let picks = tilemap_q
            .iter()
            .filter(|(.., vis)| vis.get())
            .filter_map(
                |(
                    map_s,
                    map_grid_size,
                    map_tile_size,
                    map_anchor,
                    map_type,
                    map_store,
                    map_transform,
                    ..,
                )| {
                    let in_map_pos = {
                        let pos = Vec4::from((cursor_pos_world, 0., 1.0));
                        let in_map_pos = map_transform.to_matrix().inverse() * pos;
                        in_map_pos.xy()
                    };

                    let tile_entity = TilePos::from_world_pos(
                        &in_map_pos,
                        map_s,
                        map_grid_size,
                        map_tile_size,
                        map_type,
                        map_anchor,
                    )
                    .and_then(|tile_pos| map_store.get(&tile_pos))?;

                    Some((
                        tile_entity,
                        HitData::new(cam_entity, -map_transform.translation().z, None, None),
                    ))
                },
            )
            .collect::<Vec<_>>();

        output.write(PointerHits::new(*p_id, picks, camera.order as f32));
    }
}