11use crate :: Val ;
22use bevy_reflect:: { FromReflect , Reflect , ReflectFromReflect } ;
3- use std:: ops:: { Div , DivAssign , Mul , MulAssign } ;
43
54/// A type which is commonly used to define margins, paddings and borders.
65///
@@ -282,152 +281,6 @@ impl Default for UiRect {
282281 }
283282}
284283
285- /// A 2-dimensional area defined by a width and height.
286- ///
287- /// It is commonly used to define the size of a text or UI element.
288- #[ derive( Copy , Clone , PartialEq , Debug , Reflect , FromReflect ) ]
289- #[ reflect( FromReflect , PartialEq ) ]
290- pub struct Size {
291- /// The width of the 2-dimensional area.
292- pub width : Val ,
293- /// The height of the 2-dimensional area.
294- pub height : Val ,
295- }
296-
297- impl Size {
298- pub const DEFAULT : Self = Self :: AUTO ;
299-
300- /// Creates a new [`Size`] from a width and a height.
301- ///
302- /// # Example
303- ///
304- /// ```
305- /// # use bevy_ui::{Size, Val};
306- /// #
307- /// let size = Size::new(Val::Px(100.0), Val::Px(200.0));
308- ///
309- /// assert_eq!(size.width, Val::Px(100.0));
310- /// assert_eq!(size.height, Val::Px(200.0));
311- /// ```
312- pub const fn new ( width : Val , height : Val ) -> Self {
313- Size { width, height }
314- }
315-
316- /// Creates a new [`Size`] where both sides take the given value.
317- ///
318- /// # Example
319- ///
320- /// ```
321- /// # use bevy_ui::{Size, Val};
322- /// #
323- /// let size = Size::all(Val::Px(10.));
324- ///
325- /// assert_eq!(size.width, Val::Px(10.0));
326- /// assert_eq!(size.height, Val::Px(10.0));
327- /// ```
328- pub const fn all ( value : Val ) -> Self {
329- Self {
330- width : value,
331- height : value,
332- }
333- }
334-
335- /// Creates a new [`Size`] where `width` takes the given value,
336- /// and `height` is set to [`Val::Auto`].
337-
338- ///
339- /// # Example
340- ///
341- /// ```
342- /// # use bevy_ui::{Size, Val};
343- /// #
344- /// let size = Size::width(Val::Px(10.));
345- ///
346- /// assert_eq!(size.width, Val::Px(10.0));
347- /// assert_eq!(size.height, Val::Auto);
348- /// ```
349- pub const fn width ( width : Val ) -> Self {
350- Self {
351- width,
352- height : Val :: Auto ,
353- }
354- }
355-
356- /// Creates a new [`Size`] where `height` takes the given value,
357- /// and `width` is set to [`Val::Auto`].
358- ///
359- /// # Example
360- ///
361- /// ```
362- /// # use bevy_ui::{Size, Val};
363- /// #
364- /// let size = Size::height(Val::Px(10.));
365- ///
366- /// assert_eq!(size.width, Val::Auto);
367- /// assert_eq!(size.height, Val::Px(10.));
368- /// ```
369- pub const fn height ( height : Val ) -> Self {
370- Self {
371- width : Val :: Auto ,
372- height,
373- }
374- }
375-
376- /// Creates a Size where both values are [`Val::Auto`].
377- pub const AUTO : Self = Self :: all ( Val :: Auto ) ;
378- }
379-
380- impl Default for Size {
381- fn default ( ) -> Self {
382- Self :: DEFAULT
383- }
384- }
385-
386- impl From < ( Val , Val ) > for Size {
387- fn from ( vals : ( Val , Val ) ) -> Self {
388- Self {
389- width : vals. 0 ,
390- height : vals. 1 ,
391- }
392- }
393- }
394-
395- impl Mul < f32 > for Size {
396- type Output = Size ;
397-
398- fn mul ( self , rhs : f32 ) -> Self :: Output {
399- Self :: Output {
400- width : self . width * rhs,
401- height : self . height * rhs,
402- }
403- }
404- }
405-
406- impl MulAssign < f32 > for Size {
407- fn mul_assign ( & mut self , rhs : f32 ) {
408- self . width *= rhs;
409- self . height *= rhs;
410- }
411- }
412-
413- impl Div < f32 > for Size {
414- type Output = Size ;
415-
416- fn div ( self , rhs : f32 ) -> Self :: Output {
417- Self :: Output {
418- width : self . width / rhs,
419- height : self . height / rhs,
420- }
421- }
422- }
423-
424- impl DivAssign < f32 > for Size {
425- fn div_assign ( & mut self , rhs : f32 ) {
426- self . width /= rhs;
427- self . height /= rhs;
428- }
429- }
430-
431284#[ cfg( test) ]
432285mod tests {
433286 use super :: * ;
@@ -446,91 +299,6 @@ mod tests {
446299 assert_eq ! ( UiRect :: default ( ) , UiRect :: DEFAULT ) ;
447300 }
448301
449- #[ test]
450- fn test_size_from ( ) {
451- let size: Size = ( Val :: Px ( 20. ) , Val :: Px ( 30. ) ) . into ( ) ;
452-
453- assert_eq ! (
454- size,
455- Size {
456- width: Val :: Px ( 20. ) ,
457- height: Val :: Px ( 30. ) ,
458- }
459- ) ;
460- }
461-
462- #[ test]
463- fn test_size_mul ( ) {
464- assert_eq ! ( Size :: all( Val :: Px ( 10. ) ) * 2. , Size :: all( Val :: Px ( 20. ) ) ) ;
465-
466- let mut size = Size :: all ( Val :: Px ( 10. ) ) ;
467- size *= 2. ;
468- assert_eq ! ( size, Size :: all( Val :: Px ( 20. ) ) ) ;
469- }
470-
471- #[ test]
472- fn test_size_div ( ) {
473- assert_eq ! (
474- Size :: new( Val :: Px ( 20. ) , Val :: Px ( 20. ) ) / 2. ,
475- Size :: new( Val :: Px ( 10. ) , Val :: Px ( 10. ) )
476- ) ;
477-
478- let mut size = Size :: new ( Val :: Px ( 20. ) , Val :: Px ( 20. ) ) ;
479- size /= 2. ;
480- assert_eq ! ( size, Size :: new( Val :: Px ( 10. ) , Val :: Px ( 10. ) ) ) ;
481- }
482-
483- #[ test]
484- fn test_size_all ( ) {
485- let length = Val :: Px ( 10. ) ;
486-
487- assert_eq ! (
488- Size :: all( length) ,
489- Size {
490- width: length,
491- height: length
492- }
493- ) ;
494- }
495-
496- #[ test]
497- fn test_size_width ( ) {
498- let width = Val :: Px ( 10. ) ;
499-
500- assert_eq ! (
501- Size :: width( width) ,
502- Size {
503- width,
504- ..Default :: default ( )
505- }
506- ) ;
507- }
508-
509- #[ test]
510- fn test_size_height ( ) {
511- let height = Val :: Px ( 7. ) ;
512-
513- assert_eq ! (
514- Size :: height( height) ,
515- Size {
516- height,
517- ..Default :: default ( )
518- }
519- ) ;
520- }
521-
522- #[ test]
523- fn size_default_equals_const_default ( ) {
524- assert_eq ! (
525- Size :: default ( ) ,
526- Size {
527- width: Val :: Auto ,
528- height: Val :: Auto
529- }
530- ) ;
531- assert_eq ! ( Size :: default ( ) , Size :: DEFAULT ) ;
532- }
533-
534302 #[ test]
535303 fn test_uirect_axes ( ) {
536304 let x = Val :: Px ( 1. ) ;
0 commit comments