Skip to content

Commit c18838f

Browse files
authored
ComputedNode box model helper functions (#21903)
# Objective Add helper functions to `ComputedNode` that return the bounds of the node's border, padding, and content areas. ## Solution Add helper functions `border_box`, `padding_box`, and `content_box` to `ComputedNode`. # Included a release note. These changes are very trivial and don't really need one, but I've got quite a few more less trivial changes to `ComputedNode` that I want to add later. ## Testing This PR also includes a couple of trivial tests.
1 parent 9abb623 commit c18838f

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

crates/bevy_ui/src/ui_node.rs

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,38 @@ impl ComputedNode {
302302
clip_rect
303303
}
304304

305+
/// Returns the node's border-box in object-centered physical coordinates.
306+
/// This is the full rectangle enclosing the node.
307+
#[inline]
308+
pub fn border_box(&self) -> Rect {
309+
Rect::from_center_size(Vec2::ZERO, self.size)
310+
}
311+
312+
/// Returns the node's padding-box in object-centered physical coordinates.
313+
/// This is the region inside the border containing the node's padding and content areas.
314+
#[inline]
315+
pub fn padding_box(&self) -> Rect {
316+
let mut out = self.border_box();
317+
out.min.x += self.border.left;
318+
out.max.x -= self.border.right;
319+
out.min.y += self.border.top;
320+
out.max.y -= self.border.bottom;
321+
out
322+
}
323+
324+
/// Returns the node's content-box in object-centered physical coordinates.
325+
/// This is the innermost region of the node, where its content is placed.
326+
#[inline]
327+
pub fn content_box(&self) -> Rect {
328+
let mut out = self.border_box();
329+
let content_inset = self.content_inset();
330+
out.min.x += content_inset.left;
331+
out.max.x -= content_inset.right;
332+
out.min.y += content_inset.top;
333+
out.max.y -= content_inset.bottom;
334+
out
335+
}
336+
305337
const fn compute_thumb(
306338
gutter_min: f32,
307339
content_length: f32,
@@ -2987,11 +3019,10 @@ impl ComputedUiRenderTargetInfo {
29873019

29883020
#[cfg(test)]
29893021
mod tests {
2990-
use bevy_math::Rect;
2991-
use bevy_math::Vec2;
2992-
29933022
use crate::ComputedNode;
29943023
use crate::GridPlacement;
3024+
use bevy_math::{Rect, Vec2};
3025+
use bevy_sprite::BorderRect;
29953026

29963027
#[test]
29973028
fn invalid_grid_placement_values() {
@@ -3116,4 +3147,52 @@ mod tests {
31163147
);
31173148
assert_eq!(thumb, [0., 50.]);
31183149
}
3150+
3151+
#[test]
3152+
fn border_box_is_centered_rect_of_node_size() {
3153+
let node = ComputedNode {
3154+
size: Vec2::new(100.0, 50.0),
3155+
..Default::default()
3156+
};
3157+
let border_box = node.border_box();
3158+
3159+
assert_eq!(border_box.min, Vec2::new(-50.0, -25.0));
3160+
assert_eq!(border_box.max, Vec2::new(50.0, 25.0));
3161+
}
3162+
3163+
#[test]
3164+
fn padding_box_subtracts_border_thickness() {
3165+
let node = ComputedNode {
3166+
size: Vec2::new(100.0, 60.0),
3167+
border: BorderRect {
3168+
left: 5.0,
3169+
right: 7.0,
3170+
top: 3.0,
3171+
bottom: 9.0,
3172+
},
3173+
..Default::default()
3174+
};
3175+
let padding_box = node.padding_box();
3176+
3177+
assert_eq!(padding_box.min, Vec2::new(-50.0 + 5.0, -30.0 + 3.0));
3178+
assert_eq!(padding_box.max, Vec2::new(50.0 - 7.0, 30.0 - 9.0));
3179+
}
3180+
3181+
#[test]
3182+
fn content_box_uses_content_inset() {
3183+
let node = ComputedNode {
3184+
size: Vec2::new(80.0, 40.0),
3185+
padding: BorderRect {
3186+
left: 4.0,
3187+
right: 6.0,
3188+
top: 2.0,
3189+
bottom: 8.0,
3190+
},
3191+
..Default::default()
3192+
};
3193+
let content_box = node.content_box();
3194+
3195+
assert_eq!(content_box.min, Vec2::new(-40.0 + 4.0, -20.0 + 2.0));
3196+
assert_eq!(content_box.max, Vec2::new(40.0 - 6.0, 20.0 - 8.0));
3197+
}
31193198
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: "`ComputedNode` helper functions"
3+
authors: ["@ickshonpe"]
4+
pull_requests: [21903]
5+
---
6+
7+
Helper functions `border_box`, `padding_box`, and `content_box` that return a node’s object-centered border, padding, and content boxes have been added to `ComputedNode`.

0 commit comments

Comments
 (0)