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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// This module is mostly copied and pasted from `bevy_sprite::texture_slice`
//
// A more centralized solution should be investigated in the future

use bevy_asset::{AssetEvent, Assets};
use bevy_ecs::prelude::*;
use bevy_math::{Rect, Vec2};
use bevy_render::texture::Image;
use bevy_sprite::{ImageScaleMode, TextureAtlas, TextureAtlasLayout, TextureSlice};
use bevy_transform::prelude::*;
use bevy_utils::HashSet;

use crate::{CalculatedClip, ExtractedUiNode, Node, NodeType, UiImage};

/// Component storing texture slices for image nodes entities with a tiled or sliced  [`ImageScaleMode`]
///
/// This component is automatically inserted and updated
#[derive(Debug, Clone, Component)]
pub struct ComputedTextureSlices {
    slices: Vec<TextureSlice>,
    image_size: Vec2,
}

impl ComputedTextureSlices {
    /// Computes [`ExtractedUiNode`] iterator from the sprite slices
    ///
    /// # Arguments
    ///
    /// * `transform` - the sprite entity global transform
    /// * `original_entity` - the sprite entity
    /// * `sprite` - The sprite component
    /// * `handle` - The sprite texture handle
    #[must_use]
    pub(crate) fn extract_ui_nodes<'a>(
        &'a self,
        transform: &'a GlobalTransform,
        node: &'a Node,
        image: &'a UiImage,
        clip: Option<&'a CalculatedClip>,
        camera_entity: Entity,
    ) -> impl ExactSizeIterator<Item = ExtractedUiNode> + 'a {
        let mut flip = Vec2::new(1.0, -1.0);
        let [mut flip_x, mut flip_y] = [false; 2];
        if image.flip_x {
            flip.x *= -1.0;
            flip_x = true;
        }
        if image.flip_y {
            flip.y *= -1.0;
            flip_y = true;
        }
        self.slices.iter().map(move |slice| {
            let offset = (slice.offset * flip).extend(0.0);
            let transform = transform.mul_transform(Transform::from_translation(offset));
            let scale = slice.draw_size / slice.texture_rect.size();
            let mut rect = slice.texture_rect;
            rect.min *= scale;
            rect.max *= scale;
            let atlas_size = Some(self.image_size * scale);
            ExtractedUiNode {
                stack_index: node.stack_index,
                color: image.color.into(),
                transform: transform.compute_matrix(),
                rect,
                flip_x,
                flip_y,
                image: image.texture.id(),
                atlas_size,
                clip: clip.map(|clip| clip.clip),
                camera_entity,
                border: [0.; 4],
                border_radius: [0.; 4],
                node_type: NodeType::Rect,
            }
        })
    }
}

/// Generates sprite slices for a `sprite` given a `scale_mode`. The slices
/// will be computed according to the `image_handle` dimensions.
///
/// Returns `None` if the image asset is not loaded
///
/// # Arguments
///
/// * `draw_area` - The size of the drawing area the slices will have to fit into
/// * `scale_mode` - The image scaling component
/// * `image_handle` - The texture to slice or tile
/// * `images` - The image assets, use to retrieve the image dimensions
/// * `atlas` - Optional texture atlas, if set the slicing will happen on the matching sub section
/// of the texture
/// * `atlas_layouts` - The atlas layout assets, used to retrieve the texture atlas section rect
#[must_use]
fn compute_texture_slices(
    draw_area: Vec2,
    scale_mode: &ImageScaleMode,
    image_handle: &UiImage,
    images: &Assets<Image>,
    atlas: Option<&TextureAtlas>,
    atlas_layouts: &Assets<TextureAtlasLayout>,
) -> Option<ComputedTextureSlices> {
    let (image_size, texture_rect) = match atlas {
        Some(a) => {
            let layout = atlas_layouts.get(&a.layout)?;
            (
                layout.size.as_vec2(),
                layout.textures.get(a.index)?.as_rect(),
            )
        }
        None => {
            let image = images.get(&image_handle.texture)?;
            let size = Vec2::new(
                image.texture_descriptor.size.width as f32,
                image.texture_descriptor.size.height as f32,
            );
            let rect = Rect {
                min: Vec2::ZERO,
                max: size,
            };
            (size, rect)
        }
    };
    let slices = match scale_mode {
        ImageScaleMode::Sliced(slicer) => slicer.compute_slices(texture_rect, Some(draw_area)),
        ImageScaleMode::Tiled {
            tile_x,
            tile_y,
            stretch_value,
        } => {
            let slice = TextureSlice {
                texture_rect,
                draw_size: draw_area,
                offset: Vec2::ZERO,
            };
            slice.tiled(*stretch_value, (*tile_x, *tile_y))
        }
    };
    Some(ComputedTextureSlices { slices, image_size })
}

/// System reacting to added or modified [`Image`] handles, and recompute sprite slices
/// on matching sprite entities with a [`ImageScaleMode`] component
pub(crate) fn compute_slices_on_asset_event(
    mut commands: Commands,
    mut events: EventReader<AssetEvent<Image>>,
    images: Res<Assets<Image>>,
    atlas_layouts: Res<Assets<TextureAtlasLayout>>,
    ui_nodes: Query<(
        Entity,
        &ImageScaleMode,
        &Node,
        &UiImage,
        Option<&TextureAtlas>,
    )>,
) {
    // We store the asset ids of added/modified image assets
    let added_handles: HashSet<_> = events
        .read()
        .filter_map(|e| match e {
            AssetEvent::Added { id } | AssetEvent::Modified { id } => Some(*id),
            _ => None,
        })
        .collect();
    if added_handles.is_empty() {
        return;
    }
    // We recompute the sprite slices for sprite entities with a matching asset handle id
    for (entity, scale_mode, ui_node, image, atlas) in &ui_nodes {
        if !added_handles.contains(&image.texture.id()) {
            continue;
        }
        if let Some(slices) = compute_texture_slices(
            ui_node.size(),
            scale_mode,
            image,
            &images,
            atlas,
            &atlas_layouts,
        ) {
            commands.entity(entity).try_insert(slices);
        }
    }
}

/// System reacting to changes on relevant sprite bundle components to compute the sprite slices
/// on matching sprite entities with a [`ImageScaleMode`] component
pub(crate) fn compute_slices_on_image_change(
    mut commands: Commands,
    images: Res<Assets<Image>>,
    atlas_layouts: Res<Assets<TextureAtlasLayout>>,
    changed_nodes: Query<
        (
            Entity,
            &ImageScaleMode,
            &Node,
            &UiImage,
            Option<&TextureAtlas>,
        ),
        Or<(
            Changed<ImageScaleMode>,
            Changed<UiImage>,
            Changed<Node>,
            Changed<TextureAtlas>,
        )>,
    >,
) {
    for (entity, scale_mode, ui_node, image, atlas) in &changed_nodes {
        if let Some(slices) = compute_texture_slices(
            ui_node.size(),
            scale_mode,
            image,
            &images,
            atlas,
            &atlas_layouts,
        ) {
            commands.entity(entity).try_insert(slices);
        }
    }
}