
# Objective - Make the function signature for `ComponentHook` less verbose ## Solution - Refactored `Entity`, `ComponentId`, and `Option<&Location>` into a new `HookContext` struct. ## Testing - CI --- ## Migration Guide Update the function signatures for your component hooks to only take 2 arguments, `world` and `context`. Note that because `HookContext` is plain data with all members public, you can use de-structuring to simplify migration. ```rust // Before fn my_hook( mut world: DeferredWorld, entity: Entity, component_id: ComponentId, ) { ... } // After fn my_hook( mut world: DeferredWorld, HookContext { entity, component_id, caller }: HookContext, ) { ... } ``` Likewise, if you were discarding certain parameters, you can use `..` in the de-structuring: ```rust // Before fn my_hook( mut world: DeferredWorld, entity: Entity, _: ComponentId, ) { ... } // After fn my_hook( mut world: DeferredWorld, HookContext { entity, .. }: HookContext, ) { ... } ```
31 lines
981 B
Rust
31 lines
981 B
Rust
//! Contains the [`AutoFocus`] component and related machinery.
|
|
|
|
use bevy_ecs::{component::HookContext, prelude::*, world::DeferredWorld};
|
|
|
|
use crate::InputFocus;
|
|
|
|
#[cfg(feature = "bevy_reflect")]
|
|
use bevy_reflect::{prelude::*, Reflect};
|
|
|
|
/// Indicates that this widget should automatically receive [`InputFocus`].
|
|
///
|
|
/// This can be useful for things like dialog boxes, the first text input in a form,
|
|
/// or the first button in a game menu.
|
|
///
|
|
/// The focus is swapped when this component is added
|
|
/// or an entity with this component is spawned.
|
|
#[derive(Debug, Default, Component, Copy, Clone)]
|
|
#[cfg_attr(
|
|
feature = "bevy_reflect",
|
|
derive(Reflect),
|
|
reflect(Debug, Default, Component)
|
|
)]
|
|
#[component(on_add = on_auto_focus_added)]
|
|
pub struct AutoFocus;
|
|
|
|
fn on_auto_focus_added(mut world: DeferredWorld, HookContext { entity, .. }: HookContext) {
|
|
if let Some(mut input_focus) = world.get_resource_mut::<InputFocus>() {
|
|
input_focus.set(entity);
|
|
}
|
|
}
|