Fix panic on Text UI without Cameras (#11405)

# Objective

Fix https://github.com/bevyengine/bevy/issues/11396.

## Solution

Don't panic on taffy node not existing.

Plus minor warning text improvement.
This commit is contained in:
Doonv 2024-01-18 22:33:22 +02:00 committed by GitHub
parent 056b006d4e
commit 03ee959809
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -102,10 +102,15 @@ impl UiSurface {
} }
} }
/// Update the `MeasureFunc` of the taffy node corresponding to the given [`Entity`]. /// Update the `MeasureFunc` of the taffy node corresponding to the given [`Entity`] if the node exists.
pub fn update_measure(&mut self, entity: Entity, measure_func: taffy::node::MeasureFunc) { pub fn try_update_measure(
let taffy_node = self.entity_to_taffy.get(&entity).unwrap(); &mut self,
self.taffy.set_measure(*taffy_node, Some(measure_func)).ok(); entity: Entity,
measure_func: taffy::node::MeasureFunc,
) -> Option<()> {
let taffy_node = self.entity_to_taffy.get(&entity)?;
self.taffy.set_measure(*taffy_node, Some(measure_func)).ok()
} }
/// Update the children of the taffy node corresponding to the given [`Entity`]. /// Update the children of the taffy node corresponding to the given [`Entity`].
@ -305,7 +310,7 @@ pub fn ui_layout_system(
Some(camera_entity) => { Some(camera_entity) => {
let Ok((_, camera)) = cameras.get(camera_entity) else { let Ok((_, camera)) = cameras.get(camera_entity) else {
warn!( warn!(
"TargetCamera is pointing to a camera {:?} which doesn't exist", "TargetCamera (of root UI node {entity:?}) is pointing to a camera {:?} which doesn't exist",
camera_entity camera_entity
); );
continue; continue;
@ -356,7 +361,7 @@ pub fn ui_layout_system(
} }
for (entity, mut content_size) in &mut measure_query { for (entity, mut content_size) in &mut measure_query {
if let Some(measure_func) = content_size.measure_func.take() { if let Some(measure_func) = content_size.measure_func.take() {
ui_surface.update_measure(entity, measure_func); ui_surface.try_update_measure(entity, measure_func);
} }
} }