More idiomatic texture atlas builder (#13238)
# Objective - TextureAtlasBuilder has some non-idiomatic methods. ## Solution - Refactor non-idiomatic methods --- ## Changelog - Renamed `TextureAtlasBuilder::finish()` to `TextureAtlasBuilder::build()` - Builder methods return `&mut Self` instead of `Self` ## Migration Guide ```diff - let mut texture_atlas_builder = TextureAtlasBuilder::default().padding(UVec2::default()).format(..); + let mut texture_atlas_builder = TextureAtlasBuilder::default(); + texture_atlas_builder.padding(UVec2::default()).format(..); - let (texture_atlas_layout, texture) = texture_atlas_builder.finish().unwrap(); + let (texture_atlas_layout, texture) = texture_atlas_builder.build().unwrap(); ```
This commit is contained in:
parent
5536079945
commit
21b3666abf
@ -59,25 +59,25 @@ pub type TextureAtlasBuilderResult<T> = Result<T, TextureAtlasBuilderError>;
|
|||||||
|
|
||||||
impl<'a> TextureAtlasBuilder<'a> {
|
impl<'a> TextureAtlasBuilder<'a> {
|
||||||
/// Sets the initial size of the atlas in pixels.
|
/// 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.initial_size = size;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the max size of the atlas in pixels.
|
/// 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.max_size = size;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the texture format for textures in the atlas.
|
/// 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.format = format;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Control whether the added texture should be converted to the atlas format, if different.
|
/// 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.auto_format_conversion = auto_format_conversion;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -86,14 +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.
|
/// 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.
|
/// 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) {
|
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.textures_to_place.push((image_id, texture));
|
||||||
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the amount of padding in pixels to add between the textures in the texture atlas.
|
/// 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.
|
/// 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.padding = padding;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -148,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(&mut self) -> Result<(TextureAtlasLayout, Image), TextureAtlasBuilderError> {
|
||||||
|
self.build()
|
||||||
|
}
|
||||||
|
|
||||||
/// Consumes the builder, and returns the newly created texture atlas and
|
/// Consumes the builder, and returns the newly created texture atlas and
|
||||||
/// the associated atlas layout.
|
/// the associated atlas layout.
|
||||||
///
|
///
|
||||||
@ -169,7 +182,7 @@ impl<'a> TextureAtlasBuilder<'a> {
|
|||||||
/// // Customize it
|
/// // Customize it
|
||||||
/// // ...
|
/// // ...
|
||||||
/// // Build your texture and the atlas layout
|
/// // Build your texture and the atlas layout
|
||||||
/// let (atlas_layout, texture) = builder.finish().unwrap();
|
/// let (atlas_layout, texture) = builder.build().unwrap();
|
||||||
/// let texture = textures.add(texture);
|
/// let texture = textures.add(texture);
|
||||||
/// let layout = layouts.add(atlas_layout);
|
/// let layout = layouts.add(atlas_layout);
|
||||||
/// // Spawn your sprite
|
/// // Spawn your sprite
|
||||||
@ -184,7 +197,7 @@ impl<'a> TextureAtlasBuilder<'a> {
|
|||||||
///
|
///
|
||||||
/// If there is not enough space in the atlas texture, an error will
|
/// If there is not enough space in the atlas texture, an error will
|
||||||
/// be returned. It is then recommended to make a larger sprite sheet.
|
/// be returned. It is then recommended to make a larger sprite sheet.
|
||||||
pub fn finish(self) -> Result<(TextureAtlasLayout, Image), TextureAtlasBuilderError> {
|
pub fn build(&mut self) -> Result<(TextureAtlasLayout, Image), TextureAtlasBuilderError> {
|
||||||
let max_width = self.max_size.x;
|
let max_width = self.max_size.x;
|
||||||
let max_height = self.max_size.y;
|
let max_height = self.max_size.y;
|
||||||
|
|
||||||
|
@ -207,8 +207,8 @@ fn create_texture_atlas(
|
|||||||
textures: &mut ResMut<Assets<Image>>,
|
textures: &mut ResMut<Assets<Image>>,
|
||||||
) -> (TextureAtlasLayout, Handle<Image>) {
|
) -> (TextureAtlasLayout, Handle<Image>) {
|
||||||
// Build a texture atlas using the individual sprites
|
// Build a texture atlas using the individual sprites
|
||||||
let mut texture_atlas_builder =
|
let mut texture_atlas_builder = TextureAtlasBuilder::default();
|
||||||
TextureAtlasBuilder::default().padding(padding.unwrap_or_default());
|
texture_atlas_builder.padding(padding.unwrap_or_default());
|
||||||
for handle in folder.handles.iter() {
|
for handle in folder.handles.iter() {
|
||||||
let id = handle.id().typed_unchecked::<Image>();
|
let id = handle.id().typed_unchecked::<Image>();
|
||||||
let Some(texture) = textures.get(id) else {
|
let Some(texture) = textures.get(id) else {
|
||||||
@ -222,7 +222,7 @@ fn create_texture_atlas(
|
|||||||
texture_atlas_builder.add_texture(Some(id), texture);
|
texture_atlas_builder.add_texture(Some(id), texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
let (texture_atlas_layout, texture) = texture_atlas_builder.finish().unwrap();
|
let (texture_atlas_layout, texture) = texture_atlas_builder.build().unwrap();
|
||||||
let texture = textures.add(texture);
|
let texture = textures.add(texture);
|
||||||
|
|
||||||
// Update the sampling settings of the texture atlas
|
// Update the sampling settings of the texture atlas
|
||||||
|
Loading…
Reference in New Issue
Block a user