|
1 | 1 | use crate::Val; |
2 | 2 | use crate::ValArithmeticError; |
| 3 | +use crate::ValNum; |
3 | 4 | use bevy_derive::Deref; |
4 | 5 | use bevy_ecs::component::Component; |
5 | 6 | use bevy_ecs::prelude::ReflectComponent; |
@@ -35,25 +36,24 @@ impl Val2 { |
35 | 36 | y: Val::ZERO, |
36 | 37 | }; |
37 | 38 |
|
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 } |
44 | 42 | } |
45 | 43 |
|
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) |
52 | 47 | } |
53 | 48 |
|
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())) |
57 | 57 | } |
58 | 58 |
|
59 | 59 | /// Resolves this [`Val2`] from the given `scale_factor`, `parent_size`, |
@@ -167,6 +167,14 @@ impl UiTransform { |
167 | 167 | } |
168 | 168 | } |
169 | 169 |
|
| 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 | + |
170 | 178 | /// Resolves the translation from the given `scale_factor`, `base_value`, and `target_size` |
171 | 179 | /// and returns a 2d affine transform from the resolved translation, and the `UiTransform`'s rotation, and scale. |
172 | 180 | pub fn compute_affine(&self, scale_factor: f32, base_size: Vec2, target_size: Vec2) -> Affine2 { |
|
0 commit comments