Skip to content

Commit 4b77775

Browse files
ickshonpekfc35
andauthored
Improved Val2 and UiTransform constructors (#24074)
# Objective - Add more constructors for `Val2` and `UiTransform`. - `Val2's `px` and `percent` functions should accept `ValNum`'s like the `Val` helper functions do. ## Solution * Add `new` and `all` constructors for `Val2`. * Add a `from_xy` constructor for `UiTransform`. * `Val2`'s `px` and `percent` methods are no longer `const` and now accept `ValNum`s. --------- Co-authored-by: Kevin Chen <chen.kevin.f@gmail.com>
1 parent 0945436 commit 4b77775

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

crates/bevy_ui/src/ui_transform.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::Val;
22
use crate::ValArithmeticError;
3+
use crate::ValNum;
34
use bevy_derive::Deref;
45
use bevy_ecs::component::Component;
56
use bevy_ecs::prelude::ReflectComponent;
@@ -35,25 +36,24 @@ impl Val2 {
3536
y: Val::ZERO,
3637
};
3738

38-
/// Creates a new [`Val2`] where both components are in logical pixels
39-
pub const fn px(x: f32, y: f32) -> Self {
40-
Self {
41-
x: Val::Px(x),
42-
y: Val::Px(y),
43-
}
39+
/// Creates a new [`Val2`]
40+
pub const fn new(x: Val, y: Val) -> Self {
41+
Self { x, y }
4442
}
4543

46-
/// Creates a new [`Val2`] where both components are percentage values
47-
pub const fn percent(x: f32, y: f32) -> Self {
48-
Self {
49-
x: Val::Percent(x),
50-
y: Val::Percent(y),
51-
}
44+
/// Creates a new [`Val2`] where both components are the same value
45+
pub const fn all(val: Val) -> Self {
46+
Self::new(val, val)
5247
}
5348

54-
/// Creates a new [`Val2`]
55-
pub const fn new(x: Val, y: Val) -> Self {
56-
Self { x, y }
49+
/// Creates a new [`Val2`] where both components are in logical pixels
50+
pub fn px<X: ValNum, Y: ValNum>(x: X, y: Y) -> Self {
51+
Self::new(Val::Px(x.val_num_f32()), Val::Px(y.val_num_f32()))
52+
}
53+
54+
/// Creates a new [`Val2`] where both components are percentage values
55+
pub fn percent<X: ValNum, Y: ValNum>(x: X, y: Y) -> Self {
56+
Self::new(Val::Percent(x.val_num_f32()), Val::Percent(y.val_num_f32()))
5757
}
5858

5959
/// Resolves this [`Val2`] from the given `scale_factor`, `parent_size`,
@@ -167,6 +167,14 @@ impl UiTransform {
167167
}
168168
}
169169

170+
/// Create a new UI transform at the position `(x, y)`
171+
pub const fn from_xy(x: Val, y: Val) -> Self {
172+
Self {
173+
translation: Val2::new(x, y),
174+
..Self::IDENTITY
175+
}
176+
}
177+
170178
/// Resolves the translation from the given `scale_factor`, `base_value`, and `target_size`
171179
/// and returns a 2d affine transform from the resolved translation, and the `UiTransform`'s rotation, and scale.
172180
pub fn compute_affine(&self, scale_factor: f32, base_size: Vec2, target_size: Vec2) -> Affine2 {

0 commit comments

Comments
 (0)