2023-02-13 18:20:29 +00:00
|
|
|
use crate::{CalculatedSize, UiImage};
|
2021-12-14 03:58:23 +00:00
|
|
|
use bevy_asset::Assets;
|
2023-02-24 02:21:07 +00:00
|
|
|
#[cfg(feature = "bevy_text")]
|
|
|
|
use bevy_ecs::query::Without;
|
|
|
|
use bevy_ecs::system::{Query, Res};
|
2023-02-13 18:20:29 +00:00
|
|
|
use bevy_math::Vec2;
|
2021-12-14 03:58:23 +00:00
|
|
|
use bevy_render::texture::Image;
|
2023-02-24 02:21:07 +00:00
|
|
|
#[cfg(feature = "bevy_text")]
|
2022-10-27 12:56:03 +00:00
|
|
|
use bevy_text::Text;
|
2020-07-28 07:37:25 +00:00
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Updates calculated size of the node based on the image provided
|
2022-11-18 21:16:32 +00:00
|
|
|
pub fn update_image_calculated_size_system(
|
2021-12-14 03:58:23 +00:00
|
|
|
textures: Res<Assets<Image>>,
|
2023-02-24 02:21:07 +00:00
|
|
|
#[cfg(feature = "bevy_text")] mut query: Query<(&mut CalculatedSize, &UiImage), Without<Text>>,
|
|
|
|
#[cfg(not(feature = "bevy_text"))] mut query: Query<(&mut CalculatedSize, &UiImage)>,
|
2020-07-28 07:37:25 +00:00
|
|
|
) {
|
2022-07-11 15:28:50 +00:00
|
|
|
for (mut calculated_size, image) in &mut query {
|
2022-11-14 21:59:17 +00:00
|
|
|
if let Some(texture) = textures.get(&image.texture) {
|
2023-02-13 18:20:29 +00:00
|
|
|
let size = Vec2::new(
|
|
|
|
texture.texture_descriptor.size.width as f32,
|
|
|
|
texture.texture_descriptor.size.height as f32,
|
|
|
|
);
|
2021-01-25 05:00:20 +00:00
|
|
|
// Update only if size has changed to avoid needless layout calculations
|
|
|
|
if size != calculated_size.size {
|
|
|
|
calculated_size.size = size;
|
2022-12-20 16:44:12 +00:00
|
|
|
calculated_size.preserve_aspect_ratio = true;
|
2021-01-25 05:00:20 +00:00
|
|
|
}
|
2020-08-16 07:30:04 +00:00
|
|
|
}
|
2020-07-28 07:37:25 +00:00
|
|
|
}
|
|
|
|
}
|