Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Image fills aspect_ratio of texture dimensions #13381

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions crates/bevy_ui/src/widget/image.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
measurement::AvailableSpace, ContentSize, Measure, Node, NodeMeasure, UiImage, UiScale,
measurement::AvailableSpace, ContentSize, Measure, Node, NodeMeasure, Style, UiImage, UiScale,
};
use bevy_asset::Assets;
use bevy_ecs::prelude::*;
Expand Down Expand Up @@ -82,6 +82,7 @@ pub fn update_image_content_size_system(
&UiImage,
&mut UiImageSize,
Option<&TextureAtlas>,
Option<&mut Style>,
),
UpdateImageFilter,
>,
Expand All @@ -92,7 +93,7 @@ pub fn update_image_content_size_system(
.unwrap_or(1.)
* ui_scale.0;

for (mut content_size, image, mut image_size, atlas_image) in &mut query {
for (mut content_size, image, mut image_size, atlas_image, style) in &mut query {
if let Some(size) = match atlas_image {
Some(atlas) => atlas.texture_rect(&atlases).map(|t| t.size()),
None => textures.get(&image.texture).map(|t| t.size()),
Expand All @@ -107,6 +108,13 @@ pub fn update_image_content_size_system(
// multiply the image size by the scale factor to get the physical size
size: size.as_vec2() * combined_scale_factor,
}));
// Image should keep its proportions even if no width or height is specified in its style
if let Some(mut style) = style {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment here would make this code easier to follow.

if style.aspect_ratio.is_none() {
let Vec2 { x, y } = size.as_vec2();
style.aspect_ratio = Some(x / y);
}
}
}
}
}
Expand Down