mirror of
https://github.com/bevyengine/bevy
synced 2024-12-22 11:03:06 +00:00
67aa2953d0
# Objective In simple cases we might want to derive the `ExtractComponent` trait. This adds symmetry to the existing `ExtractResource` derive. ## Solution Add an implementation of `#[derive(ExtractComponent)]`. The implementation is adapted from the existing `ExtractResource` derive macro. Additionally, there is an attribute called `extract_component_filter`. This allows specifying a query filter type used when extracting. If not specified, no filter (equal to `()`) is used. So: ```rust #[derive(Component, Clone, ExtractComponent)] #[extract_component_filter(With<Fuel>)] pub struct Car { pub wheels: usize, } ``` would expand to (a bit cleaned up here): ```rust impl ExtractComponent for Car { type Query = &'static Self; type Filter = With<Fuel>; type Out = Self; fn extract_component(item: QueryItem<'_, Self::Query>) -> Option<Self::Out> { Some(item.clone()) } } ``` --- ## Changelog - Added the ability to `#[derive(ExtractComponent)]` with an optional filter.
60 lines
2 KiB
Rust
60 lines
2 KiB
Rust
mod as_bind_group;
|
|
mod extract_component;
|
|
mod extract_resource;
|
|
|
|
use bevy_macro_utils::BevyManifest;
|
|
use proc_macro::TokenStream;
|
|
use syn::{parse_macro_input, DeriveInput};
|
|
|
|
pub(crate) fn bevy_render_path() -> syn::Path {
|
|
BevyManifest::default()
|
|
.maybe_get_path("bevy_render")
|
|
// NOTE: If the derivation is within bevy_render, then we need to return 'crate'
|
|
.unwrap_or_else(|| BevyManifest::parse_str("crate"))
|
|
}
|
|
|
|
#[proc_macro_derive(ExtractResource)]
|
|
pub fn derive_extract_resource(input: TokenStream) -> TokenStream {
|
|
extract_resource::derive_extract_resource(input)
|
|
}
|
|
|
|
/// Implements `ExtractComponent` trait for a component.
|
|
/// The component must implement [`Clone`].
|
|
/// The component will be extracted into the render world via cloning.
|
|
/// Note that this only enables extraction of the component, it does not execute the extraction.
|
|
/// See `ExtractComponentPlugin` to actually perform the extraction.
|
|
///
|
|
/// If you only want to extract a component conditionally, you may use the `extract_component_filter` attribute.
|
|
///
|
|
/// # Example
|
|
///
|
|
/// ```no_compile
|
|
/// use bevy_ecs::component::Component;
|
|
/// use bevy_render_macros::ExtractComponent;
|
|
///
|
|
/// #[derive(Component, Clone, ExtractComponent)]
|
|
/// #[extract_component_filter(With<Camera>)]
|
|
/// pub struct Foo {
|
|
/// pub should_foo: bool,
|
|
/// }
|
|
///
|
|
/// // Without a filter (unconditional).
|
|
/// #[derive(Component, Clone, ExtractComponent)]
|
|
/// pub struct Bar {
|
|
/// pub should_bar: bool,
|
|
/// }
|
|
/// ```
|
|
#[proc_macro_derive(ExtractComponent, attributes(extract_component_filter))]
|
|
pub fn derive_extract_component(input: TokenStream) -> TokenStream {
|
|
extract_component::derive_extract_component(input)
|
|
}
|
|
|
|
#[proc_macro_derive(
|
|
AsBindGroup,
|
|
attributes(uniform, texture, sampler, bind_group_data, storage)
|
|
)]
|
|
pub fn derive_as_bind_group(input: TokenStream) -> TokenStream {
|
|
let input = parse_macro_input!(input as DeriveInput);
|
|
|
|
as_bind_group::derive_as_bind_group(input).unwrap_or_else(|err| err.to_compile_error().into())
|
|
}
|