Skip to content

Commit 13877fa

Browse files
authored
Add a new trait to accept more types in the Val-helper functions (#20551)
# Objective - Allow the `Val`-helper functions to accept more types besides just `f32` Fixes #20549 ## Solution - Adds a new trait that can be implemented for numbers - That trait has a method that converts `self` to `f32` ## Testing - I tested it using Rust's testing framework (although I didn't leave the tests in, as I don't deem them important enough) <details> <summary>Rust test</summary> ```rust #[cfg(test)] mod tests { use super::*; #[test] fn test_val_helpers_work() { let p = px(10_u8); assert_eq!(p, Val::Px(10.0)); let p = px(10_u16); assert_eq!(p, Val::Px(10.0)); let p = px(10_u32); assert_eq!(p, Val::Px(10.0)); let p = px(10_u64); assert_eq!(p, Val::Px(10.0)); let p = px(10_u128); assert_eq!(p, Val::Px(10.0)); let p = px(10_i8); assert_eq!(p, Val::Px(10.0)); let p = px(10_i16); assert_eq!(p, Val::Px(10.0)); let p = px(10_i32); assert_eq!(p, Val::Px(10.0)); let p = px(10_i64); assert_eq!(p, Val::Px(10.0)); let p = px(10_i128); assert_eq!(p, Val::Px(10.0)); let p = px(10.3_f32); assert_eq!(p, Val::Px(10.3)); let p = px(10.6_f64); assert_eq!(p, Val::Px(10.6)); } } ``` </details> --- ## Showcase ```rust // Same as Val::Px(10.) px(10); px(10_u8); px(10.0); ```
1 parent cbf989c commit 13877fa

File tree

160 files changed

+1215
-1196
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+1215
-1196
lines changed

crates/bevy_ui/src/geometry.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,39 @@ impl Val {
282282
}
283283
}
284284

285+
/// All the types that should be able to be used in the [`Val`] enum should implement this trait.
286+
///
287+
/// Instead of just implementing `Into<Val>` a custom trait is added.
288+
/// This is done in order to prevent having to define a default unit, which could lead to confusion especially for newcomers.
289+
pub trait ValNum {
290+
/// Called by the [`Val`] helper functions to convert the implementing type to an `f32` that can
291+
/// be used by [`Val`].
292+
fn val_num_f32(self) -> f32;
293+
}
294+
295+
macro_rules! impl_to_val_num {
296+
($($impl_type:ty),*$(,)?) => {
297+
$(
298+
impl ValNum for $impl_type {
299+
fn val_num_f32(self) -> f32 {
300+
self as f32
301+
}
302+
}
303+
)*
304+
};
305+
}
306+
307+
impl_to_val_num!(f32, f64, i8, i16, i32, i64, u8, u16, u32, u64, usize, isize);
308+
285309
/// Returns a [`Val::Auto`] where the value is automatically determined
286310
/// based on the context and other [`Node`](crate::Node) properties.
287311
pub const fn auto() -> Val {
288312
Val::Auto
289313
}
290314

291315
/// Returns a [`Val::Px`] representing a value in logical pixels.
292-
pub const fn px(value: f32) -> Val {
293-
Val::Px(value)
316+
pub fn px<T: ValNum>(value: T) -> Val {
317+
Val::Px(value.val_num_f32())
294318
}
295319

296320
/// Returns a [`Val::Percent`] representing a percentage of the parent node's length
@@ -306,28 +330,28 @@ pub const fn px(value: f32) -> Val {
306330
/// - `height` is relative to the parent's height.
307331
/// * For `margin`, `padding`, and `border` values: the percentage is relative to the parent's width.
308332
/// * For positions, `left` and `right` are relative to the parent's width, while `bottom` and `top` are relative to the parent's height.
309-
pub const fn percent(value: f32) -> Val {
310-
Val::Percent(value)
333+
pub fn percent<T: ValNum>(value: T) -> Val {
334+
Val::Percent(value.val_num_f32())
311335
}
312336

313337
/// Returns a [`Val::Vw`] representing a percentage of the viewport width.
314-
pub const fn vw(value: f32) -> Val {
315-
Val::Vw(value)
338+
pub fn vw<T: ValNum>(value: T) -> Val {
339+
Val::Vw(value.val_num_f32())
316340
}
317341

318342
/// Returns a [`Val::Vh`] representing a percentage of the viewport height.
319-
pub const fn vh(value: f32) -> Val {
320-
Val::Vh(value)
343+
pub fn vh<T: ValNum>(value: T) -> Val {
344+
Val::Vh(value.val_num_f32())
321345
}
322346

323347
/// Returns a [`Val::VMin`] representing a percentage of the viewport's smaller dimension.
324-
pub const fn vmin(value: f32) -> Val {
325-
Val::VMin(value)
348+
pub fn vmin<T: ValNum>(value: T) -> Val {
349+
Val::VMin(value.val_num_f32())
326350
}
327351

328352
/// Returns a [`Val::VMax`] representing a percentage of the viewport's larger dimension.
329-
pub const fn vmax(value: f32) -> Val {
330-
Val::VMax(value)
353+
pub fn vmax<T: ValNum>(value: T) -> Val {
354+
Val::VMax(value.val_num_f32())
331355
}
332356

333357
/// A type which is commonly used to define margins, paddings and borders.

examples/2d/2d_shapes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ fn setup(
8787
Text::new("Press space to toggle wireframes"),
8888
Node {
8989
position_type: PositionType::Absolute,
90-
top: Val::Px(12.0),
91-
left: Val::Px(12.0),
90+
top: px(12),
91+
left: px(12),
9292
..default()
9393
},
9494
));

examples/2d/2d_viewport_to_world.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ fn setup(
150150
),
151151
Node {
152152
position_type: PositionType::Absolute,
153-
top: Val::Px(12.0),
154-
left: Val::Px(12.0),
153+
top: px(12),
154+
left: px(12),
155155
..default()
156156
},
157157
));

examples/2d/bloom_2d.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ fn setup(
6262
Text::default(),
6363
Node {
6464
position_type: PositionType::Absolute,
65-
top: Val::Px(12.0),
66-
left: Val::Px(12.0),
65+
top: px(12),
66+
left: px(12),
6767
..default()
6868
},
6969
));

examples/2d/rotation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
6161
Text::new("Up Arrow: Move Forward\nLeft / Right Arrow: Turn"),
6262
Node {
6363
position_type: PositionType::Absolute,
64-
top: Val::Px(12.0),
65-
left: Val::Px(12.0),
64+
top: px(12),
65+
left: px(12),
6666
..default()
6767
},
6868
));

examples/2d/sprite_animation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ fn setup(
9494
Text::new("Left Arrow: Animate Left Sprite\nRight Arrow: Animate Right Sprite"),
9595
Node {
9696
position_type: PositionType::Absolute,
97-
top: Val::Px(12.0),
98-
left: Val::Px(12.0),
97+
top: px(12),
98+
left: px(12),
9999
..default()
100100
},
101101
));

examples/2d/wireframe_2d.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ fn setup(
9393
Text::default(),
9494
Node {
9595
position_type: PositionType::Absolute,
96-
top: Val::Px(12.0),
97-
left: Val::Px(12.0),
96+
top: px(12),
97+
left: px(12),
9898
..default()
9999
},
100100
));

examples/3d/3d_shapes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ fn setup(
150150
Text::new("Press space to toggle wireframes"),
151151
Node {
152152
position_type: PositionType::Absolute,
153-
top: Val::Px(12.0),
154-
left: Val::Px(12.0),
153+
top: px(12),
154+
left: px(12),
155155
..default()
156156
},
157157
));

examples/3d/anisotropy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ fn spawn_text(commands: &mut Commands, app_status: &AppStatus) {
138138
app_status.create_help_text(),
139139
Node {
140140
position_type: PositionType::Absolute,
141-
bottom: Val::Px(12.0),
142-
left: Val::Px(12.0),
141+
bottom: px(12),
142+
left: px(12),
143143
..default()
144144
},
145145
));

examples/3d/anti_aliasing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@ fn setup(
456456
Text::default(),
457457
Node {
458458
position_type: PositionType::Absolute,
459-
top: Val::Px(12.0),
460-
left: Val::Px(12.0),
459+
top: px(12),
460+
left: px(12),
461461
..default()
462462
},
463463
));

0 commit comments

Comments
 (0)