Skip to content

Commit b838d2e

Browse files
committed
use u32 types in TextureAtlasLayout and TextureAtlasBuilder
1 parent 80e61f8 commit b838d2e

File tree

9 files changed

+57
-50
lines changed

9 files changed

+57
-50
lines changed

crates/bevy_sprite/src/dynamic_texture_atlas_builder.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::TextureAtlasLayout;
22
use bevy_asset::{Assets, Handle};
3-
use bevy_math::{IVec2, Rect, UVec2};
3+
use bevy_math::{URect, UVec2};
44
use bevy_render::{
55
render_asset::{RenderAsset, RenderAssetUsages},
66
texture::{Image, TextureFormatPixelInfo},
@@ -13,7 +13,7 @@ use guillotiere::{size2, Allocation, AtlasAllocator};
1313
/// e.g: in a font glyph [`TextureAtlasLayout`], only add the [`Image`] texture for letters to be rendered.
1414
pub struct DynamicTextureAtlasBuilder {
1515
atlas_allocator: AtlasAllocator,
16-
padding: i32,
16+
padding: u32,
1717
}
1818

1919
impl DynamicTextureAtlasBuilder {
@@ -23,7 +23,7 @@ impl DynamicTextureAtlasBuilder {
2323
///
2424
/// * `size` - total size for the atlas
2525
/// * `padding` - gap added between textures in the atlas, both in x axis and y axis
26-
pub fn new(size: UVec2, padding: i32) -> Self {
26+
pub fn new(size: UVec2, padding: u32) -> Self {
2727
Self {
2828
atlas_allocator: AtlasAllocator::new(to_size2(size)),
2929
padding,
@@ -50,8 +50,8 @@ impl DynamicTextureAtlasBuilder {
5050
atlas_texture_handle: &Handle<Image>,
5151
) -> Option<usize> {
5252
let allocation = self.atlas_allocator.allocate(size2(
53-
texture.width() as i32 + self.padding,
54-
texture.height() as i32 + self.padding,
53+
(texture.width() + self.padding).try_into().unwrap(),
54+
(texture.height() + self.padding).try_into().unwrap(),
5555
));
5656
if let Some(allocation) = allocation {
5757
let atlas_texture = textures.get_mut(atlas_texture_handle).unwrap();
@@ -63,8 +63,8 @@ impl DynamicTextureAtlasBuilder {
6363
);
6464

6565
self.place_texture(atlas_texture, allocation, texture);
66-
let mut rect: Rect = to_rect(allocation.rectangle);
67-
rect.max -= self.padding as f32;
66+
let mut rect: URect = to_rect(allocation.rectangle);
67+
rect.max = rect.max.saturating_sub(UVec2::splat(self.padding));
6868
Some(atlas_layout.add_texture(rect))
6969
} else {
7070
None
@@ -78,8 +78,8 @@ impl DynamicTextureAtlasBuilder {
7878
texture: &Image,
7979
) {
8080
let mut rect = allocation.rectangle;
81-
rect.max.x -= self.padding;
82-
rect.max.y -= self.padding;
81+
rect.max.x -= self.padding as i32;
82+
rect.max.y -= self.padding as i32;
8383
let atlas_width = atlas_texture.width() as usize;
8484
let rect_width = rect.width() as usize;
8585
let format_size = atlas_texture.texture_descriptor.format.pixel_size();
@@ -95,10 +95,16 @@ impl DynamicTextureAtlasBuilder {
9595
}
9696
}
9797

98-
fn to_rect(rectangle: guillotiere::Rectangle) -> Rect {
99-
Rect {
100-
min: IVec2::new(rectangle.min.x, rectangle.min.y).as_vec2(),
101-
max: IVec2::new(rectangle.max.x, rectangle.max.y).as_vec2(),
98+
fn to_rect(rectangle: guillotiere::Rectangle) -> URect {
99+
URect {
100+
min: UVec2::new(
101+
rectangle.min.x.try_into().unwrap(),
102+
rectangle.min.y.try_into().unwrap(),
103+
),
104+
max: UVec2::new(
105+
rectangle.max.x.try_into().unwrap(),
106+
rectangle.max.y.try_into().unwrap(),
107+
),
102108
}
103109
}
104110

crates/bevy_sprite/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ pub fn calculate_bounds_2d(
154154
// We default to the texture size for regular sprites
155155
None => images.get(texture_handle).map(|image| image.size_f32()),
156156
// We default to the drawn rect for atlas sprites
157-
Some(atlas) => atlas.texture_rect(&atlases).map(|rect| rect.size()),
157+
Some(atlas) => atlas
158+
.texture_rect(&atlases)
159+
.map(|rect| rect.size().as_vec2()),
158160
}) {
159161
let aabb = Aabb {
160162
center: (-sprite.anchor.as_vec() * size).extend(0.0).into(),

crates/bevy_sprite/src/render/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,10 @@ pub fn extract_sprites(
365365
let rect = match (atlas_rect, sprite.rect) {
366366
(None, None) => None,
367367
(None, Some(sprite_rect)) => Some(sprite_rect),
368-
(Some(atlas_rect), None) => Some(atlas_rect),
368+
(Some(atlas_rect), None) => Some(atlas_rect.as_rect()),
369369
(Some(atlas_rect), Some(mut sprite_rect)) => {
370-
sprite_rect.min += atlas_rect.min;
371-
sprite_rect.max += atlas_rect.min;
370+
sprite_rect.min += atlas_rect.min.as_vec2();
371+
sprite_rect.max += atlas_rect.min.as_vec2();
372372

373373
Some(sprite_rect)
374374
}

crates/bevy_sprite/src/texture_atlas.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bevy_asset::{Asset, AssetId, Assets, Handle};
22
use bevy_ecs::component::Component;
3-
use bevy_math::{Rect, UVec2};
3+
use bevy_math::{URect, UVec2};
44
use bevy_reflect::Reflect;
55
use bevy_render::texture::Image;
66
use bevy_utils::HashMap;
@@ -21,7 +21,7 @@ pub struct TextureAtlasLayout {
2121
// TODO: add support to Uniforms derive to write dimensions and sprites to the same buffer
2222
pub size: UVec2,
2323
/// The specific areas of the atlas where each texture can be found
24-
pub textures: Vec<Rect>,
24+
pub textures: Vec<URect>,
2525
/// Maps from a specific image handle to the index in `textures` where they can be found.
2626
///
2727
/// This field is set by [`TextureAtlasBuilder`].
@@ -94,12 +94,11 @@ impl TextureAtlasLayout {
9494
}
9595

9696
let cell = UVec2::new(x, y);
97-
9897
let rect_min = (tile_size + current_padding) * cell + offset;
9998

100-
sprites.push(Rect {
101-
min: rect_min.as_vec2(),
102-
max: (rect_min + tile_size).as_vec2(),
99+
sprites.push(URect {
100+
min: rect_min,
101+
max: rect_min + tile_size,
103102
});
104103
}
105104
}
@@ -121,7 +120,7 @@ impl TextureAtlasLayout {
121120
/// * `rect` - The section of the texture to be added
122121
///
123122
/// [`TextureAtlas`]: crate::TextureAtlas
124-
pub fn add_texture(&mut self, rect: Rect) -> usize {
123+
pub fn add_texture(&mut self, rect: URect) -> usize {
125124
self.textures.push(rect);
126125
self.textures.len() - 1
127126
}
@@ -150,7 +149,7 @@ impl TextureAtlasLayout {
150149

151150
impl TextureAtlas {
152151
/// Retrieves the current texture [`Rect`] of the sprite sheet according to the section `index`
153-
pub fn texture_rect(&self, texture_atlases: &Assets<TextureAtlasLayout>) -> Option<Rect> {
152+
pub fn texture_rect(&self, texture_atlases: &Assets<TextureAtlasLayout>) -> Option<URect> {
154153
let atlas = texture_atlases.get(&self.layout)?;
155154
atlas.textures.get(self.index).copied()
156155
}

crates/bevy_sprite/src/texture_atlas_builder.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bevy_asset::AssetId;
22
use bevy_log::{debug, error, warn};
3-
use bevy_math::{Rect, UVec2, Vec2};
3+
use bevy_math::{URect, UVec2};
44
use bevy_render::{
55
render_asset::RenderAssetUsages,
66
render_resource::{Extent3d, TextureDimension, TextureFormat},
@@ -31,9 +31,9 @@ pub struct TextureAtlasBuilder<'a> {
3131
/// Collection of texture's asset id (optional) and image data to be packed into an atlas
3232
textures_to_place: Vec<(Option<AssetId<Image>>, &'a Image)>,
3333
/// The initial atlas size in pixels.
34-
initial_size: Vec2,
34+
initial_size: UVec2,
3535
/// The absolute maximum size of the texture atlas in pixels.
36-
max_size: Vec2,
36+
max_size: UVec2,
3737
/// The texture format for the textures that will be loaded in the atlas.
3838
format: TextureFormat,
3939
/// Enable automatic format conversion for textures if they are not in the atlas format.
@@ -46,8 +46,8 @@ impl Default for TextureAtlasBuilder<'_> {
4646
fn default() -> Self {
4747
Self {
4848
textures_to_place: Vec::new(),
49-
initial_size: Vec2::new(256., 256.),
50-
max_size: Vec2::new(2048., 2048.),
49+
initial_size: UVec2::splat(256),
50+
max_size: UVec2::splat(2048),
5151
format: TextureFormat::Rgba8UnormSrgb,
5252
auto_format_conversion: true,
5353
padding: UVec2::ZERO,
@@ -59,13 +59,13 @@ pub type TextureAtlasBuilderResult<T> = Result<T, TextureAtlasBuilderError>;
5959

6060
impl<'a> TextureAtlasBuilder<'a> {
6161
/// Sets the initial size of the atlas in pixels.
62-
pub fn initial_size(mut self, size: Vec2) -> Self {
62+
pub fn initial_size(mut self, size: UVec2) -> Self {
6363
self.initial_size = size;
6464
self
6565
}
6666

6767
/// Sets the max size of the atlas in pixels.
68-
pub fn max_size(mut self, size: Vec2) -> Self {
68+
pub fn max_size(mut self, size: UVec2) -> Self {
6969
self.max_size = size;
7070
self
7171
}
@@ -189,10 +189,10 @@ impl<'a> TextureAtlasBuilder<'a> {
189189
/// If there is not enough space in the atlas texture, an error will
190190
/// be returned. It is then recommended to make a larger sprite sheet.
191191
pub fn finish(self) -> Result<(TextureAtlasLayout, Image), TextureAtlasBuilderError> {
192-
let initial_width = self.initial_size.x as u32;
193-
let initial_height = self.initial_size.y as u32;
194-
let max_width = self.max_size.x as u32;
195-
let max_height = self.max_size.y as u32;
192+
let initial_width = self.initial_size.x;
193+
let initial_height = self.initial_size.y;
194+
let max_width = self.max_size.x;
195+
let max_height = self.max_size.y;
196196

197197
let mut current_width = initial_width;
198198
let mut current_height = initial_height;
@@ -265,16 +265,16 @@ impl<'a> TextureAtlasBuilder<'a> {
265265
for (index, (image_id, texture)) in self.textures_to_place.iter().enumerate() {
266266
let (_, packed_location) = rect_placements.packed_locations().get(&index).unwrap();
267267

268-
let min = Vec2::new(packed_location.x() as f32, packed_location.y() as f32);
268+
let min = UVec2::new(packed_location.x(), packed_location.y());
269269
let max = min
270-
+ Vec2::new(
271-
(packed_location.width() - self.padding.x) as f32,
272-
(packed_location.height() - self.padding.y) as f32,
270+
+ UVec2::new(
271+
packed_location.width() - self.padding.x,
272+
packed_location.height() - self.padding.y,
273273
);
274274
if let Some(image_id) = image_id {
275275
texture_ids.insert(*image_id, index);
276276
}
277-
texture_rects.push(Rect { min, max });
277+
texture_rects.push(URect { min, max });
278278
if texture.texture_descriptor.format != self.format && !self.auto_format_conversion {
279279
warn!(
280280
"Loading a texture of format '{:?}' in an atlas with format '{:?}'",

crates/bevy_text/src/glyph_brush.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl GlyphBrush {
123123

124124
let texture_atlas = texture_atlases.get(&atlas_info.texture_atlas).unwrap();
125125
let glyph_rect = texture_atlas.textures[atlas_info.glyph_index];
126-
let size = Vec2::new(glyph_rect.width(), glyph_rect.height());
126+
let size = glyph_rect.size().as_vec2();
127127

128128
let x = bounds.min.x + size.x / 2.0 - text_bounds.min.x;
129129

crates/bevy_text/src/text2d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub fn extract_text2d_sprite(
136136
ExtractedSprite {
137137
transform: transform * GlobalTransform::from_translation(position.extend(0.)),
138138
color,
139-
rect: Some(atlas.textures[atlas_info.glyph_index]),
139+
rect: Some(atlas.textures[atlas_info.glyph_index].as_rect()),
140140
custom_size: None,
141141
image_handle_id: atlas_info.texture.id(),
142142
flip_x: false,

crates/bevy_ui/src/render/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ pub fn extract_uinodes(
418418
// Atlas not present in assets resource (should this warn the user?)
419419
continue;
420420
};
421-
let mut atlas_rect = layout.textures[atlas.index];
421+
let mut atlas_rect = layout.textures[atlas.index].as_rect();
422422
let mut atlas_size = layout.size.as_vec2();
423423
let scale = uinode.size() / atlas_rect.size();
424424
atlas_rect.min *= scale;
@@ -595,7 +595,7 @@ pub fn extract_text_uinodes(
595595
}
596596
let atlas = texture_atlases.get(&atlas_info.texture_atlas).unwrap();
597597

598-
let mut rect = atlas.textures[atlas_info.glyph_index];
598+
let mut rect = atlas.textures[atlas_info.glyph_index].as_rect();
599599
rect.min *= inverse_scale_factor;
600600
rect.max *= inverse_scale_factor;
601601
extracted_uinodes.uinodes.insert(

crates/bevy_ui/src/widget/image.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{measurement::AvailableSpace, ContentSize, Measure, Node, UiImage, UiScale};
22
use bevy_asset::Assets;
33
use bevy_ecs::prelude::*;
4-
use bevy_math::Vec2;
4+
use bevy_math::{UVec2, Vec2};
55
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
66
use bevy_render::texture::Image;
77
use bevy_sprite::{TextureAtlas, TextureAtlasLayout};
@@ -16,12 +16,12 @@ pub struct UiImageSize {
1616
/// The size of the image's texture
1717
///
1818
/// This field is updated automatically by [`update_image_content_size_system`]
19-
size: Vec2,
19+
size: UVec2,
2020
}
2121

2222
impl UiImageSize {
2323
/// The size of the image's texture
24-
pub fn size(&self) -> Vec2 {
24+
pub fn size(&self) -> UVec2 {
2525
self.size
2626
}
2727
}
@@ -92,7 +92,7 @@ pub fn update_image_content_size_system(
9292
for (mut content_size, image, mut image_size, atlas_image) in &mut query {
9393
if let Some(size) = match atlas_image {
9494
Some(atlas) => atlas.texture_rect(&atlases).map(|t| t.size()),
95-
None => textures.get(&image.texture).map(|t| t.size_f32()),
95+
None => textures.get(&image.texture).map(|t| t.size()),
9696
} {
9797
// Update only if size or scale factor has changed to avoid needless layout calculations
9898
if size != image_size.size
@@ -102,7 +102,7 @@ pub fn update_image_content_size_system(
102102
image_size.size = size;
103103
content_size.set(ImageMeasure {
104104
// multiply the image size by the scale factor to get the physical size
105-
size: size * combined_scale_factor,
105+
size: size.as_vec2() * combined_scale_factor,
106106
});
107107
}
108108
}

0 commit comments

Comments
 (0)