1
- use taffy:: style:: LengthPercentageAuto ;
2
-
3
1
use crate :: {
4
2
AlignContent , AlignItems , AlignSelf , Display , FlexDirection , FlexWrap , JustifyContent ,
5
3
PositionType , Size , Style , UiRect , Val ,
6
4
} ;
7
5
8
- impl Val {
9
- fn scaled ( self , scale_factor : f64 ) -> Self {
10
- match self {
11
- Val :: Auto => Val :: Auto ,
12
- Val :: Percent ( value) => Val :: Percent ( value) ,
13
- Val :: Px ( value) => Val :: Px ( ( scale_factor * value as f64 ) as f32 ) ,
14
- }
15
- }
6
+ use super :: LayoutContext ;
16
7
17
- fn to_inset ( self ) -> LengthPercentageAuto {
8
+ impl Val {
9
+ fn into_length_percentage_auto (
10
+ self ,
11
+ context : & LayoutContext ,
12
+ ) -> taffy:: style:: LengthPercentageAuto {
18
13
match self {
19
14
Val :: Auto => taffy:: style:: LengthPercentageAuto :: Auto ,
20
- Val :: Percent ( value) => taffy:: style:: LengthPercentageAuto :: Percent ( value / 100.0 ) ,
21
- Val :: Px ( value) => taffy:: style:: LengthPercentageAuto :: Points ( value) ,
15
+ Val :: Percent ( value) => taffy:: style:: LengthPercentageAuto :: Percent ( value / 100. ) ,
16
+ Val :: Px ( value) => taffy:: style:: LengthPercentageAuto :: Points (
17
+ ( context. scale_factor * value as f64 ) as f32 ,
18
+ ) ,
19
+ Val :: VMin ( value) => {
20
+ taffy:: style:: LengthPercentageAuto :: Points ( context. min_size * value / 100. )
21
+ }
22
+ Val :: VMax ( value) => {
23
+ taffy:: style:: LengthPercentageAuto :: Points ( context. max_size * value / 100. )
24
+ }
25
+ Val :: Vw ( value) => {
26
+ taffy:: style:: LengthPercentageAuto :: Points ( context. physical_size . x * value / 100. )
27
+ }
28
+ Val :: Vh ( value) => {
29
+ taffy:: style:: LengthPercentageAuto :: Points ( context. physical_size . y * value / 100. )
30
+ }
22
31
}
23
32
}
24
- }
25
33
26
- impl UiRect {
27
- fn scaled ( self , scale_factor : f64 ) -> Self {
28
- Self {
29
- left : self . left . scaled ( scale_factor) ,
30
- right : self . right . scaled ( scale_factor) ,
31
- top : self . top . scaled ( scale_factor) ,
32
- bottom : self . bottom . scaled ( scale_factor) ,
34
+ fn into_length_percentage ( self , context : & LayoutContext ) -> taffy:: style:: LengthPercentage {
35
+ match self . into_length_percentage_auto ( context) {
36
+ taffy:: style:: LengthPercentageAuto :: Auto => taffy:: style:: LengthPercentage :: Points ( 0.0 ) ,
37
+ taffy:: style:: LengthPercentageAuto :: Percent ( value) => {
38
+ taffy:: style:: LengthPercentage :: Percent ( value)
39
+ }
40
+ taffy:: style:: LengthPercentageAuto :: Points ( value) => {
41
+ taffy:: style:: LengthPercentage :: Points ( value)
42
+ }
33
43
}
34
44
}
35
- }
36
45
37
- impl Size {
38
- fn scaled ( self , scale_factor : f64 ) -> Self {
39
- Self {
40
- width : self . width . scaled ( scale_factor) ,
41
- height : self . height . scaled ( scale_factor) ,
42
- }
43
- }
44
- }
45
-
46
- impl < T : From < Val > > From < UiRect > for taffy:: prelude:: Rect < T > {
47
- fn from ( value : UiRect ) -> Self {
48
- Self {
49
- left : value. left . into ( ) ,
50
- right : value. right . into ( ) ,
51
- top : value. top . into ( ) ,
52
- bottom : value. bottom . into ( ) ,
53
- }
46
+ fn into_dimension ( self , context : & LayoutContext ) -> taffy:: style:: Dimension {
47
+ self . into_length_percentage_auto ( context) . into ( )
54
48
}
55
49
}
56
50
57
- impl < T : From < Val > > From < Size > for taffy:: prelude:: Size < T > {
58
- fn from ( value : Size ) -> Self {
59
- Self {
60
- width : value. width . into ( ) ,
61
- height : value. height . into ( ) ,
62
- }
63
- }
64
- }
65
-
66
- impl From < Val > for taffy:: style:: Dimension {
67
- fn from ( value : Val ) -> Self {
68
- match value {
69
- Val :: Auto => taffy:: style:: Dimension :: Auto ,
70
- Val :: Percent ( value) => taffy:: style:: Dimension :: Percent ( value / 100.0 ) ,
71
- Val :: Px ( value) => taffy:: style:: Dimension :: Points ( value) ,
72
- }
73
- }
74
- }
75
-
76
- impl From < Val > for taffy:: style:: LengthPercentage {
77
- fn from ( value : Val ) -> Self {
78
- match value {
79
- Val :: Auto => taffy:: style:: LengthPercentage :: Points ( 0.0 ) ,
80
- Val :: Percent ( value) => taffy:: style:: LengthPercentage :: Percent ( value / 100.0 ) ,
81
- Val :: Px ( value) => taffy:: style:: LengthPercentage :: Points ( value) ,
51
+ impl UiRect {
52
+ fn map_to_taffy_rect < T > ( self , map_fn : impl Fn ( Val ) -> T ) -> taffy:: geometry:: Rect < T > {
53
+ taffy:: geometry:: Rect {
54
+ left : map_fn ( self . left ) ,
55
+ right : map_fn ( self . right ) ,
56
+ top : map_fn ( self . top ) ,
57
+ bottom : map_fn ( self . bottom ) ,
82
58
}
83
59
}
84
60
}
85
61
86
- impl From < Val > for taffy:: style:: LengthPercentageAuto {
87
- fn from ( value : Val ) -> Self {
88
- match value {
89
- Val :: Auto => taffy:: style:: LengthPercentageAuto :: Auto ,
90
- Val :: Percent ( value) => taffy:: style:: LengthPercentageAuto :: Percent ( value / 100.0 ) ,
91
- Val :: Px ( value) => taffy:: style:: LengthPercentageAuto :: Points ( value) ,
62
+ impl Size {
63
+ fn map_to_taffy_size < T > ( self , map_fn : impl Fn ( Val ) -> T ) -> taffy:: geometry:: Size < T > {
64
+ taffy:: geometry:: Size {
65
+ width : map_fn ( self . width ) ,
66
+ height : map_fn ( self . height ) ,
92
67
}
93
68
}
94
69
}
95
70
96
- pub fn from_style ( scale_factor : f64 , style : & Style ) -> taffy:: style:: Style {
71
+ pub fn from_style ( context : & LayoutContext , style : & Style ) -> taffy:: style:: Style {
97
72
taffy:: style:: Style {
98
73
display : style. display . into ( ) ,
99
74
position : style. position_type . into ( ) ,
@@ -104,22 +79,34 @@ pub fn from_style(scale_factor: f64, style: &Style) -> taffy::style::Style {
104
79
align_content : Some ( style. align_content . into ( ) ) ,
105
80
justify_content : Some ( style. justify_content . into ( ) ) ,
106
81
inset : taffy:: prelude:: Rect {
107
- left : style. left . scaled ( scale_factor ) . to_inset ( ) ,
108
- right : style. right . scaled ( scale_factor ) . to_inset ( ) ,
109
- top : style. top . scaled ( scale_factor ) . to_inset ( ) ,
110
- bottom : style. bottom . scaled ( scale_factor ) . to_inset ( ) ,
82
+ left : style. left . into_length_percentage_auto ( context ) ,
83
+ right : style. right . into_length_percentage_auto ( context ) ,
84
+ top : style. top . into_length_percentage_auto ( context ) ,
85
+ bottom : style. bottom . into_length_percentage_auto ( context ) ,
111
86
} ,
112
- margin : style. margin . scaled ( scale_factor) . into ( ) ,
113
- padding : style. padding . scaled ( scale_factor) . into ( ) ,
114
- border : style. border . scaled ( scale_factor) . into ( ) ,
87
+ margin : style
88
+ . margin
89
+ . map_to_taffy_rect ( |m| m. into_length_percentage_auto ( context) ) ,
90
+ padding : style
91
+ . padding
92
+ . map_to_taffy_rect ( |m| m. into_length_percentage ( context) ) ,
93
+ border : style
94
+ . border
95
+ . map_to_taffy_rect ( |m| m. into_length_percentage ( context) ) ,
115
96
flex_grow : style. flex_grow ,
116
97
flex_shrink : style. flex_shrink ,
117
- flex_basis : style. flex_basis . scaled ( scale_factor) . into ( ) ,
118
- size : style. size . scaled ( scale_factor) . into ( ) ,
119
- min_size : style. min_size . scaled ( scale_factor) . into ( ) ,
120
- max_size : style. max_size . scaled ( scale_factor) . into ( ) ,
98
+ flex_basis : style. flex_basis . into_dimension ( context) ,
99
+ size : style. size . map_to_taffy_size ( |s| s. into_dimension ( context) ) ,
100
+ min_size : style
101
+ . min_size
102
+ . map_to_taffy_size ( |s| s. into_dimension ( context) ) ,
103
+ max_size : style
104
+ . max_size
105
+ . map_to_taffy_size ( |s| s. into_dimension ( context) ) ,
121
106
aspect_ratio : style. aspect_ratio ,
122
- gap : style. gap . scaled ( scale_factor) . into ( ) ,
107
+ gap : style
108
+ . gap
109
+ . map_to_taffy_size ( |s| s. into_length_percentage ( context) ) ,
123
110
justify_self : None ,
124
111
}
125
112
}
@@ -283,7 +270,8 @@ mod tests {
283
270
height : Val :: Percent ( 0. ) ,
284
271
} ,
285
272
} ;
286
- let taffy_style = from_style ( 1.0 , & bevy_style) ;
273
+ let viewport_values = LayoutContext :: new ( 1.0 , bevy_math:: Vec2 :: new ( 800. , 600. ) ) ;
274
+ let taffy_style = from_style ( & viewport_values, & bevy_style) ;
287
275
assert_eq ! ( taffy_style. display, taffy:: style:: Display :: Flex ) ;
288
276
assert_eq ! ( taffy_style. position, taffy:: style:: Position :: Absolute ) ;
289
277
assert ! ( matches!(
@@ -408,4 +396,27 @@ mod tests {
408
396
taffy:: style:: LengthPercentage :: Percent ( 0. )
409
397
) ;
410
398
}
399
+
400
+ #[ test]
401
+ fn test_into_length_percentage ( ) {
402
+ use taffy:: style:: LengthPercentage ;
403
+ let context = LayoutContext :: new ( 2.0 , bevy_math:: Vec2 :: new ( 800. , 600. ) ) ;
404
+ let cases = [
405
+ ( Val :: Auto , LengthPercentage :: Points ( 0. ) ) ,
406
+ ( Val :: Percent ( 1. ) , LengthPercentage :: Percent ( 0.01 ) ) ,
407
+ ( Val :: Px ( 1. ) , LengthPercentage :: Points ( 2. ) ) ,
408
+ ( Val :: Vw ( 1. ) , LengthPercentage :: Points ( 8. ) ) ,
409
+ ( Val :: Vh ( 1. ) , LengthPercentage :: Points ( 6. ) ) ,
410
+ ( Val :: VMin ( 2. ) , LengthPercentage :: Points ( 12. ) ) ,
411
+ ( Val :: VMax ( 2. ) , LengthPercentage :: Points ( 16. ) ) ,
412
+ ] ;
413
+ for ( val, length) in cases {
414
+ assert ! ( match ( val. into_length_percentage( & context) , length) {
415
+ ( LengthPercentage :: Points ( a) , LengthPercentage :: Points ( b) )
416
+ | ( LengthPercentage :: Percent ( a) , LengthPercentage :: Percent ( b) ) =>
417
+ ( a - b) . abs( ) < 0.0001 ,
418
+ _ => false ,
419
+ } , ) ;
420
+ }
421
+ }
411
422
}
0 commit comments