bevy/crates/bevy_render/src/camera/active_cameras.rs
Paweł Grabarz 07ed1d053e Implement and require #[derive(Component)] on all component structs (#2254)
This implements the most minimal variant of #1843 - a derive for marker trait. This is a prerequisite to more complicated features like statically defined storage type or opt-out component reflection.

In order to make component struct's purpose explicit and avoid misuse, it must be annotated with `#[derive(Component)]` (manual impl is discouraged for compatibility). Right now this is just a marker trait, but in the future it might be expanded. Making this change early allows us to make further changes later without breaking backward compatibility for derive macro users.

This already prevents a lot of issues, like using bundles in `insert` calls. Primitive types are no longer valid components as well. This can be easily worked around by adding newtype wrappers and deriving `Component` for them.

One funny example of prevented bad code (from our own tests) is when an newtype struct or enum variant is used. Previously, it was possible to write `insert(Newtype)` instead of `insert(Newtype(value))`. That code compiled, because function pointers (in this case newtype struct constructor) implement `Send + Sync + 'static`, so we allowed them to be used as components. This is no longer the case and such invalid code will trigger a compile error.


Co-authored-by: = <=>
Co-authored-by: TheRawMeatball <therawmeatball@gmail.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-10-03 19:23:44 +00:00

78 lines
1.9 KiB
Rust

use crate::renderer::RenderResourceBindings;
use super::Camera;
use bevy_ecs::{
component::Component,
entity::Entity,
system::{Query, ResMut},
};
use bevy_utils::HashMap;
#[derive(Component, Debug, Default)]
pub struct ActiveCamera {
pub name: String,
pub entity: Option<Entity>,
pub bindings: RenderResourceBindings,
}
#[derive(Debug, Default)]
pub struct ActiveCameras {
cameras: HashMap<String, ActiveCamera>,
}
impl ActiveCameras {
pub fn add(&mut self, name: &str) {
self.cameras.insert(
name.to_string(),
ActiveCamera {
name: name.to_string(),
..Default::default()
},
);
}
pub fn get(&self, name: &str) -> Option<&ActiveCamera> {
self.cameras.get(name)
}
pub fn get_mut(&mut self, name: &str) -> Option<&mut ActiveCamera> {
self.cameras.get_mut(name)
}
pub fn remove(&mut self, name: &str) -> Option<ActiveCamera> {
self.cameras.remove(name)
}
pub fn iter(&self) -> impl Iterator<Item = &ActiveCamera> {
self.cameras.values()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut ActiveCamera> {
self.cameras.values_mut()
}
}
pub fn active_cameras_system(
mut active_cameras: ResMut<ActiveCameras>,
query: Query<(Entity, &Camera)>,
) {
for (name, active_camera) in active_cameras.cameras.iter_mut() {
if active_camera
.entity
.map_or(false, |entity| query.get(entity).is_err())
{
active_camera.entity = None;
}
if active_camera.entity.is_none() {
for (camera_entity, camera) in query.iter() {
if let Some(ref current_name) = camera.name {
if current_name == name {
active_camera.entity = Some(camera_entity);
}
}
}
}
}
}