Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Automatically initializes a state in the app.
Passes through any additional reflects listed.
If enabled in tandem with `derive` it also includes `#[derive(Reflect)]`
- `register` - Enables type registration for the `States`
Same as having `#[auto_register_type]`
Same as having `#[auto_register_type]` and `#[auto_register_state_type]`
- `init` - Initializes the `States` with default values
Same as having `#[auto_init_state]`

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Automatically initializes a substate in the app.

# Parameters
- `plugin = PluginType` - Required. Specifies which plugin should initialize this substate.
- `generics(T1, T2, ...)` - Optional. Specifies concrete types for generic parameters.
When provided, the substates will be registered with these specific generic parameters.
- `derive` | `derive(Debug, Default, ..)` - Optional. Specifies that the macro should handle deriving `SubStates`.
Passes through any additional derives listed.
When enabled, `SubStates` include these additional derives:
- `Debug`
- `Default`
- `Copy`
- `Clone`
- `PartialEq`
- `Eq`
- `Hash`
- `reflect` | `reflect(Debug, Default, ..)` - Optional. Specifies that the macro should handle emitting the single `#[reflect(...)]`.
Passes through any additional reflects listed.
If enabled in tandem with `derive` it also includes `#[derive(Reflect)]`
- `register` - Enables type registration for the `SubStates`
Same as having `#[auto_register_type]` and `#[auto_register_state_type]`
- `init` - Initializes the `SubStates` with default values
Same as having `#[auto_init_sub_state]`

// Debug, Default, Copy, Clone, PartialEq, Eq, Hash

# Example
```rust
use bevy::prelude::*;
use bevy_auto_plugin::prelude::*;

#[derive(AutoPlugin)]
#[auto_plugin(impl_plugin_trait)]
struct MyPlugin;

#[auto_states(plugin = MyPlugin, derive, reflect, register, init)]
enum AppState {
#[default]
Menu,
InGame
}

#[auto_sub_states(plugin = MyPlugin, derive, reflect, register, init)]
#[source(AppState = AppState::InGame)]
enum GamePhase {
#[default]
Setup,
Battle,
Conclusion
}
```
7 changes: 7 additions & 0 deletions crates/bevy_auto_plugin_proc_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ pub fn auto_states(attr: CompilerStream, input: CompilerStream) -> CompilerStrea
handle_attribute(expand::attr::auto_states, attr, input)
}

/// Automatically registers item as SubStates for bevy app. (See below for additional options)
#[doc = include_str!("../docs/proc_attributes/auto_sub_states.md")]
#[proc_macro_attribute]
pub fn auto_sub_states(attr: CompilerStream, input: CompilerStream) -> CompilerStream {
handle_attribute(expand::attr::auto_sub_states, attr, input)
}

/// Automatically adds the fn as a system for bevy app. (See below for additional options)
#[doc = include_str!("../docs/proc_attributes/auto_system.md")]
#[proc_macro_attribute]
Expand Down
19 changes: 10 additions & 9 deletions crates/bevy_auto_plugin_shared/src/__private/expand/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,22 +366,23 @@ gen_auto_attribute_outers! {
auto_init_resource => InitResourceArgs,
auto_insert_resource => InsertResourceArgs,
auto_init_state => InitStateArgs,
auto_init_sub_state => InitSubStateArgs,
auto_init_sub_state => InitSubStateArgs,
auto_name => NameArgs,
auto_register_state_type => RegisterStateTypeArgs,
auto_add_system => AddSystemArgs,
auto_add_observer => AddObserverArgs,
auto_add_plugin => AddPluginArgs,
auto_configure_system_set => ConfigureSystemSetArgs:
auto_configure_system_set => ConfigureSystemSetArgs:
parser = ArgParser::Custom(CustomParser::AttrInput(configure_system_set_args_from_attr_input)),
}

gen_auto_outers! {
auto_component => ComponentArgs,
auto_resource => ResourceArgs,
auto_system => SystemArgs,
auto_event => EventArgs,
auto_message => MessageArgs,
auto_observer => ObserverArgs,
auto_states => StatesArgs,
auto_component => ComponentArgs,
auto_resource => ResourceArgs,
auto_system => SystemArgs,
auto_event => EventArgs,
auto_message => MessageArgs,
auto_observer => ObserverArgs,
auto_states => StatesArgs,
auto_sub_states => SubStatesArgs,
}
33 changes: 33 additions & 0 deletions crates/bevy_auto_plugin_shared/src/codegen/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ pub fn derive_states_path() -> NonEmptyPath {
parse_quote!(#states::state::States)
}

pub fn derive_sub_states_path() -> NonEmptyPath {
let states = crate::__private::paths::state::root_path();
parse_quote!(#states::state::SubStates)
}

pub fn derive_component<'a>(
extra_items: impl IntoIterator<Item = &'a NonEmptyPath>,
) -> TokenStream {
Expand Down Expand Up @@ -196,6 +201,28 @@ pub fn derive_states<'a>(extra_items: impl IntoIterator<Item = &'a NonEmptyPath>
)],
}
}
pub fn derive_sub_states<'a>(
extra_items: impl IntoIterator<Item = &'a NonEmptyPath>,
) -> ExpandAttrs {
ExpandAttrs {
use_items: vec![crate::__private::paths::state::derive_use_tokens()],
attrs: vec![derive_from(
[
vec![
&derive_sub_states_path(),
&parse_quote!(Debug),
&parse_quote!(Default),
&parse_quote!(Clone),
&parse_quote!(PartialEq),
&parse_quote!(Eq),
&parse_quote!(Hash),
],
extra_items.into_iter().collect::<Vec<_>>(),
]
.concat(),
)],
}
}
pub fn derive_reflect() -> TokenStream {
let derive_reflect_path = derive_reflect_path();
quote! { #[derive(#derive_reflect_path)] }
Expand All @@ -209,6 +236,9 @@ pub fn use_bevy_state_app_ext_states() -> syn::ItemUse {
pub fn auto_register_type(plugin: NonEmptyPath, args: RegisterTypeArgs) -> TokenStream {
ArgsWithPlugin::new(plugin, args).to_token_stream()
}
pub fn auto_register_state_type(plugin: NonEmptyPath, args: RegisterStateTypeArgs) -> TokenStream {
ArgsWithPlugin::new(plugin, args).to_token_stream()
}
pub fn auto_name(plugin: NonEmptyPath, args: NameArgs) -> TokenStream {
ArgsWithPlugin::new(plugin, args).to_token_stream()
}
Expand All @@ -218,6 +248,9 @@ pub fn auto_init_resource(plugin: NonEmptyPath, args: InitResourceArgs) -> Token
pub fn auto_init_states(plugin: NonEmptyPath, args: InitStateArgs) -> TokenStream {
ArgsWithPlugin::new(plugin, args).to_token_stream()
}
pub fn auto_init_sub_states(plugin: NonEmptyPath, args: InitSubStateArgs) -> TokenStream {
ArgsWithPlugin::new(plugin, args).to_token_stream()
}
pub fn auto_add_systems(plugin: NonEmptyPath, args: AddSystemArgs) -> TokenStream {
ArgsWithPlugin::new(plugin, args).to_token_stream()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::codegen::tokens::ArgsBackToTokens;
use crate::codegen::with_target_path::ToTokensWithConcreteTargetPath;
use crate::macro_api::attributes::prelude::GenericsArgs;
use crate::macro_api::attributes::{AttributeIdent, ItemAttributeArgs};
Expand Down Expand Up @@ -46,6 +47,12 @@ impl ToTokensWithConcreteTargetPath for RegisterStateTypeArgs {
}
}

impl ArgsBackToTokens for RegisterStateTypeArgs {
fn back_to_inner_arg_tokens(&self, tokens: &mut TokenStream) {
tokens.extend(self.generics().to_attribute_arg_tokens());
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod prelude {
pub use crate::macro_api::attributes::rewrites::auto_observer::ObserverArgs;
pub use crate::macro_api::attributes::rewrites::auto_resource::ResourceArgs;
pub use crate::macro_api::attributes::rewrites::auto_states::StatesArgs;
pub use crate::macro_api::attributes::rewrites::auto_sub_states::SubStatesArgs;
pub use crate::macro_api::attributes::rewrites::auto_system::SystemArgs;
pub use crate::macro_api::attributes::traits::prelude::*;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ impl<'a> From<&'a StatesArgs> for RegisterTypeArgs {
}
}

impl<'a> From<&'a StatesArgs> for RegisterStateTypeArgs {
fn from(_value: &'a StatesArgs) -> Self {
Self::default()
}
}

impl<'a> From<&'a StatesArgs> for InitStateArgs {
fn from(_value: &'a StatesArgs) -> Self {
Self::default()
Expand Down Expand Up @@ -88,6 +94,10 @@ impl RewriteAttribute for StatesArgs {
expanded_attrs
.attrs
.push(tokens::auto_register_type(plugin.clone(), self.into()));
expanded_attrs.attrs.push(tokens::auto_register_state_type(
plugin.clone(),
self.into(),
));
}
if self.init {
expanded_attrs
Expand Down Expand Up @@ -159,6 +169,7 @@ mod tests {
quote! { #[derive(#derive_reflect_path)] },
quote! { #[reflect(#(#reflect_args),*)] },
tokens::auto_register_type(args.plugin(), (&args.inner).into()),
tokens::auto_register_state_type(args.plugin(), (&args.inner).into()),
tokens::auto_init_states(args.plugin(), (&args.inner).into()),
]
}
Expand Down
Loading
Loading