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

added an example showing how to use with_rect #17664

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
67 changes: 67 additions & 0 deletions examples/2d/image_rect.rs
Original file line number Diff line number Diff line change
@@ -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<AssetServer>,
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
mut ui_scale : ResMut<UiScale>,
) {
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.)
)
);

});
}
Loading