Skip to content

Commit 989fb8a

Browse files
KDecayKDecay
KDecay
and
KDecay
committed
Move Rect to bevy_ui and rename it to UiRect (#4276)
# Objective - Closes #335. - Related #4285. - Part of the splitting process of #3503. ## Solution - Move `Rect` to `bevy_ui` and rename it to `UiRect`. ## Reasons - `Rect` is only used in `bevy_ui` and therefore calling it `UiRect` makes the intent clearer. - We have two types that are called `Rect` currently and it's missleading (see `bevy_sprite::Rect` and #335). - Discussion in #3503. ## Changelog ### Changed - The `Rect` type got moved from `bevy_math` to `bevy_ui` and renamed to `UiRect`. ## Migration Guide - The `Rect` type got renamed to `UiRect`. To migrate you just have to change every occurrence of `Rect` to `UiRect`. Co-authored-by: KDecay <[email protected]>
1 parent 91f2b51 commit 989fb8a

19 files changed

+84
-91
lines changed

crates/bevy_math/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ keywords = ["bevy"]
1010

1111
[dependencies]
1212
glam = { version = "0.20.0", features = ["serde", "bytemuck"] }
13-
bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] }

crates/bevy_math/src/geometry.rs

-36
This file was deleted.

crates/bevy_math/src/lib.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
mod geometry;
2-
3-
pub use geometry::*;
41
pub use glam::*;
52

63
pub mod prelude {
74
#[doc(hidden)]
85
pub use crate::{
9-
BVec2, BVec3, BVec4, EulerRot, IVec2, IVec3, IVec4, Mat3, Mat4, Quat, Rect, UVec2, UVec3,
10-
UVec4, Vec2, Vec3, Vec4,
6+
BVec2, BVec3, BVec4, EulerRot, IVec2, IVec3, IVec4, Mat3, Mat4, Quat, UVec2, UVec3, UVec4,
7+
Vec2, Vec3, Vec4,
118
};
129
}

crates/bevy_ui/src/flex/convert.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use crate::{
22
AlignContent, AlignItems, AlignSelf, Direction, Display, FlexDirection, FlexWrap,
3-
JustifyContent, PositionType, Size, Style, Val,
3+
JustifyContent, PositionType, Size, Style, UiRect, Val,
44
};
5-
use bevy_math::Rect;
65

76
pub fn from_rect(
87
scale_factor: f64,
9-
rect: Rect<Val>,
8+
rect: UiRect<Val>,
109
) -> stretch::geometry::Rect<stretch::style::Dimension> {
1110
stretch::geometry::Rect {
1211
start: from_val(scale_factor, rect.left),

crates/bevy_ui/src/geometry.rs

+35
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,41 @@ use bevy_math::Vec2;
22
use bevy_reflect::Reflect;
33
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
44

5+
/// A rect, as defined by its "side" locations
6+
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
7+
#[reflect(PartialEq)]
8+
pub struct UiRect<T: Reflect + PartialEq> {
9+
pub left: T,
10+
pub right: T,
11+
pub top: T,
12+
pub bottom: T,
13+
}
14+
15+
impl<T: Reflect + PartialEq> UiRect<T> {
16+
pub fn all(value: T) -> Self
17+
where
18+
T: Clone,
19+
{
20+
UiRect {
21+
left: value.clone(),
22+
right: value.clone(),
23+
top: value.clone(),
24+
bottom: value,
25+
}
26+
}
27+
}
28+
29+
impl<T: Default + Reflect + PartialEq> Default for UiRect<T> {
30+
fn default() -> Self {
31+
Self {
32+
left: Default::default(),
33+
right: Default::default(),
34+
top: Default::default(),
35+
bottom: Default::default(),
36+
}
37+
}
38+
}
39+
540
/// A two dimensional "size" as defined by a width and height
641
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
742
#[reflect(PartialEq)]

crates/bevy_ui/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use crate::Size;
2929
use bevy_app::prelude::*;
3030
use bevy_ecs::schedule::{ParallelSystemDescriptorCoercion, SystemLabel};
3131
use bevy_input::InputSystem;
32-
use bevy_math::Rect;
3332
use bevy_transform::TransformSystem;
3433
use bevy_window::ModifiesWindows;
3534
use update::{ui_z_system, update_clipping_system};
@@ -71,7 +70,7 @@ impl Plugin for UiPlugin {
7170
.register_type::<PositionType>()
7271
.register_type::<Size<f32>>()
7372
.register_type::<Size<Val>>()
74-
.register_type::<Rect<Val>>()
73+
.register_type::<UiRect<Val>>()
7574
.register_type::<Style>()
7675
.register_type::<UiColor>()
7776
.register_type::<UiImage>()

crates/bevy_ui/src/ui_node.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::Size;
1+
use crate::{Size, UiRect};
22
use bevy_asset::Handle;
33
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
4-
use bevy_math::{Rect, Vec2};
4+
use bevy_math::Vec2;
55
use bevy_reflect::{Reflect, ReflectDeserialize};
66
use bevy_render::{
77
color::Color,
@@ -90,13 +90,13 @@ pub struct Style {
9090
/// How items align according to the main axis
9191
pub justify_content: JustifyContent,
9292
/// The position of the node as descrided by its Rect
93-
pub position: Rect<Val>,
93+
pub position: UiRect<Val>,
9494
/// The margin of the node
95-
pub margin: Rect<Val>,
95+
pub margin: UiRect<Val>,
9696
/// The padding of the node
97-
pub padding: Rect<Val>,
97+
pub padding: UiRect<Val>,
9898
/// The border of the node
99-
pub border: Rect<Val>,
99+
pub border: UiRect<Val>,
100100
/// Defines how much a flexbox item should grow if there's space available
101101
pub flex_grow: f32,
102102
/// How to shrink if there's not enough space available

examples/ecs/state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn setup_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
4040
style: Style {
4141
size: Size::new(Val::Px(150.0), Val::Px(65.0)),
4242
// center button
43-
margin: Rect::all(Val::Auto),
43+
margin: UiRect::all(Val::Auto),
4444
// horizontally center child text
4545
justify_content: JustifyContent::Center,
4646
// vertically center child text

examples/games/alien_cake_addict.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
165165
),
166166
style: Style {
167167
position_type: PositionType::Absolute,
168-
position: Rect {
168+
position: UiRect {
169169
top: Val::Px(5.0),
170170
left: Val::Px(5.0),
171171
..default()
@@ -374,7 +374,7 @@ fn display_score(mut commands: Commands, asset_server: Res<AssetServer>, game: R
374374
commands
375375
.spawn_bundle(NodeBundle {
376376
style: Style {
377-
margin: Rect::all(Val::Auto),
377+
margin: UiRect::all(Val::Auto),
378378
justify_content: JustifyContent::Center,
379379
align_items: AlignItems::Center,
380380
..default()

examples/games/breakout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
241241
},
242242
style: Style {
243243
position_type: PositionType::Absolute,
244-
position: Rect {
244+
position: UiRect {
245245
top: SCOREBOARD_TEXT_PADDING,
246246
left: SCOREBOARD_TEXT_PADDING,
247247
..default()

examples/games/game_menu.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ mod splash {
8686
.spawn_bundle(ImageBundle {
8787
style: Style {
8888
// This will center the logo
89-
margin: Rect::all(Val::Auto),
89+
margin: UiRect::all(Val::Auto),
9090
// This will set the logo to be 200px wide, and auto adjust its height
9191
size: Size::new(Val::Px(200.0), Val::Auto),
9292
..default()
@@ -150,7 +150,7 @@ mod game {
150150
.spawn_bundle(NodeBundle {
151151
style: Style {
152152
// This will center the current node
153-
margin: Rect::all(Val::Auto),
153+
margin: UiRect::all(Val::Auto),
154154
// This will display its children in a column, from top to bottom. Unlike
155155
// in Flexbox, Bevy origin is on bottom left, so the vertical axis is reversed
156156
flex_direction: FlexDirection::ColumnReverse,
@@ -168,7 +168,7 @@ mod game {
168168
// Display two lines of text, the second one with the current settings
169169
parent.spawn_bundle(TextBundle {
170170
style: Style {
171-
margin: Rect::all(Val::Px(50.0)),
171+
margin: UiRect::all(Val::Px(50.0)),
172172
..default()
173173
},
174174
text: Text::with_section(
@@ -184,7 +184,7 @@ mod game {
184184
});
185185
parent.spawn_bundle(TextBundle {
186186
style: Style {
187-
margin: Rect::all(Val::Px(50.0)),
187+
margin: UiRect::all(Val::Px(50.0)),
188188
..default()
189189
},
190190
text: Text {
@@ -396,7 +396,7 @@ mod menu {
396396
// Common style for all buttons on the screen
397397
let button_style = Style {
398398
size: Size::new(Val::Px(250.0), Val::Px(65.0)),
399-
margin: Rect::all(Val::Px(20.0)),
399+
margin: UiRect::all(Val::Px(20.0)),
400400
justify_content: JustifyContent::Center,
401401
align_items: AlignItems::Center,
402402
..default()
@@ -406,7 +406,7 @@ mod menu {
406406
// This takes the icons out of the flexbox flow, to be positionned exactly
407407
position_type: PositionType::Absolute,
408408
// The icon will be close to the left border of the button
409-
position: Rect {
409+
position: UiRect {
410410
left: Val::Px(10.0),
411411
right: Val::Auto,
412412
top: Val::Auto,
@@ -423,7 +423,7 @@ mod menu {
423423
commands
424424
.spawn_bundle(NodeBundle {
425425
style: Style {
426-
margin: Rect::all(Val::Auto),
426+
margin: UiRect::all(Val::Auto),
427427
flex_direction: FlexDirection::ColumnReverse,
428428
align_items: AlignItems::Center,
429429
..default()
@@ -436,7 +436,7 @@ mod menu {
436436
// Display the game name
437437
parent.spawn_bundle(TextBundle {
438438
style: Style {
439-
margin: Rect::all(Val::Px(50.0)),
439+
margin: UiRect::all(Val::Px(50.0)),
440440
..default()
441441
},
442442
text: Text::with_section(
@@ -526,7 +526,7 @@ mod menu {
526526
fn settings_menu_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
527527
let button_style = Style {
528528
size: Size::new(Val::Px(200.0), Val::Px(65.0)),
529-
margin: Rect::all(Val::Px(20.0)),
529+
margin: UiRect::all(Val::Px(20.0)),
530530
justify_content: JustifyContent::Center,
531531
align_items: AlignItems::Center,
532532
..default()
@@ -540,7 +540,7 @@ mod menu {
540540
commands
541541
.spawn_bundle(NodeBundle {
542542
style: Style {
543-
margin: Rect::all(Val::Auto),
543+
margin: UiRect::all(Val::Auto),
544544
flex_direction: FlexDirection::ColumnReverse,
545545
align_items: AlignItems::Center,
546546
..default()
@@ -609,7 +609,7 @@ mod menu {
609609
) {
610610
let button_style = Style {
611611
size: Size::new(Val::Px(200.0), Val::Px(65.0)),
612-
margin: Rect::all(Val::Px(20.0)),
612+
margin: UiRect::all(Val::Px(20.0)),
613613
justify_content: JustifyContent::Center,
614614
align_items: AlignItems::Center,
615615
..default()
@@ -623,7 +623,7 @@ mod menu {
623623
commands
624624
.spawn_bundle(NodeBundle {
625625
style: Style {
626-
margin: Rect::all(Val::Auto),
626+
margin: UiRect::all(Val::Auto),
627627
flex_direction: FlexDirection::ColumnReverse,
628628
align_items: AlignItems::Center,
629629
..default()
@@ -707,7 +707,7 @@ mod menu {
707707
) {
708708
let button_style = Style {
709709
size: Size::new(Val::Px(200.0), Val::Px(65.0)),
710-
margin: Rect::all(Val::Px(20.0)),
710+
margin: UiRect::all(Val::Px(20.0)),
711711
justify_content: JustifyContent::Center,
712712
align_items: AlignItems::Center,
713713
..default()
@@ -721,7 +721,7 @@ mod menu {
721721
commands
722722
.spawn_bundle(NodeBundle {
723723
style: Style {
724-
margin: Rect::all(Val::Auto),
724+
margin: UiRect::all(Val::Auto),
725725
flex_direction: FlexDirection::ColumnReverse,
726726
align_items: AlignItems::Center,
727727
..default()

examples/stress_tests/bevymark.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
134134
},
135135
style: Style {
136136
position_type: PositionType::Absolute,
137-
position: Rect {
137+
position: UiRect {
138138
top: Val::Px(5.0),
139139
left: Val::Px(5.0),
140140
..default()

examples/ui/button.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
5050
style: Style {
5151
size: Size::new(Val::Px(150.0), Val::Px(65.0)),
5252
// center button
53-
margin: Rect::all(Val::Auto),
53+
margin: UiRect::all(Val::Auto),
5454
// horizontally center child text
5555
justify_content: JustifyContent::Center,
5656
// vertically center child text

examples/ui/font_atlas_debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn atlas_render_system(
5050
image: texture_atlas.texture.clone().into(),
5151
style: Style {
5252
position_type: PositionType::Absolute,
53-
position: Rect {
53+
position: UiRect {
5454
top: Val::Px(0.0),
5555
left: Val::Px(512.0 * x_offset),
5656
..default()

examples/ui/text.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
3333
style: Style {
3434
align_self: AlignSelf::FlexEnd,
3535
position_type: PositionType::Absolute,
36-
position: Rect {
36+
position: UiRect {
3737
bottom: Val::Px(5.0),
3838
right: Val::Px(15.0),
3939
..default()

examples/ui/text_debug.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
2828
style: Style {
2929
align_self: AlignSelf::FlexEnd,
3030
position_type: PositionType::Absolute,
31-
position: Rect {
31+
position: UiRect {
3232
top: Val::Px(5.0),
3333
left: Val::Px(15.0),
3434
..default()
@@ -50,7 +50,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
5050
style: Style {
5151
align_self: AlignSelf::FlexEnd,
5252
position_type: PositionType::Absolute,
53-
position: Rect {
53+
position: UiRect {
5454
top: Val::Px(5.0),
5555
right: Val::Px(15.0),
5656
..default()
@@ -80,7 +80,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
8080
style: Style {
8181
align_self: AlignSelf::FlexEnd,
8282
position_type: PositionType::Absolute,
83-
position: Rect {
83+
position: UiRect {
8484
bottom: Val::Px(5.0),
8585
right: Val::Px(15.0),
8686
..default()
@@ -147,7 +147,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
147147
style: Style {
148148
align_self: AlignSelf::FlexEnd,
149149
position_type: PositionType::Absolute,
150-
position: Rect {
150+
position: UiRect {
151151
bottom: Val::Px(5.0),
152152
left: Val::Px(15.0),
153153
..default()

0 commit comments

Comments
 (0)