From 6650529f309f4daef73562b2184a968a82c4defb Mon Sep 17 00:00:00 2001 From: felix de Maneville Date: Sun, 4 Dec 2022 13:35:57 +0100 Subject: [PATCH] Ui texture Atlas --- crates/bevy_ui/src/render/mod.rs | 22 ++++++++++---- examples/ui/button.rs | 52 +++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index bce044e96c2716..020146a6c597a4 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -24,7 +24,7 @@ use bevy_render::{ view::{ComputedVisibility, ExtractedView, ViewUniforms}, Extract, RenderApp, RenderStage, }; -use bevy_sprite::{SpriteAssetEvents, TextureAtlasLayout}; +use bevy_sprite::{SpriteAssetEvents, TextureAtlas, TextureAtlasLayout}; use bevy_text::{Text, TextLayoutInfo}; use bevy_transform::components::GlobalTransform; use bevy_utils::FloatOrd; @@ -188,6 +188,7 @@ pub fn extract_uinodes( mut extracted_uinodes: ResMut, images: Extract>>, ui_stack: Extract>, + atlas_layouts: Extract>>, windows: Extract>, uinode_query: Extract< Query<( @@ -196,6 +197,7 @@ pub fn extract_uinodes( &BackgroundColor, Option<&UiImage>, &ComputedVisibility, + Option<&TextureAtlas>, Option<&CalculatedClip>, )>, >, @@ -203,7 +205,7 @@ pub fn extract_uinodes( let scale_factor = windows.scale_factor(WindowId::primary()) as f32; 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) { if !visibility.is_visible() { @@ -222,17 +224,25 @@ pub fn extract_uinodes( 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(), background_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, diff --git a/examples/ui/button.rs b/examples/ui/button.rs index ab8b625e09f94f..f775fac9c31486 100644 --- a/examples/ui/button.rs +++ b/examples/ui/button.rs @@ -10,6 +10,7 @@ fn main() { .insert_resource(WinitSettings::desktop_app()) .add_startup_system(setup) .add_system(button_system) + .add_system(sheet_button_system) .run(); } @@ -43,7 +44,40 @@ fn button_system( } } -fn setup(mut commands: Commands, asset_server: Res) { +fn sheet_button_system( + mut interaction_query: Query< + (&Interaction, &mut TextureAtlas), + (Changed, With