Skip to content
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

Constify ComputedNode #16134

Merged
merged 5 commits into from
Oct 29, 2024
Merged
Changes from all 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
37 changes: 20 additions & 17 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@ 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.size
}

/// 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.size.x <= 0. || self.size.y <= 0.
}

/// The order of the node in the UI layout.
Expand All @@ -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
}
Expand All @@ -93,24 +95,25 @@ 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
}

/// Returns the amount of space between the outline and the edge of the node in logical pixels.
///
/// 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
}

/// Returns the size of the node when including its outline.
///
/// 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.size.x + offset, self.size.y + offset)
}

/// Returns the border radius for each corner of the outline
Expand All @@ -119,36 +122,36 @@ 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),
}
}

/// Returns the thickness of the node's border on each edge in logical pixels.
///
/// Automatically calculated by [`super::layout::ui_layout_system`].
#[inline]
pub fn border(&self) -> BorderRect {
pub const fn border(&self) -> BorderRect {
self.border
}

/// Returns the border radius for each of the node's corners in logical pixels.
///
/// 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
}

Expand Down Expand Up @@ -178,13 +181,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,
Expand Down