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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * librePvZ: game logic implementation.
 * Copyright (c) 2022  Ruifeng Xie
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

//! Diagnostics support for 2D graphics.

use bevy::prelude::*;
use bevy::render::view::VisibilitySystems;
use bevy::sprite::Anchor;

/// Plugin for displaying bounding boxes for 2D sprite graphics.
#[derive(Copy, Clone)]
#[allow(missing_debug_implementations)]
pub struct BoundingBoxPlugin;

/// Gizmo group for bounding boxes.
#[derive(Default, Reflect, GizmoConfigGroup)]
#[allow(missing_debug_implementations)]
pub struct BoundingBoxGizmos;

/// Labels for the bounding box systems.
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, SystemSet)]
pub enum BoundingBoxSystem {
    /// Set up for newly-[`Added`] [`BoundingBoxRoot`]s.
    AddBoundingBox,
    /// Update bounding boxes upon changes of sprites or textures.
    UpdateBoundingBox,
}

impl Plugin for BoundingBoxPlugin {
    fn build(&self, app: &mut App) {
        use BoundingBoxSystem::*;
        app.init_gizmo_group::<BoundingBoxGizmos>()
            .configure_sets(PostUpdate, (
                AddBoundingBox,
                UpdateBoundingBox,
            ).after(VisibilitySystems::CheckVisibility))
            .add_systems(PostUpdate, add_bounding_box_system.in_set(AddBoundingBox))
            .add_systems(PostUpdate, update_bounding_box_system.in_set(UpdateBoundingBox));
    }
}

/// Component marking the root entity for bounding boxes.
#[derive(Debug, Default, Component)]
pub struct BoundingBoxRoot {
    /// Visibility of all these bounding boxes.
    pub is_visible: bool,
}

/// Bounding box component.
#[derive(Debug, Component)]
pub struct BoundingBox {
    root: Entity,
    size: Vec2,
    anchor: Anchor,
}

fn add_bounding_box_system(
    roots: Query<Entity, Added<BoundingBoxRoot>>,
    children: Query<&Children>,
    sprites: Query<(&Sprite, Option<&Handle<Image>>)>,
    images: Res<Assets<Image>>,
    mut commands: Commands,
) {
    for root in roots.iter() {
        let mut pending = vec![root];
        while let Some(current) = pending.pop() {
            if let Ok(children) = children.get(current) {
                pending.extend(children.iter());
            }
            if let Ok((sprite, texture)) = sprites.get(current) {
                if let Some(size) = sprite_size(&images, sprite, texture) {
                    commands.entity(current).insert(BoundingBox { root, size, anchor: sprite.anchor });
                }
            }
        }
    }
}

#[allow(clippy::type_complexity)]
fn update_bounding_box_system(
    mut gizmos: Gizmos<BoundingBoxGizmos>,
    roots: Query<&BoundingBoxRoot>,
    mut boxes: Query<(
        Entity, &mut BoundingBox,
        &GlobalTransform, &ViewVisibility,
        &Sprite, Option<&Handle<Image>>,
    )>,
    images: Res<Assets<Image>>,
    mut commands: Commands,
) {
    for (this, mut bb, global_transform, visible, sprite, texture) in boxes.iter_mut() {
        if let Ok(root) = roots.get(bb.root) {
            if let Some(size) = sprite_size(&images, sprite, texture) {
                if bb.size != size { bb.size = size; }
            }
            bb.anchor = sprite.anchor;
            if visible.get() && root.is_visible {
                let base = bb.anchor.as_vec();
                let make_vertex = |anchor: Anchor| {
                    let inner_pos = (anchor.as_vec() - base) * bb.size;
                    let pos = global_transform.transform_point(inner_pos.extend(0.0));
                    Vec2::new(pos.x, pos.y)
                };
                let top_left = make_vertex(Anchor::TopLeft);
                let top_right = make_vertex(Anchor::TopRight);
                let bottom_right = make_vertex(Anchor::BottomRight);
                let bottom_left = make_vertex(Anchor::BottomLeft);
                let vertices = [top_left, top_right, bottom_right, bottom_left, top_left];
                gizmos.linestrip_2d(vertices, Color::WHITE);
            }
        } else { // root is no longer BoundingBoxRoot
            commands.entity(this).remove::<BoundingBox>();
        }
    }
}

fn sprite_size(images: &Assets<Image>, sprite: &Sprite, texture: Option<&Handle<Image>>) -> Option<Vec2> {
    sprite.custom_size.or_else(|| texture
        .and_then(|texture| images.get(texture))
        .map(|image| image.size_f32()))
}