Skip to content

Commit f4800c2

Browse files
BorderRect maintenance (#16727)
# Objective The doc comments and function namings for `BorderRect` feel imprecise to me. Particularly the `square` function which is used to define a uniform `BorderRect` with equal widths on each edge. But this is potentially confusing since this "square" border could be around an oblong shape. Using "padding" to refer to the border extents seems undesirable too since "padding" is typically used to refer to the area between border and content, not the border itself. ## Solution * Rename `square` to `all` (this matches the name of the similar method on `UiRect`). * Rename `rectangle` to `axes` (this matches the name of the similar method on `UiRect`). * Update doc comments. ## Migration Guide The `square` and `rectangle` functions belonging to `BorderRect` have been renamed to `all` and `axes`. --------- Co-authored-by: Alice Cecile <[email protected]>
1 parent a900f68 commit f4800c2

File tree

8 files changed

+30
-29
lines changed

8 files changed

+30
-29
lines changed

crates/bevy_sprite/src/texture_slice/border_rect.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
use bevy_reflect::Reflect;
22

3-
/// Struct defining a [`Sprite`](crate::Sprite) border with padding values
3+
/// Defines the extents of the border of a rectangle.
4+
///
5+
/// This struct is used to represent thickness or offsets from the edges
6+
/// of a rectangle (left, right, top, and bottom), with values increasing inwards.
47
#[derive(Default, Copy, Clone, PartialEq, Debug, Reflect)]
58
pub struct BorderRect {
6-
/// Pixel padding to the left
9+
/// Extent of the border along the left edge
710
pub left: f32,
8-
/// Pixel padding to the right
11+
/// Extent of the border along the right edge
912
pub right: f32,
10-
/// Pixel padding to the top
13+
/// Extent of the border along the top edge
1114
pub top: f32,
12-
/// Pixel padding to the bottom
15+
/// Extent of the border along the bottom edge
1316
pub bottom: f32,
1417
}
1518

1619
impl BorderRect {
17-
/// An empty border with zero padding values in each direction
18-
pub const ZERO: Self = Self::square(0.);
20+
/// An empty border with zero thickness along each edge
21+
pub const ZERO: Self = Self::all(0.);
1922

20-
/// Creates a new border as a square, with identical pixel padding values on every direction
23+
/// Creates a border with the same `extent` along each edge
2124
#[must_use]
2225
#[inline]
23-
pub const fn square(value: f32) -> Self {
26+
pub const fn all(extent: f32) -> Self {
2427
Self {
25-
left: value,
26-
right: value,
27-
top: value,
28-
bottom: value,
28+
left: extent,
29+
right: extent,
30+
top: extent,
31+
bottom: extent,
2932
}
3033
}
3134

32-
/// Creates a new border as a rectangle, with:
33-
/// - `horizontal` for left and right pixel padding
34-
/// - `vertical` for top and bottom pixel padding
35+
/// Creates a new border with the `left` and `right` extents equal to `horizontal`, and `top` and `bottom` extents equal to `vertical`.
3536
#[must_use]
3637
#[inline]
37-
pub const fn rectangle(horizontal: f32, vertical: f32) -> Self {
38+
pub const fn axes(horizontal: f32, vertical: f32) -> Self {
3839
Self {
3940
left: horizontal,
4041
right: horizontal,
@@ -45,8 +46,8 @@ impl BorderRect {
4546
}
4647

4748
impl From<f32> for BorderRect {
48-
fn from(v: f32) -> Self {
49-
Self::square(v)
49+
fn from(extent: f32) -> Self {
50+
Self::all(extent)
5051
}
5152
}
5253

crates/bevy_sprite/src/texture_slice/slicer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use bevy_reflect::Reflect;
1212
/// See [9-sliced](https://en.wikipedia.org/wiki/9-slice_scaling) textures.
1313
#[derive(Debug, Clone, Reflect, PartialEq)]
1414
pub struct TextureSlicer {
15-
/// The sprite borders, defining the 9 sections of the image
15+
/// Inset values in pixels that define the four slicing lines dividing the texture into nine sections.
1616
pub border: BorderRect,
1717
/// Defines how the center part of the 9 slices will scale
1818
pub center_scale_mode: SliceScaleMode,

crates/bevy_ui/src/render/debug_overlay.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub fn extract_debug_overlay(
9696
transform: transform.compute_matrix(),
9797
flip_x: false,
9898
flip_y: false,
99-
border: BorderRect::square(
99+
border: BorderRect::all(
100100
debug_options.line_width / uinode.inverse_scale_factor(),
101101
),
102102
border_radius: uinode.border_radius(),

crates/bevy_ui/src/render/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ pub fn extract_uinode_borders(
511511
atlas_scaling: None,
512512
flip_x: false,
513513
flip_y: false,
514-
border: BorderRect::square(computed_node.outline_width()),
514+
border: BorderRect::all(computed_node.outline_width()),
515515
border_radius: computed_node.outline_radius(),
516516
node_type: NodeType::Border,
517517
},

examples/2d/sprite_slice.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn spawn_sprites(
3838
style.clone(),
3939
Vec2::new(100.0, 200.0),
4040
SpriteImageMode::Sliced(TextureSlicer {
41-
border: BorderRect::square(slice_border),
41+
border: BorderRect::all(slice_border),
4242
center_scale_mode: SliceScaleMode::Stretch,
4343
..default()
4444
}),
@@ -49,7 +49,7 @@ fn spawn_sprites(
4949
style.clone(),
5050
Vec2::new(100.0, 200.0),
5151
SpriteImageMode::Sliced(TextureSlicer {
52-
border: BorderRect::square(slice_border),
52+
border: BorderRect::all(slice_border),
5353
center_scale_mode: SliceScaleMode::Tile { stretch_value: 0.5 },
5454
sides_scale_mode: SliceScaleMode::Tile { stretch_value: 0.2 },
5555
..default()
@@ -61,7 +61,7 @@ fn spawn_sprites(
6161
style.clone(),
6262
Vec2::new(300.0, 200.0),
6363
SpriteImageMode::Sliced(TextureSlicer {
64-
border: BorderRect::square(slice_border),
64+
border: BorderRect::all(slice_border),
6565
center_scale_mode: SliceScaleMode::Tile { stretch_value: 0.2 },
6666
sides_scale_mode: SliceScaleMode::Tile { stretch_value: 0.3 },
6767
..default()
@@ -73,7 +73,7 @@ fn spawn_sprites(
7373
style,
7474
Vec2::new(300.0, 200.0),
7575
SpriteImageMode::Sliced(TextureSlicer {
76-
border: BorderRect::square(slice_border),
76+
border: BorderRect::all(slice_border),
7777
center_scale_mode: SliceScaleMode::Tile { stretch_value: 0.1 },
7878
sides_scale_mode: SliceScaleMode::Tile { stretch_value: 0.2 },
7979
max_corner_scale: 0.2,

examples/ui/ui_texture_atlas_slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn setup(
5858
let atlas_layout_handle = texture_atlases.add(atlas_layout);
5959

6060
let slicer = TextureSlicer {
61-
border: BorderRect::square(24.0),
61+
border: BorderRect::all(24.0),
6262
center_scale_mode: SliceScaleMode::Stretch,
6363
sides_scale_mode: SliceScaleMode::Stretch,
6464
max_corner_scale: 1.0,

examples/ui/ui_texture_slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
4848
let image = asset_server.load("textures/fantasy_ui_borders/panel-border-010.png");
4949

5050
let slicer = TextureSlicer {
51-
border: BorderRect::square(22.0),
51+
border: BorderRect::all(22.0),
5252
center_scale_mode: SliceScaleMode::Stretch,
5353
sides_scale_mode: SliceScaleMode::Stretch,
5454
max_corner_scale: 1.0,

examples/ui/ui_texture_slice_flip_and_tile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
2828

2929
let slicer = TextureSlicer {
3030
// `numbered_slices.png` is 48 pixels square. `BorderRect::square(16.)` insets the slicing line from each edge by 16 pixels, resulting in nine slices that are each 16 pixels square.
31-
border: BorderRect::square(16.),
31+
border: BorderRect::all(16.),
3232
// With `SliceScaleMode::Tile` the side and center slices are tiled to fill the side and center sections of the target.
3333
// And with a `stretch_value` of `1.` the tiles will have the same size as the corresponding slices in the source image.
3434
center_scale_mode: SliceScaleMode::Tile { stretch_value: 1. },

0 commit comments

Comments
 (0)