@@ -41,26 +41,64 @@ public Rect Relative(float left, float top, float right, float bottom)
4141
4242 public Rect ToRect ( )
4343 {
44- var left = Left . Apply ( parent . xMin ) ;
45- var top = Top . Apply ( parent . yMin ) ;
46- var width = Width . IsSet ? Width . Apply ( parent . width ) : CalculateWidth ( ) ;
47- var height = Height . IsSet ? Height . Apply ( parent . height ) : CalculateHeight ( ) ;
44+ float left = 0 , top = 0 , right = 0 , bottom = 0 ;
4845
49- return new Rect ( left , top , width , height ) ;
46+ CalculateHorizontal ( out left , out right ) ;
47+ CalculateVertical ( out top , out bottom ) ;
48+
49+ return new Rect ( left , top , right , bottom ) ;
5050 }
5151
52- private float CalculateWidth ( )
52+ private void CalculateHorizontal ( out float left , out float right )
5353 {
54- var right = Right . Apply ( parent . xMax ) ;
55- var left = Left . Apply ( parent . xMin ) ;
56- return right - left ;
54+ if ( ! Width . IsSet || ( Left . IsSet && Right . IsSet ) )
55+ {
56+ left = Left . Apply ( parent . xMin ) ;
57+ right = Right . Apply ( parent . xMax ) - left ;
58+ return ;
59+ }
60+
61+ if ( Left . IsSet && ! Right . IsSet )
62+ {
63+ left = Left . Apply ( parent . xMin ) ;
64+ right = Width . Apply ( parent . width ) ;
65+ }
66+ else if ( ! Left . IsSet && Right . IsSet )
67+ {
68+ right = Width . Apply ( parent . width ) ;
69+ left = Right . Apply ( parent . xMax ) - right ;
70+ }
71+ else
72+ {
73+ left = Left . Apply ( parent . xMin ) ;
74+ right = Width . Apply ( parent . width ) ;
75+ }
5776 }
5877
59- private float CalculateHeight ( )
78+ private void CalculateVertical ( out float top , out float bottom )
6079 {
61- var bottom = Bottom . Apply ( parent . yMax ) ;
62- var top = Top . Apply ( parent . yMin ) ;
63- return bottom - top ;
80+ if ( ! Height . IsSet || ( Top . IsSet && Bottom . IsSet ) )
81+ {
82+ top = Top . Apply ( parent . yMin ) ;
83+ bottom = Bottom . Apply ( parent . yMax ) - top ;
84+ return ;
85+ }
86+
87+ if ( Top . IsSet && ! Bottom . IsSet )
88+ {
89+ top = Top . Apply ( parent . yMin ) ;
90+ bottom = Height . Apply ( parent . height ) ;
91+ }
92+ else if ( ! Top . IsSet && Bottom . IsSet )
93+ {
94+ bottom = Height . Apply ( parent . height ) ;
95+ top = Bottom . Apply ( parent . yMax ) - bottom ;
96+ }
97+ else
98+ {
99+ top = Top . Apply ( parent . yMin ) ;
100+ bottom = Height . Apply ( parent . height ) ;
101+ }
64102 }
65103 }
66104}
0 commit comments