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

Add rect field to UI image #15095

Merged
Merged
Show file tree
Hide file tree
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
42 changes: 24 additions & 18 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,25 +329,31 @@ pub fn extract_uinode_images(
continue;
}

let (rect, atlas_scaling) = match atlas {
Some(atlas) => {
let Some(layout) = texture_atlases.get(&atlas.layout) else {
// Atlas not present in assets resource (should this warn the user?)
continue;
};
let mut atlas_rect = layout.textures[atlas.index].as_rect();
let atlas_scaling = uinode.size() / atlas_rect.size();
atlas_rect.min *= atlas_scaling;
atlas_rect.max *= atlas_scaling;
(atlas_rect, Some(atlas_scaling))
let atlas_rect = atlas
.and_then(|s| s.texture_rect(&texture_atlases))
.map(|r| r.as_rect());

let mut rect = match (atlas_rect, image.rect) {
(None, None) => Rect {
min: Vec2::ZERO,
max: uinode.calculated_size,
},
(None, Some(image_rect)) => image_rect,
(Some(atlas_rect), None) => atlas_rect,
(Some(atlas_rect), Some(mut image_rect)) => {
image_rect.min += atlas_rect.min;
image_rect.max += atlas_rect.min;
image_rect
}
None => (
Rect {
min: Vec2::ZERO,
max: uinode.calculated_size,
},
None,
),
};

let atlas_scaling = if atlas_rect.is_some() || image.rect.is_some() {
let atlas_scaling = uinode.size() / rect.size();
rect.min *= atlas_scaling;
rect.max *= atlas_scaling;
Some(atlas_scaling)
} else {
None
};

let ui_logical_viewport_size = camera_query
Expand Down
19 changes: 14 additions & 5 deletions crates/bevy_ui/src/render/ui_texture_slice_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,20 @@ pub fn extract_ui_texture_slices(
continue;
}

let atlas_rect = atlas.and_then(|atlas| {
texture_atlases
.get(&atlas.layout)
.map(|layout| layout.textures[atlas.index].as_rect())
});
let atlas_rect = atlas
.and_then(|s| s.texture_rect(&texture_atlases))
.map(|r| r.as_rect());

let atlas_rect = match (atlas_rect, image.rect) {
(None, None) => None,
(None, Some(image_rect)) => Some(image_rect),
(Some(atlas_rect), None) => Some(atlas_rect),
(Some(atlas_rect), Some(mut image_rect)) => {
image_rect.min += atlas_rect.min;
image_rect.max += atlas_rect.min;
Some(image_rect)
}
};

extracted_ui_slicers.slices.insert(
commands.spawn_empty().id(),
Expand Down
14 changes: 14 additions & 0 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1837,6 +1837,12 @@ pub struct UiImage {
pub flip_x: bool,
/// Whether the image should be flipped along its y-axis
pub flip_y: bool,
/// An optional rectangle representing the region of the image to render, instead of rendering
/// the full image. This is an easy one-off alternative to using a [`TextureAtlas`](bevy_sprite::TextureAtlas).
///
/// When used with a [`TextureAtlas`](bevy_sprite::TextureAtlas), the rect
/// is offset by the atlas's minimal (top-left) corner position.
pub rect: Option<Rect>,
}

impl Default for UiImage {
Expand All @@ -1856,6 +1862,7 @@ impl Default for UiImage {
texture: TRANSPARENT_IMAGE_HANDLE,
flip_x: false,
flip_y: false,
rect: None,
}
}
}
Expand All @@ -1879,6 +1886,7 @@ impl UiImage {
color,
flip_x: false,
flip_y: false,
rect: None,
}
}

Expand All @@ -1902,6 +1910,12 @@ impl UiImage {
self.flip_y = true;
self
}

#[must_use]
pub const fn with_rect(mut self, rect: Rect) -> Self {
self.rect = Some(rect);
self
}
}

impl From<Handle<Image>> for UiImage {
Expand Down