Skip to content

Commit

Permalink
Move Rect to bevy_ui and rename it to UiRect (bevyengine#4276)
Browse files Browse the repository at this point in the history
# Objective

- Closes bevyengine#335.
- Related bevyengine#4285.
- Part of the splitting process of bevyengine#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 bevyengine#335).
- Discussion in bevyengine#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]>
  • Loading branch information
2 people authored and exjam committed May 22, 2022
1 parent faef11e commit d994d1d
Show file tree
Hide file tree
Showing 19 changed files with 84 additions and 91 deletions.
1 change: 0 additions & 1 deletion crates/bevy_math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ keywords = ["bevy"]

[dependencies]
glam = { version = "0.20.0", features = ["serde", "bytemuck"] }
bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] }
36 changes: 0 additions & 36 deletions crates/bevy_math/src/geometry.rs

This file was deleted.

7 changes: 2 additions & 5 deletions crates/bevy_math/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
mod geometry;

pub use geometry::*;
pub use glam::*;

pub mod prelude {
#[doc(hidden)]
pub use crate::{
BVec2, BVec3, BVec4, EulerRot, IVec2, IVec3, IVec4, Mat3, Mat4, Quat, Rect, UVec2, UVec3,
UVec4, Vec2, Vec3, Vec4,
BVec2, BVec3, BVec4, EulerRot, IVec2, IVec3, IVec4, Mat3, Mat4, Quat, UVec2, UVec3, UVec4,
Vec2, Vec3, Vec4,
};
}
5 changes: 2 additions & 3 deletions crates/bevy_ui/src/flex/convert.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::{
AlignContent, AlignItems, AlignSelf, Direction, Display, FlexDirection, FlexWrap,
JustifyContent, PositionType, Size, Style, Val,
JustifyContent, PositionType, Size, Style, UiRect, Val,
};
use bevy_math::Rect;

pub fn from_rect(
scale_factor: f64,
rect: Rect<Val>,
rect: UiRect<Val>,
) -> stretch::geometry::Rect<stretch::style::Dimension> {
stretch::geometry::Rect {
start: from_val(scale_factor, rect.left),
Expand Down
35 changes: 35 additions & 0 deletions crates/bevy_ui/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,41 @@ use bevy_math::Vec2;
use bevy_reflect::Reflect;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

/// A rect, as defined by its "side" locations
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
#[reflect(PartialEq)]
pub struct UiRect<T: Reflect + PartialEq> {
pub left: T,
pub right: T,
pub top: T,
pub bottom: T,
}

impl<T: Reflect + PartialEq> UiRect<T> {
pub fn all(value: T) -> Self
where
T: Clone,
{
UiRect {
left: value.clone(),
right: value.clone(),
top: value.clone(),
bottom: value,
}
}
}

impl<T: Default + Reflect + PartialEq> Default for UiRect<T> {
fn default() -> Self {
Self {
left: Default::default(),
right: Default::default(),
top: Default::default(),
bottom: Default::default(),
}
}
}

/// A two dimensional "size" as defined by a width and height
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
#[reflect(PartialEq)]
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use crate::Size;
use bevy_app::prelude::*;
use bevy_ecs::schedule::{ParallelSystemDescriptorCoercion, SystemLabel};
use bevy_input::InputSystem;
use bevy_math::Rect;
use bevy_transform::TransformSystem;
use bevy_window::ModifiesWindows;
use update::{ui_z_system, update_clipping_system};
Expand Down Expand Up @@ -71,7 +70,7 @@ impl Plugin for UiPlugin {
.register_type::<PositionType>()
.register_type::<Size<f32>>()
.register_type::<Size<Val>>()
.register_type::<Rect<Val>>()
.register_type::<UiRect<Val>>()
.register_type::<Style>()
.register_type::<UiColor>()
.register_type::<UiImage>()
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Size;
use crate::{Size, UiRect};
use bevy_asset::Handle;
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
use bevy_math::{Rect, Vec2};
use bevy_math::Vec2;
use bevy_reflect::{Reflect, ReflectDeserialize};
use bevy_render::{
color::Color,
Expand Down Expand Up @@ -90,13 +90,13 @@ pub struct Style {
/// How items align according to the main axis
pub justify_content: JustifyContent,
/// The position of the node as descrided by its Rect
pub position: Rect<Val>,
pub position: UiRect<Val>,
/// The margin of the node
pub margin: Rect<Val>,
pub margin: UiRect<Val>,
/// The padding of the node
pub padding: Rect<Val>,
pub padding: UiRect<Val>,
/// The border of the node
pub border: Rect<Val>,
pub border: UiRect<Val>,
/// Defines how much a flexbox item should grow if there's space available
pub flex_grow: f32,
/// How to shrink if there's not enough space available
Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn setup_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
style: Style {
size: Size::new(Val::Px(150.0), Val::Px(65.0)),
// center button
margin: Rect::all(Val::Auto),
margin: UiRect::all(Val::Auto),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
Expand Down
4 changes: 2 additions & 2 deletions examples/games/alien_cake_addict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
),
style: Style {
position_type: PositionType::Absolute,
position: Rect {
position: UiRect {
top: Val::Px(5.0),
left: Val::Px(5.0),
..default()
Expand Down Expand Up @@ -374,7 +374,7 @@ fn display_score(mut commands: Commands, asset_server: Res<AssetServer>, game: R
commands
.spawn_bundle(NodeBundle {
style: Style {
margin: Rect::all(Val::Auto),
margin: UiRect::all(Val::Auto),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
Expand Down
2 changes: 1 addition & 1 deletion examples/games/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
},
style: Style {
position_type: PositionType::Absolute,
position: Rect {
position: UiRect {
top: SCOREBOARD_TEXT_PADDING,
left: SCOREBOARD_TEXT_PADDING,
..default()
Expand Down
28 changes: 14 additions & 14 deletions examples/games/game_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ mod splash {
.spawn_bundle(ImageBundle {
style: Style {
// This will center the logo
margin: Rect::all(Val::Auto),
margin: UiRect::all(Val::Auto),
// This will set the logo to be 200px wide, and auto adjust its height
size: Size::new(Val::Px(200.0), Val::Auto),
..default()
Expand Down Expand Up @@ -150,7 +150,7 @@ mod game {
.spawn_bundle(NodeBundle {
style: Style {
// This will center the current node
margin: Rect::all(Val::Auto),
margin: UiRect::all(Val::Auto),
// This will display its children in a column, from top to bottom. Unlike
// in Flexbox, Bevy origin is on bottom left, so the vertical axis is reversed
flex_direction: FlexDirection::ColumnReverse,
Expand All @@ -168,7 +168,7 @@ mod game {
// Display two lines of text, the second one with the current settings
parent.spawn_bundle(TextBundle {
style: Style {
margin: Rect::all(Val::Px(50.0)),
margin: UiRect::all(Val::Px(50.0)),
..default()
},
text: Text::with_section(
Expand All @@ -184,7 +184,7 @@ mod game {
});
parent.spawn_bundle(TextBundle {
style: Style {
margin: Rect::all(Val::Px(50.0)),
margin: UiRect::all(Val::Px(50.0)),
..default()
},
text: Text {
Expand Down Expand Up @@ -396,7 +396,7 @@ mod menu {
// Common style for all buttons on the screen
let button_style = Style {
size: Size::new(Val::Px(250.0), Val::Px(65.0)),
margin: Rect::all(Val::Px(20.0)),
margin: UiRect::all(Val::Px(20.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
Expand All @@ -406,7 +406,7 @@ mod menu {
// This takes the icons out of the flexbox flow, to be positionned exactly
position_type: PositionType::Absolute,
// The icon will be close to the left border of the button
position: Rect {
position: UiRect {
left: Val::Px(10.0),
right: Val::Auto,
top: Val::Auto,
Expand All @@ -423,7 +423,7 @@ mod menu {
commands
.spawn_bundle(NodeBundle {
style: Style {
margin: Rect::all(Val::Auto),
margin: UiRect::all(Val::Auto),
flex_direction: FlexDirection::ColumnReverse,
align_items: AlignItems::Center,
..default()
Expand All @@ -436,7 +436,7 @@ mod menu {
// Display the game name
parent.spawn_bundle(TextBundle {
style: Style {
margin: Rect::all(Val::Px(50.0)),
margin: UiRect::all(Val::Px(50.0)),
..default()
},
text: Text::with_section(
Expand Down Expand Up @@ -526,7 +526,7 @@ mod menu {
fn settings_menu_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let button_style = Style {
size: Size::new(Val::Px(200.0), Val::Px(65.0)),
margin: Rect::all(Val::Px(20.0)),
margin: UiRect::all(Val::Px(20.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
Expand All @@ -540,7 +540,7 @@ mod menu {
commands
.spawn_bundle(NodeBundle {
style: Style {
margin: Rect::all(Val::Auto),
margin: UiRect::all(Val::Auto),
flex_direction: FlexDirection::ColumnReverse,
align_items: AlignItems::Center,
..default()
Expand Down Expand Up @@ -609,7 +609,7 @@ mod menu {
) {
let button_style = Style {
size: Size::new(Val::Px(200.0), Val::Px(65.0)),
margin: Rect::all(Val::Px(20.0)),
margin: UiRect::all(Val::Px(20.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
Expand All @@ -623,7 +623,7 @@ mod menu {
commands
.spawn_bundle(NodeBundle {
style: Style {
margin: Rect::all(Val::Auto),
margin: UiRect::all(Val::Auto),
flex_direction: FlexDirection::ColumnReverse,
align_items: AlignItems::Center,
..default()
Expand Down Expand Up @@ -707,7 +707,7 @@ mod menu {
) {
let button_style = Style {
size: Size::new(Val::Px(200.0), Val::Px(65.0)),
margin: Rect::all(Val::Px(20.0)),
margin: UiRect::all(Val::Px(20.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
Expand All @@ -721,7 +721,7 @@ mod menu {
commands
.spawn_bundle(NodeBundle {
style: Style {
margin: Rect::all(Val::Auto),
margin: UiRect::all(Val::Auto),
flex_direction: FlexDirection::ColumnReverse,
align_items: AlignItems::Center,
..default()
Expand Down
2 changes: 1 addition & 1 deletion examples/stress_tests/bevymark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
},
style: Style {
position_type: PositionType::Absolute,
position: Rect {
position: UiRect {
top: Val::Px(5.0),
left: Val::Px(5.0),
..default()
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
style: Style {
size: Size::new(Val::Px(150.0), Val::Px(65.0)),
// center button
margin: Rect::all(Val::Auto),
margin: UiRect::all(Val::Auto),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/font_atlas_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn atlas_render_system(
image: texture_atlas.texture.clone().into(),
style: Style {
position_type: PositionType::Absolute,
position: Rect {
position: UiRect {
top: Val::Px(0.0),
left: Val::Px(512.0 * x_offset),
..default()
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
style: Style {
align_self: AlignSelf::FlexEnd,
position_type: PositionType::Absolute,
position: Rect {
position: UiRect {
bottom: Val::Px(5.0),
right: Val::Px(15.0),
..default()
Expand Down
8 changes: 4 additions & 4 deletions examples/ui/text_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
style: Style {
align_self: AlignSelf::FlexEnd,
position_type: PositionType::Absolute,
position: Rect {
position: UiRect {
top: Val::Px(5.0),
left: Val::Px(15.0),
..default()
Expand All @@ -50,7 +50,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
style: Style {
align_self: AlignSelf::FlexEnd,
position_type: PositionType::Absolute,
position: Rect {
position: UiRect {
top: Val::Px(5.0),
right: Val::Px(15.0),
..default()
Expand Down Expand Up @@ -80,7 +80,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
style: Style {
align_self: AlignSelf::FlexEnd,
position_type: PositionType::Absolute,
position: Rect {
position: UiRect {
bottom: Val::Px(5.0),
right: Val::Px(15.0),
..default()
Expand Down Expand Up @@ -147,7 +147,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
style: Style {
align_self: AlignSelf::FlexEnd,
position_type: PositionType::Absolute,
position: Rect {
position: UiRect {
bottom: Val::Px(5.0),
left: Val::Px(15.0),
..default()
Expand Down
Loading

0 comments on commit d994d1d

Please sign in to comment.