Skip to content

Commit bf085ee

Browse files
KDecayKDecay
KDecay
and
KDecay
committed
Remove Size and UiRect generics (#5404)
# Objective - Migrate changes from #3503. ## Solution - Change `Size<T>` and `UiRect<T>` to `Size` and `UiRect` using `Val`. - Implement `Sub`, `SubAssign`, `Mul`, `MulAssign`, `Div` and `DivAssign` for `Val`. - Update tests for `Size`. --- ## Changelog ### Changed - The generic `T` of `Size` and `UiRect` got removed and instead they both now always use `Val`. ## Migration Guide - The generic `T` of `Size` and `UiRect` got removed and instead they both now always use `Val`. If you used a `Size<f32>` consider replacing it with a `Vec2` which is way more powerful. Co-authored-by: KDecay <[email protected]>
1 parent 6752c9c commit bf085ee

File tree

6 files changed

+164
-109
lines changed

6 files changed

+164
-109
lines changed

crates/bevy_ui/src/flex/convert.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55

66
pub fn from_rect(
77
scale_factor: f64,
8-
rect: UiRect<Val>,
8+
rect: UiRect,
99
) -> taffy::geometry::Rect<taffy::style::Dimension> {
1010
taffy::geometry::Rect {
1111
start: from_val(scale_factor, rect.left),
@@ -16,16 +16,16 @@ pub fn from_rect(
1616
}
1717
}
1818

19-
pub fn from_f32_size(scale_factor: f64, size: Size<f32>) -> taffy::geometry::Size<f32> {
19+
pub fn from_f32_size(scale_factor: f64, size: Size) -> taffy::geometry::Size<f32> {
2020
taffy::geometry::Size {
21-
width: (scale_factor * size.width as f64) as f32,
22-
height: (scale_factor * size.height as f64) as f32,
21+
width: val_to_f32(scale_factor, size.width),
22+
height: val_to_f32(scale_factor, size.height),
2323
}
2424
}
2525

2626
pub fn from_val_size(
2727
scale_factor: f64,
28-
size: Size<Val>,
28+
size: Size,
2929
) -> taffy::geometry::Size<taffy::style::Dimension> {
3030
taffy::geometry::Size {
3131
width: from_val(scale_factor, size.width),
@@ -60,6 +60,15 @@ pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style {
6060
}
6161
}
6262

63+
/// Converts a [`Val`] to a [`f32`] while respecting the scale factor.
64+
pub fn val_to_f32(scale_factor: f64, val: Val) -> f32 {
65+
match val {
66+
Val::Undefined | Val::Auto => 0.0,
67+
Val::Px(value) => (scale_factor * value as f64) as f32,
68+
Val::Percent(value) => value / 100.0,
69+
}
70+
}
71+
6372
pub fn from_val(scale_factor: f64, val: Val) -> taffy::style::Dimension {
6473
match val {
6574
Val::Auto => taffy::style::Dimension::Auto,

crates/bevy_ui/src/geometry.rs

Lines changed: 69 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::Val;
12
use bevy_math::Vec2;
23
use bevy_reflect::Reflect;
34
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
@@ -119,20 +120,20 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
119120
/// bottom: Val::Px(40.0),
120121
/// };
121122
/// ```
122-
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
123+
#[derive(Copy, Clone, PartialEq, Debug, Default, Reflect)]
123124
#[reflect(PartialEq)]
124-
pub struct UiRect<T: Reflect + PartialEq> {
125+
pub struct UiRect {
125126
/// The value corresponding to the left side of the UI rect.
126-
pub left: T,
127+
pub left: Val,
127128
/// The value corresponding to the right side of the UI rect.
128-
pub right: T,
129+
pub right: Val,
129130
/// The value corresponding to the top side of the UI rect.
130-
pub top: T,
131+
pub top: Val,
131132
/// The value corresponding to the bottom side of the UI rect.
132-
pub bottom: T,
133+
pub bottom: Val,
133134
}
134135

135-
impl<T: Reflect + PartialEq> UiRect<T> {
136+
impl UiRect {
136137
/// Creates a new [`UiRect`] from the values specified.
137138
///
138139
/// # Example
@@ -152,7 +153,7 @@ impl<T: Reflect + PartialEq> UiRect<T> {
152153
/// assert_eq!(ui_rect.top, Val::Px(30.0));
153154
/// assert_eq!(ui_rect.bottom, Val::Px(40.0));
154155
/// ```
155-
pub fn new(left: T, right: T, top: T, bottom: T) -> Self {
156+
pub fn new(left: Val, right: Val, top: Val, bottom: Val) -> Self {
156157
UiRect {
157158
left,
158159
right,
@@ -175,43 +176,29 @@ impl<T: Reflect + PartialEq> UiRect<T> {
175176
/// assert_eq!(ui_rect.top, Val::Px(10.0));
176177
/// assert_eq!(ui_rect.bottom, Val::Px(10.0));
177178
/// ```
178-
pub fn all(value: T) -> Self
179-
where
180-
T: Clone,
181-
{
179+
pub fn all(value: Val) -> Self {
182180
UiRect {
183-
left: value.clone(),
184-
right: value.clone(),
185-
top: value.clone(),
181+
left: value,
182+
right: value,
183+
top: value,
186184
bottom: value,
187185
}
188186
}
189187
}
190188

191-
impl<T: Default + Reflect + PartialEq> Default for UiRect<T> {
192-
fn default() -> Self {
193-
Self {
194-
left: Default::default(),
195-
right: Default::default(),
196-
top: Default::default(),
197-
bottom: Default::default(),
198-
}
199-
}
200-
}
201-
202189
/// A 2-dimensional area defined by a width and height.
203190
///
204191
/// It is commonly used to define the size of a text or UI element.
205-
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
192+
#[derive(Copy, Clone, PartialEq, Debug, Default, Reflect)]
206193
#[reflect(PartialEq)]
207-
pub struct Size<T: Reflect + PartialEq = f32> {
194+
pub struct Size {
208195
/// The width of the 2-dimensional area.
209-
pub width: T,
196+
pub width: Val,
210197
/// The height of the 2-dimensional area.
211-
pub height: T,
198+
pub height: Val,
212199
}
213200

214-
impl<T: Reflect + PartialEq> Size<T> {
201+
impl Size {
215202
/// Creates a new [`Size`] from a width and a height.
216203
///
217204
/// # Example
@@ -224,25 +211,13 @@ impl<T: Reflect + PartialEq> Size<T> {
224211
/// assert_eq!(size.width, Val::Px(100.0));
225212
/// assert_eq!(size.height, Val::Px(200.0));
226213
/// ```
227-
pub fn new(width: T, height: T) -> Self {
214+
pub fn new(width: Val, height: Val) -> Self {
228215
Size { width, height }
229216
}
230217
}
231218

232-
impl<T: Default + Reflect + PartialEq> Default for Size<T> {
233-
fn default() -> Self {
234-
Self {
235-
width: Default::default(),
236-
height: Default::default(),
237-
}
238-
}
239-
}
240-
241-
impl<T: Reflect + PartialEq> Add<Vec2> for Size<T>
242-
where
243-
T: Add<f32, Output = T>,
244-
{
245-
type Output = Size<T>;
219+
impl Add<Vec2> for Size {
220+
type Output = Size;
246221

247222
fn add(self, rhs: Vec2) -> Self::Output {
248223
Self {
@@ -252,21 +227,15 @@ where
252227
}
253228
}
254229

255-
impl<T: Reflect + PartialEq> AddAssign<Vec2> for Size<T>
256-
where
257-
T: AddAssign<f32>,
258-
{
230+
impl AddAssign<Vec2> for Size {
259231
fn add_assign(&mut self, rhs: Vec2) {
260232
self.width += rhs.x;
261233
self.height += rhs.y;
262234
}
263235
}
264236

265-
impl<T: Reflect + PartialEq> Sub<Vec2> for Size<T>
266-
where
267-
T: Sub<f32, Output = T>,
268-
{
269-
type Output = Size<T>;
237+
impl Sub<Vec2> for Size {
238+
type Output = Size;
270239

271240
fn sub(self, rhs: Vec2) -> Self::Output {
272241
Self {
@@ -276,21 +245,15 @@ where
276245
}
277246
}
278247

279-
impl<T: Reflect + PartialEq> SubAssign<Vec2> for Size<T>
280-
where
281-
T: SubAssign<f32>,
282-
{
248+
impl SubAssign<Vec2> for Size {
283249
fn sub_assign(&mut self, rhs: Vec2) {
284250
self.width -= rhs.x;
285251
self.height -= rhs.y;
286252
}
287253
}
288254

289-
impl<T: Reflect + PartialEq> Mul<f32> for Size<T>
290-
where
291-
T: Mul<f32, Output = T>,
292-
{
293-
type Output = Size<T>;
255+
impl Mul<f32> for Size {
256+
type Output = Size;
294257

295258
fn mul(self, rhs: f32) -> Self::Output {
296259
Self::Output {
@@ -300,21 +263,15 @@ where
300263
}
301264
}
302265

303-
impl<T: Reflect + PartialEq> MulAssign<f32> for Size<T>
304-
where
305-
T: MulAssign<f32>,
306-
{
266+
impl MulAssign<f32> for Size {
307267
fn mul_assign(&mut self, rhs: f32) {
308268
self.width *= rhs;
309269
self.height *= rhs;
310270
}
311271
}
312272

313-
impl<T: Reflect + PartialEq> Div<f32> for Size<T>
314-
where
315-
T: Div<f32, Output = T>,
316-
{
317-
type Output = Size<T>;
273+
impl Div<f32> for Size {
274+
type Output = Size;
318275

319276
fn div(self, rhs: f32) -> Self::Output {
320277
Self::Output {
@@ -324,10 +281,7 @@ where
324281
}
325282
}
326283

327-
impl<T: Reflect + PartialEq> DivAssign<f32> for Size<T>
328-
where
329-
T: DivAssign<f32>,
330-
{
284+
impl DivAssign<f32> for Size {
331285
fn div_assign(&mut self, rhs: f32) {
332286
self.width /= rhs;
333287
self.height /= rhs;
@@ -339,22 +293,50 @@ mod tests {
339293
use super::*;
340294

341295
#[test]
342-
fn size_ops() {
296+
fn test_size_add() {
297+
assert_eq!(
298+
Size::new(Val::Px(10.), Val::Px(10.)) + Vec2::new(10., 10.),
299+
Size::new(Val::Px(20.), Val::Px(20.))
300+
);
301+
302+
let mut size = Size::new(Val::Px(10.), Val::Px(10.));
303+
size += Vec2::new(10., 10.);
304+
assert_eq!(size, Size::new(Val::Px(20.), Val::Px(20.)));
305+
}
306+
307+
#[test]
308+
fn test_size_sub() {
343309
assert_eq!(
344-
Size::new(10., 10.) + Vec2::new(10., 10.),
345-
Size::new(20., 20.)
310+
Size::new(Val::Px(20.), Val::Px(20.)) - Vec2::new(10., 10.),
311+
Size::new(Val::Px(10.), Val::Px(10.))
346312
);
313+
314+
let mut size = Size::new(Val::Px(20.), Val::Px(20.));
315+
size -= Vec2::new(10., 10.);
316+
assert_eq!(size, Size::new(Val::Px(10.), Val::Px(10.)));
317+
}
318+
319+
#[test]
320+
fn test_size_mul() {
347321
assert_eq!(
348-
Size::new(20., 20.) - Vec2::new(10., 10.),
349-
Size::new(10., 10.)
322+
Size::new(Val::Px(10.), Val::Px(10.)) * 2.,
323+
Size::new(Val::Px(20.), Val::Px(20.))
350324
);
351-
assert_eq!(Size::new(10., 10.) * 2., Size::new(20., 20.));
352-
assert_eq!(Size::new(20., 20.) / 2., Size::new(10., 10.));
353325

354-
let mut size = Size::new(10., 10.);
326+
let mut size = Size::new(Val::Px(10.), Val::Px(10.));
327+
size *= 2.;
328+
assert_eq!(size, Size::new(Val::Px(20.), Val::Px(20.)));
329+
}
355330

356-
size += Vec2::new(10., 10.);
331+
#[test]
332+
fn test_size_div() {
333+
assert_eq!(
334+
Size::new(Val::Px(20.), Val::Px(20.)) / 2.,
335+
Size::new(Val::Px(10.), Val::Px(10.))
336+
);
357337

358-
assert_eq!(size, Size::new(20., 20.));
338+
let mut size = Size::new(Val::Px(20.), Val::Px(20.));
339+
size /= 2.;
340+
assert_eq!(size, Size::new(Val::Px(10.), Val::Px(10.)));
359341
}
360342
}

crates/bevy_ui/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ impl Plugin for UiPlugin {
6767
.register_type::<Option<f32>>()
6868
.register_type::<Overflow>()
6969
.register_type::<PositionType>()
70-
.register_type::<Size<f32>>()
71-
.register_type::<Size<Val>>()
72-
.register_type::<UiRect<Val>>()
70+
.register_type::<Size>()
71+
.register_type::<UiRect>()
7372
.register_type::<Style>()
7473
.register_type::<UiColor>()
7574
.register_type::<UiImage>()

0 commit comments

Comments
 (0)