Skip to content

Commit

Permalink
Ui texture sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
ManevilleF committed Jul 14, 2022
1 parent 8388f0b commit a97e8d0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
29 changes: 23 additions & 6 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use bevy_render::{
view::{ExtractedView, ViewUniforms, Visibility},
Extract, RenderApp, RenderStage,
};
use bevy_sprite::{Rect, SpriteAssetEvents, TextureAtlas};
use bevy_sprite::{Rect, SpriteAssetEvents, TextureAtlas, TextureSheet};
use bevy_text::{DefaultTextPipeline, Text};
use bevy_transform::components::GlobalTransform;
use bevy_utils::FloatOrd;
Expand Down Expand Up @@ -176,36 +176,53 @@ pub struct ExtractedUiNodes {
pub fn extract_uinodes(
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
images: Extract<Res<Assets<Image>>>,
texture_atlases: Extract<Res<Assets<TextureAtlas>>>,
uinode_query: Extract<
Query<(
&Node,
&GlobalTransform,
&UiColor,
&UiImage,
&Visibility,
Option<&TextureSheet>,
Option<&CalculatedClip>,
)>,
>,
) {
extracted_uinodes.uinodes.clear();
for (uinode, transform, color, image, visibility, clip) in uinode_query.iter() {
if !visibility.is_visible {
for (uinode, transform, color, image, visibility, sheet, clip) in uinode_query.iter() {
// Skip if the node is invisible or if its size is set to zero (e.g. when a parent is set to `Display::None`)
if !visibility.is_visible || uinode.size == Vec2::ZERO {
continue;
}
let image = image.0.clone_weak();
// Skip loading images
if !images.contains(&image) {
continue;
}
let (atlas_size, rect_min) = sheet
.and_then(|s| {
texture_atlases
.get(&s.texture_atlas)
.map(|a| (a, s.texture_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 {
transform: transform.compute_matrix(),
color: color.0,
rect: bevy_sprite::Rect {
min: Vec2::ZERO,
max: uinode.size,
min: rect_min,
max: rect_min + uinode.size,
},
image,
atlas_size: None,
atlas_size,
clip: clip.map(|clip| clip.clip),
});
}
Expand Down
47 changes: 46 additions & 1 deletion examples/ui/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -43,9 +44,35 @@ fn button_system(
}
}

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

fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
) {
// ui camera
commands.spawn_bundle(Camera2dBundle::default());
// Classic button
commands
.spawn_bundle(ButtonBundle {
style: Style {
Expand Down Expand Up @@ -75,4 +102,22 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
});
});
// Texture sheet button
let handle = asset_server.load("textures/array_texture.png");
let texture_atlas = texture_atlases.add(TextureAtlas::from_grid(Vec2::splat(250.0), 1, 4));
commands
.spawn_bundle(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(TextureSheet {
texture_atlas,
texture_index: 0,
});
}

0 comments on commit a97e8d0

Please sign in to comment.