1
+ use crate :: Val ;
1
2
use bevy_math:: Vec2 ;
2
3
use bevy_reflect:: Reflect ;
3
4
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};
119
120
/// bottom: Val::Px(40.0),
120
121
/// };
121
122
/// ```
122
- #[ derive( Copy , Clone , PartialEq , Debug , Reflect ) ]
123
+ #[ derive( Copy , Clone , PartialEq , Debug , Default , Reflect ) ]
123
124
#[ reflect( PartialEq ) ]
124
- pub struct UiRect < T : Reflect + PartialEq > {
125
+ pub struct UiRect {
125
126
/// The value corresponding to the left side of the UI rect.
126
- pub left : T ,
127
+ pub left : Val ,
127
128
/// The value corresponding to the right side of the UI rect.
128
- pub right : T ,
129
+ pub right : Val ,
129
130
/// The value corresponding to the top side of the UI rect.
130
- pub top : T ,
131
+ pub top : Val ,
131
132
/// The value corresponding to the bottom side of the UI rect.
132
- pub bottom : T ,
133
+ pub bottom : Val ,
133
134
}
134
135
135
- impl < T : Reflect + PartialEq > UiRect < T > {
136
+ impl UiRect {
136
137
/// Creates a new [`UiRect`] from the values specified.
137
138
///
138
139
/// # Example
@@ -152,7 +153,7 @@ impl<T: Reflect + PartialEq> UiRect<T> {
152
153
/// assert_eq!(ui_rect.top, Val::Px(30.0));
153
154
/// assert_eq!(ui_rect.bottom, Val::Px(40.0));
154
155
/// ```
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 {
156
157
UiRect {
157
158
left,
158
159
right,
@@ -175,43 +176,29 @@ impl<T: Reflect + PartialEq> UiRect<T> {
175
176
/// assert_eq!(ui_rect.top, Val::Px(10.0));
176
177
/// assert_eq!(ui_rect.bottom, Val::Px(10.0));
177
178
/// ```
178
- pub fn all ( value : T ) -> Self
179
- where
180
- T : Clone ,
181
- {
179
+ pub fn all ( value : Val ) -> Self {
182
180
UiRect {
183
- left : value. clone ( ) ,
184
- right : value. clone ( ) ,
185
- top : value. clone ( ) ,
181
+ left : value,
182
+ right : value,
183
+ top : value,
186
184
bottom : value,
187
185
}
188
186
}
189
187
}
190
188
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
-
202
189
/// A 2-dimensional area defined by a width and height.
203
190
///
204
191
/// 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 ) ]
206
193
#[ reflect( PartialEq ) ]
207
- pub struct Size < T : Reflect + PartialEq = f32 > {
194
+ pub struct Size {
208
195
/// The width of the 2-dimensional area.
209
- pub width : T ,
196
+ pub width : Val ,
210
197
/// The height of the 2-dimensional area.
211
- pub height : T ,
198
+ pub height : Val ,
212
199
}
213
200
214
- impl < T : Reflect + PartialEq > Size < T > {
201
+ impl Size {
215
202
/// Creates a new [`Size`] from a width and a height.
216
203
///
217
204
/// # Example
@@ -224,25 +211,13 @@ impl<T: Reflect + PartialEq> Size<T> {
224
211
/// assert_eq!(size.width, Val::Px(100.0));
225
212
/// assert_eq!(size.height, Val::Px(200.0));
226
213
/// ```
227
- pub fn new ( width : T , height : T ) -> Self {
214
+ pub fn new ( width : Val , height : Val ) -> Self {
228
215
Size { width, height }
229
216
}
230
217
}
231
218
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 ;
246
221
247
222
fn add ( self , rhs : Vec2 ) -> Self :: Output {
248
223
Self {
@@ -252,21 +227,15 @@ where
252
227
}
253
228
}
254
229
255
- impl < T : Reflect + PartialEq > AddAssign < Vec2 > for Size < T >
256
- where
257
- T : AddAssign < f32 > ,
258
- {
230
+ impl AddAssign < Vec2 > for Size {
259
231
fn add_assign ( & mut self , rhs : Vec2 ) {
260
232
self . width += rhs. x ;
261
233
self . height += rhs. y ;
262
234
}
263
235
}
264
236
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 ;
270
239
271
240
fn sub ( self , rhs : Vec2 ) -> Self :: Output {
272
241
Self {
@@ -276,21 +245,15 @@ where
276
245
}
277
246
}
278
247
279
- impl < T : Reflect + PartialEq > SubAssign < Vec2 > for Size < T >
280
- where
281
- T : SubAssign < f32 > ,
282
- {
248
+ impl SubAssign < Vec2 > for Size {
283
249
fn sub_assign ( & mut self , rhs : Vec2 ) {
284
250
self . width -= rhs. x ;
285
251
self . height -= rhs. y ;
286
252
}
287
253
}
288
254
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 ;
294
257
295
258
fn mul ( self , rhs : f32 ) -> Self :: Output {
296
259
Self :: Output {
@@ -300,21 +263,15 @@ where
300
263
}
301
264
}
302
265
303
- impl < T : Reflect + PartialEq > MulAssign < f32 > for Size < T >
304
- where
305
- T : MulAssign < f32 > ,
306
- {
266
+ impl MulAssign < f32 > for Size {
307
267
fn mul_assign ( & mut self , rhs : f32 ) {
308
268
self . width *= rhs;
309
269
self . height *= rhs;
310
270
}
311
271
}
312
272
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 ;
318
275
319
276
fn div ( self , rhs : f32 ) -> Self :: Output {
320
277
Self :: Output {
@@ -324,10 +281,7 @@ where
324
281
}
325
282
}
326
283
327
- impl < T : Reflect + PartialEq > DivAssign < f32 > for Size < T >
328
- where
329
- T : DivAssign < f32 > ,
330
- {
284
+ impl DivAssign < f32 > for Size {
331
285
fn div_assign ( & mut self , rhs : f32 ) {
332
286
self . width /= rhs;
333
287
self . height /= rhs;
@@ -339,22 +293,50 @@ mod tests {
339
293
use super :: * ;
340
294
341
295
#[ 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 ( ) {
343
309
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. ) )
346
312
) ;
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 ( ) {
347
321
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. ) )
350
324
) ;
351
- assert_eq ! ( Size :: new( 10. , 10. ) * 2. , Size :: new( 20. , 20. ) ) ;
352
- assert_eq ! ( Size :: new( 20. , 20. ) / 2. , Size :: new( 10. , 10. ) ) ;
353
325
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
+ }
355
330
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
+ ) ;
357
337
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. ) ) ) ;
359
341
}
360
342
}
0 commit comments