From 5340203b27255ac471bcc442e6e8d96827eaad67 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 28 Oct 2024 11:37:12 +0000 Subject: [PATCH 1/3] Made all the methods of `ComputedNode` const and inline except for `inner_radius` which uses `min` and `max`. --- crates/bevy_ui/src/ui_node.rs | 36 ++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 92092fe5f583a..0f4b5f877b338 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -62,6 +62,7 @@ impl ComputedNode { /// The calculated node size as width and height in logical pixels. /// /// Automatically calculated by [`super::layout::ui_layout_system`]. + #[inline] pub const fn size(&self) -> Vec2 { self.calculated_size } @@ -69,8 +70,8 @@ impl ComputedNode { /// Check if the node is empty. /// A node is considered empty if it has a zero or negative extent along either of its axes. #[inline] - pub fn is_empty(&self) -> bool { - self.size().cmple(Vec2::ZERO).any() + pub const fn is_empty(&self) -> bool { + self.calculated_size.x <= 0. || self.calculated_size.y <= 0. } /// The order of the node in the UI layout. @@ -84,6 +85,7 @@ impl ComputedNode { /// The calculated node size as width and height in logical pixels before rounding. /// /// Automatically calculated by [`super::layout::ui_layout_system`]. + #[inline] pub const fn unrounded_size(&self) -> Vec2 { self.unrounded_size } @@ -93,7 +95,7 @@ impl ComputedNode { /// /// Automatically calculated by [`super::layout::ui_layout_system`]. #[inline] - pub fn outline_width(&self) -> f32 { + pub const fn outline_width(&self) -> f32 { self.outline_width } @@ -101,7 +103,7 @@ impl ComputedNode { /// /// Automatically calculated by [`super::layout::ui_layout_system`]. #[inline] - pub fn outline_offset(&self) -> f32 { + pub const fn outline_offset(&self) -> f32 { self.outline_offset } @@ -109,8 +111,12 @@ impl ComputedNode { /// /// Automatically calculated by [`super::layout::ui_layout_system`]. #[inline] - pub fn outlined_node_size(&self) -> Vec2 { - self.size() + 2. * (self.outline_offset + self.outline_width) + pub const fn outlined_node_size(&self) -> Vec2 { + let offset = 2. * (self.outline_offset + self.outline_width); + Vec2::new( + self.calculated_size.x + offset, + self.calculated_size.y + offset, + ) } /// Returns the border radius for each corner of the outline @@ -119,20 +125,20 @@ impl ComputedNode { /// /// Automatically calculated by [`super::layout::ui_layout_system`]. #[inline] - pub fn outline_radius(&self) -> ResolvedBorderRadius { + pub const fn outline_radius(&self) -> ResolvedBorderRadius { let outer_distance = self.outline_width + self.outline_offset; - let compute_radius = |radius| { + const fn compute_radius(radius: f32, outer_distance: f32) -> f32 { if radius > 0. { radius + outer_distance } else { 0. } - }; + } ResolvedBorderRadius { - top_left: compute_radius(self.border_radius.top_left), - top_right: compute_radius(self.border_radius.top_right), - bottom_left: compute_radius(self.border_radius.bottom_left), - bottom_right: compute_radius(self.border_radius.bottom_right), + top_left: compute_radius(self.border_radius.top_left, outer_distance), + top_right: compute_radius(self.border_radius.top_right, outer_distance), + bottom_left: compute_radius(self.border_radius.bottom_left, outer_distance), + bottom_right: compute_radius(self.border_radius.bottom_right, outer_distance), } } @@ -178,13 +184,13 @@ impl ComputedNode { /// /// Automatically calculated by [`super::layout::ui_layout_system`]. #[inline] - pub fn padding(&self) -> BorderRect { + pub const fn padding(&self) -> BorderRect { self.padding } /// Returns the combined inset on each edge including both padding and border thickness in logical pixels. #[inline] - pub fn content_inset(&self) -> BorderRect { + pub const fn content_inset(&self) -> BorderRect { BorderRect { left: self.border.left + self.padding.left, right: self.border.right + self.padding.right, From dd17ba60c8b222245cb8476b4ba9c871dbe6a4e5 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 28 Oct 2024 11:43:11 +0000 Subject: [PATCH 2/3] Constify `border` and `border_radius` --- crates/bevy_ui/src/ui_node.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 0f4b5f877b338..b5f0f5f2eec63 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -146,7 +146,7 @@ impl ComputedNode { /// /// Automatically calculated by [`super::layout::ui_layout_system`]. #[inline] - pub fn border(&self) -> BorderRect { + pub const fn border(&self) -> BorderRect { self.border } @@ -154,7 +154,7 @@ impl ComputedNode { /// /// Automatically calculated by [`super::layout::ui_layout_system`]. #[inline] - pub fn border_radius(&self) -> ResolvedBorderRadius { + pub const fn border_radius(&self) -> ResolvedBorderRadius { self.border_radius } From e4301b23f0577695469478b3abebd8810b37e2a9 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Tue, 29 Oct 2024 05:50:40 +0000 Subject: [PATCH 3/3] update --- crates/bevy_ui/src/ui_node.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index c12eb32325589..05fb3ca4d3e46 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -71,7 +71,7 @@ impl ComputedNode { /// A node is considered empty if it has a zero or negative extent along either of its axes. #[inline] pub const fn is_empty(&self) -> bool { - self.calculated_size.x <= 0. || self.calculated_size.y <= 0. + self.size.x <= 0. || self.size.y <= 0. } /// The order of the node in the UI layout. @@ -113,10 +113,7 @@ impl ComputedNode { #[inline] pub const fn outlined_node_size(&self) -> Vec2 { let offset = 2. * (self.outline_offset + self.outline_width); - Vec2::new( - self.calculated_size.x + offset, - self.calculated_size.y + offset, - ) + Vec2::new(self.size.x + offset, self.size.y + offset) } /// Returns the border radius for each corner of the outline