Skip to content

Commit

Permalink
Pass mutable references instead of consuming and deprecate finish.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-puig committed May 30, 2024
1 parent c97f614 commit 057d027
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
24 changes: 18 additions & 6 deletions crates/bevy_sprite/src/texture_atlas_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,25 @@ pub type TextureAtlasBuilderResult<T> = Result<T, TextureAtlasBuilderError>;

impl<'a> TextureAtlasBuilder<'a> {
/// Sets the initial size of the atlas in pixels.
pub fn initial_size(mut self, size: UVec2) -> Self {
pub fn initial_size(&mut self, size: UVec2) -> &mut Self {
self.initial_size = size;
self
}

/// Sets the max size of the atlas in pixels.
pub fn max_size(mut self, size: UVec2) -> Self {
pub fn max_size(&mut self, size: UVec2) -> &mut Self {
self.max_size = size;
self
}

/// Sets the texture format for textures in the atlas.
pub fn format(mut self, format: TextureFormat) -> Self {
pub fn format(&mut self, format: TextureFormat) -> &mut Self {
self.format = format;
self
}

/// Control whether the added texture should be converted to the atlas format, if different.
pub fn auto_format_conversion(mut self, auto_format_conversion: bool) -> Self {
pub fn auto_format_conversion(&mut self, auto_format_conversion: bool) -> &mut Self {
self.auto_format_conversion = auto_format_conversion;
self
}
Expand All @@ -86,15 +86,19 @@ impl<'a> TextureAtlasBuilder<'a> {
///
/// Optionally an asset id can be passed that can later be used with the texture layout to retrieve the index of this texture.
/// The insertion order will reflect the index of the added texture in the finished texture atlas.
pub fn add_texture(mut self, image_id: Option<AssetId<Image>>, texture: &'a Image) -> Self {
pub fn add_texture(
&mut self,
image_id: Option<AssetId<Image>>,
texture: &'a Image,
) -> &mut Self {
self.textures_to_place.push((image_id, texture));
self
}

/// Sets the amount of padding in pixels to add between the textures in the texture atlas.
///
/// The `x` value provide will be added to the right edge, while the `y` value will be added to the bottom edge.
pub fn padding(mut self, padding: UVec2) -> Self {
pub fn padding(&mut self, padding: UVec2) -> &mut Self {
self.padding = padding;
self
}
Expand Down Expand Up @@ -149,6 +153,14 @@ impl<'a> TextureAtlasBuilder<'a> {
}
}

#[deprecated(
since = "0.14.0",
note = "TextureAtlasBuilder::finish() was not idiomatic. Use TextureAtlasBuilder::build() instead."
)]
pub fn finish(self) -> Result<(TextureAtlasLayout, Image), TextureAtlasBuilderError> {
self.build()
}

/// Consumes the builder, and returns the newly created texture atlas and
/// the associated atlas layout.
///
Expand Down
6 changes: 3 additions & 3 deletions examples/2d/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ fn create_texture_atlas(
textures: &mut ResMut<Assets<Image>>,
) -> (TextureAtlasLayout, Handle<Image>) {
// Build a texture atlas using the individual sprites
let mut texture_atlas_builder =
TextureAtlasBuilder::default().padding(padding.unwrap_or_default());
let mut texture_atlas_builder = TextureAtlasBuilder::default();
texture_atlas_builder.padding(padding.unwrap_or_default());
for handle in folder.handles.iter() {
let id = handle.id().typed_unchecked::<Image>();
let Some(texture) = textures.get(id) else {
Expand All @@ -219,7 +219,7 @@ fn create_texture_atlas(
continue;
};

texture_atlas_builder = texture_atlas_builder.add_texture(Some(id), texture);
texture_atlas_builder.add_texture(Some(id), texture);
}

let (texture_atlas_layout, texture) = texture_atlas_builder.build().unwrap();
Expand Down

0 comments on commit 057d027

Please sign in to comment.