From 6cee2e4ba2e063a3a9ee2b19e592e19568e4cc81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20=C3=96dmark?= Date: Thu, 10 Oct 2024 17:36:09 +0200 Subject: [PATCH 1/6] add AsVal trait that helps with constructing values --- crates/bevy_ui/src/geometry.rs | 67 ++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 83850b7e19284..b41655aace619 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -175,6 +175,73 @@ impl Neg for Val { } } +/// Helper trait that allows for easier conversion into the different Val variants, especially when writing Ui code +/// +/// # Example +/// +/// ``` +/// # use bevy_ui::{UiRect, Val, AsVal}; +/// # +/// let ui_rect = UiRect::new( +/// 10.0.px(), +/// 20.px(), +/// 30.percent(), +/// 40.0.percent(), +/// ); +/// +/// assert_eq!(ui_rect.left, Val::Px(10.0)); +/// assert_eq!(ui_rect.right, Val::Px(20.0)); +/// assert_eq!(ui_rect.top, Val::Percent(30.0)); +/// assert_eq!(ui_rect.bottom, Val::Percent(40.0)); +/// ``` +pub trait AsVal { + /// turn the value into Val::Px(self) + fn px(self) -> Val; + /// turn the value into Val::Percent(self) + fn percent(self) -> Val; + /// turn the value into Val::Vw(self) + fn vw(self) -> Val; + /// turn the value into Val::Vh(self) + fn vh(self) -> Val; + /// turn the value into Val::VMin(self) + fn vmin(self) -> Val; + /// turn the value into Val::VMax(self) + fn vmax(self) -> Val; +} + +macro_rules! impl_asval { + ($($t:ty),+) => { + $( + impl AsVal for $t { + fn px(self) -> Val { + Val::Px(self as _) + } + + fn percent(self) -> Val { + Val::Percent(self as _) + } + + fn vw(self) -> Val { + Val::Vw(self as _) + } + + fn vh(self) -> Val { + Val::Vh(self as _) + } + + fn vmin(self) -> Val { + Val::VMin(self as _) + } + + fn vmax(self) -> Val { + Val::VMax(self as _) + } + } + )+ + }; +} +impl_asval!(f32, f64, usize, u32, u64, i32, i64); + #[derive(Debug, Eq, PartialEq, Clone, Copy, Error, Display)] pub enum ValArithmeticError { #[display("the variants of the Vals don't match")] From 01696de611ad3e327259039265abd60233843da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20=C3=96dmark?= Date: Thu, 10 Oct 2024 18:25:06 +0200 Subject: [PATCH 2/6] please clippy --- crates/bevy_ui/src/geometry.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index b41655aace619..8a26f5bbe5a56 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -195,17 +195,17 @@ impl Neg for Val { /// assert_eq!(ui_rect.bottom, Val::Percent(40.0)); /// ``` pub trait AsVal { - /// turn the value into Val::Px(self) + /// turn the value into `Val::Px(self)` fn px(self) -> Val; - /// turn the value into Val::Percent(self) + /// turn the value into `Val::Percent(self)` fn percent(self) -> Val; - /// turn the value into Val::Vw(self) + /// turn the value into `Val::Vw(self)` fn vw(self) -> Val; - /// turn the value into Val::Vh(self) + /// turn the value into `Val::Vh(self)` fn vh(self) -> Val; - /// turn the value into Val::VMin(self) + /// turn the value into `Val::VMin(self)` fn vmin(self) -> Val; - /// turn the value into Val::VMax(self) + /// turn the value into `Val::VMax(self)` fn vmax(self) -> Val; } From 6ff7ad2c82f6523c142ffa3462d24775d8d736d1 Mon Sep 17 00:00:00 2001 From: TotalKrill Date: Thu, 10 Oct 2024 18:49:22 +0200 Subject: [PATCH 3/6] Update crates/bevy_ui/src/geometry.rs Co-authored-by: Joona Aalto --- 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 8a26f5bbe5a56..208d2c77480be 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -175,7 +175,7 @@ impl Neg for Val { } } -/// Helper trait that allows for easier conversion into the different Val variants, especially when writing Ui code +/// Helper trait that allows for easier conversion into the different [`Val`] variants, especially when writing UI code /// /// # Example /// From 0bff185e053ff03ef527337c9b7d5e14b03c9472 Mon Sep 17 00:00:00 2001 From: TotalKrill Date: Thu, 10 Oct 2024 18:49:41 +0200 Subject: [PATCH 4/6] Update crates/bevy_ui/src/geometry.rs Co-authored-by: Joona Aalto --- crates/bevy_ui/src/geometry.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 208d2c77480be..c2395d58c0194 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -195,17 +195,17 @@ impl Neg for Val { /// assert_eq!(ui_rect.bottom, Val::Percent(40.0)); /// ``` pub trait AsVal { - /// turn the value into `Val::Px(self)` + /// Turns the value into `Val::Px(self)`. fn px(self) -> Val; - /// turn the value into `Val::Percent(self)` + /// Turns the value into `Val::Percent(self)`. fn percent(self) -> Val; - /// turn the value into `Val::Vw(self)` + /// Turns the value into `Val::Vw(self)`. fn vw(self) -> Val; - /// turn the value into `Val::Vh(self)` + /// Turns the value into `Val::Vh(self)`. fn vh(self) -> Val; - /// turn the value into `Val::VMin(self)` + /// Turns the value into `Val::VMin(self)`. fn vmin(self) -> Val; - /// turn the value into `Val::VMax(self)` + /// Turns the value into `Val::VMax(self)`. fn vmax(self) -> Val; } From 5fbc9927d7a6d755e1b1bc07d8b77d6f185c14b5 Mon Sep 17 00:00:00 2001 From: TotalKrill Date: Thu, 10 Oct 2024 18:50:00 +0200 Subject: [PATCH 5/6] Update crates/bevy_ui/src/geometry.rs Co-authored-by: Joona Aalto --- 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 c2395d58c0194..6ec6269fc7f83 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -209,7 +209,7 @@ pub trait AsVal { fn vmax(self) -> Val; } -macro_rules! impl_asval { +macro_rules! impl_as_val { ($($t:ty),+) => { $( impl AsVal for $t { From b28ac5c3536b911ce88d3a7ff652938d9fb8010a Mon Sep 17 00:00:00 2001 From: TotalKrill Date: Thu, 10 Oct 2024 18:50:09 +0200 Subject: [PATCH 6/6] Update crates/bevy_ui/src/geometry.rs Co-authored-by: Joona Aalto --- crates/bevy_ui/src/geometry.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 6ec6269fc7f83..3c09e43ccb434 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -240,7 +240,8 @@ macro_rules! impl_as_val { )+ }; } -impl_asval!(f32, f64, usize, u32, u64, i32, i64); + +impl_as_val!(f32, f64, usize, u32, u64, i32, i64); #[derive(Debug, Eq, PartialEq, Clone, Copy, Error, Display)] pub enum ValArithmeticError {