bevy/crates
Clar Fon af9b073b0f
Split TextureAtlasSources out of TextureAtlasLayout and make TextureAtlasLayout serializable (#15344)
# Objective

Mostly covers the first point in
https://github.com/bevyengine/bevy/issues/13713#issuecomment-2364786694

The idea here is that a lot of people want to load their own texture
atlases, and many of them do this by deserializing some custom version
of `TextureAtlasLayout`. This makes that a little easier by providing
`serde` impls for them.

## Solution

In order to make `TextureAtlasLayout` serializable, the custom texture
mappings that are added by `TextureAtlasBuilder` were separated into
their own type, `TextureAtlasSources`. The inner fields are made public
so people can create their own version of this type, although because it
embeds asset IDs, it's not as easily serializable. In particular,
atlases that are loaded directly (e.g. sprite sheets) will not have a
copy of this map, and so, don't need to construct it at all.

As an aside, since this is the very first thing in `bevy_sprite` with
`serde` impls, I've added a `serialize` feature to the crate and made
sure it gets activated when the `serialize` feature is enabled on the
parent `bevy` crate.

## Testing

I was kind of shocked that there isn't anywhere in the code besides a
single example that actually used this functionality, so, it was
relatively straightforward to do.

In #13713, among other places, folks have mentioned adding custom
serialization into their pipelines. It would be nice to hear from people
whether this change matches what they're doing in their code, and if
it's relatively seamless to adapt to. I suspect that the answer is yes,
but, that's mainly the only other kind of testing that can be added.

## Migration Guide

`TextureAtlasBuilder` no longer stores a mapping back to the original
images in `TextureAtlasLayout`; that functionality has been added to a
new struct, `TextureAtlasSources`, instead. This also means that the
signature for `TextureAtlasBuilder::finish` has changed, meaning that
calls of the form:

```rust
let (atlas_layout, image) = builder.build()?;
```

Will now change to the form:

```rust
let (atlas_layout, atlas_sources, image) = builder.build()?;
```

And instead of performing a reverse-lookup from the layout, like so:

```rust
let atlas_layout_handle = texture_atlases.add(atlas_layout.clone());
let index = atlas_layout.get_texture_index(&my_handle);
let handle = TextureAtlas {
    layout: atlas_layout_handle,
    index,
};
```

You can perform the lookup from the sources instead:

```rust
let atlas_layout = texture_atlases.add(atlas_layout);
let index = atlas_sources.get_texture_index(&my_handle);
let handle = TextureAtlas {
    layout: atlas_layout,
    index,
};
```

Additionally, `TextureAtlasSources` also has a convenience method,
`handle`, which directly combines the index and an existing
`TextureAtlasLayout` handle into a new `TextureAtlas`:

```rust
let atlas_layout = texture_atlases.add(atlas_layout);
let handle = atlas_sources.handle(atlas_layout, &my_handle);
```

## Extra notes

In the future, it might make sense to combine the three types returned
by `TextureAtlasBuilder` into their own struct, just so that people
don't need to assign variable names to all three parts. In particular,
when creating a version that can be loaded directly (like #11873), we
could probably use this new type.
2024-09-30 17:11:56 +00:00
..
bevy_a11y Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_animation Simplify AnimatableProperty::Property trait bounds (#15495) 2024-09-28 15:04:00 +00:00
bevy_app Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_asset Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_audio Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_color Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_core Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_core_pipeline Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_derive Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_dev_tools Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_diagnostic Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_dylib Generate links to definition in source code pages on docs.rs and dev-docs.bevyengine.org (#12965) 2024-07-29 23:10:16 +00:00
bevy_ecs Reorganize SystemParamBuilder docs and examples. (#15102) 2024-09-30 16:59:52 +00:00
bevy_encase_derive Update `glam to 0.29, encase` to 0.10. (#15249) 2024-09-23 19:44:02 +00:00
bevy_gilrs Implement gamepads as entities (#12770) 2024-09-27 20:07:20 +00:00
bevy_gizmos Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_gltf feat(gltf): add name component to gltf mesh primitive (#13912) 2024-09-30 16:51:52 +00:00
bevy_hierarchy Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_input Implement gamepads as entities (#12770) 2024-09-27 20:07:20 +00:00
bevy_internal Split TextureAtlasSources out of TextureAtlasLayout and make TextureAtlasLayout serializable (#15344) 2024-09-30 17:11:56 +00:00
bevy_log Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_macro_utils Modify derive_label to support no_std environments (#15465) 2024-09-27 20:23:26 +00:00
bevy_math add more Curve adaptors (#14794) 2024-09-30 16:55:32 +00:00
bevy_mikktspace Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_pbr Implement volumetric fog support for both point lights and spotlights (#15361) 2024-09-29 21:30:53 +00:00
bevy_picking Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_ptr Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_reflect Use HashTable in DynamicMap and fix bug in remove (#15158) 2024-09-30 17:02:10 +00:00
bevy_remote Refactor BRP to allow for 3rd-party transports (#15438) 2024-09-27 20:09:46 +00:00
bevy_render Clear view attachments before resizing window surfaces (#15087) 2024-09-30 16:58:04 +00:00
bevy_scene System param validation for observers, system registry and run once (#15526) 2024-09-30 01:00:39 +00:00
bevy_sprite Split TextureAtlasSources out of TextureAtlasLayout and make TextureAtlasLayout serializable (#15344) 2024-09-30 17:11:56 +00:00
bevy_state Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_tasks bump async-channel to 2.3.0 (#15497) 2024-09-28 19:21:59 +00:00
bevy_text Make CosmicFontSystem and SwashCache pub resources. (#15479) 2024-09-28 00:00:27 +00:00
bevy_time Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_transform Migrate visibility to required components (#15474) 2024-09-27 19:06:16 +00:00
bevy_ui System param validation for observers, system registry and run once (#15526) 2024-09-30 01:00:39 +00:00
bevy_utils Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_window Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00
bevy_winit Add core and alloc over std Lints (#15281) 2024-09-27 00:59:59 +00:00