refactor: trying to do something right!

This commit is contained in:
nativerv
2024-02-19 03:44:00 +03:00
parent 43be3da536
commit 55feb6e67f
5 changed files with 62 additions and 31 deletions
+9
View File
@@ -400,6 +400,7 @@ name = "bevy_controls"
version = "0.1.0"
dependencies = [
"bevy_app",
"bevy_controls_macros",
"bevy_derive",
"bevy_ecs",
"bevy_input",
@@ -418,6 +419,14 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "bevy_controls_macros"
version = "0.1.0"
dependencies = [
"quote",
"syn 1.0.109",
]
[[package]]
name = "bevy_core"
version = "0.12.1"
@@ -2,7 +2,8 @@
//!
//! It displays
//!
//! Yeah I know, now it's a bit ugly
//! Yeah I know, that's kind of weird that it displays
//! But it does.........
use bevy::{ecs::schedule::States, prelude::*};
use bevy_controls::{
+38
View File
@@ -114,3 +114,41 @@ pub fn game_state_supertraits(_attrs: TokenStream, input: TokenStream) -> TokenS
TokenStream::from(out)
}
#[proc_macro_attribute]
pub fn action_supertraits(_attrs: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemTrait);
let mut bounds = Vec::new();
if cfg!(feature = "serialize") {
bounds.push(quote! { serde::Serialize });
bounds.push(quote! { serde::de::DeserializeOwned });
};
if cfg!(feature = "reflect") {
bounds.push(quote! { bevy_reflect::Reflect });
bounds.push(quote! { bevy_reflect::TypePath });
bounds.push(quote! { bevy_reflect::FromReflect });
};
if cfg!(feature = "inspector-egui") {
bounds.push(quote! { bevy_inspector_egui::inspector_options::InspectorOptionsType });
};
let ItemTrait {
ident,
vis,
items,
supertraits: bounds_old,
..
} = input;
let bounds_old = bounds_old.iter().collect::<Vec<_>>();
let out = quote! {
#vis trait #ident: #(#bounds_old)+* + #(#bounds)+* { #(#items)* }
};
// dbg!(&out);
// dbg!(out.to_string());
TokenStream::from(out)
}
+12 -27
View File
@@ -56,34 +56,19 @@ pub trait InspectorEguiImpl: InspectorOptionsType {}
not(feature = "reflect"),
not(feature = "inspector-egui")
))]
pub trait Action: ActionInner {}
#[cfg(all(
feature = "serialize",
not(feature = "reflect"),
not(feature = "inspector-egui")
))]
pub trait Action: ActionInner + SerializeImpl {}
#[cfg(all(
not(feature = "serialize"),
feature = "reflect",
not(feature = "inspector-egui")
))]
pub trait Action: ActionInner + ReflectImpl {}
#[cfg(all(
feature = "serialize",
feature = "reflect",
not(feature = "inspector-egui")
))]
pub trait Action: ActionInner + SerializeImpl + ReflectImpl {}
#[cfg(all(not(feature = "serialize"), feature = "inspector-egui"))]
pub trait Action: ActionInner + ReflectImpl + InspectorEguiImpl {}
#[cfg(all(feature = "serialize", feature = "inspector-egui"))]
pub trait Action: ActionInner + SerializeImpl + ReflectImpl + InspectorEguiImpl {}
#[game_state_supertraits]
pub trait Action: 'static
+ std::marker::Sync
+ std::marker::Send
+ PartialEq
+ Eq
+ Hash
+ IntoEnumIterator
+ Clone
+ Copy
+ std::fmt::Debug
{}
// ===== GameState =====
// SAFETY: inspector-egui includes reflect
-2
View File
@@ -11,7 +11,6 @@ pub fn action(input: TokenStream) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl bevy_controls::contract::Action for #name {}
impl bevy_controls::contract::ActionInner for #name {}
};
gen.into()
}
@@ -23,7 +22,6 @@ pub fn game_state(input: TokenStream) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl bevy_controls::contract::GameState for #name {}
impl bevy_controls::contract::GameStateInner for #name {}
};
gen.into()
}