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

Clip to the UI node's content box #15442

Open
wants to merge 47 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
bcabdbf
Taffy returns border values now, so there is no need to compute them …
ickshonpe Sep 10, 2024
c74c0cf
Merge branch 'main' into compute-borders-in-layout
ickshonpe Sep 10, 2024
266fafc
Fixed ui_material_pipeline's border normalization
ickshonpe Sep 10, 2024
bc67d38
removed unused imports
ickshonpe Sep 10, 2024
bfea0c9
Fixed border scalings.
ickshonpe Sep 10, 2024
18bb3fd
Removed unused imports
ickshonpe Sep 10, 2024
4a6b475
Removed unneed attribute.
ickshonpe Sep 10, 2024
ceefb43
removed border radius scaling
ickshonpe Sep 10, 2024
5512508
scale border radius Px values
ickshonpe Sep 10, 2024
a76f7eb
remove more scalings
ickshonpe Sep 10, 2024
ce8006e
`Node` contains logical coords, not physical.
ickshonpe Sep 10, 2024
5d99685
Cleanup.
ickshonpe Sep 11, 2024
eefc23d
Removed the `Inset` type, use a `BorderRect` to hold the resolved bor…
ickshonpe Sep 11, 2024
a31a987
added a `ZERO` const to `BorderRect`
ickshonpe Sep 11, 2024
237fd80
Use `BorderRect` in layout updates
ickshonpe Sep 11, 2024
f8b79cd
Removed old border extraction code
ickshonpe Sep 11, 2024
86aa524
clean up unused
ickshonpe Sep 11, 2024
8e35831
removed `logical_rect` method from `Node`
ickshonpe Sep 11, 2024
62d1c06
Removed unused import
ickshonpe Sep 11, 2024
d687217
improved corner radius visualization
ickshonpe Sep 11, 2024
4c31adf
Fixed `ExtractedUiNode` doc errors
ickshonpe Sep 11, 2024
334d361
removed unneeded import
ickshonpe Sep 11, 2024
789c4de
Removed doc link to private item.
ickshonpe Sep 11, 2024
c696763
Update crates/bevy_ui/src/render/ui.wgsl
ickshonpe Sep 25, 2024
f012b66
Added `outlined_node_size` method.
ickshonpe Sep 25, 2024
9d3b761
Fixed extracted image node border values
ickshonpe Sep 25, 2024
0001986
Added some non-uniform border-radius to the borders example.
ickshonpe Sep 25, 2024
1ce5bfe
Reverted shader changes
ickshonpe Sep 25, 2024
0312753
Merge branch 'main' into update-borders-in-layout
ickshonpe Sep 25, 2024
84e0a07
Fix up imports
ickshonpe Sep 25, 2024
2b78765
Fix up imports
ickshonpe Sep 25, 2024
2be7403
use `outlined_node_size` method in extaction
ickshonpe Sep 25, 2024
7cebb61
Merge branch 'update-borders-in-layout' of https://github.com/ickshon…
ickshonpe Sep 25, 2024
2878b24
Set outline width to zero width for uinodes with `Display::None` set.
ickshonpe Sep 25, 2024
68f1478
Added padding field and accessor method to `Node`
ickshonpe Sep 26, 2024
1419830
copy padding values to nodes in `ui_layout_system`
ickshonpe Sep 26, 2024
c33150a
fixed `Node::DEFAULT`
ickshonpe Sep 26, 2024
ff258e9
Added `content_inset` method to `Node`, returns the sum of the border…
ickshonpe Sep 26, 2024
d4bacbc
Fixed `content_inset` calculations
ickshonpe Sep 26, 2024
1091755
changed `update_clipping` to clip around the content box instead of t…
ickshonpe Sep 26, 2024
551f208
Added borders to the overflow example.
ickshonpe Sep 26, 2024
ec41f95
Removed ui scaling from example
ickshonpe Sep 26, 2024
fb9dfce
Merge branch 'main' into clip-inside-padding-box
ickshonpe Sep 27, 2024
2a1bad4
Merge branch 'main' into clip-inside-padding-box
ickshonpe Sep 30, 2024
8960141
Merge branch 'main' into clip-inside-padding-box
ickshonpe Oct 11, 2024
18ba0b1
Merge branch 'main' into clip-inside-padding-box
ickshonpe Oct 14, 2024
81678c5
Merge branch 'main' into clip-inside-padding-box
ickshonpe Oct 14, 2024
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
13 changes: 8 additions & 5 deletions crates/bevy_ui/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,16 @@ with UI components as a child of an entity without UI components, your UI layout
node.unrounded_size = layout_size;
}

node.bypass_change_detection().border = BorderRect {
left: layout.border.left * inverse_target_scale_factor,
right: layout.border.right * inverse_target_scale_factor,
top: layout.border.top * inverse_target_scale_factor,
bottom: layout.border.bottom * inverse_target_scale_factor,
let taffy_rect_to_border_rect = |rect: taffy::Rect<f32>| BorderRect {
left: rect.left * inverse_target_scale_factor,
right: rect.right * inverse_target_scale_factor,
top: rect.top * inverse_target_scale_factor,
bottom: rect.bottom * inverse_target_scale_factor,
};

node.bypass_change_detection().border = taffy_rect_to_border_rect(layout.border);
node.bypass_change_detection().padding = taffy_rect_to_border_rect(layout.padding);

let viewport_size = root_size.unwrap_or(node.calculated_size);

if let Some(border_radius) = maybe_border_radius {
Expand Down
25 changes: 25 additions & 0 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ pub struct Node {
///
/// Automatically calculated by [`super::layout::ui_layout_system`].
pub(crate) border_radius: ResolvedBorderRadius,
/// Resolved padding values in logical pixels
/// Padding updates bypass change detection.
///
/// Automatically calculated by [`super::layout::ui_layout_system`].
pub(crate) padding: BorderRect,
}

impl Node {
Expand Down Expand Up @@ -175,6 +180,25 @@ impl Node {
bottom_right: clamp_corner(self.border_radius.bottom_left, s, b.xw()),
}
}

/// Returns the thickness of the node's padding on each edge in logical pixels.
///
/// Automatically calculated by [`super::layout::ui_layout_system`].
#[inline]
pub 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 {
BorderRect {
left: self.border.left + self.padding.left,
right: self.border.right + self.padding.right,
top: self.border.top + self.padding.top,
bottom: self.border.bottom + self.padding.bottom,
}
}
}

impl Node {
Expand All @@ -186,6 +210,7 @@ impl Node {
unrounded_size: Vec2::ZERO,
border_radius: ResolvedBorderRadius::ZERO,
border: BorderRect::ZERO,
padding: BorderRect::ZERO,
};
}

Expand Down
13 changes: 13 additions & 0 deletions crates/bevy_ui/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,21 @@ fn update_clipping(
// current node's clip and the inherited clip. This handles the case
// of nested `Overflow::Hidden` nodes. If parent `clip` is not
// defined, use the current node's clip.

let mut node_rect =
Rect::from_center_size(global_transform.translation().truncate(), node.size());

// Content isn't clipped at the edges of the node but at the edges of its content box.
// The content box is innermost part of the node excluding the padding and border.
//
// The `content_inset` should always fit inside the `node_rect`.
// Even if it were to overflow, this won't result in a degenerate clipping rect as `Rect::intersect` clamps the intersection to an empty rect.
let content_inset = node.content_inset();
node_rect.min.x += content_inset.left;
node_rect.min.y += content_inset.top;
node_rect.max.x -= content_inset.right;
node_rect.max.y -= content_inset.bottom;

if style.overflow.x == OverflowAxis::Visible {
node_rect.min.x = -f32::INFINITY;
node_rect.max.x = f32::INFINITY;
Expand Down
2 changes: 2 additions & 0 deletions examples/ui/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
top: Val::Px(25.),
..Default::default()
},
border: UiRect::all(Val::Px(5.)),
overflow,
..Default::default()
},
border_color: Color::BLACK.into(),
background_color: GRAY.into(),
..Default::default()
})
Expand Down
Loading