# 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.
28 lines
774 B
Rust
28 lines
774 B
Rust
//! Configuration for cameras related to UI.
|
|
|
|
use bevy_ecs::component::Component;
|
|
use bevy_ecs::prelude::With;
|
|
use bevy_render::camera::Camera;
|
|
use bevy_render::extract_component::ExtractComponent;
|
|
|
|
/// Configuration for cameras related to UI.
|
|
///
|
|
/// When a [`Camera`] doesn't have the [`UiCameraConfig`] component,
|
|
/// it will display the UI by default.
|
|
///
|
|
#[derive(Component, Clone, ExtractComponent)]
|
|
#[extract_component_filter(With<Camera>)]
|
|
pub struct UiCameraConfig {
|
|
/// Whether to output UI to this camera view.
|
|
///
|
|
/// When a `Camera` doesn't have the [`UiCameraConfig`] component,
|
|
/// it will display the UI by default.
|
|
pub show_ui: bool,
|
|
}
|
|
|
|
impl Default for UiCameraConfig {
|
|
fn default() -> Self {
|
|
Self { show_ui: true }
|
|
}
|
|
}
|