From 9478432cb9d7ed0db9e5fafc8be148c0ddae35bc Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Tue, 4 Jul 2023 01:10:10 +0200 Subject: [PATCH] Fix bevy_ui compilation failure without bevy_text (#8985) # Objective - Fix #8984 ### Solution - Address compilation errors I admit: I did sneak it an unrelated mini-refactor. of the `measurment.rs` module. it seemed to me that directly importing `taffy` types helped reduce a lot of boilerplate, so I did it. --- crates/bevy_ui/src/measurement.rs | 27 +++++++++++---------------- crates/bevy_ui/src/widget/image.rs | 30 ++++++++---------------------- 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/crates/bevy_ui/src/measurement.rs b/crates/bevy_ui/src/measurement.rs index fd31ae9ab4f96..344efd3775d2e 100644 --- a/crates/bevy_ui/src/measurement.rs +++ b/crates/bevy_ui/src/measurement.rs @@ -4,6 +4,7 @@ use bevy_math::Vec2; use bevy_reflect::Reflect; use std::fmt::Formatter; pub use taffy::style::AvailableSpace; +use taffy::{node::MeasureFunc, prelude::Size as TaffySize}; impl std::fmt::Debug for ContentSize { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { @@ -50,33 +51,27 @@ impl Measure for FixedMeasure { pub struct ContentSize { /// The `Measure` used to compute the intrinsic size #[reflect(ignore)] - pub(crate) measure_func: Option, + pub(crate) measure_func: Option, } impl ContentSize { /// Set a `Measure` for this function pub fn set(&mut self, measure: impl Measure) { - let measure_func = - move |size: taffy::prelude::Size>, - available: taffy::prelude::Size| { - let size = - measure.measure(size.width, size.height, available.width, available.height); - taffy::prelude::Size { - width: size.x, - height: size.y, - } - }; - self.measure_func = Some(taffy::node::MeasureFunc::Boxed(Box::new(measure_func))); + let measure_func = move |size: TaffySize<_>, available: TaffySize<_>| { + let size = measure.measure(size.width, size.height, available.width, available.height); + TaffySize { + width: size.x, + height: size.y, + } + }; + self.measure_func = Some(MeasureFunc::Boxed(Box::new(measure_func))); } } -#[allow(clippy::derivable_impls)] impl Default for ContentSize { fn default() -> Self { Self { - measure_func: Some(taffy::node::MeasureFunc::Raw(|_, _| { - taffy::prelude::Size::ZERO - })), + measure_func: Some(MeasureFunc::Raw(|_, _| TaffySize::ZERO)), } } } diff --git a/crates/bevy_ui/src/widget/image.rs b/crates/bevy_ui/src/widget/image.rs index be9df0590c09c..f46dd5b4faa38 100644 --- a/crates/bevy_ui/src/widget/image.rs +++ b/crates/bevy_ui/src/widget/image.rs @@ -3,7 +3,6 @@ use crate::{ }; use bevy_asset::{Assets, Handle}; -#[cfg(feature = "bevy_text")] use bevy_ecs::query::Without; use bevy_ecs::{ prelude::Component, @@ -15,8 +14,6 @@ use bevy_math::Vec2; use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_render::texture::Image; use bevy_sprite::TextureAtlas; -#[cfg(feature = "bevy_text")] -use bevy_text::Text; use bevy_window::{PrimaryWindow, Window}; /// The size of the image's texture @@ -73,20 +70,18 @@ impl Measure for ImageMeasure { } } +#[cfg(feature = "bevy_text")] +type UpdateImageFilter = (With, Without); +#[cfg(not(feature = "bevy_text"))] +type UpdateImageFilter = With; + /// Updates content size of the node based on the image provided pub fn update_image_content_size_system( mut previous_combined_scale_factor: Local, windows: Query<&Window, With>, ui_scale: Res, textures: Res>, - #[cfg(feature = "bevy_text")] mut query: Query< - (&mut ContentSize, &UiImage, &mut UiImageSize), - (With, Without), - >, - #[cfg(not(feature = "bevy_text"))] mut query: Query< - (&mut ContentSize, &UiImage, &mut UiImageSize), - With, - >, + mut query: Query<(&mut ContentSize, &UiImage, &mut UiImageSize), UpdateImageFilter>, ) { let combined_scale_factor = windows .get_single() @@ -120,23 +115,14 @@ pub fn update_atlas_content_size_system( windows: Query<&Window, With>, ui_scale: Res, atlases: Res>, - #[cfg(feature = "bevy_text")] mut atlas_query: Query< - ( - &mut ContentSize, - &Handle, - &UiTextureAtlasImage, - &mut UiImageSize, - ), - (With, Without, Without), - >, - #[cfg(not(feature = "bevy_text"))] mut atlas_query: Query< + mut atlas_query: Query< ( &mut ContentSize, &Handle, &UiTextureAtlasImage, &mut UiImageSize, ), - (With, Without), + (UpdateImageFilter, Without), >, ) { let combined_scale_factor = windows