From fa1740630c41f675725411e227ef80f744c74663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Lescaudey=20de=20Maneville?= Date: Fri, 23 Feb 2024 02:08:32 +0100 Subject: [PATCH] Fixed Ui Image slicing (#12047) # Objective Fixes #11944 ## Solution #11600 made an incorrect assumption on what `UiImageSize` does, removing its usage in slicing fixes the problem --- crates/bevy_sprite/src/sprite.rs | 2 ++ crates/bevy_ui/src/texture_slice.rs | 35 +++++++---------------------- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/crates/bevy_sprite/src/sprite.rs b/crates/bevy_sprite/src/sprite.rs index 9c164d9a2c202..1cd3cf73d5dbe 100644 --- a/crates/bevy_sprite/src/sprite.rs +++ b/crates/bevy_sprite/src/sprite.rs @@ -32,6 +32,8 @@ pub struct Sprite { } /// Controls how the image is altered when scaled. +/// +/// Note: This is not yet compatible with texture atlases #[derive(Component, Debug, Clone, Reflect)] #[reflect(Component)] pub enum ImageScaleMode { diff --git a/crates/bevy_ui/src/texture_slice.rs b/crates/bevy_ui/src/texture_slice.rs index 2c77f43499cf8..24e77691e9bb8 100644 --- a/crates/bevy_ui/src/texture_slice.rs +++ b/crates/bevy_ui/src/texture_slice.rs @@ -10,7 +10,7 @@ use bevy_sprite::{ImageScaleMode, TextureSlice}; use bevy_transform::prelude::*; use bevy_utils::HashSet; -use crate::{widget::UiImageSize, BackgroundColor, CalculatedClip, ExtractedUiNode, Node, UiImage}; +use crate::{BackgroundColor, CalculatedClip, ExtractedUiNode, Node, UiImage}; /// Component storing texture slices for image nodes entities with a tiled or sliced [`ImageScaleMode`] /// @@ -119,13 +119,7 @@ pub(crate) fn compute_slices_on_asset_event( mut commands: Commands, mut events: EventReader>, images: Res>, - ui_nodes: Query<( - Entity, - &ImageScaleMode, - &Node, - Option<&UiImageSize>, - &UiImage, - )>, + ui_nodes: Query<(Entity, &ImageScaleMode, &Node, &UiImage)>, ) { // We store the asset ids of added/modified image assets let added_handles: HashSet<_> = events @@ -139,12 +133,11 @@ pub(crate) fn compute_slices_on_asset_event( return; } // We recompute the sprite slices for sprite entities with a matching asset handle id - for (entity, scale_mode, ui_node, size, image) in &ui_nodes { + for (entity, scale_mode, ui_node, image) in &ui_nodes { if !added_handles.contains(&image.texture.id()) { continue; } - let size = size.map(|s| s.size()).unwrap_or(ui_node.size()); - if let Some(slices) = compute_texture_slices(size, scale_mode, image, &images) { + if let Some(slices) = compute_texture_slices(ui_node.size(), scale_mode, image, &images) { commands.entity(entity).insert(slices); } } @@ -156,24 +149,12 @@ pub(crate) fn compute_slices_on_image_change( mut commands: Commands, images: Res>, changed_nodes: Query< - ( - Entity, - &ImageScaleMode, - &Node, - Option<&UiImageSize>, - &UiImage, - ), - Or<( - Changed, - Changed, - Changed, - Changed, - )>, + (Entity, &ImageScaleMode, &Node, &UiImage), + Or<(Changed, Changed, Changed)>, >, ) { - for (entity, scale_mode, ui_node, size, image) in &changed_nodes { - let size = size.map(|s| s.size()).unwrap_or(ui_node.size()); - if let Some(slices) = compute_texture_slices(size, scale_mode, image, &images) { + for (entity, scale_mode, ui_node, image) in &changed_nodes { + if let Some(slices) = compute_texture_slices(ui_node.size(), scale_mode, image, &images) { commands.entity(entity).insert(slices); } }