From ea7bc05a96b06d84e4309a26e122232ef744f6cb Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 2 Feb 2023 15:48:22 +0000 Subject: [PATCH 01/25] change `Size` default width and height to `Val::Auto` --- crates/bevy_ui/src/geometry.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 291bc5301fba6..7fd113e46dd16 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -339,7 +339,7 @@ pub struct Size { } impl Size { - pub const DEFAULT: Self = Self::all(Val::DEFAULT); + pub const DEFAULT: Self = Self::all(Val::Auto); /// Creates a new [`Size`] from a width and a height. /// From 5573ea6134c970109ea23a82378b2abb6d19b73f Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 2 Feb 2023 19:48:40 +0000 Subject: [PATCH 02/25] changes: * Removed the `Undefined` variant from `Val`. * Set Val's default variant to `Auto`. * Updated ui examples. * Removed `Val::Undefined` conversion code. --- crates/bevy_ui/src/flex/convert.rs | 3 +-- crates/bevy_ui/src/geometry.rs | 21 ++++++++------- crates/bevy_ui/src/ui_node.rs | 43 ++++++++---------------------- crates/bevy_ui/src/widget/text.rs | 4 +-- examples/ui/text_debug.rs | 5 +--- examples/ui/ui.rs | 21 +++++++++------ 6 files changed, 38 insertions(+), 59 deletions(-) diff --git a/crates/bevy_ui/src/flex/convert.rs b/crates/bevy_ui/src/flex/convert.rs index c60feb97cd591..6c1c6fd3982e6 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -60,7 +60,7 @@ pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style { /// Converts a [`Val`] to a [`f32`] while respecting the scale factor. pub fn val_to_f32(scale_factor: f64, val: Val) -> f32 { match val { - Val::Undefined | Val::Auto => 0.0, + Val::Auto => 0.0, Val::Px(value) => (scale_factor * value as f64) as f32, Val::Percent(value) => value / 100.0, } @@ -71,7 +71,6 @@ pub fn from_val(scale_factor: f64, val: Val) -> taffy::style::Dimension { Val::Auto => taffy::style::Dimension::Auto, Val::Percent(value) => taffy::style::Dimension::Percent(value / 100.0), Val::Px(value) => taffy::style::Dimension::Points((scale_factor * value as f64) as f32), - Val::Undefined => taffy::style::Dimension::Undefined, } } diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 291bc5301fba6..ccc93cf9f88c8 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -134,10 +134,14 @@ pub struct UiRect { impl UiRect { pub const DEFAULT: Self = Self { - left: Val::DEFAULT, - right: Val::DEFAULT, - top: Val::DEFAULT, - bottom: Val::DEFAULT, + // left: Val::DEFAULT, + // right: Val::DEFAULT, + // top: Val::DEFAULT, + // bottom: Val::DEFAULT, + left: Val::Auto, + right: Val::Auto, + top: Val::Auto, + bottom: Val::Auto, }; /// Creates a new [`UiRect`] from the values specified. @@ -374,18 +378,15 @@ impl Size { } /// Creates a new [`Size`] where `height` takes the given value. - pub const fn height(width: Val) -> Self { + pub const fn height(height: Val) -> Self { Self { - width, - height: Val::DEFAULT, + width: Val::DEFAULT, + height, } } /// Creates a Size where both values are [`Val::Auto`]. pub const AUTO: Self = Self::all(Val::Auto); - - /// Creates a Size where both values are [`Val::Undefined`]. - pub const UNDEFINED: Self = Self::all(Val::Undefined); } impl Default for Size { diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 31f232c4d1d85..7fb5acadf7244 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -44,8 +44,6 @@ impl Default for Node { #[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)] #[reflect(PartialEq, Serialize, Deserialize)] pub enum Val { - /// No value defined - Undefined, /// Automatically determine this value Auto, /// Set this value in pixels @@ -55,7 +53,7 @@ pub enum Val { } impl Val { - pub const DEFAULT: Self = Self::Undefined; + pub const DEFAULT: Self = Self::Auto; } impl Default for Val { @@ -69,7 +67,6 @@ impl Mul for Val { fn mul(self, rhs: f32) -> Self::Output { match self { - Val::Undefined => Val::Undefined, Val::Auto => Val::Auto, Val::Px(value) => Val::Px(value * rhs), Val::Percent(value) => Val::Percent(value * rhs), @@ -80,7 +77,7 @@ impl Mul for Val { impl MulAssign for Val { fn mul_assign(&mut self, rhs: f32) { match self { - Val::Undefined | Val::Auto => {} + Val::Auto => {} Val::Px(value) | Val::Percent(value) => *value *= rhs, } } @@ -91,7 +88,6 @@ impl Div for Val { fn div(self, rhs: f32) -> Self::Output { match self { - Val::Undefined => Val::Undefined, Val::Auto => Val::Auto, Val::Px(value) => Val::Px(value / rhs), Val::Percent(value) => Val::Percent(value / rhs), @@ -102,7 +98,7 @@ impl Div for Val { impl DivAssign for Val { fn div_assign(&mut self, rhs: f32) { match self { - Val::Undefined | Val::Auto => {} + Val::Auto => {} Val::Px(value) | Val::Percent(value) => *value /= rhs, } } @@ -122,7 +118,7 @@ impl Val { /// When adding non-numeric [`Val`]s, it returns the value unchanged. pub fn try_add(&self, rhs: Val) -> Result { match (self, rhs) { - (Val::Undefined, Val::Undefined) | (Val::Auto, Val::Auto) => Ok(*self), + (Val::Auto, Val::Auto) => Ok(*self), (Val::Px(value), Val::Px(rhs_value)) => Ok(Val::Px(value + rhs_value)), (Val::Percent(value), Val::Percent(rhs_value)) => Ok(Val::Percent(value + rhs_value)), _ => Err(ValArithmeticError::NonIdenticalVariants), @@ -140,7 +136,7 @@ impl Val { /// When adding non-numeric [`Val`]s, it returns the value unchanged. pub fn try_sub(&self, rhs: Val) -> Result { match (self, rhs) { - (Val::Undefined, Val::Undefined) | (Val::Auto, Val::Auto) => Ok(*self), + (Val::Auto, Val::Auto) => Ok(*self), (Val::Px(value), Val::Px(rhs_value)) => Ok(Val::Px(value - rhs_value)), (Val::Percent(value), Val::Percent(rhs_value)) => Ok(Val::Percent(value - rhs_value)), _ => Err(ValArithmeticError::NonIdenticalVariants), @@ -287,7 +283,7 @@ impl Style { max_size: Size::AUTO, aspect_ratio: None, overflow: Overflow::DEFAULT, - gap: Size::UNDEFINED, + gap: Size::AUTO, }; } @@ -676,12 +672,10 @@ mod tests { #[test] fn val_try_add() { - let undefined_sum = Val::Undefined.try_add(Val::Undefined).unwrap(); let auto_sum = Val::Auto.try_add(Val::Auto).unwrap(); let px_sum = Val::Px(20.).try_add(Val::Px(22.)).unwrap(); let percent_sum = Val::Percent(50.).try_add(Val::Percent(50.)).unwrap(); - assert_eq!(undefined_sum, Val::Undefined); assert_eq!(auto_sum, Val::Auto); assert_eq!(px_sum, Val::Px(42.)); assert_eq!(percent_sum, Val::Percent(100.)); @@ -698,12 +692,10 @@ mod tests { #[test] fn val_try_sub() { - let undefined_sum = Val::Undefined.try_sub(Val::Undefined).unwrap(); let auto_sum = Val::Auto.try_sub(Val::Auto).unwrap(); let px_sum = Val::Px(72.).try_sub(Val::Px(30.)).unwrap(); let percent_sum = Val::Percent(100.).try_sub(Val::Percent(50.)).unwrap(); - assert_eq!(undefined_sum, Val::Undefined); assert_eq!(auto_sum, Val::Auto); assert_eq!(px_sum, Val::Px(42.)); assert_eq!(percent_sum, Val::Percent(50.)); @@ -711,9 +703,8 @@ mod tests { #[test] fn different_variant_val_try_add() { - let different_variant_sum_1 = Val::Undefined.try_add(Val::Auto); - let different_variant_sum_2 = Val::Px(50.).try_add(Val::Percent(50.)); - let different_variant_sum_3 = Val::Percent(50.).try_add(Val::Undefined); + let different_variant_sum_1 = Val::Px(50.).try_add(Val::Percent(50.)); + let different_variant_sum_2 = Val::Percent(50.).try_add(Val::Auto); assert_eq!( different_variant_sum_1, @@ -723,17 +714,13 @@ mod tests { different_variant_sum_2, Err(ValArithmeticError::NonIdenticalVariants) ); - assert_eq!( - different_variant_sum_3, - Err(ValArithmeticError::NonIdenticalVariants) - ); + } #[test] fn different_variant_val_try_sub() { - let different_variant_diff_1 = Val::Undefined.try_sub(Val::Auto); - let different_variant_diff_2 = Val::Px(50.).try_sub(Val::Percent(50.)); - let different_variant_diff_3 = Val::Percent(50.).try_sub(Val::Undefined); + let different_variant_diff_1 = Val::Px(50.).try_sub(Val::Percent(50.)); + let different_variant_diff_2 = Val::Percent(50.).try_sub(Val::Auto); assert_eq!( different_variant_diff_1, @@ -743,10 +730,6 @@ mod tests { different_variant_diff_2, Err(ValArithmeticError::NonIdenticalVariants) ); - assert_eq!( - different_variant_diff_3, - Err(ValArithmeticError::NonIdenticalVariants) - ); } #[test] @@ -768,10 +751,8 @@ mod tests { #[test] fn val_invalid_evaluation() { let size = 250.; - let evaluate_undefined = Val::Undefined.evaluate(size); let evaluate_auto = Val::Auto.evaluate(size); - assert_eq!(evaluate_undefined, Err(ValArithmeticError::NonEvaluateable)); assert_eq!(evaluate_auto, Err(ValArithmeticError::NonEvaluateable)); } @@ -813,10 +794,8 @@ mod tests { fn val_try_add_non_numeric_with_size() { let size = 250.; - let undefined_sum = Val::Undefined.try_add_with_size(Val::Undefined, size); let percent_sum = Val::Auto.try_add_with_size(Val::Auto, size); - assert_eq!(undefined_sum, Err(ValArithmeticError::NonEvaluateable)); assert_eq!(percent_sum, Err(ValArithmeticError::NonEvaluateable)); } diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index 9371c0e3399f9..68674a99b0423 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -25,9 +25,7 @@ pub fn text_constraint(min_size: Val, size: Val, max_size: Val, scale_factor: f6 match (min_size, size, max_size) { (_, _, Val::Px(max)) => scale_value(max, scale_factor), (Val::Px(min), _, _) => scale_value(min, scale_factor), - (Val::Undefined, Val::Px(size), Val::Undefined) | (Val::Auto, Val::Px(size), Val::Auto) => { - scale_value(size, scale_factor) - } + (Val::Auto, Val::Px(size), Val::Auto) => scale_value(size, scale_factor), _ => f32::MAX, } } diff --git a/examples/ui/text_debug.rs b/examples/ui/text_debug.rs index 012affe9528ca..2c19d114a92b5 100644 --- a/examples/ui/text_debug.rs +++ b/examples/ui/text_debug.rs @@ -62,10 +62,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { right: Val::Px(15.0), ..default() }, - max_size: Size { - width: Val::Px(400.), - height: Val::Undefined, - }, + max_size: Size::width(Val::Px(400.)), ..default() }) ); diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index 08d5a710f9dfc..0e3672894eef2 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -95,7 +95,7 @@ fn setup(mut commands: Commands, asset_server: Res) { }, ) .with_style(Style { - size: Size::new(Val::Undefined, Val::Px(25.)), + size: Size::height(Val::Px(25.)), margin: UiRect { left: Val::Auto, right: Val::Auto, @@ -125,7 +125,7 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { flex_direction: FlexDirection::Column, flex_grow: 1.0, - max_size: Size::UNDEFINED, + ..default() }, ..default() @@ -147,7 +147,7 @@ fn setup(mut commands: Commands, asset_server: Res) { ) .with_style(Style { flex_shrink: 0., - size: Size::new(Val::Undefined, Val::Px(20.)), + size: Size::height(Val::Px(20.)), margin: UiRect { left: Val::Auto, right: Val::Auto, @@ -168,7 +168,8 @@ fn setup(mut commands: Commands, asset_server: Res) { position: UiRect { left: Val::Px(210.0), bottom: Val::Px(10.0), - ..default() + top: Val::Auto, + right: Val::Auto, }, border: UiRect::all(Val::Px(20.0)), ..default() @@ -216,7 +217,8 @@ fn setup(mut commands: Commands, asset_server: Res) { position: UiRect { left: Val::Px(20.0), bottom: Val::Px(20.0), - ..default() + top: Val::Auto, + right: Val::Auto, }, ..default() }, @@ -230,7 +232,8 @@ fn setup(mut commands: Commands, asset_server: Res) { position: UiRect { left: Val::Px(40.0), bottom: Val::Px(40.0), - ..default() + top: Val::Auto, + right: Val::Auto, }, ..default() }, @@ -244,7 +247,8 @@ fn setup(mut commands: Commands, asset_server: Res) { position: UiRect { left: Val::Px(60.0), bottom: Val::Px(60.0), - ..default() + top: Val::Auto, + right: Val::Auto, }, ..default() }, @@ -259,7 +263,8 @@ fn setup(mut commands: Commands, asset_server: Res) { position: UiRect { left: Val::Px(80.0), bottom: Val::Px(80.0), - ..default() + top: Val::Auto, + right: Val::Auto, }, ..default() }, From 26eeca76bef2c5bfb780438519cfbdbb96716579 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Fri, 3 Feb 2023 00:50:53 +0000 Subject: [PATCH 03/25] changes: * Removed the `Undefined` variant of the `Val` enum. * Changed `UiRect::default()` to set all fields to `Val::Px(0.0)`. * Added the `Inset` type that is a copy of `UiRect` but its defaults are all `Val::Auto`. * Renamed the `position` field of `Style` to `inset` and changed its type to `Inset`. * Updated the UI examples to remove or replace any use of `Val::Undefined`. * Updated the UI examples to use `Inset`. --- crates/bevy_ui/src/flex/convert.rs | 2 +- crates/bevy_ui/src/geometry.rs | 323 ++++++++++++++++++++++++-- crates/bevy_ui/src/ui_node.rs | 8 +- examples/3d/atmospheric_fog.rs | 2 +- examples/3d/blend_modes.rs | 8 +- examples/3d/bloom.rs | 2 +- examples/3d/fog.rs | 2 +- examples/games/alien_cake_addict.rs | 2 +- examples/games/breakout.rs | 2 +- examples/games/game_menu.rs | 5 +- examples/input/text_input.rs | 2 +- examples/ios/src/lib.rs | 2 +- examples/shader/shader_prepass.rs | 2 +- examples/stress_tests/bevymark.rs | 2 +- examples/stress_tests/many_buttons.rs | 2 +- examples/ui/font_atlas_debug.rs | 4 +- examples/ui/text.rs | 2 +- examples/ui/text_debug.rs | 8 +- examples/ui/ui.rs | 27 +-- examples/ui/ui_scaling.rs | 2 +- examples/ui/window_fallthrough.rs | 2 +- examples/ui/z_index.rs | 10 +- examples/window/low_power.rs | 2 +- 23 files changed, 349 insertions(+), 74 deletions(-) diff --git a/crates/bevy_ui/src/flex/convert.rs b/crates/bevy_ui/src/flex/convert.rs index 6c1c6fd3982e6..47b371d1d38a6 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -42,7 +42,7 @@ pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style { align_self: value.align_self.into(), align_content: value.align_content.into(), justify_content: value.justify_content.into(), - position: from_rect(scale_factor, value.position), + position: from_rect(scale_factor, value.inset.into()), margin: from_rect(scale_factor, value.margin), padding: from_rect(scale_factor, value.padding), border: from_rect(scale_factor, value.border), diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index ccc93cf9f88c8..b924f4a3764a1 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -2,33 +2,33 @@ use crate::Val; use bevy_reflect::Reflect; use std::ops::{Div, DivAssign, Mul, MulAssign}; -/// A type which is commonly used to define positions, margins, paddings and borders. +/// A type which is commonly used to define insets, margins, paddings and borders. /// /// # Examples /// -/// ## Position +/// ## Inset /// -/// A position is used to determine where to place a UI element. +/// A inset is used to determine where to place a UI element. /// /// ``` /// # use bevy_ui::{UiRect, Val}; /// # use bevy_utils::default; /// # -/// let position = UiRect { +/// let inset = UiRect { /// left: Val::Px(100.0), /// top: Val::Px(50.0), /// ..default() /// }; /// ``` /// -/// If you define opposite sides of the position, the size of the UI element will automatically be calculated +/// If you define opposite sides of the inset, the size of the UI element will automatically be calculated /// if not explicitly specified. This means that if you have a [`Size`] that uses [`Val::Undefined`](crate::Val::Undefined) -/// as a width and height, the size would be determined by the window size and the values specified in the position. +/// as a width and height, the size would be determined by the window size and the values specified in the inset. /// /// ``` /// # use bevy_ui::{UiRect, Val}; /// # -/// let position = UiRect { +/// let inset = UiRect { /// left: Val::Px(100.0), /// right: Val::Px(200.0), /// top: Val::Px(300.0), @@ -37,8 +37,8 @@ use std::ops::{Div, DivAssign, Mul, MulAssign}; /// ``` /// /// To determine the width of the UI element you have to take the width of the window and subtract it by the -/// left and right values of the position. To determine the height of the UI element you have to take the height -/// of the window and subtract it by the top and bottom values of the position. If we had a window with a width +/// left and right values of the inset. To determine the height of the UI element you have to take the height +/// of the window and subtract it by the top and bottom values of the inset. If we had a window with a width /// and height of 1000px, the UI element declared above would have a width of 700px and a height of 300px. /// /// ``` @@ -46,7 +46,7 @@ use std::ops::{Div, DivAssign, Mul, MulAssign}; /// let window_width = 1000.0; /// let window_height = 1000.0; /// -/// // Values of the position +/// // Values of the inset /// let left = 100.0; /// let right = 200.0; /// let top = 300.0; @@ -60,16 +60,16 @@ use std::ops::{Div, DivAssign, Mul, MulAssign}; /// assert_eq!(ui_element_height, 300.0); /// ``` /// -/// If you define a [`Size`] and also all four sides of the position, the top and left values of the position +/// If you define a [`Size`] and also all four sides of the inset, the top and left values of the inset /// are used to determine where to place the UI element. The size will not be calculated using the bottom and -/// right values of the position because the size of the UI element is already explicitly specified. +/// right values of the inset because the size of the UI element is already explicitly specified. /// /// ``` /// # use bevy_ui::{UiRect, Size, Val, Style}; /// # use bevy_utils::default; /// # /// let style = Style { -/// position: UiRect { // Defining all four sides +/// inset: Inset { // Defining all four sides /// left: Val::Px(100.0), /// right: Val::Px(200.0), /// top: Val::Px(300.0), @@ -134,14 +134,10 @@ pub struct UiRect { impl UiRect { pub const DEFAULT: Self = Self { - // left: Val::DEFAULT, - // right: Val::DEFAULT, - // top: Val::DEFAULT, - // bottom: Val::DEFAULT, - left: Val::Auto, - right: Val::Auto, - top: Val::Auto, - bottom: Val::Auto, + left: Val::Px(0.), + right: Val::Px(0.), + top: Val::Px(0.), + bottom: Val::Px(0.), }; /// Creates a new [`UiRect`] from the values specified. @@ -330,6 +326,291 @@ impl Default for UiRect { } } +/// A [`Inset`] is used to determine where to place a UI element. +/// +/// ``` +/// # use bevy_ui::{Inset, Val}; +/// # use bevy_utils::default; +/// # +/// let inset = Inset { +/// left: Val::Px(100.0), +/// top: Val::Px(50.0), +/// ..default() +/// }; +/// ``` +/// +/// If you define opposite sides of the inset, the size of the UI element will automatically be calculated +/// if not explicitly specified. This means that if you have a [`Size`] that uses [`Val::Auto`](crate::Val::Auto) +/// as a width and height, the size would be determined by the window size and the values specified in the inset. +/// +/// ``` +/// # use bevy_ui::{Inset, Val}; +/// # +/// let inset = Inset { +/// left: Val::Px(100.0), +/// right: Val::Px(200.0), +/// top: Val::Px(300.0), +/// bottom: Val::Px(400.0), +/// }; +/// ``` +/// +/// To determine the width of the UI element you have to take the width of the window and subtract it by the +/// left and right values of the inset. To determine the height of the UI element you have to take the height +/// of the window and subtract it by the top and bottom values of the inset. If we had a window with a width +/// and height of 1000px, the UI element declared above would have a width of 700px and a height of 300px. +/// +/// ``` +/// // Size of the window +/// let window_width = 1000.0; +/// let window_height = 1000.0; +/// +/// // Values of the inset +/// let left = 100.0; +/// let right = 200.0; +/// let top = 300.0; +/// let bottom = 400.0; +/// +/// // Calculation to get the size of the UI element +/// let ui_element_width = window_width - left - right; +/// let ui_element_height = window_height - top - bottom; +/// +/// assert_eq!(ui_element_width, 700.0); +/// assert_eq!(ui_element_height, 300.0); +/// ``` +/// +/// If you define a [`Size`] and also all four sides of the inset, the top and left values of the inset +/// are used to determine where to place the UI element. The size will not be calculated using the bottom and +/// right values of the inset because the size of the UI element is already explicitly specified. +/// +/// ``` +/// # use bevy_ui::{Inset, Size, Val, Style}; +/// # use bevy_utils::default; +/// # +/// let style = Style { +/// inset: Inset { // Defining all four sides +/// left: Val::Px(100.0), +/// right: Val::Px(200.0), +/// top: Val::Px(300.0), +/// bottom: Val::Px(400.0), +/// }, +/// size: Size::new(Val::Percent(100.0), Val::Percent(50.0)), // but also explicitly specifying a size +/// ..default() +/// }; +/// ``` +#[derive(Copy, Clone, PartialEq, Debug, Reflect)] +#[reflect(PartialEq)] +pub struct Inset { + /// The value corresponding to the left side of the `Inset`. + pub left: Val, + /// The value corresponding to the right side of the `Inset`. + pub right: Val, + /// The value corresponding to the top side of the `Inset`. + pub top: Val, + /// The value corresponding to the bottom side of the `Inset`. + pub bottom: Val, +} + +impl Inset { + pub const DEFAULT: Self = Self::all(Val::Auto); + + /// Creates a new [`Inset`] from the values specified. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{Inset, Val}; + /// # + /// let inset = Inset::new( + /// Val::Px(10.0), + /// Val::Px(20.0), + /// Val::Px(30.0), + /// Val::Px(40.0), + /// ); + /// + /// assert_eq!(inset.left, Val::Px(10.0)); + /// assert_eq!(inset.right, Val::Px(20.0)); + /// assert_eq!(inset.top, Val::Px(30.0)); + /// assert_eq!(inset.bottom, Val::Px(40.0)); + /// ``` + pub const fn new(left: Val, right: Val, top: Val, bottom: Val) -> Self { + Inset { + left, + right, + top, + bottom, + } + } + + /// Creates a new [`Inset`] where all sides have the same value. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{Inset, Val}; + /// # + /// let inset = Inset::all(Val::Px(10.0)); + /// + /// assert_eq!(inset.left, Val::Px(10.0)); + /// assert_eq!(inset.right, Val::Px(10.0)); + /// assert_eq!(inset.top, Val::Px(10.0)); + /// assert_eq!(inset.bottom, Val::Px(10.0)); + /// ``` + pub const fn all(value: Val) -> Self { + Inset { + left: value, + right: value, + top: value, + bottom: value, + } + } + + /// Creates a new [`Inset`] where `left` takes the given value. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{Inset, Val}; + /// # + /// let inset = Inset::left(Val::Px(10.0)); + /// + /// assert_eq!(inset.left, Val::Px(10.0)); + /// assert_eq!(inset.right, Val::Auto); + /// assert_eq!(inset.top, Val::Auto); + /// assert_eq!(inset.bottom, Val::Auto); + /// ``` + pub fn left(value: Val) -> Self { + Inset { + left: value, + ..Default::default() + } + } + + /// Creates a new [`Inset`] where `right` takes the given value. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{Inset, Val}; + /// # + /// let inset = Inset::right(Val::Px(10.0)); + /// + /// assert_eq!(inset.left, Val::Auto); + /// assert_eq!(inset.right, Val::Px(10.0)); + /// assert_eq!(inset.top, Val::Auto); + /// assert_eq!(inset.bottom, Val::Auto); + /// ``` + pub fn right(value: Val) -> Self { + Inset { + right: value, + ..Default::default() + } + } + + /// Creates a new [`Inset`] where `top` takes the given value. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{Inset, Val}; + /// # + /// let inset = Inset::top(Val::Px(10.0)); + /// + /// assert_eq!(inset.left, Val::Auto); + /// assert_eq!(inset.right, Val::Auto); + /// assert_eq!(inset.top, Val::Px(10.0)); + /// assert_eq!(inset.bottom, Val::Auto); + /// ``` + pub fn top(value: Val) -> Self { + Inset { + top: value, + ..Default::default() + } + } + + /// Creates a new [`Inset`] where `bottom` takes the given value. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{Inset, Val}; + /// # + /// let inset = Inset::bottom(Val::Px(10.0)); + /// + /// assert_eq!(inset.left, Val::Auto); + /// assert_eq!(inset.right, Val::Auto); + /// assert_eq!(inset.top, Val::Auto);:Undefined + /// assert_eq!(inset.bottom, Val::Px(10.0)); + /// ``` + pub fn bottom(value: Val) -> Self { + Inset { + bottom: value, + ..Default::default() + } + } + + /// Creates a new [`Inset`] where `left` and `right` take the given value. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{Inset, Val}; + /// # + /// let inset = Inset::horizontal(Val::Px(10.0)); + /// + /// assert_eq!(inset.left, Val::Px(10.0)); + /// assert_eq!(inset.right, Val::Px(10.0)); + /// assert_eq!(inset.top, Val::Auto); + /// assert_eq!(inset.bottom, Val::Auto); + /// ``` + pub fn horizontal(value: Val) -> Self { + Inset { + left: value, + right: value, + ..Default::default() + } + } + + /// Creates a new [`Inset`] where `top` and `bottom` take the given value. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{Inset, Val}; + /// # + /// let inset = Inset::vertical(Val::Px(10.0)); + /// + /// assert_eq!(inset.left, Val::Auto); + /// assert_eq!(inset.right, Val::Auto); + /// assert_eq!(inset.top, Val::Px(10.0)); + /// assert_eq!(inset.bottom, Val::Px(10.0)); + /// ``` + pub fn vertical(value: Val) -> Self { + Inset { + top: value, + bottom: value, + ..Default::default() + } + } +} + +impl Default for Inset { + fn default() -> Self { + Self::DEFAULT + } +} + +impl From for UiRect { + fn from(inset: Inset) -> Self { + Self { + left: inset.left, + right: inset.right, + top: inset.top, + bottom: inset.bottom, + } + } +} + + /// A 2-dimensional area defined by a width and height. /// /// It is commonly used to define the size of a text or UI element. diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 7fb5acadf7244..ec08cab54f27a 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -1,4 +1,4 @@ -use crate::{Size, UiRect}; +use crate::{Size, UiRect, Inset}; use bevy_asset::Handle; use bevy_ecs::{prelude::Component, reflect::ReflectComponent}; use bevy_math::{Rect, Vec2}; @@ -230,8 +230,8 @@ pub struct Style { pub align_content: AlignContent, /// How items align according to the main axis pub justify_content: JustifyContent, - /// The position of the node as described by its Rect - pub position: UiRect, + /// The inset of this UI node, relative to its default position + pub inset: Inset, /// The margin of the node pub margin: UiRect, /// The padding of the node @@ -271,7 +271,7 @@ impl Style { align_self: AlignSelf::DEFAULT, align_content: AlignContent::DEFAULT, justify_content: JustifyContent::DEFAULT, - position: UiRect::DEFAULT, + inset: Inset::DEFAULT, margin: UiRect::DEFAULT, padding: UiRect::DEFAULT, border: UiRect::DEFAULT, diff --git a/examples/3d/atmospheric_fog.rs b/examples/3d/atmospheric_fog.rs index d1024e83724c3..2c88463a2aee4 100644 --- a/examples/3d/atmospheric_fog.rs +++ b/examples/3d/atmospheric_fog.rs @@ -100,7 +100,7 @@ fn setup_instructions(mut commands: Commands, asset_server: Res) { ) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { bottom: Val::Px(10.0), left: Val::Px(10.0), ..default() diff --git a/examples/3d/blend_modes.rs b/examples/3d/blend_modes.rs index 30076743e5fb1..e3c83e5dd31be 100644 --- a/examples/3d/blend_modes.rs +++ b/examples/3d/blend_modes.rs @@ -203,7 +203,7 @@ fn setup( ) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { top: Val::Px(10.0), left: Val::Px(10.0), ..default() @@ -215,7 +215,7 @@ fn setup( commands.spawn(( TextBundle::from_section("", text_style).with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { top: Val::Px(10.0), right: Val::Px(10.0), ..default() @@ -365,8 +365,8 @@ fn example_control_system( .world_to_viewport(camera_global_transform, world_position) .unwrap(); - style.position.bottom = Val::Px(viewport_position.y); - style.position.left = Val::Px(viewport_position.x); + style.inset.bottom = Val::Px(viewport_position.y); + style.inset.left = Val::Px(viewport_position.x); } let mut display = display.single_mut(); diff --git a/examples/3d/bloom.rs b/examples/3d/bloom.rs index c112ffa9cda40..6896f96e1f5d9 100644 --- a/examples/3d/bloom.rs +++ b/examples/3d/bloom.rs @@ -86,7 +86,7 @@ fn setup_scene( ) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { top: Val::Px(10.0), left: Val::Px(10.0), ..default() diff --git a/examples/3d/fog.rs b/examples/3d/fog.rs index ba9f844c124db..7c108c23b44c6 100644 --- a/examples/3d/fog.rs +++ b/examples/3d/fog.rs @@ -147,7 +147,7 @@ fn setup_instructions(mut commands: Commands, asset_server: Res) { ) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { top: Val::Px(10.0), left: Val::Px(10.0), ..default() diff --git a/examples/games/alien_cake_addict.rs b/examples/games/alien_cake_addict.rs index bc4f04bd1ac6c..0f426e3d990d5 100644 --- a/examples/games/alien_cake_addict.rs +++ b/examples/games/alien_cake_addict.rs @@ -166,7 +166,7 @@ fn setup(mut commands: Commands, asset_server: Res, mut game: ResMu ) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { top: Val::Px(5.0), left: Val::Px(5.0), ..default() diff --git a/examples/games/breakout.rs b/examples/games/breakout.rs index 263a148b66cbd..8a12e3b57f112 100644 --- a/examples/games/breakout.rs +++ b/examples/games/breakout.rs @@ -235,7 +235,7 @@ fn setup( ]) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { top: SCOREBOARD_TEXT_PADDING, left: SCOREBOARD_TEXT_PADDING, ..default() diff --git a/examples/games/game_menu.rs b/examples/games/game_menu.rs index 40e1f63ebc6bd..12525539f6b07 100644 --- a/examples/games/game_menu.rs +++ b/examples/games/game_menu.rs @@ -421,11 +421,10 @@ mod menu { // This takes the icons out of the flexbox flow, to be positioned exactly position_type: PositionType::Absolute, // The icon will be close to the left border of the button - position: UiRect { + inset: Inset { left: Val::Px(10.0), right: Val::Auto, - top: Val::Auto, - bottom: Val::Auto, + ..Default::default() }, ..default() }; diff --git a/examples/input/text_input.rs b/examples/input/text_input.rs index 0861ddb8e05d4..33fb9aa0c1b81 100644 --- a/examples/input/text_input.rs +++ b/examples/input/text_input.rs @@ -76,7 +76,7 @@ fn setup_scene(mut commands: Commands, asset_server: Res) { ]) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { top: Val::Px(10.0), left: Val::Px(10.0), ..default() diff --git a/examples/ios/src/lib.rs b/examples/ios/src/lib.rs index f7474b37433a5..8c07f2d0b7c1d 100644 --- a/examples/ios/src/lib.rs +++ b/examples/ios/src/lib.rs @@ -102,7 +102,7 @@ fn setup_scene( justify_content: JustifyContent::Center, align_items: AlignItems::Center, position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { left: Val::Px(50.0), right: Val::Px(50.0), top: Val::Auto, diff --git a/examples/shader/shader_prepass.rs b/examples/shader/shader_prepass.rs index 6e0f9a934e9ec..08261d1a15769 100644 --- a/examples/shader/shader_prepass.rs +++ b/examples/shader/shader_prepass.rs @@ -143,7 +143,7 @@ fn setup( ]) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { top: Val::Px(10.0), left: Val::Px(10.0), ..default() diff --git a/examples/stress_tests/bevymark.rs b/examples/stress_tests/bevymark.rs index d536d9721a82e..ae3413c7ac010 100644 --- a/examples/stress_tests/bevymark.rs +++ b/examples/stress_tests/bevymark.rs @@ -123,7 +123,7 @@ fn setup(mut commands: Commands, asset_server: Res) { ]) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { top: Val::Px(5.0), left: Val::Px(5.0), ..default() diff --git a/examples/stress_tests/many_buttons.rs b/examples/stress_tests/many_buttons.rs index 79c761277e834..cd0500c594cb9 100644 --- a/examples/stress_tests/many_buttons.rs +++ b/examples/stress_tests/many_buttons.rs @@ -91,7 +91,7 @@ fn spawn_button( style: Style { size: Size::new(Val::Percent(width), Val::Percent(width)), - position: UiRect { + inset: Inset { bottom: Val::Percent(100.0 / total * i as f32), left: Val::Percent(100.0 / total * j as f32), ..default() diff --git a/examples/ui/font_atlas_debug.rs b/examples/ui/font_atlas_debug.rs index 8dd72311c48f6..cfddbf7650d1d 100644 --- a/examples/ui/font_atlas_debug.rs +++ b/examples/ui/font_atlas_debug.rs @@ -51,7 +51,7 @@ fn atlas_render_system( image: texture_atlas.texture.clone().into(), style: Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { top: Val::Px(0.0), left: Val::Px(512.0 * x_offset), ..default() @@ -87,7 +87,7 @@ fn setup(mut commands: Commands, asset_server: Res, mut state: ResM background_color: Color::NONE.into(), style: Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { bottom: Val::Px(0.0), ..default() }, diff --git a/examples/ui/text.rs b/examples/ui/text.rs index a25da20132d02..e463462168d34 100644 --- a/examples/ui/text.rs +++ b/examples/ui/text.rs @@ -45,7 +45,7 @@ fn setup(mut commands: Commands, asset_server: Res) { // Set the style of the TextBundle itself. .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { bottom: Val::Px(5.0), right: Val::Px(15.0), ..default() diff --git a/examples/ui/text_debug.rs b/examples/ui/text_debug.rs index 2c19d114a92b5..e3f941dd06477 100644 --- a/examples/ui/text_debug.rs +++ b/examples/ui/text_debug.rs @@ -38,7 +38,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { ) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { top: Val::Px(5.0), left: Val::Px(15.0), ..default() @@ -57,7 +57,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { .with_text_alignment(TextAlignment::Center) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { top: Val::Px(5.0), right: Val::Px(15.0), ..default() @@ -113,7 +113,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { ]) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { bottom: Val::Px(5.0), right: Val::Px(15.0), ..default() @@ -134,7 +134,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { .with_style(Style { align_self: AlignSelf::FlexEnd, position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { bottom: Val::Px(5.0), left: Val::Px(15.0), ..default() diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index 0e3672894eef2..1ba7a90955f05 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -165,11 +165,10 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { size: Size::new(Val::Px(200.0), Val::Px(200.0)), position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { left: Val::Px(210.0), bottom: Val::Px(10.0), - top: Val::Auto, - right: Val::Auto, + ..Default::default() }, border: UiRect::all(Val::Px(20.0)), ..default() @@ -214,11 +213,10 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { size: Size::new(Val::Px(100.0), Val::Px(100.0)), position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { left: Val::Px(20.0), bottom: Val::Px(20.0), - top: Val::Auto, - right: Val::Auto, + ..Default::default() }, ..default() }, @@ -229,11 +227,10 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { size: Size::new(Val::Px(100.0), Val::Px(100.0)), position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { left: Val::Px(40.0), bottom: Val::Px(40.0), - top: Val::Auto, - right: Val::Auto, + ..Default::default() }, ..default() }, @@ -244,11 +241,10 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { size: Size::new(Val::Px(100.0), Val::Px(100.0)), position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { left: Val::Px(60.0), bottom: Val::Px(60.0), - top: Val::Auto, - right: Val::Auto, + ..Default::default() }, ..default() }, @@ -260,11 +256,10 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { size: Size::new(Val::Px(100.0), Val::Px(100.0)), position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { left: Val::Px(80.0), bottom: Val::Px(80.0), - top: Val::Auto, - right: Val::Auto, + ..Default::default() }, ..default() }, @@ -323,7 +318,7 @@ fn mouse_scroll( }; scrolling_list.position += dy; scrolling_list.position = scrolling_list.position.clamp(-max_scroll, 0.); - style.position.top = Val::Px(scrolling_list.position); + style.inset.top = Val::Px(scrolling_list.position); } } } diff --git a/examples/ui/ui_scaling.rs b/examples/ui/ui_scaling.rs index 008a992060a19..3282bcddfbf63 100644 --- a/examples/ui/ui_scaling.rs +++ b/examples/ui/ui_scaling.rs @@ -39,7 +39,7 @@ fn setup(mut commands: Commands, asset_server: ResMut) { style: Style { size: Size::new(Val::Percent(50.0), Val::Percent(50.0)), position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { left: Val::Percent(25.), top: Val::Percent(25.), ..default() diff --git a/examples/ui/window_fallthrough.rs b/examples/ui/window_fallthrough.rs index 82cc275501954..c6cb7ed966d1a 100644 --- a/examples/ui/window_fallthrough.rs +++ b/examples/ui/window_fallthrough.rs @@ -39,7 +39,7 @@ fn setup(mut commands: Commands, asset_server: Res) { // Set the style of the TextBundle itself. .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { bottom: Val::Px(5.), right: Val::Px(10.), ..default() diff --git a/examples/ui/z_index.rs b/examples/ui/z_index.rs index d15f5dcbeb1f9..c356e20c05e66 100644 --- a/examples/ui/z_index.rs +++ b/examples/ui/z_index.rs @@ -45,7 +45,7 @@ fn setup(mut commands: Commands) { background_color: Color::RED.into(), style: Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { left: Val::Px(10.0), bottom: Val::Px(40.0), ..default() @@ -63,7 +63,7 @@ fn setup(mut commands: Commands) { background_color: Color::BLUE.into(), style: Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { left: Val::Px(45.0), bottom: Val::Px(30.0), ..default() @@ -81,7 +81,7 @@ fn setup(mut commands: Commands) { background_color: Color::GREEN.into(), style: Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { left: Val::Px(70.0), bottom: Val::Px(20.0), ..default() @@ -100,7 +100,7 @@ fn setup(mut commands: Commands) { background_color: Color::PURPLE.into(), style: Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { left: Val::Px(15.0), bottom: Val::Px(10.0), ..default() @@ -119,7 +119,7 @@ fn setup(mut commands: Commands) { background_color: Color::YELLOW.into(), style: Style { position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { left: Val::Px(-15.0), bottom: Val::Px(-15.0), ..default() diff --git a/examples/window/low_power.rs b/examples/window/low_power.rs index 0e8ba1e8e3a1d..b56cfe1116c01 100644 --- a/examples/window/low_power.rs +++ b/examples/window/low_power.rs @@ -199,7 +199,7 @@ pub(crate) mod test_setup { .with_style(Style { align_self: AlignSelf::FlexStart, position_type: PositionType::Absolute, - position: UiRect { + inset: Inset { top: Val::Px(5.0), left: Val::Px(5.0), ..default() From b274e9e71745c2e09aa785727a7c4b44a912f3d8 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Fri, 3 Feb 2023 10:28:20 +0000 Subject: [PATCH 04/25] Changed inset doc comments --- crates/bevy_ui/src/geometry.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index b924f4a3764a1..bff82c48120f0 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -400,13 +400,13 @@ impl Default for UiRect { #[derive(Copy, Clone, PartialEq, Debug, Reflect)] #[reflect(PartialEq)] pub struct Inset { - /// The value corresponding to the left side of the `Inset`. + /// Distance inset from the left. pub left: Val, - /// The value corresponding to the right side of the `Inset`. + /// Distance inset from the right. pub right: Val, - /// The value corresponding to the top side of the `Inset`. + /// Distance inset from the top. pub top: Val, - /// The value corresponding to the bottom side of the `Inset`. + /// Distance inset from the bottom. pub bottom: Val, } From 9cffde934ef36cd5291f75642490fa739b19e3aa Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Fri, 3 Feb 2023 10:49:17 +0000 Subject: [PATCH 05/25] cargo fmt --all --- crates/bevy_ui/src/geometry.rs | 1 - crates/bevy_ui/src/ui_node.rs | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index bff82c48120f0..4d943148a12a8 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -610,7 +610,6 @@ impl From for UiRect { } } - /// A 2-dimensional area defined by a width and height. /// /// It is commonly used to define the size of a text or UI element. diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index ec08cab54f27a..578114ff7ccb3 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -1,4 +1,4 @@ -use crate::{Size, UiRect, Inset}; +use crate::{Inset, Size, UiRect}; use bevy_asset::Handle; use bevy_ecs::{prelude::Component, reflect::ReflectComponent}; use bevy_math::{Rect, Vec2}; @@ -230,7 +230,7 @@ pub struct Style { pub align_content: AlignContent, /// How items align according to the main axis pub justify_content: JustifyContent, - /// The inset of this UI node, relative to its default position + /// The inset of this UI node, relative to its default position pub inset: Inset, /// The margin of the node pub margin: UiRect, @@ -714,7 +714,6 @@ mod tests { different_variant_sum_2, Err(ValArithmeticError::NonIdenticalVariants) ); - } #[test] From 8ed613fe87514381734c15210b24b119358595ba Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Fri, 3 Feb 2023 11:24:23 +0000 Subject: [PATCH 06/25] fixed UiRect doc tests --- crates/bevy_ui/src/geometry.rs | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 4d943148a12a8..fb9a019cab5d2 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -22,7 +22,7 @@ use std::ops::{Div, DivAssign, Mul, MulAssign}; /// ``` /// /// If you define opposite sides of the inset, the size of the UI element will automatically be calculated -/// if not explicitly specified. This means that if you have a [`Size`] that uses [`Val::Undefined`](crate::Val::Undefined) +/// if not explicitly specified. This means that if you have a [`Size`] that uses [`Val::Auto`](crate::Val::Auto) /// as a width and height, the size would be determined by the window size and the values specified in the inset. /// /// ``` @@ -202,8 +202,8 @@ impl UiRect { /// /// assert_eq!(ui_rect.left, Val::Px(10.0)); /// assert_eq!(ui_rect.right, Val::Px(10.0)); - /// assert_eq!(ui_rect.top, Val::Undefined); - /// assert_eq!(ui_rect.bottom, Val::Undefined); + /// assert_eq!(ui_rect.top, Val::Px(0.)); + /// assert_eq!(ui_rect.bottom, Val::Px(0.)); /// ``` pub fn horizontal(value: Val) -> Self { UiRect { @@ -222,8 +222,8 @@ impl UiRect { /// # /// let ui_rect = UiRect::vertical(Val::Px(10.0)); /// - /// assert_eq!(ui_rect.left, Val::Undefined); - /// assert_eq!(ui_rect.right, Val::Undefined); + /// assert_eq!(ui_rect.left, Val::Px(0.)); + /// assert_eq!(ui_rect.right, Val::Px(0.)); /// assert_eq!(ui_rect.top, Val::Px(10.0)); /// assert_eq!(ui_rect.bottom, Val::Px(10.0)); /// ``` @@ -245,9 +245,9 @@ impl UiRect { /// let ui_rect = UiRect::left(Val::Px(10.0)); /// /// assert_eq!(ui_rect.left, Val::Px(10.0)); - /// assert_eq!(ui_rect.right, Val::Undefined); - /// assert_eq!(ui_rect.top, Val::Undefined); - /// assert_eq!(ui_rect.bottom, Val::Undefined); + /// assert_eq!(ui_rect.right, Val::Px(0.)); + /// assert_eq!(ui_rect.top, Val::Px(0.)); + /// assert_eq!(ui_rect.bottom, Val::Px(0.)); /// ``` pub fn left(value: Val) -> Self { UiRect { @@ -265,10 +265,10 @@ impl UiRect { /// # /// let ui_rect = UiRect::right(Val::Px(10.0)); /// - /// assert_eq!(ui_rect.left, Val::Undefined); + /// assert_eq!(ui_rect.left, Val::Px(0.)); /// assert_eq!(ui_rect.right, Val::Px(10.0)); - /// assert_eq!(ui_rect.top, Val::Undefined); - /// assert_eq!(ui_rect.bottom, Val::Undefined); + /// assert_eq!(ui_rect.top, Val::Px(0.)); + /// assert_eq!(ui_rect.bottom, Val::Px(0.)); /// ``` pub fn right(value: Val) -> Self { UiRect { @@ -286,10 +286,10 @@ impl UiRect { /// # /// let ui_rect = UiRect::top(Val::Px(10.0)); /// - /// assert_eq!(ui_rect.left, Val::Undefined); - /// assert_eq!(ui_rect.right, Val::Undefined); + /// assert_eq!(ui_rect.left, Val::Px(0.)); + /// assert_eq!(ui_rect.right, Val::Px(0.)); /// assert_eq!(ui_rect.top, Val::Px(10.0)); - /// assert_eq!(ui_rect.bottom, Val::Undefined); + /// assert_eq!(ui_rect.bottom, Val::Px(0.)); /// ``` pub fn top(value: Val) -> Self { UiRect { @@ -307,9 +307,9 @@ impl UiRect { /// # /// let ui_rect = UiRect::bottom(Val::Px(10.0)); /// - /// assert_eq!(ui_rect.left, Val::Undefined); - /// assert_eq!(ui_rect.right, Val::Undefined); - /// assert_eq!(ui_rect.top, Val::Undefined); + /// assert_eq!(ui_rect.left, Val::Px(0.)); + /// assert_eq!(ui_rect.right, Val::Px(0.)); + /// assert_eq!(ui_rect.top, Val::Px(0.)); /// assert_eq!(ui_rect.bottom, Val::Px(10.0)); /// ``` pub fn bottom(value: Val) -> Self { @@ -538,7 +538,7 @@ impl Inset { /// /// assert_eq!(inset.left, Val::Auto); /// assert_eq!(inset.right, Val::Auto); - /// assert_eq!(inset.top, Val::Auto);:Undefined + /// assert_eq!(inset.top, Val::Auto); /// assert_eq!(inset.bottom, Val::Px(10.0)); /// ``` pub fn bottom(value: Val) -> Self { From 9bdf85e9426b9939c9705c4989def0d9c41e4b60 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Fri, 3 Feb 2023 11:28:33 +0000 Subject: [PATCH 07/25] Another Doc comment fix --- crates/bevy_ui/src/geometry.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index fb9a019cab5d2..8409684876963 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -69,7 +69,7 @@ use std::ops::{Div, DivAssign, Mul, MulAssign}; /// # use bevy_utils::default; /// # /// let style = Style { -/// inset: Inset { // Defining all four sides +/// inset: UiRect { // Defining all four sides /// left: Val::Px(100.0), /// right: Val::Px(200.0), /// top: Val::Px(300.0), From 54f68cea8d01a08e223bb14c430dcc7e31e083b3 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Fri, 3 Feb 2023 11:35:54 +0000 Subject: [PATCH 08/25] fix doc comments --- crates/bevy_ui/src/geometry.rs | 76 +--------------------------------- 1 file changed, 1 insertion(+), 75 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 8409684876963..8753cbe416c05 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -2,84 +2,10 @@ use crate::Val; use bevy_reflect::Reflect; use std::ops::{Div, DivAssign, Mul, MulAssign}; -/// A type which is commonly used to define insets, margins, paddings and borders. +/// A type which is commonly used to define margins, paddings and borders. /// /// # Examples /// -/// ## Inset -/// -/// A inset is used to determine where to place a UI element. -/// -/// ``` -/// # use bevy_ui::{UiRect, Val}; -/// # use bevy_utils::default; -/// # -/// let inset = UiRect { -/// left: Val::Px(100.0), -/// top: Val::Px(50.0), -/// ..default() -/// }; -/// ``` -/// -/// If you define opposite sides of the inset, the size of the UI element will automatically be calculated -/// if not explicitly specified. This means that if you have a [`Size`] that uses [`Val::Auto`](crate::Val::Auto) -/// as a width and height, the size would be determined by the window size and the values specified in the inset. -/// -/// ``` -/// # use bevy_ui::{UiRect, Val}; -/// # -/// let inset = UiRect { -/// left: Val::Px(100.0), -/// right: Val::Px(200.0), -/// top: Val::Px(300.0), -/// bottom: Val::Px(400.0), -/// }; -/// ``` -/// -/// To determine the width of the UI element you have to take the width of the window and subtract it by the -/// left and right values of the inset. To determine the height of the UI element you have to take the height -/// of the window and subtract it by the top and bottom values of the inset. If we had a window with a width -/// and height of 1000px, the UI element declared above would have a width of 700px and a height of 300px. -/// -/// ``` -/// // Size of the window -/// let window_width = 1000.0; -/// let window_height = 1000.0; -/// -/// // Values of the inset -/// let left = 100.0; -/// let right = 200.0; -/// let top = 300.0; -/// let bottom = 400.0; -/// -/// // Calculation to get the size of the UI element -/// let ui_element_width = window_width - left - right; -/// let ui_element_height = window_height - top - bottom; -/// -/// assert_eq!(ui_element_width, 700.0); -/// assert_eq!(ui_element_height, 300.0); -/// ``` -/// -/// If you define a [`Size`] and also all four sides of the inset, the top and left values of the inset -/// are used to determine where to place the UI element. The size will not be calculated using the bottom and -/// right values of the inset because the size of the UI element is already explicitly specified. -/// -/// ``` -/// # use bevy_ui::{UiRect, Size, Val, Style}; -/// # use bevy_utils::default; -/// # -/// let style = Style { -/// inset: UiRect { // Defining all four sides -/// left: Val::Px(100.0), -/// right: Val::Px(200.0), -/// top: Val::Px(300.0), -/// bottom: Val::Px(400.0), -/// }, -/// size: Size::new(Val::Percent(100.0), Val::Percent(50.0)), // but also explicitly specifying a size -/// ..default() -/// }; -/// ``` -/// /// ## Margin /// /// A margin is used to create space around UI elements, outside of any defined borders. From ce5287649f5cf149777386cc869ceeef0eafde03 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Fri, 3 Feb 2023 19:40:23 +0000 Subject: [PATCH 09/25] renamed Inset to Position --- crates/bevy_ui/src/flex/convert.rs | 2 +- crates/bevy_ui/src/geometry.rs | 178 +++++++++++++------------- crates/bevy_ui/src/ui_node.rs | 8 +- examples/3d/atmospheric_fog.rs | 2 +- examples/3d/blend_modes.rs | 8 +- examples/3d/bloom.rs | 2 +- examples/3d/fog.rs | 2 +- examples/games/alien_cake_addict.rs | 2 +- examples/games/breakout.rs | 2 +- examples/games/game_menu.rs | 2 +- examples/input/text_input.rs | 2 +- examples/ios/src/lib.rs | 2 +- examples/shader/shader_prepass.rs | 2 +- examples/stress_tests/bevymark.rs | 2 +- examples/stress_tests/many_buttons.rs | 2 +- examples/ui/font_atlas_debug.rs | 4 +- examples/ui/text.rs | 2 +- examples/ui/text_debug.rs | 8 +- examples/ui/ui.rs | 59 ++++----- examples/ui/ui_scaling.rs | 2 +- examples/ui/window_fallthrough.rs | 2 +- examples/ui/z_index.rs | 10 +- examples/window/low_power.rs | 2 +- 23 files changed, 145 insertions(+), 162 deletions(-) diff --git a/crates/bevy_ui/src/flex/convert.rs b/crates/bevy_ui/src/flex/convert.rs index 47b371d1d38a6..0d717b1ed7421 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -42,7 +42,7 @@ pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style { align_self: value.align_self.into(), align_content: value.align_content.into(), justify_content: value.justify_content.into(), - position: from_rect(scale_factor, value.inset.into()), + position: from_rect(scale_factor, value.position.into()), margin: from_rect(scale_factor, value.margin), padding: from_rect(scale_factor, value.padding), border: from_rect(scale_factor, value.border), diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 8753cbe416c05..0b876e904bb69 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -252,27 +252,27 @@ impl Default for UiRect { } } -/// A [`Inset`] is used to determine where to place a UI element. +/// A [`Position`] is used to determine where to place a UI element. /// /// ``` -/// # use bevy_ui::{Inset, Val}; +/// # use bevy_ui::{Position, Val}; /// # use bevy_utils::default; /// # -/// let inset = Inset { +/// let position = Position { /// left: Val::Px(100.0), /// top: Val::Px(50.0), /// ..default() /// }; /// ``` /// -/// If you define opposite sides of the inset, the size of the UI element will automatically be calculated +/// If you define opposite sides of the position, the size of the UI element will automatically be calculated /// if not explicitly specified. This means that if you have a [`Size`] that uses [`Val::Auto`](crate::Val::Auto) -/// as a width and height, the size would be determined by the window size and the values specified in the inset. +/// as a width and height, the size would be determined by the window size and the values specified in the position. /// /// ``` -/// # use bevy_ui::{Inset, Val}; +/// # use bevy_ui::{Position, Val}; /// # -/// let inset = Inset { +/// let position = Position { /// left: Val::Px(100.0), /// right: Val::Px(200.0), /// top: Val::Px(300.0), @@ -281,8 +281,8 @@ impl Default for UiRect { /// ``` /// /// To determine the width of the UI element you have to take the width of the window and subtract it by the -/// left and right values of the inset. To determine the height of the UI element you have to take the height -/// of the window and subtract it by the top and bottom values of the inset. If we had a window with a width +/// left and right values of the position. To determine the height of the UI element you have to take the height +/// of the window and subtract it by the top and bottom values of the position. If we had a window with a width /// and height of 1000px, the UI element declared above would have a width of 700px and a height of 300px. /// /// ``` @@ -290,7 +290,7 @@ impl Default for UiRect { /// let window_width = 1000.0; /// let window_height = 1000.0; /// -/// // Values of the inset +/// // Values of the position /// let left = 100.0; /// let right = 200.0; /// let top = 300.0; @@ -304,16 +304,16 @@ impl Default for UiRect { /// assert_eq!(ui_element_height, 300.0); /// ``` /// -/// If you define a [`Size`] and also all four sides of the inset, the top and left values of the inset +/// If you define a [`Size`] and also all four sides of the position, the top and left values of the position /// are used to determine where to place the UI element. The size will not be calculated using the bottom and -/// right values of the inset because the size of the UI element is already explicitly specified. +/// right values of the position because the size of the UI element is already explicitly specified. /// /// ``` -/// # use bevy_ui::{Inset, Size, Val, Style}; +/// # use bevy_ui::{Position, Size, Val, Style}; /// # use bevy_utils::default; /// # /// let style = Style { -/// inset: Inset { // Defining all four sides +/// position: Position { // Defining all four sides /// left: Val::Px(100.0), /// right: Val::Px(200.0), /// top: Val::Px(300.0), @@ -325,41 +325,37 @@ impl Default for UiRect { /// ``` #[derive(Copy, Clone, PartialEq, Debug, Reflect)] #[reflect(PartialEq)] -pub struct Inset { - /// Distance inset from the left. +pub struct Position { pub left: Val, - /// Distance inset from the right. pub right: Val, - /// Distance inset from the top. pub top: Val, - /// Distance inset from the bottom. pub bottom: Val, } -impl Inset { +impl Position { pub const DEFAULT: Self = Self::all(Val::Auto); - /// Creates a new [`Inset`] from the values specified. + /// Creates a new [`Position`] from the values specified. /// /// # Example /// /// ``` - /// # use bevy_ui::{Inset, Val}; + /// # use bevy_ui::{Position, Val}; /// # - /// let inset = Inset::new( + /// let position = Position::new( /// Val::Px(10.0), /// Val::Px(20.0), /// Val::Px(30.0), /// Val::Px(40.0), /// ); /// - /// assert_eq!(inset.left, Val::Px(10.0)); - /// assert_eq!(inset.right, Val::Px(20.0)); - /// assert_eq!(inset.top, Val::Px(30.0)); - /// assert_eq!(inset.bottom, Val::Px(40.0)); + /// assert_eq!(position.left, Val::Px(10.0)); + /// assert_eq!(position.right, Val::Px(20.0)); + /// assert_eq!(position.top, Val::Px(30.0)); + /// assert_eq!(position.bottom, Val::Px(40.0)); /// ``` pub const fn new(left: Val, right: Val, top: Val, bottom: Val) -> Self { - Inset { + Position { left, right, top, @@ -367,22 +363,22 @@ impl Inset { } } - /// Creates a new [`Inset`] where all sides have the same value. + /// Creates a new [`Position`] where all sides have the same value. /// /// # Example /// /// ``` - /// # use bevy_ui::{Inset, Val}; + /// # use bevy_ui::{Position, Val}; /// # - /// let inset = Inset::all(Val::Px(10.0)); + /// let position = Position::all(Val::Px(10.0)); /// - /// assert_eq!(inset.left, Val::Px(10.0)); - /// assert_eq!(inset.right, Val::Px(10.0)); - /// assert_eq!(inset.top, Val::Px(10.0)); - /// assert_eq!(inset.bottom, Val::Px(10.0)); + /// assert_eq!(position.left, Val::Px(10.0)); + /// assert_eq!(position.right, Val::Px(10.0)); + /// assert_eq!(position.top, Val::Px(10.0)); + /// assert_eq!(position.bottom, Val::Px(10.0)); /// ``` pub const fn all(value: Val) -> Self { - Inset { + Position { left: value, right: value, top: value, @@ -390,128 +386,128 @@ impl Inset { } } - /// Creates a new [`Inset`] where `left` takes the given value. + /// Creates a new [`Position`] where `left` takes the given value. /// /// # Example /// /// ``` - /// # use bevy_ui::{Inset, Val}; + /// # use bevy_ui::{Position, Val}; /// # - /// let inset = Inset::left(Val::Px(10.0)); + /// let position = Position::left(Val::Px(10.0)); /// - /// assert_eq!(inset.left, Val::Px(10.0)); - /// assert_eq!(inset.right, Val::Auto); - /// assert_eq!(inset.top, Val::Auto); - /// assert_eq!(inset.bottom, Val::Auto); + /// assert_eq!(position.left, Val::Px(10.0)); + /// assert_eq!(position.right, Val::Auto); + /// assert_eq!(position.top, Val::Auto); + /// assert_eq!(position.bottom, Val::Auto); /// ``` pub fn left(value: Val) -> Self { - Inset { + Position { left: value, ..Default::default() } } - /// Creates a new [`Inset`] where `right` takes the given value. + /// Creates a new [`Position`] where `right` takes the given value. /// /// # Example /// /// ``` - /// # use bevy_ui::{Inset, Val}; + /// # use bevy_ui::{Position, Val}; /// # - /// let inset = Inset::right(Val::Px(10.0)); + /// let position = Position::right(Val::Px(10.0)); /// - /// assert_eq!(inset.left, Val::Auto); - /// assert_eq!(inset.right, Val::Px(10.0)); - /// assert_eq!(inset.top, Val::Auto); - /// assert_eq!(inset.bottom, Val::Auto); + /// assert_eq!(position.left, Val::Auto); + /// assert_eq!(position.right, Val::Px(10.0)); + /// assert_eq!(position.top, Val::Auto); + /// assert_eq!(position.bottom, Val::Auto); /// ``` pub fn right(value: Val) -> Self { - Inset { + Position { right: value, ..Default::default() } } - /// Creates a new [`Inset`] where `top` takes the given value. + /// Creates a new [`Position`] where `top` takes the given value. /// /// # Example /// /// ``` - /// # use bevy_ui::{Inset, Val}; + /// # use bevy_ui::{Position, Val}; /// # - /// let inset = Inset::top(Val::Px(10.0)); + /// let position = Position::top(Val::Px(10.0)); /// - /// assert_eq!(inset.left, Val::Auto); - /// assert_eq!(inset.right, Val::Auto); - /// assert_eq!(inset.top, Val::Px(10.0)); - /// assert_eq!(inset.bottom, Val::Auto); + /// assert_eq!(position.left, Val::Auto); + /// assert_eq!(position.right, Val::Auto); + /// assert_eq!(position.top, Val::Px(10.0)); + /// assert_eq!(position.bottom, Val::Auto); /// ``` pub fn top(value: Val) -> Self { - Inset { + Position { top: value, ..Default::default() } } - /// Creates a new [`Inset`] where `bottom` takes the given value. + /// Creates a new [`Position`] where `bottom` takes the given value. /// /// # Example /// /// ``` - /// # use bevy_ui::{Inset, Val}; + /// # use bevy_ui::{Position, Val}; /// # - /// let inset = Inset::bottom(Val::Px(10.0)); + /// let position = Position::bottom(Val::Px(10.0)); /// - /// assert_eq!(inset.left, Val::Auto); - /// assert_eq!(inset.right, Val::Auto); - /// assert_eq!(inset.top, Val::Auto); - /// assert_eq!(inset.bottom, Val::Px(10.0)); + /// assert_eq!(position.left, Val::Auto); + /// assert_eq!(position.right, Val::Auto); + /// assert_eq!(position.top, Val::Auto); + /// assert_eq!(position.bottom, Val::Px(10.0)); /// ``` pub fn bottom(value: Val) -> Self { - Inset { + Position { bottom: value, ..Default::default() } } - /// Creates a new [`Inset`] where `left` and `right` take the given value. + /// Creates a new [`Position`] where `left` and `right` take the given value. /// /// # Example /// /// ``` - /// # use bevy_ui::{Inset, Val}; + /// # use bevy_ui::{Position, Val}; /// # - /// let inset = Inset::horizontal(Val::Px(10.0)); + /// let position = Position::horizontal(Val::Px(10.0)); /// - /// assert_eq!(inset.left, Val::Px(10.0)); - /// assert_eq!(inset.right, Val::Px(10.0)); - /// assert_eq!(inset.top, Val::Auto); - /// assert_eq!(inset.bottom, Val::Auto); + /// assert_eq!(position.left, Val::Px(10.0)); + /// assert_eq!(position.right, Val::Px(10.0)); + /// assert_eq!(position.top, Val::Auto); + /// assert_eq!(position.bottom, Val::Auto); /// ``` pub fn horizontal(value: Val) -> Self { - Inset { + Position { left: value, right: value, ..Default::default() } } - /// Creates a new [`Inset`] where `top` and `bottom` take the given value. + /// Creates a new [`Position`] where `top` and `bottom` take the given value. /// /// # Example /// /// ``` - /// # use bevy_ui::{Inset, Val}; + /// # use bevy_ui::{Position, Val}; /// # - /// let inset = Inset::vertical(Val::Px(10.0)); + /// let position = Position::vertical(Val::Px(10.0)); /// - /// assert_eq!(inset.left, Val::Auto); - /// assert_eq!(inset.right, Val::Auto); - /// assert_eq!(inset.top, Val::Px(10.0)); - /// assert_eq!(inset.bottom, Val::Px(10.0)); + /// assert_eq!(position.left, Val::Auto); + /// assert_eq!(position.right, Val::Auto); + /// assert_eq!(position.top, Val::Px(10.0)); + /// assert_eq!(position.bottom, Val::Px(10.0)); /// ``` pub fn vertical(value: Val) -> Self { - Inset { + Position { top: value, bottom: value, ..Default::default() @@ -519,19 +515,19 @@ impl Inset { } } -impl Default for Inset { +impl Default for Position { fn default() -> Self { Self::DEFAULT } } -impl From for UiRect { - fn from(inset: Inset) -> Self { +impl From for UiRect { + fn from(position: Position) -> Self { Self { - left: inset.left, - right: inset.right, - top: inset.top, - bottom: inset.bottom, + left: position.left, + right: position.right, + top: position.top, + bottom: position.bottom, } } } diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 578114ff7ccb3..4373351a98e90 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -1,4 +1,4 @@ -use crate::{Inset, Size, UiRect}; +use crate::{Position, Size, UiRect}; use bevy_asset::Handle; use bevy_ecs::{prelude::Component, reflect::ReflectComponent}; use bevy_math::{Rect, Vec2}; @@ -230,8 +230,8 @@ pub struct Style { pub align_content: AlignContent, /// How items align according to the main axis pub justify_content: JustifyContent, - /// The inset of this UI node, relative to its default position - pub inset: Inset, + /// The position of the node + pub position: Position, /// The margin of the node pub margin: UiRect, /// The padding of the node @@ -271,7 +271,7 @@ impl Style { align_self: AlignSelf::DEFAULT, align_content: AlignContent::DEFAULT, justify_content: JustifyContent::DEFAULT, - inset: Inset::DEFAULT, + position: Position::DEFAULT, margin: UiRect::DEFAULT, padding: UiRect::DEFAULT, border: UiRect::DEFAULT, diff --git a/examples/3d/atmospheric_fog.rs b/examples/3d/atmospheric_fog.rs index 2c88463a2aee4..64a1e19f0e2e4 100644 --- a/examples/3d/atmospheric_fog.rs +++ b/examples/3d/atmospheric_fog.rs @@ -100,7 +100,7 @@ fn setup_instructions(mut commands: Commands, asset_server: Res) { ) .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { bottom: Val::Px(10.0), left: Val::Px(10.0), ..default() diff --git a/examples/3d/blend_modes.rs b/examples/3d/blend_modes.rs index e3c83e5dd31be..1c555adfc858d 100644 --- a/examples/3d/blend_modes.rs +++ b/examples/3d/blend_modes.rs @@ -203,7 +203,7 @@ fn setup( ) .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { top: Val::Px(10.0), left: Val::Px(10.0), ..default() @@ -215,7 +215,7 @@ fn setup( commands.spawn(( TextBundle::from_section("", text_style).with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { top: Val::Px(10.0), right: Val::Px(10.0), ..default() @@ -365,8 +365,8 @@ fn example_control_system( .world_to_viewport(camera_global_transform, world_position) .unwrap(); - style.inset.bottom = Val::Px(viewport_position.y); - style.inset.left = Val::Px(viewport_position.x); + style.position.bottom = Val::Px(viewport_position.y); + style.position.left = Val::Px(viewport_position.x); } let mut display = display.single_mut(); diff --git a/examples/3d/bloom.rs b/examples/3d/bloom.rs index 6896f96e1f5d9..cdc6a380446dc 100644 --- a/examples/3d/bloom.rs +++ b/examples/3d/bloom.rs @@ -86,7 +86,7 @@ fn setup_scene( ) .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { top: Val::Px(10.0), left: Val::Px(10.0), ..default() diff --git a/examples/3d/fog.rs b/examples/3d/fog.rs index 7c108c23b44c6..c52575ad407ba 100644 --- a/examples/3d/fog.rs +++ b/examples/3d/fog.rs @@ -147,7 +147,7 @@ fn setup_instructions(mut commands: Commands, asset_server: Res) { ) .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { top: Val::Px(10.0), left: Val::Px(10.0), ..default() diff --git a/examples/games/alien_cake_addict.rs b/examples/games/alien_cake_addict.rs index 0f426e3d990d5..dcf191a2a93f0 100644 --- a/examples/games/alien_cake_addict.rs +++ b/examples/games/alien_cake_addict.rs @@ -166,7 +166,7 @@ fn setup(mut commands: Commands, asset_server: Res, mut game: ResMu ) .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { top: Val::Px(5.0), left: Val::Px(5.0), ..default() diff --git a/examples/games/breakout.rs b/examples/games/breakout.rs index 8a12e3b57f112..cc31eb3e22d17 100644 --- a/examples/games/breakout.rs +++ b/examples/games/breakout.rs @@ -235,7 +235,7 @@ fn setup( ]) .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { top: SCOREBOARD_TEXT_PADDING, left: SCOREBOARD_TEXT_PADDING, ..default() diff --git a/examples/games/game_menu.rs b/examples/games/game_menu.rs index 12525539f6b07..cdb2420fc450b 100644 --- a/examples/games/game_menu.rs +++ b/examples/games/game_menu.rs @@ -421,7 +421,7 @@ mod menu { // This takes the icons out of the flexbox flow, to be positioned exactly position_type: PositionType::Absolute, // The icon will be close to the left border of the button - inset: Inset { + position: Position { left: Val::Px(10.0), right: Val::Auto, ..Default::default() diff --git a/examples/input/text_input.rs b/examples/input/text_input.rs index 33fb9aa0c1b81..935ce1dbf4ee7 100644 --- a/examples/input/text_input.rs +++ b/examples/input/text_input.rs @@ -76,7 +76,7 @@ fn setup_scene(mut commands: Commands, asset_server: Res) { ]) .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { top: Val::Px(10.0), left: Val::Px(10.0), ..default() diff --git a/examples/ios/src/lib.rs b/examples/ios/src/lib.rs index 8c07f2d0b7c1d..91dd6a0c8116e 100644 --- a/examples/ios/src/lib.rs +++ b/examples/ios/src/lib.rs @@ -102,7 +102,7 @@ fn setup_scene( justify_content: JustifyContent::Center, align_items: AlignItems::Center, position_type: PositionType::Absolute, - inset: Inset { + position: Position { left: Val::Px(50.0), right: Val::Px(50.0), top: Val::Auto, diff --git a/examples/shader/shader_prepass.rs b/examples/shader/shader_prepass.rs index 08261d1a15769..0e9173600676d 100644 --- a/examples/shader/shader_prepass.rs +++ b/examples/shader/shader_prepass.rs @@ -143,7 +143,7 @@ fn setup( ]) .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { top: Val::Px(10.0), left: Val::Px(10.0), ..default() diff --git a/examples/stress_tests/bevymark.rs b/examples/stress_tests/bevymark.rs index ae3413c7ac010..3819f047bdade 100644 --- a/examples/stress_tests/bevymark.rs +++ b/examples/stress_tests/bevymark.rs @@ -123,7 +123,7 @@ fn setup(mut commands: Commands, asset_server: Res) { ]) .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { top: Val::Px(5.0), left: Val::Px(5.0), ..default() diff --git a/examples/stress_tests/many_buttons.rs b/examples/stress_tests/many_buttons.rs index cd0500c594cb9..416f36ff9cb26 100644 --- a/examples/stress_tests/many_buttons.rs +++ b/examples/stress_tests/many_buttons.rs @@ -91,7 +91,7 @@ fn spawn_button( style: Style { size: Size::new(Val::Percent(width), Val::Percent(width)), - inset: Inset { + position: Position { bottom: Val::Percent(100.0 / total * i as f32), left: Val::Percent(100.0 / total * j as f32), ..default() diff --git a/examples/ui/font_atlas_debug.rs b/examples/ui/font_atlas_debug.rs index cfddbf7650d1d..0092583af40b5 100644 --- a/examples/ui/font_atlas_debug.rs +++ b/examples/ui/font_atlas_debug.rs @@ -51,7 +51,7 @@ fn atlas_render_system( image: texture_atlas.texture.clone().into(), style: Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { top: Val::Px(0.0), left: Val::Px(512.0 * x_offset), ..default() @@ -87,7 +87,7 @@ fn setup(mut commands: Commands, asset_server: Res, mut state: ResM background_color: Color::NONE.into(), style: Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { bottom: Val::Px(0.0), ..default() }, diff --git a/examples/ui/text.rs b/examples/ui/text.rs index e463462168d34..85e66b5f95f14 100644 --- a/examples/ui/text.rs +++ b/examples/ui/text.rs @@ -45,7 +45,7 @@ fn setup(mut commands: Commands, asset_server: Res) { // Set the style of the TextBundle itself. .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { bottom: Val::Px(5.0), right: Val::Px(15.0), ..default() diff --git a/examples/ui/text_debug.rs b/examples/ui/text_debug.rs index e3f941dd06477..89dee8699a1ae 100644 --- a/examples/ui/text_debug.rs +++ b/examples/ui/text_debug.rs @@ -38,7 +38,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { ) .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { top: Val::Px(5.0), left: Val::Px(15.0), ..default() @@ -57,7 +57,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { .with_text_alignment(TextAlignment::Center) .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { top: Val::Px(5.0), right: Val::Px(15.0), ..default() @@ -113,7 +113,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { ]) .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { bottom: Val::Px(5.0), right: Val::Px(15.0), ..default() @@ -134,7 +134,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { .with_style(Style { align_self: AlignSelf::FlexEnd, position_type: PositionType::Absolute, - inset: Inset { + position: Position { bottom: Val::Px(5.0), left: Val::Px(15.0), ..default() diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index 1ba7a90955f05..904c9ba28169a 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -35,7 +35,7 @@ fn setup(mut commands: Commands, asset_server: Res) { parent .spawn(NodeBundle { style: Style { - size: Size::new(Val::Px(200.0), Val::Percent(100.0)), + size: Size::width(Val::Px(200.0)), border: UiRect::all(Val::Px(2.0)), ..default() }, @@ -47,7 +47,7 @@ fn setup(mut commands: Commands, asset_server: Res) { parent .spawn(NodeBundle { style: Style { - size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), + size: Size::width(Val::Percent(100.0)), ..default() }, background_color: Color::rgb(0.15, 0.15, 0.15).into(), @@ -77,7 +77,8 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { flex_direction: FlexDirection::Column, justify_content: JustifyContent::Center, - size: Size::new(Val::Px(200.0), Val::Percent(100.0)), + align_items: AlignItems::Center, + size: Size::width(Val::Px(200.0)), ..default() }, background_color: Color::rgb(0.15, 0.15, 0.15).into(), @@ -94,23 +95,14 @@ fn setup(mut commands: Commands, asset_server: Res) { color: Color::WHITE, }, ) - .with_style(Style { - size: Size::height(Val::Px(25.)), - margin: UiRect { - left: Val::Auto, - right: Val::Auto, - ..default() - }, - ..default() - }), ); // List with hidden overflow parent .spawn(NodeBundle { style: Style { flex_direction: FlexDirection::Column, - align_self: AlignSelf::Center, - size: Size::new(Val::Percent(100.0), Val::Percent(50.0)), + align_self: AlignSelf::Stretch, + size: Size::height(Val::Percent(50.0)), overflow: Overflow::Hidden, ..default() }, @@ -125,7 +117,7 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { flex_direction: FlexDirection::Column, flex_grow: 1.0, - + align_items: AlignItems::Center, ..default() }, ..default() @@ -148,11 +140,6 @@ fn setup(mut commands: Commands, asset_server: Res) { .with_style(Style { flex_shrink: 0., size: Size::height(Val::Px(20.)), - margin: UiRect { - left: Val::Auto, - right: Val::Auto, - ..default() - }, ..default() }), ); @@ -163,9 +150,9 @@ fn setup(mut commands: Commands, asset_server: Res) { parent .spawn(NodeBundle { style: Style { - size: Size::new(Val::Px(200.0), Val::Px(200.0)), + size: Size::all(Val::Px(200.0)), position_type: PositionType::Absolute, - inset: Inset { + position: Position { left: Val::Px(210.0), bottom: Val::Px(10.0), ..Default::default() @@ -179,7 +166,7 @@ fn setup(mut commands: Commands, asset_server: Res) { .with_children(|parent| { parent.spawn(NodeBundle { style: Style { - size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), + size: Size::all(Val::Percent(100.0)), ..default() }, background_color: Color::rgb(0.8, 0.8, 1.0).into(), @@ -190,7 +177,7 @@ fn setup(mut commands: Commands, asset_server: Res) { parent .spawn(NodeBundle { style: Style { - size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), + size: Size::all(Val::Percent(100.0)), position_type: PositionType::Absolute, align_items: AlignItems::Center, justify_content: JustifyContent::Center, @@ -202,7 +189,7 @@ fn setup(mut commands: Commands, asset_server: Res) { parent .spawn(NodeBundle { style: Style { - size: Size::new(Val::Px(100.0), Val::Px(100.0)), + size: Size::all(Val::Px(100.0)), ..default() }, background_color: Color::rgb(1.0, 0.0, 0.0).into(), @@ -211,9 +198,9 @@ fn setup(mut commands: Commands, asset_server: Res) { .with_children(|parent| { parent.spawn(NodeBundle { style: Style { - size: Size::new(Val::Px(100.0), Val::Px(100.0)), + size: Size::all(Val::Percent(100.0)), position_type: PositionType::Absolute, - inset: Inset { + position: Position { left: Val::Px(20.0), bottom: Val::Px(20.0), ..Default::default() @@ -225,9 +212,9 @@ fn setup(mut commands: Commands, asset_server: Res) { }); parent.spawn(NodeBundle { style: Style { - size: Size::new(Val::Px(100.0), Val::Px(100.0)), + size: Size::all(Val::Percent(100.0)), position_type: PositionType::Absolute, - inset: Inset { + position: Position { left: Val::Px(40.0), bottom: Val::Px(40.0), ..Default::default() @@ -239,9 +226,9 @@ fn setup(mut commands: Commands, asset_server: Res) { }); parent.spawn(NodeBundle { style: Style { - size: Size::new(Val::Px(100.0), Val::Px(100.0)), + size: Size::all(Val::Percent(100.0)), position_type: PositionType::Absolute, - inset: Inset { + position: Position { left: Val::Px(60.0), bottom: Val::Px(60.0), ..Default::default() @@ -254,9 +241,9 @@ fn setup(mut commands: Commands, asset_server: Res) { // alpha test parent.spawn(NodeBundle { style: Style { - size: Size::new(Val::Px(100.0), Val::Px(100.0)), + size: Size::all(Val::Percent(100.0)), position_type: PositionType::Absolute, - inset: Inset { + position: Position { left: Val::Px(80.0), bottom: Val::Px(80.0), ..Default::default() @@ -272,7 +259,7 @@ fn setup(mut commands: Commands, asset_server: Res) { parent .spawn(NodeBundle { style: Style { - size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), + size: Size::width(Val::Percent(100.0)), position_type: PositionType::Absolute, justify_content: JustifyContent::Center, align_items: AlignItems::FlexStart, @@ -284,7 +271,7 @@ fn setup(mut commands: Commands, asset_server: Res) { // bevy logo (image) parent.spawn(ImageBundle { style: Style { - size: Size::new(Val::Px(500.0), Val::Auto), + size: Size::width(Val::Px(500.0)), ..default() }, image: asset_server.load("branding/bevy_logo_dark_big.png").into(), @@ -318,7 +305,7 @@ fn mouse_scroll( }; scrolling_list.position += dy; scrolling_list.position = scrolling_list.position.clamp(-max_scroll, 0.); - style.inset.top = Val::Px(scrolling_list.position); + style.position.top = Val::Px(scrolling_list.position); } } } diff --git a/examples/ui/ui_scaling.rs b/examples/ui/ui_scaling.rs index 3282bcddfbf63..fe804f349bac6 100644 --- a/examples/ui/ui_scaling.rs +++ b/examples/ui/ui_scaling.rs @@ -39,7 +39,7 @@ fn setup(mut commands: Commands, asset_server: ResMut) { style: Style { size: Size::new(Val::Percent(50.0), Val::Percent(50.0)), position_type: PositionType::Absolute, - inset: Inset { + position: Position { left: Val::Percent(25.), top: Val::Percent(25.), ..default() diff --git a/examples/ui/window_fallthrough.rs b/examples/ui/window_fallthrough.rs index c6cb7ed966d1a..463665d4e434d 100644 --- a/examples/ui/window_fallthrough.rs +++ b/examples/ui/window_fallthrough.rs @@ -39,7 +39,7 @@ fn setup(mut commands: Commands, asset_server: Res) { // Set the style of the TextBundle itself. .with_style(Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { bottom: Val::Px(5.), right: Val::Px(10.), ..default() diff --git a/examples/ui/z_index.rs b/examples/ui/z_index.rs index c356e20c05e66..3d89b12372d27 100644 --- a/examples/ui/z_index.rs +++ b/examples/ui/z_index.rs @@ -45,7 +45,7 @@ fn setup(mut commands: Commands) { background_color: Color::RED.into(), style: Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { left: Val::Px(10.0), bottom: Val::Px(40.0), ..default() @@ -63,7 +63,7 @@ fn setup(mut commands: Commands) { background_color: Color::BLUE.into(), style: Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { left: Val::Px(45.0), bottom: Val::Px(30.0), ..default() @@ -81,7 +81,7 @@ fn setup(mut commands: Commands) { background_color: Color::GREEN.into(), style: Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { left: Val::Px(70.0), bottom: Val::Px(20.0), ..default() @@ -100,7 +100,7 @@ fn setup(mut commands: Commands) { background_color: Color::PURPLE.into(), style: Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { left: Val::Px(15.0), bottom: Val::Px(10.0), ..default() @@ -119,7 +119,7 @@ fn setup(mut commands: Commands) { background_color: Color::YELLOW.into(), style: Style { position_type: PositionType::Absolute, - inset: Inset { + position: Position { left: Val::Px(-15.0), bottom: Val::Px(-15.0), ..default() diff --git a/examples/window/low_power.rs b/examples/window/low_power.rs index b56cfe1116c01..497dd5f6692f2 100644 --- a/examples/window/low_power.rs +++ b/examples/window/low_power.rs @@ -199,7 +199,7 @@ pub(crate) mod test_setup { .with_style(Style { align_self: AlignSelf::FlexStart, position_type: PositionType::Absolute, - inset: Inset { + position: Position { top: Val::Px(5.0), left: Val::Px(5.0), ..default() From abdef25da13536b9e377a718f2f22e34e1512d48 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Fri, 3 Feb 2023 19:56:02 +0000 Subject: [PATCH 10/25] cargo fmt --all --- examples/ui/ui.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index 904c9ba28169a..f2b57db5f33bf 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -86,16 +86,14 @@ fn setup(mut commands: Commands, asset_server: Res) { }) .with_children(|parent| { // Title - parent.spawn( - TextBundle::from_section( - "Scrolling list", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 25., - color: Color::WHITE, - }, - ) - ); + parent.spawn(TextBundle::from_section( + "Scrolling list", + TextStyle { + font: asset_server.load("fonts/FiraSans-Bold.ttf"), + font_size: 25., + color: Color::WHITE, + }, + )); // List with hidden overflow parent .spawn(NodeBundle { From 5e814cf49a32ef2b5c60f5c58863c7074c600174 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Fri, 3 Feb 2023 20:09:09 +0000 Subject: [PATCH 11/25] update ui example --- examples/ui/ui.rs | 62 +++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index f2b57db5f33bf..c6eaa0f516af3 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -24,7 +24,7 @@ fn setup(mut commands: Commands, asset_server: Res) { commands .spawn(NodeBundle { style: Style { - size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), + size: Size::all(Val::Percent(100.)), justify_content: JustifyContent::SpaceBetween, ..default() }, @@ -35,8 +35,8 @@ fn setup(mut commands: Commands, asset_server: Res) { parent .spawn(NodeBundle { style: Style { - size: Size::width(Val::Px(200.0)), - border: UiRect::all(Val::Px(2.0)), + size: Size::width(Val::Px(200.)), + border: UiRect::all(Val::Px(2.)), ..default() }, background_color: Color::rgb(0.65, 0.65, 0.65).into(), @@ -47,7 +47,7 @@ fn setup(mut commands: Commands, asset_server: Res) { parent .spawn(NodeBundle { style: Style { - size: Size::width(Val::Percent(100.0)), + size: Size::width(Val::Percent(100.)), ..default() }, background_color: Color::rgb(0.15, 0.15, 0.15).into(), @@ -65,7 +65,7 @@ fn setup(mut commands: Commands, asset_server: Res) { }, ) .with_style(Style { - margin: UiRect::all(Val::Px(5.0)), + margin: UiRect::all(Val::Px(5.)), ..default() }), ); @@ -78,7 +78,7 @@ fn setup(mut commands: Commands, asset_server: Res) { flex_direction: FlexDirection::Column, justify_content: JustifyContent::Center, align_items: AlignItems::Center, - size: Size::width(Val::Px(200.0)), + size: Size::width(Val::Px(200.)), ..default() }, background_color: Color::rgb(0.15, 0.15, 0.15).into(), @@ -100,7 +100,7 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { flex_direction: FlexDirection::Column, align_self: AlignSelf::Stretch, - size: Size::height(Val::Percent(50.0)), + size: Size::height(Val::Percent(50.)), overflow: Overflow::Hidden, ..default() }, @@ -148,26 +148,26 @@ fn setup(mut commands: Commands, asset_server: Res) { parent .spawn(NodeBundle { style: Style { - size: Size::all(Val::Px(200.0)), + size: Size::all(Val::Px(200.)), position_type: PositionType::Absolute, position: Position { - left: Val::Px(210.0), - bottom: Val::Px(10.0), + left: Val::Px(210.), + bottom: Val::Px(10.), ..Default::default() }, - border: UiRect::all(Val::Px(20.0)), + border: UiRect::all(Val::Px(20.)), ..default() }, - background_color: Color::rgb(0.4, 0.4, 1.0).into(), + background_color: Color::rgb(0.4, 0.4, 1.).into(), ..default() }) .with_children(|parent| { parent.spawn(NodeBundle { style: Style { - size: Size::all(Val::Percent(100.0)), + size: Size::all(Val::Percent(100.)), ..default() }, - background_color: Color::rgb(0.8, 0.8, 1.0).into(), + background_color: Color::rgb(0.8, 0.8, 1.).into(), ..default() }); }); @@ -175,7 +175,7 @@ fn setup(mut commands: Commands, asset_server: Res) { parent .spawn(NodeBundle { style: Style { - size: Size::all(Val::Percent(100.0)), + size: Size::all(Val::Percent(100.)), position_type: PositionType::Absolute, align_items: AlignItems::Center, justify_content: JustifyContent::Center, @@ -187,20 +187,20 @@ fn setup(mut commands: Commands, asset_server: Res) { parent .spawn(NodeBundle { style: Style { - size: Size::all(Val::Px(100.0)), + size: Size::all(Val::Px(100.)), ..default() }, - background_color: Color::rgb(1.0, 0.0, 0.0).into(), + background_color: Color::rgb(1.0, 0.0, 0.).into(), ..default() }) .with_children(|parent| { parent.spawn(NodeBundle { style: Style { - size: Size::all(Val::Percent(100.0)), + size: Size::all(Val::Percent(100.)), position_type: PositionType::Absolute, position: Position { - left: Val::Px(20.0), - bottom: Val::Px(20.0), + left: Val::Px(20.), + bottom: Val::Px(20.), ..Default::default() }, ..default() @@ -210,11 +210,11 @@ fn setup(mut commands: Commands, asset_server: Res) { }); parent.spawn(NodeBundle { style: Style { - size: Size::all(Val::Percent(100.0)), + size: Size::all(Val::Percent(100.)), position_type: PositionType::Absolute, position: Position { - left: Val::Px(40.0), - bottom: Val::Px(40.0), + left: Val::Px(40.), + bottom: Val::Px(40.), ..Default::default() }, ..default() @@ -224,11 +224,11 @@ fn setup(mut commands: Commands, asset_server: Res) { }); parent.spawn(NodeBundle { style: Style { - size: Size::all(Val::Percent(100.0)), + size: Size::all(Val::Percent(100.)), position_type: PositionType::Absolute, position: Position { - left: Val::Px(60.0), - bottom: Val::Px(60.0), + left: Val::Px(60.), + bottom: Val::Px(60.), ..Default::default() }, ..default() @@ -239,11 +239,11 @@ fn setup(mut commands: Commands, asset_server: Res) { // alpha test parent.spawn(NodeBundle { style: Style { - size: Size::all(Val::Percent(100.0)), + size: Size::all(Val::Percent(100.)), position_type: PositionType::Absolute, position: Position { - left: Val::Px(80.0), - bottom: Val::Px(80.0), + left: Val::Px(80.), + bottom: Val::Px(80.), ..Default::default() }, ..default() @@ -257,7 +257,7 @@ fn setup(mut commands: Commands, asset_server: Res) { parent .spawn(NodeBundle { style: Style { - size: Size::width(Val::Percent(100.0)), + size: Size::width(Val::Percent(100.)), position_type: PositionType::Absolute, justify_content: JustifyContent::Center, align_items: AlignItems::FlexStart, @@ -269,7 +269,7 @@ fn setup(mut commands: Commands, asset_server: Res) { // bevy logo (image) parent.spawn(ImageBundle { style: Style { - size: Size::width(Val::Px(500.0)), + size: Size::width(Val::Px(500.)), ..default() }, image: asset_server.load("branding/bevy_logo_dark_big.png").into(), From 2087be6ca79a03c22882baf95e06dea4a47e0b5e Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Fri, 3 Feb 2023 22:01:05 +0000 Subject: [PATCH 12/25] Removed Position struct, added its fields directly to Style --- crates/bevy_ui/src/flex/convert.rs | 5 +- crates/bevy_ui/src/geometry.rs | 380 ++++++-------------------- crates/bevy_ui/src/ui_node.rs | 15 +- examples/3d/atmospheric_fog.rs | 7 +- examples/3d/blend_modes.rs | 19 +- examples/3d/bloom.rs | 7 +- examples/3d/fog.rs | 7 +- examples/games/alien_cake_addict.rs | 7 +- examples/games/breakout.rs | 7 +- examples/games/game_menu.rs | 7 +- examples/input/text_input.rs | 7 +- examples/ios/src/lib.rs | 10 +- examples/shader/shader_prepass.rs | 7 +- examples/stress_tests/bevymark.rs | 7 +- examples/stress_tests/many_buttons.rs | 8 +- examples/ui/font_atlas_debug.rs | 12 +- examples/ui/text.rs | 7 +- examples/ui/text_debug.rs | 28 +- examples/ui/ui.rs | 37 +-- examples/ui/ui_scaling.rs | 7 +- examples/ui/window_fallthrough.rs | 7 +- examples/ui/z_index.rs | 35 +-- examples/window/low_power.rs | 7 +- 23 files changed, 171 insertions(+), 469 deletions(-) diff --git a/crates/bevy_ui/src/flex/convert.rs b/crates/bevy_ui/src/flex/convert.rs index 0d717b1ed7421..335074d2b4fde 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -42,7 +42,10 @@ pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style { align_self: value.align_self.into(), align_content: value.align_content.into(), justify_content: value.justify_content.into(), - position: from_rect(scale_factor, value.position.into()), + position: from_rect( + scale_factor, + UiRect::new(value.left, value.right, value.top, value.bottom), + ), margin: from_rect(scale_factor, value.margin), padding: from_rect(scale_factor, value.padding), border: from_rect(scale_factor, value.border), diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 0b876e904bb69..ee6499a7510ad 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -2,10 +2,84 @@ use crate::Val; use bevy_reflect::Reflect; use std::ops::{Div, DivAssign, Mul, MulAssign}; -/// A type which is commonly used to define margins, paddings and borders. +/// A type which is commonly used to define positions, margins, paddings and borders. /// /// # Examples /// +/// ## Position +/// +/// A position is used to determine where to place a UI element. +/// +/// ``` +/// # use bevy_ui::{UiRect, Val}; +/// # use bevy_utils::default; +/// # +/// let position = UiRect { +/// left: Val::Px(100.0), +/// top: Val::Px(50.0), +/// ..default() +/// }; +/// ``` +/// +/// If you define opposite sides of the position, the size of the UI element will automatically be calculated +/// if not explicitly specified. This means that if you have a [`Size`] that uses [`Val::Auto`](crate::Val::Auto) +/// as a width and height, the size would be determined by the window size and the values specified in the position. +/// +/// ``` +/// # use bevy_ui::{UiRect, Val}; +/// # +/// let position = UiRect { +/// left: Val::Px(100.0), +/// right: Val::Px(200.0), +/// top: Val::Px(300.0), +/// bottom: Val::Px(400.0), +/// }; +/// ``` +/// +/// To determine the width of the UI element you have to take the width of the window and subtract it by the +/// left and right values of the position. To determine the height of the UI element you have to take the height +/// of the window and subtract it by the top and bottom values of the position. If we had a window with a width +/// and height of 1000px, the UI element declared above would have a width of 700px and a height of 300px. +/// +/// ``` +/// // Size of the window +/// let window_width = 1000.0; +/// let window_height = 1000.0; +/// +/// // Values of the position +/// let left = 100.0; +/// let right = 200.0; +/// let top = 300.0; +/// let bottom = 400.0; +/// +/// // Calculation to get the size of the UI element +/// let ui_element_width = window_width - left - right; +/// let ui_element_height = window_height - top - bottom; +/// +/// assert_eq!(ui_element_width, 700.0); +/// assert_eq!(ui_element_height, 300.0); +/// ``` +/// +/// If you define a [`Size`] and also all four sides of the position, the top and left values of the position +/// are used to determine where to place the UI element. The size will not be calculated using the bottom and +/// right values of the position because the size of the UI element is already explicitly specified. +/// +/// ``` +/// # use bevy_ui::{UiRect, Size, Val, Style}; +/// # use bevy_utils::default; +/// # +/// let style = Style { +/// position: UiRect { // Defining all four sides +/// left: Val::Px(100.0), +/// right: Val::Px(200.0), +/// top: Val::Px(300.0), +/// bottom: Val::Px(400.0), +/// }, +/// size: Size::new(Val::Percent(100.0), Val::Percent(50.0)), // but also explicitly specifying a size +/// ..default() +/// }; +/// ``` +/// /// ## Margin /// /// A margin is used to create space around UI elements, outside of any defined borders. @@ -171,9 +245,9 @@ impl UiRect { /// let ui_rect = UiRect::left(Val::Px(10.0)); /// /// assert_eq!(ui_rect.left, Val::Px(10.0)); - /// assert_eq!(ui_rect.right, Val::Px(0.)); - /// assert_eq!(ui_rect.top, Val::Px(0.)); - /// assert_eq!(ui_rect.bottom, Val::Px(0.)); + /// assert_eq!(ui_rect.right, Val::Undefined); + /// assert_eq!(ui_rect.top, Val::Undefined); + /// assert_eq!(ui_rect.bottom, Val::Undefined); /// ``` pub fn left(value: Val) -> Self { UiRect { @@ -191,10 +265,10 @@ impl UiRect { /// # /// let ui_rect = UiRect::right(Val::Px(10.0)); /// - /// assert_eq!(ui_rect.left, Val::Px(0.)); + /// assert_eq!(ui_rect.left, Val::Undefined); /// assert_eq!(ui_rect.right, Val::Px(10.0)); - /// assert_eq!(ui_rect.top, Val::Px(0.)); - /// assert_eq!(ui_rect.bottom, Val::Px(0.)); + /// assert_eq!(ui_rect.top, Val::Undefined); + /// assert_eq!(ui_rect.bottom, Val::Undefined); /// ``` pub fn right(value: Val) -> Self { UiRect { @@ -212,10 +286,10 @@ impl UiRect { /// # /// let ui_rect = UiRect::top(Val::Px(10.0)); /// - /// assert_eq!(ui_rect.left, Val::Px(0.)); - /// assert_eq!(ui_rect.right, Val::Px(0.)); + /// assert_eq!(ui_rect.left, Val::Undefined); + /// assert_eq!(ui_rect.right, Val::Undefined); /// assert_eq!(ui_rect.top, Val::Px(10.0)); - /// assert_eq!(ui_rect.bottom, Val::Px(0.)); + /// assert_eq!(ui_rect.bottom, Val::Undefined); /// ``` pub fn top(value: Val) -> Self { UiRect { @@ -233,9 +307,9 @@ impl UiRect { /// # /// let ui_rect = UiRect::bottom(Val::Px(10.0)); /// - /// assert_eq!(ui_rect.left, Val::Px(0.)); - /// assert_eq!(ui_rect.right, Val::Px(0.)); - /// assert_eq!(ui_rect.top, Val::Px(0.)); + /// assert_eq!(ui_rect.left, Val::Undefined); + /// assert_eq!(ui_rect.right, Val::Undefined); + /// assert_eq!(ui_rect.top, Val::Undefined); /// assert_eq!(ui_rect.bottom, Val::Px(10.0)); /// ``` pub fn bottom(value: Val) -> Self { @@ -252,286 +326,6 @@ impl Default for UiRect { } } -/// A [`Position`] is used to determine where to place a UI element. -/// -/// ``` -/// # use bevy_ui::{Position, Val}; -/// # use bevy_utils::default; -/// # -/// let position = Position { -/// left: Val::Px(100.0), -/// top: Val::Px(50.0), -/// ..default() -/// }; -/// ``` -/// -/// If you define opposite sides of the position, the size of the UI element will automatically be calculated -/// if not explicitly specified. This means that if you have a [`Size`] that uses [`Val::Auto`](crate::Val::Auto) -/// as a width and height, the size would be determined by the window size and the values specified in the position. -/// -/// ``` -/// # use bevy_ui::{Position, Val}; -/// # -/// let position = Position { -/// left: Val::Px(100.0), -/// right: Val::Px(200.0), -/// top: Val::Px(300.0), -/// bottom: Val::Px(400.0), -/// }; -/// ``` -/// -/// To determine the width of the UI element you have to take the width of the window and subtract it by the -/// left and right values of the position. To determine the height of the UI element you have to take the height -/// of the window and subtract it by the top and bottom values of the position. If we had a window with a width -/// and height of 1000px, the UI element declared above would have a width of 700px and a height of 300px. -/// -/// ``` -/// // Size of the window -/// let window_width = 1000.0; -/// let window_height = 1000.0; -/// -/// // Values of the position -/// let left = 100.0; -/// let right = 200.0; -/// let top = 300.0; -/// let bottom = 400.0; -/// -/// // Calculation to get the size of the UI element -/// let ui_element_width = window_width - left - right; -/// let ui_element_height = window_height - top - bottom; -/// -/// assert_eq!(ui_element_width, 700.0); -/// assert_eq!(ui_element_height, 300.0); -/// ``` -/// -/// If you define a [`Size`] and also all four sides of the position, the top and left values of the position -/// are used to determine where to place the UI element. The size will not be calculated using the bottom and -/// right values of the position because the size of the UI element is already explicitly specified. -/// -/// ``` -/// # use bevy_ui::{Position, Size, Val, Style}; -/// # use bevy_utils::default; -/// # -/// let style = Style { -/// position: Position { // Defining all four sides -/// left: Val::Px(100.0), -/// right: Val::Px(200.0), -/// top: Val::Px(300.0), -/// bottom: Val::Px(400.0), -/// }, -/// size: Size::new(Val::Percent(100.0), Val::Percent(50.0)), // but also explicitly specifying a size -/// ..default() -/// }; -/// ``` -#[derive(Copy, Clone, PartialEq, Debug, Reflect)] -#[reflect(PartialEq)] -pub struct Position { - pub left: Val, - pub right: Val, - pub top: Val, - pub bottom: Val, -} - -impl Position { - pub const DEFAULT: Self = Self::all(Val::Auto); - - /// Creates a new [`Position`] from the values specified. - /// - /// # Example - /// - /// ``` - /// # use bevy_ui::{Position, Val}; - /// # - /// let position = Position::new( - /// Val::Px(10.0), - /// Val::Px(20.0), - /// Val::Px(30.0), - /// Val::Px(40.0), - /// ); - /// - /// assert_eq!(position.left, Val::Px(10.0)); - /// assert_eq!(position.right, Val::Px(20.0)); - /// assert_eq!(position.top, Val::Px(30.0)); - /// assert_eq!(position.bottom, Val::Px(40.0)); - /// ``` - pub const fn new(left: Val, right: Val, top: Val, bottom: Val) -> Self { - Position { - left, - right, - top, - bottom, - } - } - - /// Creates a new [`Position`] where all sides have the same value. - /// - /// # Example - /// - /// ``` - /// # use bevy_ui::{Position, Val}; - /// # - /// let position = Position::all(Val::Px(10.0)); - /// - /// assert_eq!(position.left, Val::Px(10.0)); - /// assert_eq!(position.right, Val::Px(10.0)); - /// assert_eq!(position.top, Val::Px(10.0)); - /// assert_eq!(position.bottom, Val::Px(10.0)); - /// ``` - pub const fn all(value: Val) -> Self { - Position { - left: value, - right: value, - top: value, - bottom: value, - } - } - - /// Creates a new [`Position`] where `left` takes the given value. - /// - /// # Example - /// - /// ``` - /// # use bevy_ui::{Position, Val}; - /// # - /// let position = Position::left(Val::Px(10.0)); - /// - /// assert_eq!(position.left, Val::Px(10.0)); - /// assert_eq!(position.right, Val::Auto); - /// assert_eq!(position.top, Val::Auto); - /// assert_eq!(position.bottom, Val::Auto); - /// ``` - pub fn left(value: Val) -> Self { - Position { - left: value, - ..Default::default() - } - } - - /// Creates a new [`Position`] where `right` takes the given value. - /// - /// # Example - /// - /// ``` - /// # use bevy_ui::{Position, Val}; - /// # - /// let position = Position::right(Val::Px(10.0)); - /// - /// assert_eq!(position.left, Val::Auto); - /// assert_eq!(position.right, Val::Px(10.0)); - /// assert_eq!(position.top, Val::Auto); - /// assert_eq!(position.bottom, Val::Auto); - /// ``` - pub fn right(value: Val) -> Self { - Position { - right: value, - ..Default::default() - } - } - - /// Creates a new [`Position`] where `top` takes the given value. - /// - /// # Example - /// - /// ``` - /// # use bevy_ui::{Position, Val}; - /// # - /// let position = Position::top(Val::Px(10.0)); - /// - /// assert_eq!(position.left, Val::Auto); - /// assert_eq!(position.right, Val::Auto); - /// assert_eq!(position.top, Val::Px(10.0)); - /// assert_eq!(position.bottom, Val::Auto); - /// ``` - pub fn top(value: Val) -> Self { - Position { - top: value, - ..Default::default() - } - } - - /// Creates a new [`Position`] where `bottom` takes the given value. - /// - /// # Example - /// - /// ``` - /// # use bevy_ui::{Position, Val}; - /// # - /// let position = Position::bottom(Val::Px(10.0)); - /// - /// assert_eq!(position.left, Val::Auto); - /// assert_eq!(position.right, Val::Auto); - /// assert_eq!(position.top, Val::Auto); - /// assert_eq!(position.bottom, Val::Px(10.0)); - /// ``` - pub fn bottom(value: Val) -> Self { - Position { - bottom: value, - ..Default::default() - } - } - - /// Creates a new [`Position`] where `left` and `right` take the given value. - /// - /// # Example - /// - /// ``` - /// # use bevy_ui::{Position, Val}; - /// # - /// let position = Position::horizontal(Val::Px(10.0)); - /// - /// assert_eq!(position.left, Val::Px(10.0)); - /// assert_eq!(position.right, Val::Px(10.0)); - /// assert_eq!(position.top, Val::Auto); - /// assert_eq!(position.bottom, Val::Auto); - /// ``` - pub fn horizontal(value: Val) -> Self { - Position { - left: value, - right: value, - ..Default::default() - } - } - - /// Creates a new [`Position`] where `top` and `bottom` take the given value. - /// - /// # Example - /// - /// ``` - /// # use bevy_ui::{Position, Val}; - /// # - /// let position = Position::vertical(Val::Px(10.0)); - /// - /// assert_eq!(position.left, Val::Auto); - /// assert_eq!(position.right, Val::Auto); - /// assert_eq!(position.top, Val::Px(10.0)); - /// assert_eq!(position.bottom, Val::Px(10.0)); - /// ``` - pub fn vertical(value: Val) -> Self { - Position { - top: value, - bottom: value, - ..Default::default() - } - } -} - -impl Default for Position { - fn default() -> Self { - Self::DEFAULT - } -} - -impl From for UiRect { - fn from(position: Position) -> Self { - Self { - left: position.left, - right: position.right, - top: position.top, - bottom: position.bottom, - } - } -} - /// A 2-dimensional area defined by a width and height. /// /// It is commonly used to define the size of a text or UI element. diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 4373351a98e90..5b58b2019d516 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -1,4 +1,4 @@ -use crate::{Position, Size, UiRect}; +use crate::{Size, UiRect}; use bevy_asset::Handle; use bevy_ecs::{prelude::Component, reflect::ReflectComponent}; use bevy_math::{Rect, Vec2}; @@ -215,6 +215,10 @@ pub struct Style { pub display: Display, /// Whether to arrange this node relative to other nodes, or positioned absolutely pub position_type: PositionType, + pub left: Val, + pub right: Val, + pub top: Val, + pub bottom: Val, /// Which direction the content of this node should go pub direction: Direction, /// Whether to use column or row layout @@ -230,8 +234,6 @@ pub struct Style { pub align_content: AlignContent, /// How items align according to the main axis pub justify_content: JustifyContent, - /// The position of the node - pub position: Position, /// The margin of the node pub margin: UiRect, /// The padding of the node @@ -256,7 +258,7 @@ pub struct Style { pub overflow: Overflow, /// The size of the gutters between the rows and columns of the flexbox layout /// - /// Values of `Size::UNDEFINED` and `Size::AUTO` are treated as zero. + /// A value of `Size::AUTO` is treated as zero. pub gap: Size, } @@ -264,6 +266,10 @@ impl Style { pub const DEFAULT: Self = Self { display: Display::DEFAULT, position_type: PositionType::DEFAULT, + left: Val::Auto, + right: Val::Auto, + top: Val::Auto, + bottom: Val::Auto, direction: Direction::DEFAULT, flex_direction: FlexDirection::DEFAULT, flex_wrap: FlexWrap::DEFAULT, @@ -271,7 +277,6 @@ impl Style { align_self: AlignSelf::DEFAULT, align_content: AlignContent::DEFAULT, justify_content: JustifyContent::DEFAULT, - position: Position::DEFAULT, margin: UiRect::DEFAULT, padding: UiRect::DEFAULT, border: UiRect::DEFAULT, diff --git a/examples/3d/atmospheric_fog.rs b/examples/3d/atmospheric_fog.rs index 64a1e19f0e2e4..e69d87c8239c0 100644 --- a/examples/3d/atmospheric_fog.rs +++ b/examples/3d/atmospheric_fog.rs @@ -100,11 +100,8 @@ fn setup_instructions(mut commands: Commands, asset_server: Res) { ) .with_style(Style { position_type: PositionType::Absolute, - position: Position { - bottom: Val::Px(10.0), - left: Val::Px(10.0), - ..default() - }, + bottom: Val::Px(10.0), + left: Val::Px(10.0), ..default() }),)); } diff --git a/examples/3d/blend_modes.rs b/examples/3d/blend_modes.rs index 1c555adfc858d..c236e6fde4b9f 100644 --- a/examples/3d/blend_modes.rs +++ b/examples/3d/blend_modes.rs @@ -203,23 +203,16 @@ fn setup( ) .with_style(Style { position_type: PositionType::Absolute, - position: Position { - top: Val::Px(10.0), - left: Val::Px(10.0), - ..default() - }, + top: Val::Px(10.0), + left: Val::Px(10.0), ..default() }), ); commands.spawn(( TextBundle::from_section("", text_style).with_style(Style { - position_type: PositionType::Absolute, - position: Position { - top: Val::Px(10.0), - right: Val::Px(10.0), - ..default() - }, + top: Val::Px(10.0), + right: Val::Px(10.0), ..default() }), ExampleDisplay, @@ -365,8 +358,8 @@ fn example_control_system( .world_to_viewport(camera_global_transform, world_position) .unwrap(); - style.position.bottom = Val::Px(viewport_position.y); - style.position.left = Val::Px(viewport_position.x); + style.bottom = Val::Px(viewport_position.y); + style.left = Val::Px(viewport_position.x); } let mut display = display.single_mut(); diff --git a/examples/3d/bloom.rs b/examples/3d/bloom.rs index cdc6a380446dc..2f13afb1b7ead 100644 --- a/examples/3d/bloom.rs +++ b/examples/3d/bloom.rs @@ -86,11 +86,8 @@ fn setup_scene( ) .with_style(Style { position_type: PositionType::Absolute, - position: Position { - top: Val::Px(10.0), - left: Val::Px(10.0), - ..default() - }, + top: Val::Px(10.0), + left: Val::Px(10.0), ..default() }), ); diff --git a/examples/3d/fog.rs b/examples/3d/fog.rs index c52575ad407ba..ddad74a72052a 100644 --- a/examples/3d/fog.rs +++ b/examples/3d/fog.rs @@ -147,11 +147,8 @@ fn setup_instructions(mut commands: Commands, asset_server: Res) { ) .with_style(Style { position_type: PositionType::Absolute, - position: Position { - top: Val::Px(10.0), - left: Val::Px(10.0), - ..default() - }, + top: Val::Px(10.0), + left: Val::Px(10.0), ..default() }),)); } diff --git a/examples/games/alien_cake_addict.rs b/examples/games/alien_cake_addict.rs index dcf191a2a93f0..5c6aea7def4a7 100644 --- a/examples/games/alien_cake_addict.rs +++ b/examples/games/alien_cake_addict.rs @@ -166,11 +166,8 @@ fn setup(mut commands: Commands, asset_server: Res, mut game: ResMu ) .with_style(Style { position_type: PositionType::Absolute, - position: Position { - top: Val::Px(5.0), - left: Val::Px(5.0), - ..default() - }, + top: Val::Px(5.0), + left: Val::Px(5.0), ..default() }), ); diff --git a/examples/games/breakout.rs b/examples/games/breakout.rs index cc31eb3e22d17..d9565c763b07a 100644 --- a/examples/games/breakout.rs +++ b/examples/games/breakout.rs @@ -235,11 +235,8 @@ fn setup( ]) .with_style(Style { position_type: PositionType::Absolute, - position: Position { - top: SCOREBOARD_TEXT_PADDING, - left: SCOREBOARD_TEXT_PADDING, - ..default() - }, + top: SCOREBOARD_TEXT_PADDING, + left: SCOREBOARD_TEXT_PADDING, ..default() }), ); diff --git a/examples/games/game_menu.rs b/examples/games/game_menu.rs index cdb2420fc450b..3aeac5cc6b6da 100644 --- a/examples/games/game_menu.rs +++ b/examples/games/game_menu.rs @@ -421,11 +421,8 @@ mod menu { // This takes the icons out of the flexbox flow, to be positioned exactly position_type: PositionType::Absolute, // The icon will be close to the left border of the button - position: Position { - left: Val::Px(10.0), - right: Val::Auto, - ..Default::default() - }, + left: Val::Px(10.0), + right: Val::Auto, ..default() }; let button_text_style = TextStyle { diff --git a/examples/input/text_input.rs b/examples/input/text_input.rs index 935ce1dbf4ee7..b41660cd8782a 100644 --- a/examples/input/text_input.rs +++ b/examples/input/text_input.rs @@ -76,11 +76,8 @@ fn setup_scene(mut commands: Commands, asset_server: Res) { ]) .with_style(Style { position_type: PositionType::Absolute, - position: Position { - top: Val::Px(10.0), - left: Val::Px(10.0), - ..default() - }, + top: Val::Px(10.0), + left: Val::Px(10.0), ..default() }), ); diff --git a/examples/ios/src/lib.rs b/examples/ios/src/lib.rs index 91dd6a0c8116e..4255fd09c50b8 100644 --- a/examples/ios/src/lib.rs +++ b/examples/ios/src/lib.rs @@ -102,12 +102,10 @@ fn setup_scene( justify_content: JustifyContent::Center, align_items: AlignItems::Center, position_type: PositionType::Absolute, - position: Position { - left: Val::Px(50.0), - right: Val::Px(50.0), - top: Val::Auto, - bottom: Val::Px(50.0), - }, + left: Val::Px(50.0), + right: Val::Px(50.0), + top: Val::Auto, + bottom: Val::Px(50.0), ..default() }, ..default() diff --git a/examples/shader/shader_prepass.rs b/examples/shader/shader_prepass.rs index 0e9173600676d..2c213573ace6c 100644 --- a/examples/shader/shader_prepass.rs +++ b/examples/shader/shader_prepass.rs @@ -143,11 +143,8 @@ fn setup( ]) .with_style(Style { position_type: PositionType::Absolute, - position: Position { - top: Val::Px(10.0), - left: Val::Px(10.0), - ..default() - }, + top: Val::Px(10.0), + left: Val::Px(10.0), ..default() }), ); diff --git a/examples/stress_tests/bevymark.rs b/examples/stress_tests/bevymark.rs index 3819f047bdade..3752fec604c27 100644 --- a/examples/stress_tests/bevymark.rs +++ b/examples/stress_tests/bevymark.rs @@ -123,11 +123,8 @@ fn setup(mut commands: Commands, asset_server: Res) { ]) .with_style(Style { position_type: PositionType::Absolute, - position: Position { - top: Val::Px(5.0), - left: Val::Px(5.0), - ..default() - }, + top: Val::Px(5.0), + left: Val::Px(5.0), ..default() }), StatsText, diff --git a/examples/stress_tests/many_buttons.rs b/examples/stress_tests/many_buttons.rs index 416f36ff9cb26..54fc2d44e6ce2 100644 --- a/examples/stress_tests/many_buttons.rs +++ b/examples/stress_tests/many_buttons.rs @@ -90,12 +90,8 @@ fn spawn_button( ButtonBundle { style: Style { size: Size::new(Val::Percent(width), Val::Percent(width)), - - position: Position { - bottom: Val::Percent(100.0 / total * i as f32), - left: Val::Percent(100.0 / total * j as f32), - ..default() - }, + bottom: Val::Percent(100.0 / total * i as f32), + left: Val::Percent(100.0 / total * j as f32), align_items: AlignItems::Center, position_type: PositionType::Absolute, ..default() diff --git a/examples/ui/font_atlas_debug.rs b/examples/ui/font_atlas_debug.rs index 0092583af40b5..28b3bede6caca 100644 --- a/examples/ui/font_atlas_debug.rs +++ b/examples/ui/font_atlas_debug.rs @@ -51,11 +51,8 @@ fn atlas_render_system( image: texture_atlas.texture.clone().into(), style: Style { position_type: PositionType::Absolute, - position: Position { - top: Val::Px(0.0), - left: Val::Px(512.0 * x_offset), - ..default() - }, + top: Val::Px(0.0), + left: Val::Px(512.0 * x_offset), ..default() }, ..default() @@ -87,10 +84,7 @@ fn setup(mut commands: Commands, asset_server: Res, mut state: ResM background_color: Color::NONE.into(), style: Style { position_type: PositionType::Absolute, - position: Position { - bottom: Val::Px(0.0), - ..default() - }, + bottom: Val::Px(0.0), ..default() }, ..default() diff --git a/examples/ui/text.rs b/examples/ui/text.rs index 85e66b5f95f14..f7b16133878ca 100644 --- a/examples/ui/text.rs +++ b/examples/ui/text.rs @@ -45,11 +45,8 @@ fn setup(mut commands: Commands, asset_server: Res) { // Set the style of the TextBundle itself. .with_style(Style { position_type: PositionType::Absolute, - position: Position { - bottom: Val::Px(5.0), - right: Val::Px(15.0), - ..default() - }, + bottom: Val::Px(5.0), + right: Val::Px(15.0), ..default() }), ColorText, diff --git a/examples/ui/text_debug.rs b/examples/ui/text_debug.rs index 89dee8699a1ae..504b3e752ad82 100644 --- a/examples/ui/text_debug.rs +++ b/examples/ui/text_debug.rs @@ -38,11 +38,8 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { ) .with_style(Style { position_type: PositionType::Absolute, - position: Position { - top: Val::Px(5.0), - left: Val::Px(15.0), - ..default() - }, + top: Val::Px(5.0), + left: Val::Px(15.0), ..default() }), ); @@ -57,11 +54,8 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { .with_text_alignment(TextAlignment::Center) .with_style(Style { position_type: PositionType::Absolute, - position: Position { - top: Val::Px(5.0), - right: Val::Px(15.0), - ..default() - }, + top: Val::Px(5.0), + right: Val::Px(15.0), max_size: Size::width(Val::Px(400.)), ..default() }) @@ -113,11 +107,8 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { ]) .with_style(Style { position_type: PositionType::Absolute, - position: Position { - bottom: Val::Px(5.0), - right: Val::Px(15.0), - ..default() - }, + bottom: Val::Px(5.0), + right: Val::Px(15.0), ..default() }), TextChanges, @@ -134,11 +125,8 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { .with_style(Style { align_self: AlignSelf::FlexEnd, position_type: PositionType::Absolute, - position: Position { - bottom: Val::Px(5.0), - left: Val::Px(15.0), - ..default() - }, + bottom: Val::Px(5.0), + left: Val::Px(15.0), size: Size { width: Val::Px(200.0), ..default() diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index c6eaa0f516af3..342692e246438 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -150,11 +150,8 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { size: Size::all(Val::Px(200.)), position_type: PositionType::Absolute, - position: Position { - left: Val::Px(210.), - bottom: Val::Px(10.), - ..Default::default() - }, + left: Val::Px(210.), + bottom: Val::Px(10.), border: UiRect::all(Val::Px(20.)), ..default() }, @@ -198,11 +195,8 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { size: Size::all(Val::Percent(100.)), position_type: PositionType::Absolute, - position: Position { - left: Val::Px(20.), - bottom: Val::Px(20.), - ..Default::default() - }, + left: Val::Px(20.), + bottom: Val::Px(20.), ..default() }, background_color: Color::rgb(1.0, 0.3, 0.3).into(), @@ -212,11 +206,8 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { size: Size::all(Val::Percent(100.)), position_type: PositionType::Absolute, - position: Position { - left: Val::Px(40.), - bottom: Val::Px(40.), - ..Default::default() - }, + left: Val::Px(40.), + bottom: Val::Px(40.), ..default() }, background_color: Color::rgb(1.0, 0.5, 0.5).into(), @@ -226,11 +217,8 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { size: Size::all(Val::Percent(100.)), position_type: PositionType::Absolute, - position: Position { - left: Val::Px(60.), - bottom: Val::Px(60.), - ..Default::default() - }, + left: Val::Px(60.), + bottom: Val::Px(60.), ..default() }, background_color: Color::rgb(1.0, 0.7, 0.7).into(), @@ -241,11 +229,8 @@ fn setup(mut commands: Commands, asset_server: Res) { style: Style { size: Size::all(Val::Percent(100.)), position_type: PositionType::Absolute, - position: Position { - left: Val::Px(80.), - bottom: Val::Px(80.), - ..Default::default() - }, + left: Val::Px(80.), + bottom: Val::Px(80.), ..default() }, background_color: Color::rgba(1.0, 0.9, 0.9, 0.4).into(), @@ -303,7 +288,7 @@ fn mouse_scroll( }; scrolling_list.position += dy; scrolling_list.position = scrolling_list.position.clamp(-max_scroll, 0.); - style.position.top = Val::Px(scrolling_list.position); + style.top = Val::Px(scrolling_list.position); } } } diff --git a/examples/ui/ui_scaling.rs b/examples/ui/ui_scaling.rs index fe804f349bac6..b27b2f9ebdfc3 100644 --- a/examples/ui/ui_scaling.rs +++ b/examples/ui/ui_scaling.rs @@ -39,11 +39,8 @@ fn setup(mut commands: Commands, asset_server: ResMut) { style: Style { size: Size::new(Val::Percent(50.0), Val::Percent(50.0)), position_type: PositionType::Absolute, - position: Position { - left: Val::Percent(25.), - top: Val::Percent(25.), - ..default() - }, + left: Val::Percent(25.), + top: Val::Percent(25.), justify_content: JustifyContent::SpaceAround, align_items: AlignItems::Center, ..default() diff --git a/examples/ui/window_fallthrough.rs b/examples/ui/window_fallthrough.rs index 463665d4e434d..41f2c16112eb2 100644 --- a/examples/ui/window_fallthrough.rs +++ b/examples/ui/window_fallthrough.rs @@ -39,11 +39,8 @@ fn setup(mut commands: Commands, asset_server: Res) { // Set the style of the TextBundle itself. .with_style(Style { position_type: PositionType::Absolute, - position: Position { - bottom: Val::Px(5.), - right: Val::Px(10.), - ..default() - }, + bottom: Val::Px(5.), + right: Val::Px(10.), ..default() }), )); diff --git a/examples/ui/z_index.rs b/examples/ui/z_index.rs index 3d89b12372d27..241d0133b18aa 100644 --- a/examples/ui/z_index.rs +++ b/examples/ui/z_index.rs @@ -45,11 +45,8 @@ fn setup(mut commands: Commands) { background_color: Color::RED.into(), style: Style { position_type: PositionType::Absolute, - position: Position { - left: Val::Px(10.0), - bottom: Val::Px(40.0), - ..default() - }, + left: Val::Px(10.0), + bottom: Val::Px(40.0), size: Size::new(Val::Px(100.0), Val::Px(50.0)), ..default() }, @@ -63,11 +60,8 @@ fn setup(mut commands: Commands) { background_color: Color::BLUE.into(), style: Style { position_type: PositionType::Absolute, - position: Position { - left: Val::Px(45.0), - bottom: Val::Px(30.0), - ..default() - }, + left: Val::Px(45.0), + bottom: Val::Px(30.0), size: Size::new(Val::Px(100.0), Val::Px(50.0)), ..default() }, @@ -81,11 +75,8 @@ fn setup(mut commands: Commands) { background_color: Color::GREEN.into(), style: Style { position_type: PositionType::Absolute, - position: Position { - left: Val::Px(70.0), - bottom: Val::Px(20.0), - ..default() - }, + left: Val::Px(70.0), + bottom: Val::Px(20.0), size: Size::new(Val::Px(100.0), Val::Px(75.0)), ..default() }, @@ -100,11 +91,8 @@ fn setup(mut commands: Commands) { background_color: Color::PURPLE.into(), style: Style { position_type: PositionType::Absolute, - position: Position { - left: Val::Px(15.0), - bottom: Val::Px(10.0), - ..default() - }, + left: Val::Px(15.0), + bottom: Val::Px(10.0), size: Size::new(Val::Px(100.0), Val::Px(60.0)), ..default() }, @@ -119,11 +107,8 @@ fn setup(mut commands: Commands) { background_color: Color::YELLOW.into(), style: Style { position_type: PositionType::Absolute, - position: Position { - left: Val::Px(-15.0), - bottom: Val::Px(-15.0), - ..default() - }, + left: Val::Px(-15.0), + bottom: Val::Px(-15.0), size: Size::new(Val::Px(100.0), Val::Px(125.0)), ..default() }, diff --git a/examples/window/low_power.rs b/examples/window/low_power.rs index 497dd5f6692f2..36ef1988f4e73 100644 --- a/examples/window/low_power.rs +++ b/examples/window/low_power.rs @@ -199,11 +199,8 @@ pub(crate) mod test_setup { .with_style(Style { align_self: AlignSelf::FlexStart, position_type: PositionType::Absolute, - position: Position { - top: Val::Px(5.0), - left: Val::Px(5.0), - ..default() - }, + top: Val::Px(5.0), + left: Val::Px(5.0), ..default() }), ModeText, From 99b93f8952e2cbe678839ff6590b65e865e71a3c Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Fri, 3 Feb 2023 22:14:53 +0000 Subject: [PATCH 13/25] fix doc comments --- crates/bevy_ui/src/geometry.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index ee6499a7510ad..ac8438ccadd5f 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -245,9 +245,9 @@ impl UiRect { /// let ui_rect = UiRect::left(Val::Px(10.0)); /// /// assert_eq!(ui_rect.left, Val::Px(10.0)); - /// assert_eq!(ui_rect.right, Val::Undefined); - /// assert_eq!(ui_rect.top, Val::Undefined); - /// assert_eq!(ui_rect.bottom, Val::Undefined); + /// assert_eq!(ui_rect.right, Val::Px(0.)); + /// assert_eq!(ui_rect.top, Val::Px(0.)); + /// assert_eq!(ui_rect.bottom, Val::Px(0.)); /// ``` pub fn left(value: Val) -> Self { UiRect { @@ -265,10 +265,10 @@ impl UiRect { /// # /// let ui_rect = UiRect::right(Val::Px(10.0)); /// - /// assert_eq!(ui_rect.left, Val::Undefined); + /// assert_eq!(ui_rect.left, Val::Px(0.)); /// assert_eq!(ui_rect.right, Val::Px(10.0)); - /// assert_eq!(ui_rect.top, Val::Undefined); - /// assert_eq!(ui_rect.bottom, Val::Undefined); + /// assert_eq!(ui_rect.top, Val::Px(0.)); + /// assert_eq!(ui_rect.bottom, Val::Px(0.)); /// ``` pub fn right(value: Val) -> Self { UiRect { @@ -286,10 +286,10 @@ impl UiRect { /// # /// let ui_rect = UiRect::top(Val::Px(10.0)); /// - /// assert_eq!(ui_rect.left, Val::Undefined); - /// assert_eq!(ui_rect.right, Val::Undefined); + /// assert_eq!(ui_rect.left, Val::Px(0.)); + /// assert_eq!(ui_rect.right, Val::Px(0.)); /// assert_eq!(ui_rect.top, Val::Px(10.0)); - /// assert_eq!(ui_rect.bottom, Val::Undefined); + /// assert_eq!(ui_rect.bottom, Val::Px(0.)); /// ``` pub fn top(value: Val) -> Self { UiRect { @@ -307,9 +307,9 @@ impl UiRect { /// # /// let ui_rect = UiRect::bottom(Val::Px(10.0)); /// - /// assert_eq!(ui_rect.left, Val::Undefined); - /// assert_eq!(ui_rect.right, Val::Undefined); - /// assert_eq!(ui_rect.top, Val::Undefined); + /// assert_eq!(ui_rect.left, Val::Px(0.)); + /// assert_eq!(ui_rect.right, Val::Px(0.)); + /// assert_eq!(ui_rect.top, Val::Px(0.)); /// assert_eq!(ui_rect.bottom, Val::Px(10.0)); /// ``` pub fn bottom(value: Val) -> Self { From 5ab3ffd4c8bbd67eee90a87bfd76bd4e314f080c Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Fri, 3 Feb 2023 22:37:39 +0000 Subject: [PATCH 14/25] fix doc comment --- crates/bevy_ui/src/geometry.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index ac8438ccadd5f..e237769275a32 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -65,16 +65,14 @@ use std::ops::{Div, DivAssign, Mul, MulAssign}; /// right values of the position because the size of the UI element is already explicitly specified. /// /// ``` -/// # use bevy_ui::{UiRect, Size, Val, Style}; +/// # use bevy_ui::{Size, Val, Style}; /// # use bevy_utils::default; /// # /// let style = Style { -/// position: UiRect { // Defining all four sides -/// left: Val::Px(100.0), -/// right: Val::Px(200.0), -/// top: Val::Px(300.0), -/// bottom: Val::Px(400.0), -/// }, +/// left: Val::Px(100.0), +/// right: Val::Px(200.0), +/// top: Val::Px(300.0), +/// bottom: Val::Px(400.0), /// size: Size::new(Val::Percent(100.0), Val::Percent(50.0)), // but also explicitly specifying a size /// ..default() /// }; From eb88a97111e7adc77d3552778fc1b2ee8d9643c6 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Tue, 7 Feb 2023 19:50:30 +0000 Subject: [PATCH 15/25] added some simple tests --- crates/bevy_ui/src/geometry.rs | 38 ++++++++++++++++++++++++++++++++++ crates/bevy_ui/src/ui_node.rs | 5 +++++ 2 files changed, 43 insertions(+) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index e237769275a32..104eb5e46cf7a 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -438,6 +438,12 @@ impl DivAssign for Size { mod tests { use super::*; + #[test] + fn uirect_default_equals_const_default() { + assert_eq!(UiRect::default(), UiRect::DEFAULT); + } + + #[test] fn test_size_from() { let size: Size = (Val::Px(20.), Val::Px(30.)).into(); @@ -471,4 +477,36 @@ mod tests { size /= 2.; assert_eq!(size, Size::new(Val::Px(10.), Val::Px(10.))); } + + #[test] + fn test_size_all() { + let length = Val::Px(10.); + + assert_eq!(Size::all(length), Size { width: length, height: length }); + } + + #[test] + fn test_size_width() { + let width = Val::Px(10.); + + assert_eq!(Size::width(width), Size { width, ..Default::default() }); + } + + #[test] + fn test_size_height() { + let height = Val::Px(7.); + + assert_eq!( + Size::height(height), + Size { + height, + ..Default::default() + } + ); + } + + #[test] + fn size_default_equals_const_default() { + assert_eq!(Size::default(), Size::DEFAULT); + } } diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 5b58b2019d516..606e226d3a0a1 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -814,4 +814,9 @@ mod tests { "the given variant of Val is not evaluateable (non-numeric)" ); } + + #[test] + fn default_val_equals_const_default_val() { + assert_eq!(Val::default(), Val::DEFAULT); + } } From 61139110e37b0a80e2ef3afa933d7d2601f05710 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Tue, 7 Feb 2023 20:24:25 +0000 Subject: [PATCH 16/25] cargo fmt --- crates/bevy_ui/src/geometry.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 104eb5e46cf7a..deb278458102d 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -443,7 +443,6 @@ mod tests { assert_eq!(UiRect::default(), UiRect::DEFAULT); } - #[test] fn test_size_from() { let size: Size = (Val::Px(20.), Val::Px(30.)).into(); @@ -482,14 +481,26 @@ mod tests { fn test_size_all() { let length = Val::Px(10.); - assert_eq!(Size::all(length), Size { width: length, height: length }); + assert_eq!( + Size::all(length), + Size { + width: length, + height: length + } + ); } #[test] fn test_size_width() { let width = Val::Px(10.); - assert_eq!(Size::width(width), Size { width, ..Default::default() }); + assert_eq!( + Size::width(width), + Size { + width, + ..Default::default() + } + ); } #[test] @@ -504,7 +515,7 @@ mod tests { } ); } - + #[test] fn size_default_equals_const_default() { assert_eq!(Size::default(), Size::DEFAULT); From 431d97e8e540e5df1f8159db6b6d93969c32ea9f Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 2 Mar 2023 21:58:47 +0000 Subject: [PATCH 17/25] Changed the doc comments to be explicit about the values the helper functions set to fields with no given value. --- crates/bevy_ui/src/geometry.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index d7d1ec21db323..d8b3f47e7835c 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -189,7 +189,8 @@ impl UiRect { } } - /// Creates a new [`UiRect`] where `left` and `right` take the given value. + /// Creates a new [`UiRect`] where `left` and `right` take the given value, + /// and `top` and `bottom` set to zero `Val::Px(0.)`. /// /// # Example /// @@ -211,7 +212,8 @@ impl UiRect { } } - /// Creates a new [`UiRect`] where `top` and `bottom` take the given value. + /// Creates a new [`UiRect`] where `top` and `bottom` take the given value, + /// and `left` and `right` are set to `Val::Px(0.)`. /// /// # Example /// @@ -233,7 +235,8 @@ impl UiRect { } } - /// Creates a new [`UiRect`] where `left` takes the given value. + /// Creates a new [`UiRect`] where `left` takes the given value, and + /// the other fields are set to `Val::Px(0.)`. /// /// # Example /// @@ -254,7 +257,8 @@ impl UiRect { } } - /// Creates a new [`UiRect`] where `right` takes the given value. + /// Creates a new [`UiRect`] where `right` takes the given value, + /// and the other fields are set to `Val::Px(0.)`. /// /// # Example /// @@ -275,7 +279,8 @@ impl UiRect { } } - /// Creates a new [`UiRect`] where `top` takes the given value. + /// Creates a new [`UiRect`] where `top` takes the given value, + /// and the other fields are set to `Val::Px(0.)`. /// /// # Example /// @@ -296,7 +301,8 @@ impl UiRect { } } - /// Creates a new [`UiRect`] where `bottom` takes the given value. + /// Creates a new [`UiRect`] where `bottom` takes the given value, + /// and the other fields are set to `Val::Px(0.)`. /// /// # Example /// @@ -363,18 +369,20 @@ impl Size { } } - /// Creates a new [`Size`] where `width` takes the given value. + /// Creates a new [`Size`] where `width` takes the given value, + /// and `height` is set to [`Val::Auto`]. pub const fn width(width: Val) -> Self { Self { width, - height: Val::DEFAULT, + height: Val::Auto, } } - /// Creates a new [`Size`] where `height` takes the given value. + /// Creates a new [`Size`] where `height` takes the given value, + /// and `width` is set to [`Val::Auto`]. pub const fn height(height: Val) -> Self { Self { - width: Val::DEFAULT, + width: Val::Auto, height, } } From c8f200e8ff40c7a67c60e1d3814655bc2d4057e2 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 2 Mar 2023 22:39:10 +0000 Subject: [PATCH 18/25] fixed errors in ui example --- examples/ui/ui.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index 3c48655b45dfe..36936389f9a2c 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -94,13 +94,15 @@ fn setup(mut commands: Commands, asset_server: Res) { }) .with_children(|parent| { // Title - parent.spawn((TextBundle::from_section( - "Scrolling list", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 25., - color: Color::WHITE, - }, + parent.spawn(( + TextBundle::from_section( + "Scrolling list", + TextStyle { + font: asset_server.load("fonts/FiraSans-Bold.ttf"), + font_size: 25., + color: Color::WHITE, + }, + ), Label, )); // List with hidden overflow From f3f02d86798a954d824ffe1c8834c6621b9487d3 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 2 Mar 2023 23:40:21 +0000 Subject: [PATCH 19/25] fixed positions --- examples/3d/pbr.rs | 21 ++++++--------------- examples/3d/tonemapping.rs | 7 ++----- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/examples/3d/pbr.rs b/examples/3d/pbr.rs index 89532aee75c06..2120eaa2c243c 100644 --- a/examples/3d/pbr.rs +++ b/examples/3d/pbr.rs @@ -85,11 +85,8 @@ fn setup( ) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { - top: Val::Px(20.0), - left: Val::Px(100.0), - ..default() - }, + top: Val::Px(20.0), + left: Val::Px(100.0), ..default() }), ); @@ -105,11 +102,8 @@ fn setup( ), style: Style { position_type: PositionType::Absolute, - position: UiRect { - top: Val::Px(130.0), - right: Val::Px(0.0), - ..default() - }, + top: Val::Px(130.0), + right: Val::Px(0.0), ..default() }, transform: Transform { @@ -130,11 +124,8 @@ fn setup( ) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { - bottom: Val::Px(20.0), - right: Val::Px(20.0), - ..default() - }, + bottom: Val::Px(20.0), + right: Val::Px(20.0), ..default() }), EnvironmentMapLabel, diff --git a/examples/3d/tonemapping.rs b/examples/3d/tonemapping.rs index cc4f2cc3f7951..dd90db5ab2e37 100644 --- a/examples/3d/tonemapping.rs +++ b/examples/3d/tonemapping.rs @@ -72,11 +72,8 @@ fn setup( ) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { - top: Val::Px(10.0), - left: Val::Px(10.0), - ..default() - }, + top: Val::Px(10.0), + left: Val::Px(10.0), ..default() }), ); From 5e3e7cf6ee5edff34c86b3b566ebd25c109f207d Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Wed, 8 Mar 2023 10:29:12 +0000 Subject: [PATCH 20/25] Removed position example from `UiRect` --- crates/bevy_ui/src/geometry.rs | 75 +--------------------------------- 1 file changed, 2 insertions(+), 73 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 433ad953f2740..ac70c93834d32 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -2,81 +2,10 @@ use crate::Val; use bevy_reflect::Reflect; use std::ops::{Div, DivAssign, Mul, MulAssign}; -/// A type which is commonly used to define positions, margins, paddings and borders. +/// A type which is commonly used to define margins, paddings and borders. /// /// # Examples -/// -/// ## Position -/// -/// A position is used to determine where to place a UI element. -/// -/// ``` -/// # use bevy_ui::{UiRect, Val}; -/// # use bevy_utils::default; -/// # -/// let position = UiRect { -/// left: Val::Px(100.0), -/// top: Val::Px(50.0), -/// ..default() -/// }; -/// ``` -/// -/// If you define opposite sides of the position, the size of the UI element will automatically be calculated -/// if not explicitly specified. This means that if you have a [`Size`] that uses [`Val::Auto`](crate::Val::Auto) -/// as a width and height, the size would be determined by the window size and the values specified in the position. -/// -/// ``` -/// # use bevy_ui::{UiRect, Val}; -/// # -/// let position = UiRect { -/// left: Val::Px(100.0), -/// right: Val::Px(200.0), -/// top: Val::Px(300.0), -/// bottom: Val::Px(400.0), -/// }; -/// ``` -/// -/// To determine the width of the UI element you have to take the width of the window and subtract it by the -/// left and right values of the position. To determine the height of the UI element you have to take the height -/// of the window and subtract it by the top and bottom values of the position. If we had a window with a width -/// and height of 1000px, the UI element declared above would have a width of 700px and a height of 300px. -/// -/// ``` -/// // Size of the window -/// let window_width = 1000.0; -/// let window_height = 1000.0; -/// -/// // Values of the position -/// let left = 100.0; -/// let right = 200.0; -/// let top = 300.0; -/// let bottom = 400.0; -/// -/// // Calculation to get the size of the UI element -/// let ui_element_width = window_width - left - right; -/// let ui_element_height = window_height - top - bottom; -/// -/// assert_eq!(ui_element_width, 700.0); -/// assert_eq!(ui_element_height, 300.0); -/// ``` -/// -/// If you define a [`Size`] and also all four sides of the position, the top and left values of the position -/// are used to determine where to place the UI element. The size will not be calculated using the bottom and -/// right values of the position because the size of the UI element is already explicitly specified. -/// -/// ``` -/// # use bevy_ui::{Size, Val, Style}; -/// # use bevy_utils::default; -/// # -/// let style = Style { -/// left: Val::Px(100.0), -/// right: Val::Px(200.0), -/// top: Val::Px(300.0), -/// bottom: Val::Px(400.0), -/// size: Size::new(Val::Percent(100.0), Val::Percent(50.0)), // but also explicitly specifying a size -/// ..default() -/// }; -/// ``` + /// /// ## Margin /// From e72dca83722bdd157a06965b02717308ef0a89de Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Thu, 9 Mar 2023 12:24:43 +0000 Subject: [PATCH 21/25] update convert module --- crates/bevy_ui/src/flex/convert.rs | 101 +++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/crates/bevy_ui/src/flex/convert.rs b/crates/bevy_ui/src/flex/convert.rs index 9cf19cc9e9b6c..f7b214b7193aa 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -222,3 +222,104 @@ impl From for taffy::style::FlexWrap { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_convert_from() { + let bevy_style = crate::Style { + display: Display::Flex, + position_type: PositionType::Absolute, + left: Val::Px(0.), + right: Val::Percent(0.), + top: Val::Auto, + bottom: Val::Auto, + direction: crate::Direction::Inherit, + flex_direction: FlexDirection::ColumnReverse, + flex_wrap: FlexWrap::WrapReverse, + align_items: AlignItems::Baseline, + align_self: AlignSelf::Start, + align_content: AlignContent::SpaceAround, + justify_content: JustifyContent::SpaceEvenly, + margin: UiRect { + left: Val::Percent(0.), + right: Val::Px(0.), + top: Val::Auto, + bottom: Val::Auto, + }, + padding: UiRect { + left: Val::Percent(0.), + right: Val::Px(0.), + top: Val::Percent(0.), + bottom: Val::Percent(0.), + }, + border: UiRect { + left: Val::Px(0.), + right: Val::Px(0.), + top: Val::Auto, + bottom: Val::Px(0.), + }, + flex_grow: 1., + flex_shrink: 0., + flex_basis: Val::Px(0.), + size: Size { + width: Val::Px(0.), + height: Val::Auto, + }, + min_size: Size { + width: Val::Px(0.), + height: Val::Percent(0.), + }, + max_size: Size { + width: Val::Auto, + height: Val::Px(0.), + }, + aspect_ratio: None, + overflow: crate::Overflow::Hidden, + gap: Size { + width: Val::Px(0.), + height: Val::Percent(0.), + }, + }; + let taffy_style = from_style(1.0, &bevy_style); + assert_eq!(taffy_style.display, taffy::style::Display::Flex); + assert_eq!(taffy_style.position, taffy::style::Position::Absolute); + assert!(matches!(taffy_style.inset.left, taffy::style::LengthPercentageAuto::Points(_))); + assert!(matches!(taffy_style.inset.right, taffy::style::LengthPercentageAuto::Percent(_))); + assert!(matches!(taffy_style.inset.top, taffy::style::LengthPercentageAuto::Auto)); + assert!(matches!(taffy_style.inset.bottom, taffy::style::LengthPercentageAuto::Auto)); + assert_eq!(taffy_style.flex_direction, taffy::style::FlexDirection::ColumnReverse); + assert_eq!(taffy_style.flex_wrap, taffy::style::FlexWrap::WrapReverse); + assert_eq!(taffy_style.align_items, Some(taffy::style::AlignItems::Baseline)); + assert_eq!(taffy_style.align_self, Some(taffy::style::AlignSelf::Start)); + assert_eq!(taffy_style.align_content, Some(taffy::style::AlignContent::SpaceAround)); + assert_eq!(taffy_style.justify_content, Some(taffy::style::JustifyContent::SpaceEvenly)); + assert!(matches!(taffy_style.margin.left, taffy::style::LengthPercentageAuto::Percent(_))); + assert!(matches!(taffy_style.margin.right, taffy::style::LengthPercentageAuto::Points(_))); + assert!(matches!(taffy_style.margin.top, taffy::style::LengthPercentageAuto::Auto)); + assert!(matches!(taffy_style.margin.bottom, taffy::style::LengthPercentageAuto::Auto)); + assert!(matches!(taffy_style.padding.left, taffy::style::LengthPercentage::Percent(_))); + assert!(matches!(taffy_style.padding.right, taffy::style::LengthPercentage::Points(_))); + assert!(matches!(taffy_style.padding.top, taffy::style::LengthPercentage::Percent(_))); + assert!(matches!(taffy_style.padding.bottom, taffy::style::LengthPercentage::Percent(_))); + assert!(matches!(taffy_style.border.left, taffy::style::LengthPercentage::Points(_))); + assert!(matches!(taffy_style.border.right, taffy::style::LengthPercentage::Points(_))); + assert!(matches!(taffy_style.border.top, taffy::style::LengthPercentage::Points(_))); + assert!(matches!(taffy_style.border.bottom, taffy::style::LengthPercentage::Points(_))); + assert_eq!(taffy_style.flex_grow, 1.); + assert_eq!(taffy_style.flex_shrink, 0.); + assert!(matches!(taffy_style.flex_basis, taffy::style::Dimension::Points(_))); + assert!(matches!(taffy_style.size.width, taffy::style::Dimension::Points(_))); + assert!(matches!(taffy_style.size.height, taffy::style::Dimension::Auto)); + assert!(matches!(taffy_style.min_size.width, taffy::style::Dimension::Points(_))); + assert!(matches!(taffy_style.min_size.height, taffy::style::Dimension::Percent(_))); + assert!(matches!(taffy_style.max_size.width, taffy::style::Dimension::Auto)); + assert!(matches!(taffy_style.max_size.height, taffy::style::Dimension::Points(_))); + assert_eq!(taffy_style.aspect_ratio, None); + assert_eq!(taffy_style.gap.width, taffy::style::LengthPercentage::Points(0.)); + assert_eq!(taffy_style.gap.width, taffy::style::LengthPercentage::Percent(0.)); + } +} + From 29db012ea1ebef68a691c6cdeb2ff01ba55ff86d Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Thu, 9 Mar 2023 12:26:06 +0000 Subject: [PATCH 22/25] Fixed position in `bloom_3d` example --- examples/3d/bloom_3d.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/examples/3d/bloom_3d.rs b/examples/3d/bloom_3d.rs index da922c4b378d1..83ae6ce89bd64 100644 --- a/examples/3d/bloom_3d.rs +++ b/examples/3d/bloom_3d.rs @@ -104,11 +104,8 @@ fn setup_scene( ) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { - bottom: Val::Px(10.0), - left: Val::Px(10.0), - ..default() - }, + bottom: Val::Px(10.0), + left: Val::Px(10.0), ..default() }), ); From 5cca5200b8ec93bb2096576089baec73710161a0 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Thu, 9 Mar 2023 12:27:51 +0000 Subject: [PATCH 23/25] Fixed position in `bloom_2d` example --- examples/2d/bloom_2d.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/examples/2d/bloom_2d.rs b/examples/2d/bloom_2d.rs index e46f24c751a85..eac63605efeee 100644 --- a/examples/2d/bloom_2d.rs +++ b/examples/2d/bloom_2d.rs @@ -79,11 +79,8 @@ fn setup( ) .with_style(Style { position_type: PositionType::Absolute, - position: UiRect { - bottom: Val::Px(10.0), - left: Val::Px(10.0), - ..default() - }, + bottom: Val::Px(10.0), + left: Val::Px(10.0), ..default() }), ); From 51bd6dfadb284ce551eea4893984eb59f495543a Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Thu, 9 Mar 2023 12:31:13 +0000 Subject: [PATCH 24/25] cargo fmt --- crates/bevy_ui/src/flex/convert.rs | 170 ++++++++++++++++++++++------- 1 file changed, 128 insertions(+), 42 deletions(-) diff --git a/crates/bevy_ui/src/flex/convert.rs b/crates/bevy_ui/src/flex/convert.rs index f7b214b7193aa..6578fc8a8e73e 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -243,22 +243,22 @@ mod tests { align_self: AlignSelf::Start, align_content: AlignContent::SpaceAround, justify_content: JustifyContent::SpaceEvenly, - margin: UiRect { - left: Val::Percent(0.), - right: Val::Px(0.), - top: Val::Auto, + margin: UiRect { + left: Val::Percent(0.), + right: Val::Px(0.), + top: Val::Auto, bottom: Val::Auto, }, - padding: UiRect { - left: Val::Percent(0.), - right: Val::Px(0.), - top: Val::Percent(0.), + padding: UiRect { + left: Val::Percent(0.), + right: Val::Px(0.), + top: Val::Percent(0.), bottom: Val::Percent(0.), }, - border: UiRect { - left: Val::Px(0.), - right: Val::Px(0.), - top: Val::Auto, + border: UiRect { + left: Val::Px(0.), + right: Val::Px(0.), + top: Val::Auto, bottom: Val::Px(0.), }, flex_grow: 1., @@ -286,40 +286,126 @@ mod tests { let taffy_style = from_style(1.0, &bevy_style); assert_eq!(taffy_style.display, taffy::style::Display::Flex); assert_eq!(taffy_style.position, taffy::style::Position::Absolute); - assert!(matches!(taffy_style.inset.left, taffy::style::LengthPercentageAuto::Points(_))); - assert!(matches!(taffy_style.inset.right, taffy::style::LengthPercentageAuto::Percent(_))); - assert!(matches!(taffy_style.inset.top, taffy::style::LengthPercentageAuto::Auto)); - assert!(matches!(taffy_style.inset.bottom, taffy::style::LengthPercentageAuto::Auto)); - assert_eq!(taffy_style.flex_direction, taffy::style::FlexDirection::ColumnReverse); + assert!(matches!( + taffy_style.inset.left, + taffy::style::LengthPercentageAuto::Points(_) + )); + assert!(matches!( + taffy_style.inset.right, + taffy::style::LengthPercentageAuto::Percent(_) + )); + assert!(matches!( + taffy_style.inset.top, + taffy::style::LengthPercentageAuto::Auto + )); + assert!(matches!( + taffy_style.inset.bottom, + taffy::style::LengthPercentageAuto::Auto + )); + assert_eq!( + taffy_style.flex_direction, + taffy::style::FlexDirection::ColumnReverse + ); assert_eq!(taffy_style.flex_wrap, taffy::style::FlexWrap::WrapReverse); - assert_eq!(taffy_style.align_items, Some(taffy::style::AlignItems::Baseline)); + assert_eq!( + taffy_style.align_items, + Some(taffy::style::AlignItems::Baseline) + ); assert_eq!(taffy_style.align_self, Some(taffy::style::AlignSelf::Start)); - assert_eq!(taffy_style.align_content, Some(taffy::style::AlignContent::SpaceAround)); - assert_eq!(taffy_style.justify_content, Some(taffy::style::JustifyContent::SpaceEvenly)); - assert!(matches!(taffy_style.margin.left, taffy::style::LengthPercentageAuto::Percent(_))); - assert!(matches!(taffy_style.margin.right, taffy::style::LengthPercentageAuto::Points(_))); - assert!(matches!(taffy_style.margin.top, taffy::style::LengthPercentageAuto::Auto)); - assert!(matches!(taffy_style.margin.bottom, taffy::style::LengthPercentageAuto::Auto)); - assert!(matches!(taffy_style.padding.left, taffy::style::LengthPercentage::Percent(_))); - assert!(matches!(taffy_style.padding.right, taffy::style::LengthPercentage::Points(_))); - assert!(matches!(taffy_style.padding.top, taffy::style::LengthPercentage::Percent(_))); - assert!(matches!(taffy_style.padding.bottom, taffy::style::LengthPercentage::Percent(_))); - assert!(matches!(taffy_style.border.left, taffy::style::LengthPercentage::Points(_))); - assert!(matches!(taffy_style.border.right, taffy::style::LengthPercentage::Points(_))); - assert!(matches!(taffy_style.border.top, taffy::style::LengthPercentage::Points(_))); - assert!(matches!(taffy_style.border.bottom, taffy::style::LengthPercentage::Points(_))); + assert_eq!( + taffy_style.align_content, + Some(taffy::style::AlignContent::SpaceAround) + ); + assert_eq!( + taffy_style.justify_content, + Some(taffy::style::JustifyContent::SpaceEvenly) + ); + assert!(matches!( + taffy_style.margin.left, + taffy::style::LengthPercentageAuto::Percent(_) + )); + assert!(matches!( + taffy_style.margin.right, + taffy::style::LengthPercentageAuto::Points(_) + )); + assert!(matches!( + taffy_style.margin.top, + taffy::style::LengthPercentageAuto::Auto + )); + assert!(matches!( + taffy_style.margin.bottom, + taffy::style::LengthPercentageAuto::Auto + )); + assert!(matches!( + taffy_style.padding.left, + taffy::style::LengthPercentage::Percent(_) + )); + assert!(matches!( + taffy_style.padding.right, + taffy::style::LengthPercentage::Points(_) + )); + assert!(matches!( + taffy_style.padding.top, + taffy::style::LengthPercentage::Percent(_) + )); + assert!(matches!( + taffy_style.padding.bottom, + taffy::style::LengthPercentage::Percent(_) + )); + assert!(matches!( + taffy_style.border.left, + taffy::style::LengthPercentage::Points(_) + )); + assert!(matches!( + taffy_style.border.right, + taffy::style::LengthPercentage::Points(_) + )); + assert!(matches!( + taffy_style.border.top, + taffy::style::LengthPercentage::Points(_) + )); + assert!(matches!( + taffy_style.border.bottom, + taffy::style::LengthPercentage::Points(_) + )); assert_eq!(taffy_style.flex_grow, 1.); assert_eq!(taffy_style.flex_shrink, 0.); - assert!(matches!(taffy_style.flex_basis, taffy::style::Dimension::Points(_))); - assert!(matches!(taffy_style.size.width, taffy::style::Dimension::Points(_))); - assert!(matches!(taffy_style.size.height, taffy::style::Dimension::Auto)); - assert!(matches!(taffy_style.min_size.width, taffy::style::Dimension::Points(_))); - assert!(matches!(taffy_style.min_size.height, taffy::style::Dimension::Percent(_))); - assert!(matches!(taffy_style.max_size.width, taffy::style::Dimension::Auto)); - assert!(matches!(taffy_style.max_size.height, taffy::style::Dimension::Points(_))); + assert!(matches!( + taffy_style.flex_basis, + taffy::style::Dimension::Points(_) + )); + assert!(matches!( + taffy_style.size.width, + taffy::style::Dimension::Points(_) + )); + assert!(matches!( + taffy_style.size.height, + taffy::style::Dimension::Auto + )); + assert!(matches!( + taffy_style.min_size.width, + taffy::style::Dimension::Points(_) + )); + assert!(matches!( + taffy_style.min_size.height, + taffy::style::Dimension::Percent(_) + )); + assert!(matches!( + taffy_style.max_size.width, + taffy::style::Dimension::Auto + )); + assert!(matches!( + taffy_style.max_size.height, + taffy::style::Dimension::Points(_) + )); assert_eq!(taffy_style.aspect_ratio, None); - assert_eq!(taffy_style.gap.width, taffy::style::LengthPercentage::Points(0.)); - assert_eq!(taffy_style.gap.width, taffy::style::LengthPercentage::Percent(0.)); + assert_eq!( + taffy_style.gap.width, + taffy::style::LengthPercentage::Points(0.) + ); + assert_eq!( + taffy_style.gap.width, + taffy::style::LengthPercentage::Percent(0.) + ); } } - From 9369aa1c7afdc8c3ae10fa432adf267dc10add84 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Thu, 9 Mar 2023 12:37:38 +0000 Subject: [PATCH 25/25] fix test --- crates/bevy_ui/src/flex/convert.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ui/src/flex/convert.rs b/crates/bevy_ui/src/flex/convert.rs index 6578fc8a8e73e..60e22f0e0b9ba 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -404,7 +404,7 @@ mod tests { taffy::style::LengthPercentage::Points(0.) ); assert_eq!( - taffy_style.gap.width, + taffy_style.gap.height, taffy::style::LengthPercentage::Percent(0.) ); }