refactor: Fixed a bug in NoteLineCount... not seriously...

This commit is contained in:
nativerv
2024-02-19 04:02:17 +03:00
parent 55feb6e67f
commit 4888e60ccf
5 changed files with 41 additions and 145 deletions
@@ -7,14 +7,12 @@
use bevy::{ecs::schedule::States, prelude::*}; use bevy::{ecs::schedule::States, prelude::*};
use bevy_controls::{ use bevy_controls::{
contract::{ contract::InputsContainer,
InputsContainer, InputsContainerInner,
},
plugin::ControlsPlugin, plugin::ControlsPlugin,
resource::{Binding, ButtonCombination, Controls, InputType, InputValue, Keyboard, PlayerInputs}, resource::{Binding, ButtonCombination, Controls, InputType, InputValue, Keyboard, PlayerInputs},
}; };
use strum_macros::EnumIter;
use bevy_controls_derive::{Action, GameState}; use bevy_controls_derive::{Action, GameState};
use strum_macros::EnumIter;
#[derive(PartialEq, Eq, Hash, EnumIter, Clone, Copy, Debug, Action)] #[derive(PartialEq, Eq, Hash, EnumIter, Clone, Copy, Debug, Action)]
enum MyAction { enum MyAction {
@@ -37,9 +35,7 @@ struct MyInputsContainer {
player_inputs: PlayerInputs<MyAction>, player_inputs: PlayerInputs<MyAction>,
} }
impl InputsContainer<MyAction> for MyInputsContainer {} impl InputsContainer<MyAction> for MyInputsContainer {
impl InputsContainerInner<MyAction> for MyInputsContainer {
fn iter_inputs<'a>(&'a self) -> Box<dyn Iterator<Item = &'a PlayerInputs<MyAction>> + 'a> { fn iter_inputs<'a>(&'a self) -> Box<dyn Iterator<Item = &'a PlayerInputs<MyAction>> + 'a> {
todo!() todo!()
} }
+7 -45
View File
@@ -3,14 +3,14 @@ extern crate proc_macro;
use proc_macro::TokenStream; use proc_macro::TokenStream;
use std::collections::HashMap; use std::collections::HashMap;
//use quote::quote; //use quote::quote;
use quote::quote;
use syn::{ use syn::{
parse::{Parse, ParseStream, Result}, parse::{Parse, ParseStream, Result},
parse_macro_input, parse_macro_input,
punctuated::Punctuated, punctuated::Punctuated,
token::{Bracket, Add}, token::{Add, Bracket},
Expr, Ident, ItemTrait, Token, TypeParamBound, LitStr, Expr, Ident, ItemTrait, LitStr, Token, TypeParamBound,
}; };
use quote::quote;
// #[cfg_feature_trait_bounds { // #[cfg_feature_trait_bounds {
// "serialize" => Serialize + DeserializeOwned, // "serialize" => Serialize + DeserializeOwned,
@@ -67,18 +67,17 @@ pub fn cfg_feature_trait_bounds(attr: TokenStream, input: TokenStream) -> TokenS
dbg!(std::env::vars().map(|(key, _)| key).collect::<Vec<_>>()); dbg!(std::env::vars().map(|(key, _)| key).collect::<Vec<_>>());
// let code = quote! { // let code = quote! {
// trait #name: #rules {} // trait #name: #rules {}
// }; // };
// //
// dbg!(code); // dbg!(code);
TokenStream::new() TokenStream::new()
} }
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn game_state_supertraits(_attrs: TokenStream, input: TokenStream) -> TokenStream { pub fn supertraits(_attrs: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemTrait); let input = parse_macro_input!(input as ItemTrait);
let mut bounds = Vec::new(); let mut bounds = Vec::new();
@@ -99,6 +98,7 @@ pub fn game_state_supertraits(_attrs: TokenStream, input: TokenStream) -> TokenS
ident, ident,
vis, vis,
items, items,
generics,
supertraits: bounds_old, supertraits: bounds_old,
.. ..
} = input; } = input;
@@ -106,45 +106,7 @@ pub fn game_state_supertraits(_attrs: TokenStream, input: TokenStream) -> TokenS
let bounds_old = bounds_old.iter().collect::<Vec<_>>(); let bounds_old = bounds_old.iter().collect::<Vec<_>>();
let out = quote! { let out = quote! {
#vis trait #ident: #(#bounds_old)+* + #(#bounds)+* { #(#items)* } #vis trait #ident #generics: #(#bounds_old)+* + #(#bounds)+* { #(#items)* }
};
// dbg!(&out);
// dbg!(out.to_string());
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);
+22 -91
View File
@@ -6,15 +6,23 @@ use bevy_inspector_egui::inspector_options::InspectorOptionsType;
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
// #[cfg(all(feature = "reflect", feature = "serialize"))] // #[cfg(all(feature = "reflect", feature = "serialize"))]
// use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; // use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
use bevy_controls_macros::{cfg_feature_trait_bounds, supertraits};
#[cfg(feature = "reflect")] #[cfg(feature = "reflect")]
use bevy_reflect::{FromReflect, Reflect, TypePath}; use bevy_reflect::{FromReflect, Reflect, TypePath};
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use bevy_controls_macros::{cfg_feature_trait_bounds, game_state_supertraits};
use crate::resource::PlayerInputs; use crate::resource::PlayerInputs;
pub trait ActionInner: // ===== Action =====
// SAFETY: inspector-egui includes reflect
#[cfg(all(
not(feature = "serialize"),
not(feature = "reflect"),
not(feature = "inspector-egui")
))]
#[supertraits]
pub trait Action:
'static 'static
+ std::marker::Sync + std::marker::Sync
+ std::marker::Send + std::marker::Send
@@ -28,7 +36,18 @@ pub trait ActionInner:
{ {
} }
pub trait InputsContainerInner<A: Action>: // ===== GameState =====
// SAFETY: inspector-egui includes reflect
#[supertraits]
pub trait GameState:
'static + std::marker::Sync + std::marker::Send + States + Default + std::fmt::Debug
{
}
// ===== InputsContainer =====
// SAFETY: inspector-egui includes reflect
#[supertraits]
pub trait InputsContainer<A: Action>:
'static + std::marker::Sync + std::marker::Send + Resource + Default + std::fmt::Debug 'static + std::marker::Sync + std::marker::Send + Resource + Default + std::fmt::Debug
{ {
/// The method returning an iterator over references to the PlayerInputs. /// The method returning an iterator over references to the PlayerInputs.
@@ -40,94 +59,6 @@ pub trait InputsContainerInner<A: Action>:
fn me_mut<'a>(&'a mut self) -> Option<&'a mut PlayerInputs<A>>; fn me_mut<'a>(&'a mut self) -> Option<&'a mut PlayerInputs<A>>;
} }
#[cfg(feature = "serialize")]
pub trait SerializeImpl: Serialize + DeserializeOwned {}
#[cfg(feature = "reflect")]
pub trait ReflectImpl: Reflect + TypePath + FromReflect {}
#[cfg(feature = "inspector-egui")]
pub trait InspectorEguiImpl: InspectorOptionsType {}
// ===== Action =====
// SAFETY: inspector-egui includes reflect
#[cfg(all(
not(feature = "serialize"),
not(feature = "reflect"),
not(feature = "inspector-egui")
))]
#[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
#[game_state_supertraits]
pub trait GameState:
'static
+ std::marker::Sync
+ std::marker::Send
+ States
+ Default
+ std::fmt::Debug
{
}
// ===== InputsContainer =====
// SAFETY: inspector-egui includes reflect
#[cfg(all(
not(feature = "serialize"),
not(feature = "reflect"),
not(feature = "inspector-egui")
))]
pub trait InputsContainer<A: Action>: InputsContainerInner<A> {}
#[cfg(all(
feature = "serialize",
not(feature = "reflect"),
not(feature = "inspector-egui")
))]
pub trait InputsContainer<A: Action>: InputsContainerInner<A> + SerializeImpl {}
#[cfg(all(
not(feature = "serialize"),
feature = "reflect",
not(feature = "inspector-egui")
))]
pub trait InputsContainer<A: Action>: InputsContainerInner<A> + ReflectImpl {}
#[cfg(all(
feature = "serialize",
feature = "reflect",
not(feature = "inspector-egui")
))]
pub trait InputsContainer<A: Action>:
InputsContainerInner<A> + SerializeImpl + ReflectImpl
{
}
#[cfg(all(not(feature = "serialize"), feature = "inspector-egui"))]
pub trait InputsContainer<A: Action>:
InputsContainerInner<A> + ReflectImpl + InspectorEguiImpl
{
}
#[cfg(all(feature = "serialize", feature = "inspector-egui"))]
pub trait InputsContainer<A: Action>:
InputsContainerInner<A> + SerializeImpl + ReflectImpl + InspectorEguiImpl
{
}
// #[cfg(test)] // #[cfg(test)]
// mod test { // mod test {
// use bevy_ecs::{entity::Entity, schedule::States}; // use bevy_ecs::{entity::Entity, schedule::States};
+5 -1
View File
@@ -3,7 +3,11 @@ use bevy_ecs::{
schedule::State, schedule::State,
system::{Res, ResMut}, system::{Res, ResMut},
}; };
use bevy_input::{keyboard::{KeyCode, ScanCode}, mouse::MouseButton, Input}; use bevy_input::{
keyboard::{KeyCode, ScanCode},
mouse::MouseButton,
Input,
};
use crate::{ use crate::{
contract::{Action, GameState, InputsContainer}, contract::{Action, GameState, InputsContainer},
+4 -1
View File
@@ -2,7 +2,10 @@ use std::error;
use bevy_derive::{Deref, DerefMut}; use bevy_derive::{Deref, DerefMut};
use bevy_ecs::system::Resource; use bevy_ecs::system::Resource;
use bevy_input::{keyboard::{KeyCode, ScanCode}, mouse::MouseButton}; use bevy_input::{
keyboard::{KeyCode, ScanCode},
mouse::MouseButton,
};
#[cfg(feature = "inspector-egui")] #[cfg(feature = "inspector-egui")]
use bevy_inspector_egui::prelude::*; use bevy_inspector_egui::prelude::*;
#[cfg(all(feature = "reflect", feature = "serialize"))] #[cfg(all(feature = "reflect", feature = "serialize"))]