You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Objective
Implement borders for UI nodes.
Relevant discussion: #7785
Related: #5924, #3991
<img width="283" alt="borders"
src="https://user-images.githubusercontent.com/27962798/220968899-7661d5ec-6f5b-4b0f-af29-bf9af02259b5.PNG">
## Solution
Add an extraction function to draw the borders.
---
Can only do one colour rectangular borders due to the limitations of the
Bevy UI renderer.
Maybe it can be combined with #3991 eventually to add curved border
support.
## Changelog
* Added a component `BorderColor`.
* Added the `extract_uinode_borders` system to the UI Render App.
* Added the UI example `borders`
---------
Co-authored-by: Nico Burns <[email protected]>
let left = resolve_border_thickness(style.border.left, parent_width, viewport_size);
228
+
let right = resolve_border_thickness(style.border.right, parent_width, viewport_size);
229
+
let top = resolve_border_thickness(style.border.top, parent_width, viewport_size);
230
+
let bottom = resolve_border_thickness(style.border.bottom, parent_width, viewport_size);
231
+
232
+
// Calculate the border rects, ensuring no overlap.
233
+
// The border occupies the space between the node's bounding rect and the node's bounding rect inset in each direction by the node's corresponding border value.
234
+
let max = 0.5* node.size();
235
+
let min = -max;
236
+
let inner_min = min + Vec2::new(left, top);
237
+
let inner_max = (max - Vec2::new(right, bottom)).max(inner_min);
238
+
let border_rects = [
239
+
// Left border
240
+
Rect{
241
+
min,
242
+
max:Vec2::new(inner_min.x, max.y),
243
+
},
244
+
// Right border
245
+
Rect{
246
+
min:Vec2::new(inner_max.x, min.y),
247
+
max,
248
+
},
249
+
// Top border
250
+
Rect{
251
+
min:Vec2::new(inner_min.x, min.y),
252
+
max:Vec2::new(inner_max.x, inner_min.y),
253
+
},
254
+
// Bottom border
255
+
Rect{
256
+
min:Vec2::new(inner_min.x, inner_max.y),
257
+
max:Vec2::new(inner_max.x, max.y),
258
+
},
259
+
];
260
+
261
+
let transform = global_transform.compute_matrix();
262
+
263
+
for edge in border_rects {
264
+
if edge.min.x < edge.max.x && edge.min.y < edge.max.y{
265
+
extracted_uinodes.uinodes.push(ExtractedUiNode{
266
+
stack_index,
267
+
// This translates the uinode's transform to the center of the current border rectangle
Copy file name to clipboardExpand all lines: examples/README.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -335,6 +335,7 @@ Example | Description
335
335
336
336
Example | Description
337
337
--- | ---
338
+
[Borders](../examples/ui/borders.rs) | Demonstrates how to create a node with a border
338
339
[Button](../examples/ui/button.rs) | Illustrates creating and updating a button
339
340
[CSS Grid](../examples/ui/grid.rs) | An example for CSS Grid layout
340
341
[Flex Layout](../examples/ui/flex_layout.rs) | Demonstrates how the AlignItems and JustifyContent properties can be composed to layout nodes and position text
@@ -350,6 +351,7 @@ Example | Description
350
351
[UI](../examples/ui/ui.rs) | Illustrates various features of Bevy UI
351
352
[UI Scaling](../examples/ui/ui_scaling.rs) | Illustrates how to scale the UI
352
353
[UI Z-Index](../examples/ui/z_index.rs) | Demonstrates how to control the relative depth (z-position) of UI elements
354
+
[Viewport Debug](../examples/ui/viewport_debug.rs) | An example for debugging viewport coordinates
353
355
[Window Fallthrough](../examples/ui/window_fallthrough.rs) | Illustrates how to access `winit::window::Window`'s `hittest` functionality.
0 commit comments