Skip to content

Commit caa1499

Browse files
fjkorfalice-i-cecilecart
authored
added an example showing how to use with_rect (#17664)
# Objective Add an example demonstrating the use of `ImageNode::rect` Fixes #16482 ## Solution Modified the code in the original issue #16457 ## Testing to test run `cargo run --example image_rect --release` --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: Carter Anderson <mcanders1@gmail.com>
1 parent 03f16a4 commit caa1499

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

crates/bevy_ui/src/widget/image.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,56 @@ impl ImageNode {
125125
self
126126
}
127127

128+
/// Crops an `ImageNode` to the portion described by
129+
/// the provided `Rect`, measured from the top-left corner. This can be applied to `ImageNode`s created from
130+
/// texture atlases.
131+
/// The following example setup function demonstrates this use.
132+
///
133+
/// # Example
134+
///
135+
/// ```
136+
/// use bevy_asset::{Assets,AssetServer};
137+
/// use bevy_ecs::prelude::{Commands,Res,ResMut};
138+
/// use bevy_image::{TextureAtlas,TextureAtlasLayout};
139+
/// use bevy_math::{UVec2,Rect};
140+
/// use bevy_ui::Node;
141+
/// use bevy_ui::prelude::{Display,ImageNode};
142+
/// use std::default::Default;
143+
///
144+
/// fn setup(
145+
/// mut commands: Commands,
146+
/// asset_server: Res<AssetServer>,
147+
/// mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
148+
/// ) {
149+
/// let texture = asset_server.load("textures/array_texture.png");
150+
/// let layout = TextureAtlasLayout::from_grid(UVec2::splat(250), 1, 3, None, None);
151+
/// let texture_atlas_layout = texture_atlas_layouts.add(layout);
152+
///
153+
/// commands.spawn(Node {
154+
/// display: Display::Flex,
155+
/// ..Default::default()
156+
/// })
157+
/// .with_children(|parent| {
158+
/// // this example node shows a texture constrained by a rect
159+
/// parent.spawn(
160+
/// ImageNode::new(texture.clone())
161+
/// .with_rect(
162+
/// Rect::new(0., 200., 250., 450.)
163+
/// ));
164+
/// // this example node displays an index within a texture atlas
165+
/// // constrained by a rect
166+
/// parent.spawn(ImageNode::from_atlas_image(
167+
/// texture.clone(),
168+
/// TextureAtlas {
169+
/// layout: texture_atlas_layout.clone(),
170+
/// index: 1,
171+
/// },
172+
/// ).with_rect(
173+
/// Rect::new(0., 0., 150., 150.)
174+
/// ));
175+
/// });
176+
/// }
177+
///````
128178
#[must_use]
129179
pub const fn with_rect(mut self, rect: Rect) -> Self {
130180
self.rect = Some(rect);

0 commit comments

Comments
 (0)