Skip to content

Commit

Permalink
Ui texture Atlas
Browse files Browse the repository at this point in the history
  • Loading branch information
ManevilleF committed May 13, 2023
1 parent 1b0969d commit c8763f6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
30 changes: 24 additions & 6 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use bevy_render::{
};
use bevy_sprite::SpriteAssetEvents;
#[cfg(feature = "bevy_text")]
use bevy_sprite::TextureAtlasLayout;
use bevy_sprite::{TextureAtlas, TextureAtlasLayout};
#[cfg(feature = "bevy_text")]
use bevy_text::{PositionedGlyph, Text, TextLayoutInfo};
use bevy_transform::components::GlobalTransform;
Expand Down Expand Up @@ -165,20 +165,22 @@ pub fn extract_uinodes(
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
images: Extract<Res<Assets<Image>>>,
ui_stack: Extract<Res<UiStack>>,
atlas_layouts: Extract<Res<Assets<TextureAtlasLayout>>>,
uinode_query: Extract<
Query<(
&Node,
&GlobalTransform,
&BackgroundColor,
Option<&UiImage>,
&ComputedVisibility,
Option<&TextureAtlas>,
Option<&CalculatedClip>,
)>,
>,
) {
extracted_uinodes.uinodes.clear();
for (stack_index, entity) in ui_stack.uinodes.iter().enumerate() {
if let Ok((uinode, transform, color, maybe_image, visibility, clip)) =
if let Ok((uinode, transform, color, maybe_image, visibility, atlas, clip)) =
uinode_query.get(*entity)
{
// Skip invisible and completely transparent nodes
Expand All @@ -195,17 +197,33 @@ pub fn extract_uinodes(
} else {
(DEFAULT_IMAGE_HANDLE.typed().clone_weak(), false, false)
};

// Skip loading images
if !images.contains(&image) {
continue;
}
// Skip completely transparent nodes
if color.0.a() == 0.0 {
continue;
}
let (atlas_size, rect_min) = atlas
.and_then(|a| atlas_layouts.get(&a.layout).map(|l| (l, a.index)))
.and_then(|(atlas, index)| {
atlas
.textures
.get(index)
.map(|rect| (Some(atlas.size), rect.min))
})
.unwrap_or((None, Vec2::ZERO));
extracted_uinodes.uinodes.push(ExtractedUiNode {
stack_index,
transform: transform.compute_matrix(),
color: color.0,
rect: Rect {
min: Vec2::ZERO,
max: uinode.calculated_size,
min: rect_min,
max: rect_min + uinode.calculated_size,
},
image,
atlas_size: None,
atlas_size,
clip: clip.map(|clip| clip.clip),
flip_x,
flip_y,
Expand Down
53 changes: 51 additions & 2 deletions examples/ui/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
// Only run the app when there is user input. This will significantly reduce CPU/GPU use.
.insert_resource(WinitSettings::desktop_app())
.add_systems(Startup, setup)
.add_systems(Update, button_system)
.add_systems(Update, (button_system, sheet_button_system))
.run();
}

Expand Down Expand Up @@ -43,7 +43,40 @@ fn button_system(
}
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
fn sheet_button_system(
mut interaction_query: Query<
(&Interaction, &mut TextureAtlas),
(Changed<Interaction>, With<Button>),
>,
) {
for (interaction, mut atlas) in interaction_query.iter_mut() {
match *interaction {
Interaction::Clicked => {
atlas.index = 2;
}
Interaction::Hovered => {
atlas.index = 1;
}
Interaction::None => {
atlas.index = 0;
}
}
}
}

fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
) {
let handle = asset_server.load("textures/array_texture.png");
let layout = texture_atlas_layouts.add(TextureAtlasLayout::from_grid(
Vec2::splat(250.0),
1,
4,
None,
None,
));
// ui camera
commands.spawn(Camera2dBundle::default());
commands
Expand All @@ -57,6 +90,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
})
.with_children(|parent| {
// Classic button
parent
.spawn(ButtonBundle {
style: Style {
Expand All @@ -65,6 +99,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
justify_content: JustifyContent::Center,
// vertically center child text
align_items: AlignItems::Center,
// center button
margin: UiRect::all(Val::Auto),
..default()
},
background_color: NORMAL_BUTTON.into(),
Expand All @@ -80,5 +116,18 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
},
));
});
// Texture sheet button
parent
.spawn(ButtonBundle {
style: Style {
size: Size::new(Val::Px(150.0), Val::Px(65.0)),
// center button
margin: UiRect::all(Val::Auto),
..default()
},
image: handle.into(),
..default()
})
.insert(TextureAtlas { layout, index: 0 });
});
}

0 comments on commit c8763f6

Please sign in to comment.