Skip to content

Commit

Permalink
Make the ui cam extract system immutable
Browse files Browse the repository at this point in the history
Following #4402, it is now impossible to mutate the main world from the
render world, so we split the update to the projection into a different
system ran in the main world.
  • Loading branch information
nicopap committed Jul 9, 2022
1 parent 109a06d commit 3072955
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
8 changes: 7 additions & 1 deletion crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use bevy_ecs::schedule::{ParallelSystemDescriptorCoercion, SystemLabel};
use bevy_input::InputSystem;
use bevy_transform::TransformSystem;
use bevy_window::ModifiesWindows;
use update::{ui_z_system, update_clipping_system};
use update::{ui_z_system, update_clipping_system, update_ui_camera_perspective};

/// The basic plugin for Bevy UI
#[derive(Default)]
Expand All @@ -43,6 +43,8 @@ pub enum UiSystem {
Flex,
/// After this label, input interactions with UI entities have been updated for this frame
Focus,
/// Update Ui camera perspective to fit new viewport logical size.
UpdateUiCameraPerspective,
}

impl Plugin for UiPlugin {
Expand Down Expand Up @@ -88,6 +90,10 @@ impl Plugin for UiPlugin {
CoreStage::PostUpdate,
widget::image_node_system.before(UiSystem::Flex),
)
.add_system_to_stage(
CoreStage::Last,
update_ui_camera_perspective.label(UiSystem::UpdateUiCameraPerspective),
)
.add_system_to_stage(
CoreStage::PostUpdate,
flex_node_system
Expand Down
19 changes: 5 additions & 14 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,28 +233,19 @@ pub struct UiCamera {

pub fn extract_default_ui_camera_view<T: Component>(
mut commands: Commands,
mut query: Extract<Query<(Entity, &Camera, Option<&mut UiCameraConfig>), With<T>>>,
query: Extract<Query<(Entity, &Camera, Option<&UiCameraConfig>), With<T>>>,
) {
for (camera_entity, camera, opt_ui_config) in query.iter_mut() {
let ui_config = opt_ui_config.as_deref().cloned().unwrap_or_default();
for (camera_entity, camera, opt_ui_config) in query.iter() {
let ui_config = opt_ui_config.cloned().unwrap_or_default();
// ignore cameras with disabled ui
if !ui_config.show_ui {
continue;
}
if let (Some(logical_size), Some(physical_size)) = (
camera.logical_viewport_size(),
camera.physical_viewport_size(),
) {
let mut projection = ui_config.projection.clone();
projection.update(logical_size.x, logical_size.y);
let projection_matrix = projection.get_projection_matrix();
if let Some(mut config_to_update) = opt_ui_config {
config_to_update.projection = projection;
}
if let Some(physical_size) = camera.physical_viewport_size() {
let ui_camera = commands
.spawn()
.insert(ExtractedView {
projection: projection_matrix,
projection: ui_config.projection.get_projection_matrix(),
transform: GlobalTransform::from_xyz(
ui_config.position.x,
ui_config.position.y,
Expand Down
17 changes: 16 additions & 1 deletion crates/bevy_ui/src/update.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//! This module contains systems that update the UI when something changes

use crate::{CalculatedClip, Overflow, Style};
use crate::{prelude::UiCameraConfig, CalculatedClip, Overflow, Style};

use super::Node;
use bevy_ecs::{
entity::Entity,
prelude::{Changed, Or},
query::{With, Without},
system::{Commands, Query},
};
use bevy_hierarchy::{Children, Parent};
use bevy_math::Vec2;
use bevy_render::camera::{Camera, CameraProjection};
use bevy_sprite::Rect;
use bevy_transform::components::{GlobalTransform, Transform};

Expand Down Expand Up @@ -82,6 +84,19 @@ pub fn update_clipping_system(
}
}

pub fn update_ui_camera_perspective(
mut query: Query<
(&Camera, &mut UiCameraConfig),
Or<(Changed<Camera>, Changed<UiCameraConfig>)>,
>,
) {
for (camera, mut ui_config) in query.iter_mut() {
if let Some(logical_size) = camera.logical_viewport_size() {
ui_config.projection.update(logical_size.x, logical_size.y);
}
}
}

fn update_clipping(
commands: &mut Commands,
children_query: &Query<&Children>,
Expand Down

0 comments on commit 3072955

Please sign in to comment.