bevy/crates/bevy_pbr/src/render
Joona Aalto e5dc177b4b
Rename Trigger to On (#19596)
# Objective

Currently, the observer API looks like this:

```rust
app.add_observer(|trigger: Trigger<Explode>| {
    info!("Entity {} exploded!", trigger.target());
});
```

Future plans for observers also include "multi-event observers" with a
trigger that looks like this (see [Cart's
example](https://github.com/bevyengine/bevy/issues/14649#issuecomment-2960402508)):

```rust
trigger: Trigger<(
    OnAdd<Pressed>,
    OnRemove<Pressed>,
    OnAdd<InteractionDisabled>,
    OnRemove<InteractionDisabled>,
    OnInsert<Hovered>,
)>,
```

In scenarios like this, there is a lot of repetition of `On`. These are
expected to be very high-traffic APIs especially in UI contexts, so
ergonomics and readability are critical.

By renaming `Trigger` to `On`, we can make these APIs read more cleanly
and get rid of the repetition:

```rust
app.add_observer(|trigger: On<Explode>| {
    info!("Entity {} exploded!", trigger.target());
});
```

```rust
trigger: On<(
    Add<Pressed>,
    Remove<Pressed>,
    Add<InteractionDisabled>,
    Remove<InteractionDisabled>,
    Insert<Hovered>,
)>,
```

Names like `On<Add<Pressed>>` emphasize the actual event listener nature
more than `Trigger<OnAdd<Pressed>>`, and look cleaner. This *also* frees
up the `Trigger` name if we want to use it for the observer event type,
splitting them out from buffered events (bikeshedding this is out of
scope for this PR though).

For prior art:
[`bevy_eventlistener`](https://github.com/aevyrie/bevy_eventlistener)
used
[`On`](https://docs.rs/bevy_eventlistener/latest/bevy_eventlistener/event_listener/struct.On.html)
for its event listener type. Though in our case, the observer is the
event listener, and `On` is just a type containing information about the
triggered event.

## Solution

Steal from `bevy_event_listener` by @aevyrie and use `On`.

- Rename `Trigger` to `On`
- Rename `OnAdd` to `Add`
- Rename `OnInsert` to `Insert`
- Rename `OnReplace` to `Replace`
- Rename `OnRemove` to `Remove`
- Rename `OnDespawn` to `Despawn`

## Discussion

### Naming Conflicts??

Using a name like `Add` might initially feel like a very bad idea, since
it risks conflict with `core::ops::Add`. However, I don't expect this to
be a big problem in practice.

- You rarely need to actually implement the `Add` trait, especially in
modules that would use the Bevy ECS.
- In the rare cases where you *do* get a conflict, it is very easy to
fix by just disambiguating, for example using `ops::Add`.
- The `Add` event is a struct while the `Add` trait is a trait (duh), so
the compiler error should be very obvious.

For the record, renaming `OnAdd` to `Add`, I got exactly *zero* errors
or conflicts within Bevy itself. But this is of course not entirely
representative of actual projects *using* Bevy.

You might then wonder, why not use `Added`? This would conflict with the
`Added` query filter, so it wouldn't work. Additionally, the current
naming convention for observer events does not use past tense.

### Documentation

This does make documentation slightly more awkward when referring to
`On` or its methods. Previous docs often referred to `Trigger::target`
or "sends a `Trigger`" (which is... a bit strange anyway), which would
now be `On::target` and "sends an observer `Event`".

You can see the diff in this PR to see some of the effects. I think it
should be fine though, we may just need to reword more documentation to
read better.
2025-06-12 18:22:33 +00:00
..
build_indirect_params.wgsl Split out the IndirectParametersMetadata into CPU-populated and GPU-populated buffers. (#17863) 2025-02-18 00:53:44 +00:00
clustered_forward.wgsl Implement basic clustered decal projectors. (#17315) 2025-01-26 20:13:39 +00:00
fog.rs Remove Shader weak_handles from bevy_pbr (excluding meshlets). (#19365) 2025-05-27 22:32:47 +00:00
fog.wgsl
forward_io.wgsl
gpu_preprocess.rs Remove Shader weak_handles from bevy_pbr (excluding meshlets). (#19365) 2025-05-27 22:32:47 +00:00
light.rs Rename Trigger to On (#19596) 2025-06-12 18:22:33 +00:00
mesh_bindings.rs Allow clippy::too_many_arguments to lint without warnings (#17249) 2025-01-09 07:26:15 +00:00
mesh_bindings.wgsl
mesh_functions.wgsl Helper function for getting inverse model matrix in WGSL shaders (#10462) 2025-03-06 16:43:22 +00:00
mesh_preprocess.wgsl Fix false positive GPU frustum culling (#17939) 2025-02-20 05:35:21 +00:00
mesh_types.wgsl Retain skins from frame to frame. (#17818) 2025-02-18 00:56:04 +00:00
mesh_view_bindings.rs Partially fix panics when setting WGPU_SETTINGS_PRIO=webgl2 (#18113) 2025-03-09 20:14:27 +00:00
mesh_view_bindings.wgsl Implement basic clustered decal projectors. (#17315) 2025-01-26 20:13:39 +00:00
mesh_view_types.wgsl fix distinct directional lights per view (#19147) 2025-05-30 19:35:55 +00:00
mesh.rs Remove Shader weak_handles from bevy_pbr (excluding meshlets). (#19365) 2025-05-27 22:32:47 +00:00
mesh.wgsl
mod.rs Expose symbols needed to replicate SetMeshBindGroup in ecosystem crates. (#18613) 2025-03-31 18:27:40 +00:00
morph.rs Expose symbols needed to replicate SetMeshBindGroup in ecosystem crates. (#18613) 2025-03-31 18:27:40 +00:00
morph.wgsl
occlusion_culling.wgsl Implement experimental GPU two-phase occlusion culling for the standard 3D mesh pipeline. (#17413) 2025-01-27 05:02:46 +00:00
parallax_mapping.wgsl Use global binding arrays for bindless resources. (#17898) 2025-02-21 05:55:36 +00:00
pbr_ambient.wgsl Revert "Replace Ambient Lights with Environment Map Lights (#17482)" (#18167) 2025-03-05 23:08:46 +00:00
pbr_bindings.wgsl Make the StandardMaterial bindless index table have a fixed size regardless of the features that are enabled. (#18771) 2025-04-09 20:39:42 +00:00
pbr_fragment.wgsl Use global binding arrays for bindless resources. (#17898) 2025-02-21 05:55:36 +00:00
pbr_functions.wgsl fix distinct directional lights per view (#19147) 2025-05-30 19:35:55 +00:00
pbr_lighting.wgsl Fix specular cutoff on lights with radius overlapping with mesh (#19157) 2025-05-12 19:14:13 +00:00
pbr_prepass_functions.wgsl Fix shader pre-pass compile failure when using AlphaMode::Blend and a Mesh without UVs (0.16.0-rc.2) (#18602) 2025-03-30 01:25:42 +00:00
pbr_prepass.wgsl Add missing bindless imports in pbr_prepass.wgsl. (#18110) 2025-03-02 09:12:18 +00:00
pbr_transmission.wgsl Update typos (#17126) 2025-01-03 17:44:26 +00:00
pbr_types.wgsl Add support for specular tints and maps per the KHR_materials_specular glTF extension. (#14069) 2025-01-26 20:38:46 +00:00
pbr.wgsl Implement basic clustered decal projectors. (#17315) 2025-01-26 20:13:39 +00:00
reset_indirect_batch_sets.wgsl Implement experimental GPU two-phase occlusion culling for the standard 3D mesh pipeline. (#17413) 2025-01-27 05:02:46 +00:00
rgb9e5.wgsl
shadow_sampling.wgsl Upgrade to wgpu v24 (#17542) 2025-02-09 19:40:53 +00:00
shadows.wgsl
skin.rs Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00
skinning.wgsl Fix motion vector computation after #17688. (#17717) 2025-02-18 09:34:19 +00:00
utils.wgsl
view_transformations.wgsl Implement experimental GPU two-phase occlusion culling for the standard 3D mesh pipeline. (#17413) 2025-01-27 05:02:46 +00:00
wireframe.wgsl Add binned 2d/3d Wireframe render phase (#18587) 2025-04-09 21:34:53 +00:00