
# 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.
154 lines
6.0 KiB
Rust
154 lines
6.0 KiB
Rust
//! This example illustrates the use of tab navigation.
|
|
|
|
use bevy::{
|
|
color::palettes::basic::*,
|
|
input_focus::{
|
|
tab_navigation::{TabGroup, TabIndex, TabNavigationPlugin},
|
|
InputDispatchPlugin, InputFocus,
|
|
},
|
|
prelude::*,
|
|
winit::WinitSettings,
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins((DefaultPlugins, InputDispatchPlugin, TabNavigationPlugin))
|
|
// Only run the app when there is user input. This will significantly reduce CPU/GPU use.
|
|
.insert_resource(WinitSettings::desktop_app())
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, (button_system, focus_system))
|
|
.run();
|
|
}
|
|
|
|
const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15);
|
|
const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25);
|
|
const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35);
|
|
|
|
fn button_system(
|
|
mut interaction_query: Query<
|
|
(&Interaction, &mut BackgroundColor, &mut BorderColor),
|
|
(Changed<Interaction>, With<Button>),
|
|
>,
|
|
) {
|
|
for (interaction, mut color, mut border_color) in &mut interaction_query {
|
|
match *interaction {
|
|
Interaction::Pressed => {
|
|
*color = PRESSED_BUTTON.into();
|
|
*border_color = BorderColor::all(RED.into());
|
|
}
|
|
Interaction::Hovered => {
|
|
*color = HOVERED_BUTTON.into();
|
|
*border_color = BorderColor::all(Color::WHITE);
|
|
}
|
|
Interaction::None => {
|
|
*color = NORMAL_BUTTON.into();
|
|
*border_color = BorderColor::all(Color::BLACK);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn focus_system(
|
|
mut commands: Commands,
|
|
focus: Res<InputFocus>,
|
|
mut query: Query<Entity, With<Button>>,
|
|
) {
|
|
if focus.is_changed() {
|
|
for button in query.iter_mut() {
|
|
if focus.0 == Some(button) {
|
|
commands.entity(button).insert(Outline {
|
|
color: Color::WHITE,
|
|
width: Val::Px(2.0),
|
|
offset: Val::Px(2.0),
|
|
});
|
|
} else {
|
|
commands.entity(button).remove::<Outline>();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn setup(mut commands: Commands) {
|
|
// ui camera
|
|
commands.spawn(Camera2d);
|
|
commands
|
|
.spawn(Node {
|
|
width: Val::Percent(100.0),
|
|
height: Val::Percent(100.0),
|
|
display: Display::Flex,
|
|
flex_direction: FlexDirection::Column,
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
row_gap: Val::Px(6.0),
|
|
..default()
|
|
})
|
|
.observe(
|
|
|mut trigger: On<Pointer<Click>>, mut focus: ResMut<InputFocus>| {
|
|
focus.0 = None;
|
|
trigger.propagate(false);
|
|
},
|
|
)
|
|
.with_children(|parent| {
|
|
for (label, tab_group, indices) in [
|
|
// In this group all the buttons have the same `TabIndex` so they will be visited according to their order as children.
|
|
("TabGroup 0", TabGroup::new(0), [0, 0, 0, 0]),
|
|
// In this group the `TabIndex`s are reversed so the buttons will be visited in right-to-left order.
|
|
("TabGroup 2", TabGroup::new(2), [3, 2, 1, 0]),
|
|
// In this group the orders of the indices and buttons match so the buttons will be visited in left-to-right order.
|
|
("TabGroup 1", TabGroup::new(1), [0, 1, 2, 3]),
|
|
// Visit the modal group's buttons in an arbitrary order.
|
|
("Modal TabGroup", TabGroup::modal(), [0, 3, 1, 2]),
|
|
] {
|
|
parent.spawn(Text::new(label));
|
|
parent
|
|
.spawn((
|
|
Node {
|
|
display: Display::Flex,
|
|
flex_direction: FlexDirection::Row,
|
|
column_gap: Val::Px(6.0),
|
|
margin: UiRect {
|
|
bottom: Val::Px(10.0),
|
|
..default()
|
|
},
|
|
..default()
|
|
},
|
|
tab_group,
|
|
))
|
|
.with_children(|parent| {
|
|
for i in indices {
|
|
parent
|
|
.spawn((
|
|
Button,
|
|
Node {
|
|
width: Val::Px(200.0),
|
|
height: Val::Px(65.0),
|
|
border: UiRect::all(Val::Px(5.0)),
|
|
justify_content: JustifyContent::Center,
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
},
|
|
BorderColor::all(Color::BLACK),
|
|
BackgroundColor(NORMAL_BUTTON),
|
|
TabIndex(i),
|
|
children![(
|
|
Text::new(format!("TabIndex {}", i)),
|
|
TextFont {
|
|
font_size: 20.0,
|
|
..default()
|
|
},
|
|
TextColor(Color::srgb(0.9, 0.9, 0.9)),
|
|
)],
|
|
))
|
|
.observe(
|
|
|mut trigger: On<Pointer<Click>>,
|
|
mut focus: ResMut<InputFocus>| {
|
|
focus.0 = Some(trigger.target().unwrap());
|
|
trigger.propagate(false);
|
|
},
|
|
);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|