bevy/crates
mgi388 0756a19f28
Support texture atlases in CustomCursor::Image (#17121)
# Objective

- Bevy 0.15 added support for custom cursor images in
https://github.com/bevyengine/bevy/pull/14284.
- However, to do animated cursors using the initial support shipped in
0.15 means you'd have to animate the `Handle<Image>`: You can't use a
`TextureAtlas` like you can with sprites and UI images.
- For my use case, my cursors are spritesheets. To animate them, I'd
have to break them down into multiple `Image` assets, but that seems
less than ideal.


## Solution

- Allow users to specify a `TextureAtlas` field when creating a custom
cursor image.
- To create parity with Bevy's `TextureAtlas` support on `Sprite`s and
`ImageNode`s, this also allows users to specify `rect`, `flip_x` and
`flip_y`. In fact, for my own use case, I need to `flip_y`.

## Testing

- I added unit tests for `calculate_effective_rect` and
`extract_and_transform_rgba_pixels`.
- I added a brand new example for custom cursor images. It has controls
to toggle fields on and off. I opted to add a new example because the
existing cursor example (`window_settings`) would be far too messy for
showcasing these custom cursor features (I did start down that path but
decided to stop and make a brand new example).
- The new example uses a [Kenny cursor icon] sprite sheet. I included
the licence even though it's not required (and it's CC0).
- I decided to make the example just loop through all cursor icons for
its animation even though it's not a _realistic_ in-game animation
sequence.
- I ran the PNG through https://tinypng.com. Looks like it's about 35KB.
- I'm open to adjusting the example spritesheet if required, but if it's
fine as is, great.

[Kenny cursor icon]: https://kenney-assets.itch.io/crosshair-pack

---

## Showcase


https://github.com/user-attachments/assets/8f6be8d7-d1d4-42f9-b769-ef8532367749

## Migration Guide

The `CustomCursor::Image` enum variant has some new fields. Update your
code to set them.

Before:

```rust
CustomCursor::Image {
    handle: asset_server.load("branding/icon.png"),
    hotspot: (128, 128),
}
```

After:

```rust
CustomCursor::Image {
    handle: asset_server.load("branding/icon.png"),
    texture_atlas: None,
    flip_x: false,
    flip_y: false,
    rect: None,
    hotspot: (128, 128),
}
```

## References

- Feature request [originally raised in Discord].

[originally raised in Discord]:
https://discord.com/channels/691052431525675048/692572690833473578/1319836362219847681
2025-01-14 22:27:24 +00:00
..
bevy_a11y Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_animation Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_app Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_asset Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_audio Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_color Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_core_pipeline bevy_core_pipeline: Apply #![warn(clippy::allow_attributes, clippy::allow_attributes_without_reason)] (#17137) 2025-01-14 21:33:28 +00:00
bevy_derive Bump Version after Release (#17176) 2025-01-06 00:04:44 +00:00
bevy_dev_tools Allow users to customize history length in FrameTimeDiagnosticsPlugin (#17259) 2025-01-12 18:18:14 +00:00
bevy_diagnostic Diagnostics smoothing factor fix (#17354) 2025-01-14 01:13:24 +00:00
bevy_dylib bevy_dylib: Apply #![warn(clippy::allow_attributes, clippy::allow_attributes_without_reason)] (#17332) 2025-01-14 21:33:41 +00:00
bevy_ecs Make ObservedBy public (#17297) 2025-01-14 21:53:42 +00:00
bevy_encase_derive Bump Version after Release (#17176) 2025-01-06 00:04:44 +00:00
bevy_gilrs Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_gizmos Use multi_draw_indirect_count where available, in preparation for two-phase occlusion culling. (#17211) 2025-01-14 21:19:20 +00:00
bevy_gltf Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_hierarchy Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_image Support texture atlases in CustomCursor::Image (#17121) 2025-01-14 22:27:24 +00:00
bevy_input Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_input_focus bevy_input_focus: Apply #![warn(clippy::allow_attributes, clippy::allow_attributes_without_reason)] (#17323) 2025-01-14 21:34:01 +00:00
bevy_internal Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_log Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_macro_utils bevy_macro_utils: Apply #![warn(clippy::allow_attributes, clippy::allow_attributes_without_reason)] (#17304) 2025-01-14 21:34:12 +00:00
bevy_math Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_mesh Create missing constructors for 2D primitive mesh builders (#17291) 2025-01-14 19:38:45 +00:00
bevy_mikktspace Bump Version after Release (#17176) 2025-01-06 00:04:44 +00:00
bevy_pbr Fix some punctuation (#17368) 2025-01-14 21:51:12 +00:00
bevy_picking Rename PickingBehavior to Pickable (#17266) 2025-01-12 05:36:52 +00:00
bevy_ptr Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_reflect Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_remote Add BRP method to mutate a component (#16940) 2025-01-14 07:55:40 +00:00
bevy_render Fix some punctuation (#17368) 2025-01-14 21:51:12 +00:00
bevy_scene Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_sprite Use multi_draw_indirect_count where available, in preparation for two-phase occlusion culling. (#17211) 2025-01-14 21:19:20 +00:00
bevy_state Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_tasks Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_text update_text2d_layout creates new font atlases when the primary window is closed (#7849) 2025-01-14 01:01:31 +00:00
bevy_time Fix bevy_time tests occasionally failing on optimised Windows builds (#17349) 2025-01-14 00:30:07 +00:00
bevy_transform Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_ui Use multi_draw_indirect_count where available, in preparation for two-phase occlusion culling. (#17211) 2025-01-14 21:19:20 +00:00
bevy_utils Downgrade clippy::allow_attributes and clippy::allow_attributes_without_reason to warn (#17320) 2025-01-12 05:28:26 +00:00
bevy_window feat: support for clip children on windows (#16545) 2025-01-14 22:22:20 +00:00
bevy_winit Support texture atlases in CustomCursor::Image (#17121) 2025-01-14 22:27:24 +00:00