From be27be51140e7c248b449e2368c223b1ad2eb467 Mon Sep 17 00:00:00 2001 From: omg-frank Date: Mon, 3 Feb 2025 14:21:31 -0600 Subject: [PATCH 1/9] added an example showing how to use with_rect --- Cargo.toml | 5 +++ examples/2d/image_rect.rs | 67 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 examples/2d/image_rect.rs diff --git a/Cargo.toml b/Cargo.toml index 8c2543542b..f40332c081 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -644,6 +644,11 @@ description = "Manually read/write the pixels of a texture" category = "2D Rendering" wasm = true +[[example]] +name = "image_rect" +path = "examples/2d/image_rect.rs" +doc-scrape-examples = true + [[example]] name = "sprite" path = "examples/2d/sprite.rs" diff --git a/examples/2d/image_rect.rs b/examples/2d/image_rect.rs new file mode 100644 index 0000000000..53e0a8cac5 --- /dev/null +++ b/examples/2d/image_rect.rs @@ -0,0 +1,67 @@ +//! Demonstrates the use of with_rect on ImageNodes. + +use bevy::prelude::*; + +fn main() { + App::new() + .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) + .add_systems(Startup, setup) + .run(); +} + +fn setup( + mut commands: Commands, + asset_server: Res, + mut texture_atlas_layouts: ResMut>, + mut ui_scale : ResMut, +) { + let texture = asset_server.load("textures/array_texture.png"); + let layout = TextureAtlasLayout::from_grid(UVec2::splat(250), 1, 3, None, None); + let texture_atlas_layout = texture_atlas_layouts.add(layout); + + ui_scale.0 = 0.5; + + commands.spawn(Camera2d); + + commands.spawn(Node { + display: Display::Flex, + align_items: AlignItems::Center, + ..default() + }) + .with_children(|parent| { + + // this example node displays an texture in its entirety + parent.spawn(ImageNode::new(texture.clone())); + + // this example node shows a texture constrained by a rect + parent.spawn( + ImageNode::new(texture.clone()) + .with_rect( + Rect::new(0., 200., 250., 450.) + ) + ); + + // this example node displays an index within a texture atlas + parent.spawn(ImageNode::from_atlas_image( + texture.clone(), + TextureAtlas { + layout: texture_atlas_layout.clone(), + index: 1, + }, + )); + + // this example node displays an index within a texture atlas + // constrained by a rect + parent.spawn(ImageNode::from_atlas_image( + texture.clone(), + TextureAtlas { + layout: texture_atlas_layout.clone(), + index: 1, + }, + ).with_rect( + Rect::new(0., 0., 150., 150.) + ) + ); + + }); +} From 04c298881fa9be638c2143470b1f3b90ddb5f42e Mon Sep 17 00:00:00 2001 From: omg-frank Date: Tue, 4 Feb 2025 13:36:53 -0600 Subject: [PATCH 2/9] transformed example into interior doc --- crates/bevy_ui/src/widget/image.rs | 39 +++++++++++++++++ examples/2d/image_rect.rs | 67 ------------------------------ 2 files changed, 39 insertions(+), 67 deletions(-) delete mode 100644 examples/2d/image_rect.rs diff --git a/crates/bevy_ui/src/widget/image.rs b/crates/bevy_ui/src/widget/image.rs index e34efdc715..932241c969 100644 --- a/crates/bevy_ui/src/widget/image.rs +++ b/crates/bevy_ui/src/widget/image.rs @@ -119,6 +119,45 @@ impl ImageNode { self } + /// with_rect crops an ImageNode to the portion described by + /// a Rect. This can be applied to ImageNodes created from + /// texture atlases. + /// + /// the following example setup function demonstrates this use + /// ```rust + /// fn setup( + /// mut commands: Commands, + /// asset_server: Res, + /// mut texture_atlas_layouts: ResMut>, + /// mut ui_scale : ResMut, + /// ) { + /// let texture = asset_server.load("textures/array_texture.png"); + /// let layout = TextureAtlasLayout::from_grid(UVec2::splat(250), 1, 3, None, None); + /// let texture_atlas_layout = texture_atlas_layouts.add(layout); + /// + /// commands.spawn(Node { + /// display: Display::Flex, + /// ..default() + /// }) + /// .with_children(|parent| { + /// // this example node shows a texture constrained by a rect + /// parent.spawn( + /// ImageNode::new(texture.clone()) + /// .with_rect( + /// Rect::new(0., 200., 250., 450.) + /// )); + /// // this example node displays an index within a texture atlas + /// // constrained by a rect + /// parent.spawn(ImageNode::from_atlas_image( + /// texture.clone(), + /// TextureAtlas { + /// layout: texture_atlas_layout.clone(), + /// index: 1, + /// }, + /// ).with_rect( + /// Rect::new(0., 0., 150., 150.) + /// ));});} + ///```` #[must_use] pub const fn with_rect(mut self, rect: Rect) -> Self { self.rect = Some(rect); diff --git a/examples/2d/image_rect.rs b/examples/2d/image_rect.rs deleted file mode 100644 index 53e0a8cac5..0000000000 --- a/examples/2d/image_rect.rs +++ /dev/null @@ -1,67 +0,0 @@ -//! Demonstrates the use of with_rect on ImageNodes. - -use bevy::prelude::*; - -fn main() { - App::new() - .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) - .add_systems(Startup, setup) - .run(); -} - -fn setup( - mut commands: Commands, - asset_server: Res, - mut texture_atlas_layouts: ResMut>, - mut ui_scale : ResMut, -) { - let texture = asset_server.load("textures/array_texture.png"); - let layout = TextureAtlasLayout::from_grid(UVec2::splat(250), 1, 3, None, None); - let texture_atlas_layout = texture_atlas_layouts.add(layout); - - ui_scale.0 = 0.5; - - commands.spawn(Camera2d); - - commands.spawn(Node { - display: Display::Flex, - align_items: AlignItems::Center, - ..default() - }) - .with_children(|parent| { - - // this example node displays an texture in its entirety - parent.spawn(ImageNode::new(texture.clone())); - - // this example node shows a texture constrained by a rect - parent.spawn( - ImageNode::new(texture.clone()) - .with_rect( - Rect::new(0., 200., 250., 450.) - ) - ); - - // this example node displays an index within a texture atlas - parent.spawn(ImageNode::from_atlas_image( - texture.clone(), - TextureAtlas { - layout: texture_atlas_layout.clone(), - index: 1, - }, - )); - - // this example node displays an index within a texture atlas - // constrained by a rect - parent.spawn(ImageNode::from_atlas_image( - texture.clone(), - TextureAtlas { - layout: texture_atlas_layout.clone(), - index: 1, - }, - ).with_rect( - Rect::new(0., 0., 150., 150.) - ) - ); - - }); -} From 58d00613fd12d194fb90fb518d69a2d40e7daa29 Mon Sep 17 00:00:00 2001 From: omg-frank Date: Tue, 4 Feb 2025 14:24:41 -0600 Subject: [PATCH 3/9] removed example from cargo file --- Cargo.toml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f40332c081..8c2543542b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -644,11 +644,6 @@ description = "Manually read/write the pixels of a texture" category = "2D Rendering" wasm = true -[[example]] -name = "image_rect" -path = "examples/2d/image_rect.rs" -doc-scrape-examples = true - [[example]] name = "sprite" path = "examples/2d/sprite.rs" From 3297294c465ec1ade62dc47f40f7ed7a7c817628 Mon Sep 17 00:00:00 2001 From: omg-frank Date: Tue, 4 Feb 2025 19:52:59 -0600 Subject: [PATCH 4/9] document formatting --- crates/bevy_ui/src/widget/image.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/bevy_ui/src/widget/image.rs b/crates/bevy_ui/src/widget/image.rs index 932241c969..5032a063a6 100644 --- a/crates/bevy_ui/src/widget/image.rs +++ b/crates/bevy_ui/src/widget/image.rs @@ -119,11 +119,10 @@ impl ImageNode { self } - /// with_rect crops an ImageNode to the portion described by - /// a Rect. This can be applied to ImageNodes created from + /// `with_rect` crops an `ImageNode` to the portion described by + /// a `Rect`. This can be applied to `ImageNode`s created from /// texture atlases. - /// - /// the following example setup function demonstrates this use + /// The following example setup function demonstrates this use. /// ```rust /// fn setup( /// mut commands: Commands, @@ -156,7 +155,9 @@ impl ImageNode { /// }, /// ).with_rect( /// Rect::new(0., 0., 150., 150.) - /// ));});} + /// )); + /// }); + /// } ///```` #[must_use] pub const fn with_rect(mut self, rect: Rect) -> Self { From 3156a72f2771287de8d111df050d3c791bde914e Mon Sep 17 00:00:00 2001 From: omg-frank Date: Tue, 4 Feb 2025 20:06:05 -0600 Subject: [PATCH 5/9] added the bevy prelude --- crates/bevy_ui/src/widget/image.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bevy_ui/src/widget/image.rs b/crates/bevy_ui/src/widget/image.rs index 5032a063a6..453bbddaae 100644 --- a/crates/bevy_ui/src/widget/image.rs +++ b/crates/bevy_ui/src/widget/image.rs @@ -124,6 +124,7 @@ impl ImageNode { /// texture atlases. /// The following example setup function demonstrates this use. /// ```rust + /// use bevy::prelude::*; /// fn setup( /// mut commands: Commands, /// asset_server: Res, From c2143418fa9d76312dddc99673bc6b5e2eeae5fa Mon Sep 17 00:00:00 2001 From: omg-frank Date: Tue, 4 Feb 2025 20:26:05 -0600 Subject: [PATCH 6/9] replaced bevy prelude with explicit use statements --- crates/bevy_ui/src/widget/image.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ui/src/widget/image.rs b/crates/bevy_ui/src/widget/image.rs index 453bbddaae..8f661367e1 100644 --- a/crates/bevy_ui/src/widget/image.rs +++ b/crates/bevy_ui/src/widget/image.rs @@ -124,12 +124,16 @@ impl ImageNode { /// texture atlases. /// The following example setup function demonstrates this use. /// ```rust - /// use bevy::prelude::*; + /// use bevy_asset::{Asset,AssetServer}; + /// use bevy_ecs::prelude::{Commands,Res,ResMut}; + /// use bevy_image::TextureAtlasLayout; + /// use bevy_math::UVec2; + /// use bevy_ui::Node; + /// use bevy_ui::prelude::Display; /// fn setup( /// mut commands: Commands, /// asset_server: Res, /// mut texture_atlas_layouts: ResMut>, - /// mut ui_scale : ResMut, /// ) { /// let texture = asset_server.load("textures/array_texture.png"); /// let layout = TextureAtlasLayout::from_grid(UVec2::splat(250), 1, 3, None, None); From e312afda0a059b8f255b87b042ed61578d5b58af Mon Sep 17 00:00:00 2001 From: omg-frank Date: Tue, 4 Feb 2025 21:52:46 -0600 Subject: [PATCH 7/9] added additional use statements --- crates/bevy_ui/src/widget/image.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/bevy_ui/src/widget/image.rs b/crates/bevy_ui/src/widget/image.rs index 8f661367e1..32cf22aa75 100644 --- a/crates/bevy_ui/src/widget/image.rs +++ b/crates/bevy_ui/src/widget/image.rs @@ -123,13 +123,16 @@ impl ImageNode { /// a `Rect`. This can be applied to `ImageNode`s created from /// texture atlases. /// The following example setup function demonstrates this use. - /// ```rust - /// use bevy_asset::{Asset,AssetServer}; + /// + /// # Example + /// + /// ``` + /// use bevy_asset::{Assets,AssetServer}; /// use bevy_ecs::prelude::{Commands,Res,ResMut}; - /// use bevy_image::TextureAtlasLayout; - /// use bevy_math::UVec2; + /// use bevy_image::{TextureAtlas,TextureAtlasLayout}; + /// use bevy_math::{UVec2,Rect}; /// use bevy_ui::Node; - /// use bevy_ui::prelude::Display; + /// use bevy_ui::prelude::{Display,ImageNode}; /// fn setup( /// mut commands: Commands, /// asset_server: Res, From 3b1f01984b6d49a5f70430791a5f72a6327f7bef Mon Sep 17 00:00:00 2001 From: omg-frank Date: Tue, 4 Feb 2025 22:08:54 -0600 Subject: [PATCH 8/9] added explicit default use --- crates/bevy_ui/src/widget/image.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ui/src/widget/image.rs b/crates/bevy_ui/src/widget/image.rs index 32cf22aa75..60ccce6828 100644 --- a/crates/bevy_ui/src/widget/image.rs +++ b/crates/bevy_ui/src/widget/image.rs @@ -133,6 +133,7 @@ impl ImageNode { /// use bevy_math::{UVec2,Rect}; /// use bevy_ui::Node; /// use bevy_ui::prelude::{Display,ImageNode}; + /// use std::default::Default; /// fn setup( /// mut commands: Commands, /// asset_server: Res, @@ -144,7 +145,7 @@ impl ImageNode { /// /// commands.spawn(Node { /// display: Display::Flex, - /// ..default() + /// ..Default::default() /// }) /// .with_children(|parent| { /// // this example node shows a texture constrained by a rect From 12945084e8e1bd68b2bc6f13ad7a2faf894941ef Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 5 Feb 2025 13:55:57 -0600 Subject: [PATCH 9/9] Apply suggestions from code review Co-authored-by: Alice Cecile --- crates/bevy_ui/src/widget/image.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ui/src/widget/image.rs b/crates/bevy_ui/src/widget/image.rs index 60ccce6828..5e27a1a64e 100644 --- a/crates/bevy_ui/src/widget/image.rs +++ b/crates/bevy_ui/src/widget/image.rs @@ -119,8 +119,8 @@ impl ImageNode { self } - /// `with_rect` crops an `ImageNode` to the portion described by - /// a `Rect`. This can be applied to `ImageNode`s created from + /// Crops an `ImageNode` to the portion described by + /// the provided `Rect`, measured from the top-left corner. This can be applied to `ImageNode`s created from /// texture atlases. /// The following example setup function demonstrates this use. /// @@ -134,6 +134,7 @@ impl ImageNode { /// use bevy_ui::Node; /// use bevy_ui::prelude::{Display,ImageNode}; /// use std::default::Default; + /// /// fn setup( /// mut commands: Commands, /// asset_server: Res,