Skip to content

Correctly handle UI hierarchy without a camera #12816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 22, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions crates/bevy_ui/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ pub struct LayoutContext {
}

impl LayoutContext {
fn default() -> Self {
Self {
scale_factor: 1.0,
physical_size: Vec2::ZERO,
min_size: 0.0,
max_size: 0.0,
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a Default trait impl.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that make sense! Added those changes in the newest commit!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, as in LayoutContext should implement Default instead of a default method like this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impl Default for LayoutContext {
   fn default() -> Self {
        Self {
            scale_factor: 1.0,
            physical_size: Vec2::ZERO,
            min_size: 0.0,
            max_size: 0.0,
        }
   }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I misunderstood your sugestion, thanks for clarifying!
The last commit should already have those changes implemented.

/// create new a [`LayoutContext`] from the window's physical size and scale factor
fn new(scale_factor: f32, physical_size: Vec2) -> Self {
Self {
Expand Down Expand Up @@ -366,6 +374,8 @@ pub fn ui_layout_system(
);
ui_surface.upsert_node(entity, &style, &layout_context);
}
} else {
ui_surface.upsert_node(entity, &Style::default(), &LayoutContext::default());
}
}
scale_factor_events.clear();
Expand Down Expand Up @@ -1209,4 +1219,65 @@ mod tests {
}
}
}

#[test]
fn no_camera_ui() {
let mut world = World::new();
world.init_resource::<UiScale>();
world.init_resource::<UiSurface>();
world.init_resource::<Events<WindowScaleFactorChanged>>();
world.init_resource::<Events<WindowResized>>();
// Required for the camera system
world.init_resource::<Events<WindowCreated>>();
world.init_resource::<Events<AssetEvent<Image>>>();
world.init_resource::<Assets<Image>>();
world.init_resource::<ManualTextureViews>();

// spawn a dummy primary window and camera
world.spawn((
Window {
resolution: WindowResolution::new(WINDOW_WIDTH, WINDOW_HEIGHT),
..default()
},
PrimaryWindow,
));

let mut ui_schedule = Schedule::default();
ui_schedule.add_systems(
(
// UI is driven by calculated camera target info, so we need to run the camera system first
bevy_render::camera::camera_system::<OrthographicProjection>,
update_target_camera_system,
apply_deferred,
ui_layout_system,
)
.chain(),
);

let ui_root = world
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.),
height: Val::Percent(100.),
..default()
},
..default()
})
.id();

let ui_child = world
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.),
height: Val::Percent(100.),
..default()
},
..default()
})
.id();

world.entity_mut(ui_root).add_child(ui_child);

ui_schedule.run(&mut world);
}
}