Skip to content

Commit 469c927

Browse files
committed
Merge branch 'main' of https://github.com/bevyengine/bevy into HEAD
2 parents 4e8dfa7 + cdaae01 commit 469c927

File tree

1 file changed

+48
-17
lines changed

1 file changed

+48
-17
lines changed

crates/bevy_ui/src/widget/image.rs

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
11
use crate::{
2-
measurement::AvailableSpace, ContentSize, Measure, Node, UiImage, UiTextureAtlasImage,
2+
measurement::AvailableSpace, ContentSize, Measure, Node, UiImage, UiScale, UiTextureAtlasImage,
33
};
44
use bevy_asset::{Assets, Handle};
5+
56
#[cfg(feature = "bevy_text")]
67
use bevy_ecs::query::Without;
78
use bevy_ecs::{
89
prelude::Component,
910
query::With,
1011
reflect::ReflectComponent,
11-
system::{Query, Res},
12+
system::{Local, Query, Res},
1213
};
1314
use bevy_math::Vec2;
1415
use bevy_reflect::{std_traits::ReflectDefault, FromReflect, Reflect, ReflectFromReflect};
1516
use bevy_render::texture::Image;
1617
use bevy_sprite::TextureAtlas;
1718
#[cfg(feature = "bevy_text")]
1819
use bevy_text::Text;
20+
use bevy_window::{PrimaryWindow, Window};
1921

20-
/// The size of the image in physical pixels
22+
/// The size of the image's texture
2123
///
22-
/// This field is set automatically by `update_image_calculated_size_system`
24+
/// This component is updated automatically by [`update_image_content_size_system`]
2325
#[derive(Component, Debug, Copy, Clone, Default, Reflect, FromReflect)]
2426
#[reflect(Component, Default, FromReflect)]
2527
pub struct UiImageSize {
28+
/// The size of the image's texture
29+
///
30+
/// This field is updated automatically by [`update_image_content_size_system`]
2631
size: Vec2,
2732
}
2833

2934
impl UiImageSize {
35+
/// The size of the image's texture
3036
pub fn size(&self) -> Vec2 {
3137
self.size
3238
}
3339
}
3440

3541
#[derive(Clone)]
42+
/// Used to calculate the size of UI image nodes
3643
pub struct ImageMeasure {
37-
// target size of the image
38-
size: Vec2,
44+
/// The size of the image's texture
45+
pub size: Vec2,
3946
}
4047

4148
impl Measure for ImageMeasure {
@@ -68,6 +75,9 @@ impl Measure for ImageMeasure {
6875

6976
/// Updates content size of the node based on the image provided
7077
pub fn update_image_content_size_system(
78+
mut previous_combined_scale_factor: Local<f64>,
79+
windows: Query<&Window, With<PrimaryWindow>>,
80+
ui_scale: Res<UiScale>,
7181
textures: Res<Assets<Image>>,
7282
#[cfg(feature = "bevy_text")] mut query: Query<
7383
(&mut ContentSize, &UiImage, &mut UiImageSize),
@@ -78,23 +88,37 @@ pub fn update_image_content_size_system(
7888
With<Node>,
7989
>,
8090
) {
91+
let combined_scale_factor = windows
92+
.get_single()
93+
.map(|window| window.resolution.scale_factor())
94+
.unwrap_or(1.)
95+
* ui_scale.scale;
96+
8197
for (mut content_size, image, mut image_size) in &mut query {
8298
if let Some(texture) = textures.get(&image.texture) {
8399
let size = Vec2::new(
84100
texture.texture_descriptor.size.width as f32,
85101
texture.texture_descriptor.size.height as f32,
86102
);
87-
// Update only if size has changed to avoid needless layout calculations
88-
if size != image_size.size {
103+
// Update only if size or scale factor has changed to avoid needless layout calculations
104+
if size != image_size.size || combined_scale_factor != *previous_combined_scale_factor {
89105
image_size.size = size;
90-
content_size.set(ImageMeasure { size });
106+
content_size.set(ImageMeasure {
107+
// multiply the image size by the scale factor to get the physical size
108+
size: size * combined_scale_factor as f32,
109+
});
91110
}
92111
}
93112
}
113+
114+
*previous_combined_scale_factor = combined_scale_factor;
94115
}
95116

96117
/// Updates content size of the node based on the texture atlas sprite
97118
pub fn update_atlas_content_size_system(
119+
mut previous_combined_scale_factor: Local<f64>,
120+
windows: Query<&Window, With<PrimaryWindow>>,
121+
ui_scale: Res<UiScale>,
98122
atlases: Res<Assets<TextureAtlas>>,
99123
#[cfg(feature = "bevy_text")] mut atlas_query: Query<
100124
(
@@ -115,18 +139,25 @@ pub fn update_atlas_content_size_system(
115139
(With<Node>, Without<UiImage>),
116140
>,
117141
) {
142+
let combined_scale_factor = windows
143+
.get_single()
144+
.map(|window| window.resolution.scale_factor())
145+
.unwrap_or(1.)
146+
* ui_scale.scale;
147+
118148
for (mut content_size, atlas, atlas_image, mut image_size) in &mut atlas_query {
119149
if let Some(atlas) = atlases.get(atlas) {
120-
let texture_rect = atlas.textures[atlas_image.index];
121-
let size = Vec2::new(
122-
texture_rect.max.x - texture_rect.min.x,
123-
texture_rect.max.y - texture_rect.min.y,
124-
);
125-
// Update only if size has changed to avoid needless layout calculations
126-
if size != image_size.size {
150+
let size = atlas.textures[atlas_image.index].size();
151+
// Update only if size or scale factor has changed to avoid needless layout calculations
152+
if size != image_size.size || combined_scale_factor != *previous_combined_scale_factor {
127153
image_size.size = size;
128-
content_size.set(ImageMeasure { size });
154+
content_size.set(ImageMeasure {
155+
// multiply the image size by the scale factor to get the physical size
156+
size: size * combined_scale_factor as f32,
157+
});
129158
}
130159
}
131160
}
161+
162+
*previous_combined_scale_factor = combined_scale_factor;
132163
}

0 commit comments

Comments
 (0)