Skip to content

Commit 3578f9e

Browse files
authored
Reflect bevy_input_focus (#17212)
# Objective Fixes #17099. ## Solution Derive, register, and feature flag. ## Testing Ran CI.
1 parent 8baf4e5 commit 3578f9e

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

crates/bevy_input_focus/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ license = "MIT OR Apache-2.0"
99
keywords = ["bevy"]
1010
rust-version = "1.83.0"
1111

12+
[features]
13+
default = ["bevy_reflect"]
14+
15+
## Adds runtime reflection support using `bevy_reflect`.
16+
bevy_reflect = [
17+
"dep:bevy_reflect",
18+
"bevy_app/bevy_reflect",
19+
"bevy_ecs/bevy_reflect",
20+
"bevy_math/bevy_reflect",
21+
]
22+
1223
[dependencies]
1324
# bevy
1425
bevy_app = { path = "../bevy_app", version = "0.16.0-dev", default-features = false }
@@ -17,6 +28,9 @@ bevy_input = { path = "../bevy_input", version = "0.16.0-dev", default-features
1728
bevy_hierarchy = { path = "../bevy_hierarchy", version = "0.16.0-dev", default-features = false }
1829
bevy_math = { path = "../bevy_math", version = "0.16.0-dev", default-features = false }
1930
bevy_window = { path = "../bevy_window", version = "0.16.0-dev", default-features = false }
31+
bevy_reflect = { path = "../bevy_reflect", version = "0.16.0-dev", features = [
32+
"glam",
33+
], default-features = false, optional = true }
2034

2135
# other
2236
thiserror = { version = "2", default-features = false }

crates/bevy_input_focus/src/autofocus.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Contains the [`AutoFocus`] component and related machinery.
22
33
use bevy_ecs::{component::ComponentId, prelude::*, world::DeferredWorld};
4+
#[cfg(feature = "bevy_reflect")]
5+
use bevy_reflect::{prelude::*, Reflect};
46

57
use crate::InputFocus;
68

@@ -12,6 +14,11 @@ use crate::InputFocus;
1214
/// The focus is swapped when this component is added
1315
/// or an entity with this component is spawned.
1416
#[derive(Debug, Default, Component, Copy, Clone)]
17+
#[cfg_attr(
18+
feature = "bevy_reflect",
19+
derive(Reflect),
20+
reflect(Debug, Default, Component)
21+
)]
1522
#[component(on_add = on_auto_focus_added)]
1623
pub struct AutoFocus;
1724

crates/bevy_input_focus/src/directional_navigation.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use bevy_ecs::{
2222
system::SystemParam,
2323
};
2424
use bevy_math::CompassOctant;
25+
#[cfg(feature = "bevy_reflect")]
26+
use bevy_reflect::{prelude::*, Reflect};
2527
use thiserror::Error;
2628

2729
use crate::InputFocus;
@@ -33,11 +35,20 @@ pub struct DirectionalNavigationPlugin;
3335
impl Plugin for DirectionalNavigationPlugin {
3436
fn build(&self, app: &mut App) {
3537
app.init_resource::<DirectionalNavigationMap>();
38+
39+
#[cfg(feature = "bevy_reflect")]
40+
app.register_type::<NavNeighbors>()
41+
.register_type::<DirectionalNavigationMap>();
3642
}
3743
}
3844

3945
/// The up-to-eight neighbors of a focusable entity, one for each [`CompassOctant`].
4046
#[derive(Default, Debug, Clone, PartialEq)]
47+
#[cfg_attr(
48+
feature = "bevy_reflect",
49+
derive(Reflect),
50+
reflect(Default, Debug, PartialEq)
51+
)]
4152
pub struct NavNeighbors {
4253
/// The array of neighbors, one for each [`CompassOctant`].
4354
/// The mapping between array elements and directions is determined by [`CompassOctant::to_index`].
@@ -79,6 +90,11 @@ impl NavNeighbors {
7990
///
8091
/// For now, this graph must be built manually, and the developer is responsible for ensuring that it meets the above criteria.
8192
#[derive(Resource, Debug, Default, Clone, PartialEq)]
93+
#[cfg_attr(
94+
feature = "bevy_reflect",
95+
derive(Reflect),
96+
reflect(Resource, Debug, Default, PartialEq)
97+
)]
8298
pub struct DirectionalNavigationMap {
8399
/// A directed graph of focusable entities.
84100
///

crates/bevy_input_focus/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ use bevy_app::{App, Plugin, PreUpdate, Startup};
2828
use bevy_ecs::{prelude::*, query::QueryData, system::SystemParam, traversal::Traversal};
2929
use bevy_hierarchy::{HierarchyQueryExt, Parent};
3030
use bevy_input::{gamepad::GamepadButtonChangedEvent, keyboard::KeyboardInput, mouse::MouseWheel};
31+
#[cfg(feature = "bevy_reflect")]
32+
use bevy_reflect::{prelude::*, Reflect};
3133
use bevy_window::{PrimaryWindow, Window};
3234
use core::fmt::Debug;
3335

@@ -69,6 +71,11 @@ use core::fmt::Debug;
6971
/// }
7072
/// ```
7173
#[derive(Clone, Debug, Default, Resource)]
74+
#[cfg_attr(
75+
feature = "bevy_reflect",
76+
derive(Reflect),
77+
reflect(Debug, Default, Resource)
78+
)]
7279
pub struct InputFocus(pub Option<Entity>);
7380

7481
impl InputFocus {
@@ -108,6 +115,7 @@ impl InputFocus {
108115
///
109116
/// To easily access information about whether focus indicators should be shown for a given entity, use the [`IsFocused`] trait.
110117
#[derive(Clone, Debug, Resource)]
118+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, Resource))]
111119
pub struct InputFocusVisible(pub bool);
112120

113121
/// A bubble-able user input event that starts at the currently focused entity.
@@ -118,6 +126,7 @@ pub struct InputFocusVisible(pub bool);
118126
/// To set up your own bubbling input event, add the [`dispatch_focused_input::<MyEvent>`](dispatch_focused_input) system to your app,
119127
/// in the [`InputFocusSet::Dispatch`] system set during [`PreUpdate`].
120128
#[derive(Clone, Debug, Component)]
129+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Component))]
121130
pub struct FocusedInput<E: Event + Clone> {
122131
/// The underlying input event.
123132
pub input: E,
@@ -176,6 +185,11 @@ impl Plugin for InputDispatchPlugin {
176185
)
177186
.in_set(InputFocusSet::Dispatch),
178187
);
188+
189+
#[cfg(feature = "bevy_reflect")]
190+
app.register_type::<AutoFocus>()
191+
.register_type::<InputFocus>()
192+
.register_type::<InputFocusVisible>();
179193
}
180194
}
181195

crates/bevy_input_focus/src/tab_navigation.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
//! This object can be injected into your systems, and provides a [`navigate`](`TabNavigation::navigate`) method which can be
2525
//! used to navigate between focusable entities.
2626
use bevy_app::{App, Plugin, Startup};
27+
#[cfg(feature = "bevy_reflect")]
28+
use bevy_ecs::prelude::ReflectComponent;
2729
use bevy_ecs::{
2830
component::Component,
2931
entity::Entity,
@@ -36,6 +38,8 @@ use bevy_input::{
3638
keyboard::{KeyCode, KeyboardInput},
3739
ButtonInput, ButtonState,
3840
};
41+
#[cfg(feature = "bevy_reflect")]
42+
use bevy_reflect::{prelude::*, Reflect};
3943
use bevy_window::PrimaryWindow;
4044
use thiserror::Error;
4145
use tracing::warn;
@@ -47,10 +51,20 @@ use crate::{FocusedInput, InputFocus, InputFocusVisible};
4751
/// Note that you must also add the [`TabGroup`] component to the entity's ancestor in order
4852
/// for this component to have any effect.
4953
#[derive(Debug, Default, Component, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
54+
#[cfg_attr(
55+
feature = "bevy_reflect",
56+
derive(Reflect),
57+
reflect(Debug, Default, Component, PartialEq)
58+
)]
5059
pub struct TabIndex(pub i32);
5160

5261
/// A component used to mark a tree of entities as containing tabbable elements.
5362
#[derive(Debug, Default, Component, Copy, Clone)]
63+
#[cfg_attr(
64+
feature = "bevy_reflect",
65+
derive(Reflect),
66+
reflect(Debug, Default, Component)
67+
)]
5468
pub struct TabGroup {
5569
/// The order of the tab group relative to other tab groups.
5670
pub order: i32,
@@ -287,6 +301,9 @@ pub struct TabNavigationPlugin;
287301
impl Plugin for TabNavigationPlugin {
288302
fn build(&self, app: &mut App) {
289303
app.add_systems(Startup, setup_tab_navigation);
304+
305+
#[cfg(feature = "bevy_reflect")]
306+
app.register_type::<TabIndex>().register_type::<TabGroup>();
290307
}
291308
}
292309

0 commit comments

Comments
 (0)