bevy/examples/2d
jeliag f6b40a6e43
Multiple Configurations for Gizmos (#10342)
# Objective

This PR aims to implement multiple configs for gizmos as discussed in
#9187.

## Solution

Configs for the new `GizmoConfigGroup`s are stored in a
`GizmoConfigStore` resource and can be accesses using a type based key
or iterated over. This type based key doubles as a standardized location
where plugin authors can put their own configuration not covered by the
standard `GizmoConfig` struct. For example the `AabbGizmoGroup` has a
default color and toggle to show all AABBs. New configs can be
registered using `app.init_gizmo_group::<T>()` during startup.

When requesting the `Gizmos<T>` system parameter the generic type
determines which config is used. The config structs are available
through the `Gizmos` system parameter allowing for easy access while
drawing your gizmos.

Internally, resources and systems used for rendering (up to an including
the extract system) are generic over the type based key and inserted on
registering a new config.

## Alternatives

The configs could be stored as components on entities with markers which
would make better use of the ECS. I also implemented this approach
([here](https://github.com/jeliag/bevy/tree/gizmo-multiconf-comp)) and
believe that the ergonomic benefits of a central config store outweigh
the decreased use of the ECS.

## Unsafe Code

Implementing system parameter by hand is unsafe but seems to be required
to access the config store once and not on every gizmo draw function
call. This is critical for performance. ~Is there a better way to do
this?~

## Future Work

New gizmos (such as #10038, and ideas from #9400) will require custom
configuration structs. Should there be a new custom config for every
gizmo type, or should we group them together in a common configuration?
(for example `EditorGizmoConfig`, or something more fine-grained)

## Changelog

- Added `GizmoConfigStore` resource and `GizmoConfigGroup` trait
- Added `init_gizmo_group` to `App`
- Added early returns to gizmo drawing increasing performance when
gizmos are disabled
- Changed `GizmoConfig` and aabb gizmos to use new `GizmoConfigStore`
- Changed `Gizmos` system parameter to use type based key to retrieve
config
- Changed resources and systems used for gizmo rendering to be generic
over type based key
- Changed examples (3d_gizmos, 2d_gizmos) to showcase new API

## Migration Guide

- `GizmoConfig` is no longer a resource and has to be accessed through
`GizmoConfigStore` resource. The default config group is
`DefaultGizmoGroup`, but consider using your own custom config group if
applicable.

---------

Co-authored-by: Nicola Papale <nicopap@users.noreply.github.com>
2024-01-18 15:52:50 +00:00
..
2d_gizmos.rs Multiple Configurations for Gizmos (#10342) 2024-01-18 15:52:50 +00:00
2d_shapes.rs Fix missed explicit conversions in examples (#11261) 2024-01-09 00:44:24 +00:00
2d_viewport_to_world.rs Add example for Camera::viewport_to_world (#7179) 2023-09-11 18:52:11 +00:00
bloom_2d.rs Fix missed explicit conversions in examples (#11261) 2024-01-09 00:44:24 +00:00
custom_gltf_vertex_attribute.rs Reorder impl to be the same as the trait (#11076) 2023-12-24 17:43:55 +00:00
mesh2d.rs Fix missed explicit conversions in examples (#11261) 2024-01-09 00:44:24 +00:00
mesh2d_manual.rs Unload render assets from RAM (#10520) 2024-01-03 03:31:04 +00:00
mesh2d_vertex_color_texture.rs Fix missed explicit conversions in examples (#11261) 2024-01-09 00:44:24 +00:00
move_sprite.rs Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
pixel_grid_snap.rs Fix missed explicit conversions in examples (#11261) 2024-01-09 00:44:24 +00:00
rotation.rs Update winit dependency to 0.29 (#10702) 2023-12-21 07:40:47 +00:00
sprite.rs Update color and naming for consistency (#10367) 2023-11-04 02:09:23 +00:00
sprite_flipping.rs Update color and naming for consistency (#10367) 2023-11-04 02:09:23 +00:00
sprite_sheet.rs Texture Atlas rework (#5103) 2024-01-16 13:59:08 +00:00
sprite_slice.rs Sprite slicing and tiling (#10588) 2024-01-15 15:40:06 +00:00
sprite_tile.rs Sprite slicing and tiling (#10588) 2024-01-15 15:40:06 +00:00
text2d.rs Rename TextAlignment to JustifyText. (#10854) 2023-12-05 03:00:41 +00:00
texture_atlas.rs Texture Atlas rework (#5103) 2024-01-16 13:59:08 +00:00
transparency_2d.rs Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00