|
|
|
|
@ -6,7 +6,8 @@ pub struct EditorCameraPlugin;
|
|
|
|
|
impl Plugin for EditorCameraPlugin {
|
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
|
app.add_systems(Update, cameras_ui)
|
|
|
|
|
.add_systems(Update, manage_active_camera)
|
|
|
|
|
.add_systems(Update, control_active_camera)
|
|
|
|
|
.add_systems(Update, ui_control_active_camera)
|
|
|
|
|
.add_systems(Update, fallback_camera);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -42,21 +43,50 @@ pub fn cameras_ui(
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set the camera active component based on button clicks
|
|
|
|
|
pub fn manage_active_camera(
|
|
|
|
|
events: Query<&ui::TargetEntity, Added<ui::Active>>,
|
|
|
|
|
mut cameras: Query<(Entity, &mut Camera)>,
|
|
|
|
|
pub fn control_active_camera(
|
|
|
|
|
events: Query<(Entity, &Camera), Changed<Camera>>,
|
|
|
|
|
buttons: Query<(Entity, &ui::TargetEntity)>,
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
) {
|
|
|
|
|
events.iter().for_each(|ui::TargetEntity { entity }| {
|
|
|
|
|
cameras.iter_mut().for_each(|(this_entity, mut camera)| {
|
|
|
|
|
if this_entity == *entity {
|
|
|
|
|
info!("Marking {:?} as active camera", entity);
|
|
|
|
|
camera.is_active = true;
|
|
|
|
|
events
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|(e, c)| {
|
|
|
|
|
buttons
|
|
|
|
|
.iter()
|
|
|
|
|
.find_map(|(button, ui::TargetEntity { entity })| {
|
|
|
|
|
(e == *entity).then_some((button, c))
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.for_each(|(button, camera)| {
|
|
|
|
|
if camera.is_active {
|
|
|
|
|
commands.entity(button).insert(ui::Active);
|
|
|
|
|
} else {
|
|
|
|
|
info!("Marking {:?} as inactive camera", entity);
|
|
|
|
|
camera.is_active = false;
|
|
|
|
|
commands.entity(button).remove::<ui::Active>();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn ui_control_active_camera(
|
|
|
|
|
events: Query<
|
|
|
|
|
(&Interaction, &ui::TargetEntity, Option<&ui::Active>),
|
|
|
|
|
(With<Button>, Changed<Interaction>),
|
|
|
|
|
>,
|
|
|
|
|
mut cameras: Query<(Entity, &mut Camera)>,
|
|
|
|
|
) {
|
|
|
|
|
events
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|(&interaction, ui::TargetEntity { entity }, active)| {
|
|
|
|
|
(interaction == Interaction::Pressed).then_some((entity, active))
|
|
|
|
|
})
|
|
|
|
|
.for_each(|(&entity, active)| {
|
|
|
|
|
cameras.iter_mut().for_each(|(this, mut camera)| {
|
|
|
|
|
if this == entity {
|
|
|
|
|
camera.is_active = !active.is_some();
|
|
|
|
|
} else {
|
|
|
|
|
camera.is_active = false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// In the event that an active camera is despawned, fall back to the editor camera
|
|
|
|
|
|