Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_state/src/reflect.rs
6595 views
1
use crate::state::{FreelyMutableState, NextState, State, States};
2
3
use bevy_ecs::{reflect::from_reflect_with_fallback, world::World};
4
use bevy_reflect::{FromType, Reflect, TypePath, TypeRegistry};
5
6
/// A struct used to operate on the reflected [`States`] trait of a type.
7
///
8
/// A [`ReflectState`] for type `T` can be obtained via
9
/// [`bevy_reflect::TypeRegistration::data`].
10
#[derive(Clone)]
11
pub struct ReflectState(ReflectStateFns);
12
13
/// The raw function pointers needed to make up a [`ReflectState`].
14
#[derive(Clone)]
15
pub struct ReflectStateFns {
16
/// Function pointer implementing [`ReflectState::reflect()`].
17
pub reflect: fn(&World) -> Option<&dyn Reflect>,
18
}
19
20
impl ReflectStateFns {
21
/// Get the default set of [`ReflectStateFns`] for a specific component type using its
22
/// [`FromType`] implementation.
23
///
24
/// This is useful if you want to start with the default implementation before overriding some
25
/// of the functions to create a custom implementation.
26
pub fn new<T: States + Reflect>() -> Self {
27
<ReflectState as FromType<T>>::from_type().0
28
}
29
}
30
31
impl ReflectState {
32
/// Gets the value of this [`States`] type from the world as a reflected reference.
33
pub fn reflect<'a>(&self, world: &'a World) -> Option<&'a dyn Reflect> {
34
(self.0.reflect)(world)
35
}
36
}
37
38
impl<S: States + Reflect> FromType<S> for ReflectState {
39
fn from_type() -> Self {
40
ReflectState(ReflectStateFns {
41
reflect: |world| {
42
world
43
.get_resource::<State<S>>()
44
.map(|res| res.get() as &dyn Reflect)
45
},
46
})
47
}
48
}
49
50
/// A struct used to operate on the reflected [`FreelyMutableState`] trait of a type.
51
///
52
/// A [`ReflectFreelyMutableState`] for type `T` can be obtained via
53
/// [`bevy_reflect::TypeRegistration::data`].
54
#[derive(Clone)]
55
pub struct ReflectFreelyMutableState(ReflectFreelyMutableStateFns);
56
57
/// The raw function pointers needed to make up a [`ReflectFreelyMutableState`].
58
#[derive(Clone)]
59
pub struct ReflectFreelyMutableStateFns {
60
/// Function pointer implementing [`ReflectFreelyMutableState::set_next_state()`].
61
pub set_next_state: fn(&mut World, &dyn Reflect, &TypeRegistry),
62
}
63
64
impl ReflectFreelyMutableStateFns {
65
/// Get the default set of [`ReflectFreelyMutableStateFns`] for a specific component type using its
66
/// [`FromType`] implementation.
67
///
68
/// This is useful if you want to start with the default implementation before overriding some
69
/// of the functions to create a custom implementation.
70
pub fn new<T: FreelyMutableState + Reflect + TypePath>() -> Self {
71
<ReflectFreelyMutableState as FromType<T>>::from_type().0
72
}
73
}
74
75
impl ReflectFreelyMutableState {
76
/// Tentatively set a pending state transition to a reflected [`ReflectFreelyMutableState`].
77
pub fn set_next_state(&self, world: &mut World, state: &dyn Reflect, registry: &TypeRegistry) {
78
(self.0.set_next_state)(world, state, registry);
79
}
80
}
81
82
impl<S: FreelyMutableState + Reflect + TypePath> FromType<S> for ReflectFreelyMutableState {
83
fn from_type() -> Self {
84
ReflectFreelyMutableState(ReflectFreelyMutableStateFns {
85
set_next_state: |world, reflected_state, registry| {
86
let new_state: S = from_reflect_with_fallback(
87
reflected_state.as_partial_reflect(),
88
world,
89
registry,
90
);
91
if let Some(mut next_state) = world.get_resource_mut::<NextState<S>>() {
92
next_state.set(new_state);
93
}
94
},
95
})
96
}
97
}
98
99
#[cfg(test)]
100
mod tests {
101
use crate::{
102
app::{AppExtStates, StatesPlugin},
103
reflect::{ReflectFreelyMutableState, ReflectState},
104
state::State,
105
};
106
use bevy_app::App;
107
use bevy_ecs::prelude::AppTypeRegistry;
108
use bevy_reflect::Reflect;
109
use bevy_state_macros::States;
110
use core::any::TypeId;
111
112
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, States, Reflect)]
113
enum StateTest {
114
A,
115
B,
116
}
117
118
#[test]
119
fn test_reflect_state_operations() {
120
let mut app = App::new();
121
app.add_plugins(StatesPlugin)
122
.insert_state(StateTest::A)
123
.register_type_mutable_state::<StateTest>();
124
125
let type_registry = app.world_mut().resource::<AppTypeRegistry>().0.clone();
126
let type_registry = type_registry.read();
127
128
let (reflect_state, reflect_mutable_state) = (
129
type_registry
130
.get_type_data::<ReflectState>(TypeId::of::<StateTest>())
131
.unwrap()
132
.clone(),
133
type_registry
134
.get_type_data::<ReflectFreelyMutableState>(TypeId::of::<StateTest>())
135
.unwrap()
136
.clone(),
137
);
138
139
let current_value = reflect_state.reflect(app.world()).unwrap();
140
assert_eq!(
141
current_value.downcast_ref::<StateTest>().unwrap(),
142
&StateTest::A
143
);
144
145
reflect_mutable_state.set_next_state(app.world_mut(), &StateTest::B, &type_registry);
146
147
assert_ne!(
148
app.world().resource::<State<StateTest>>().get(),
149
&StateTest::B
150
);
151
152
app.update();
153
154
assert_eq!(
155
app.world().resource::<State<StateTest>>().get(),
156
&StateTest::B
157
);
158
159
let current_value = reflect_state.reflect(app.world()).unwrap();
160
assert_eq!(
161
current_value.downcast_ref::<StateTest>().unwrap(),
162
&StateTest::B
163
);
164
}
165
}
166
167