@@ -150,22 +150,23 @@ public void apply(Area area, IResizeable relativeTo, IntSupplier defaultSize) {
150150 int p , s ;
151151 int parentSize = relativeTo .getArea ().getSize (this .axis );
152152 boolean calcParent = relativeTo .isSizeCalculated (this .axis );
153+ Box padding = relativeTo .getArea ().getPadding ();
153154
154155 if (this .sizeCalculated && !this .posCalculated ) {
155156 // size was calculated before
156157 s = area .getSize (this .axis );
157158 if (this .start != null ) {
158- p = calcPoint (this .start , s , parentSize , calcParent );
159+ p = calcPoint (this .start , padding , s , parentSize , calcParent );
159160 } else if (this .end != null ) {
160- p = calcPoint (this .end , s , parentSize , calcParent ) - s ;
161+ p = calcPoint (this .end , padding , s , parentSize , calcParent ) - s ;
161162 } else {
162163 throw new IllegalStateException ();
163164 }
164165 } else if (!this .sizeCalculated && this .posCalculated ) {
165166 // pos was calculated before
166167 p = area .getRelativePoint (this .axis );
167168 if (this .size != null ) {
168- s = this .coverChildren ? 18 : calcSize (this .size , parentSize , calcParent );
169+ s = this .coverChildren ? 18 : calcSize (this .size , padding , parentSize , calcParent );
169170 } else {
170171 s = defaultSize .getAsInt ();
171172 this .sizeCalculated = s > 0 ;
@@ -178,49 +179,50 @@ public void apply(Area area, IResizeable relativeTo, IntSupplier defaultSize) {
178179 s = defaultSize .getAsInt ();
179180 this .sizeCalculated = s > 0 && !this .expanded ;
180181 } else {
181- s = calcSize (this .size , parentSize , calcParent );
182+ s = calcSize (this .size , padding , parentSize , calcParent );
182183 }
183184 this .posCalculated = true ;
184185 } else {
185186 if (this .size == null ) {
186187 if (this .start != null && this .end != null ) {
187- p = calcPoint (this .start , -1 , parentSize , calcParent );
188+ p = calcPoint (this .start , padding , -1 , parentSize , calcParent );
188189 boolean b = this .posCalculated ;
189190 this .posCalculated = false ;
190- int p2 = calcPoint (this .end , -1 , parentSize , calcParent );
191+ int p2 = calcPoint (this .end , padding , -1 , parentSize , calcParent );
191192 s = Math .abs (p2 - p );
192193 this .posCalculated &= b ;
193194 this .sizeCalculated |= this .posCalculated ;
194195 } else {
195196 s = defaultSize .getAsInt ();
196197 this .sizeCalculated = s > 0 && !this .expanded ;
197198 if (this .start == null ) {
198- p = calcPoint (this .end , s , parentSize , calcParent );
199+ p = calcPoint (this .end , padding , s , parentSize , calcParent );
199200 p -= s ;
200201 this .posCalculated &= this .sizeCalculated ;
201202 } else {
202- p = calcPoint (this .start , s , parentSize , calcParent );
203+ p = calcPoint (this .start , padding , s , parentSize , calcParent );
203204 this .posCalculated &= (this .sizeCalculated || !needsSize (this .start ));
204205 }
205206 }
206207 } else if (this .start != null ) {
207- s = calcSize (this .size , parentSize , calcParent );
208- p = calcPoint (this .start , s , parentSize , calcParent );
208+ s = calcSize (this .size , padding , parentSize , calcParent );
209+ p = calcPoint (this .start , padding , s , parentSize , calcParent );
209210 this .posCalculated &= (this .sizeCalculated || !needsSize (this .start ));
210211 } else {
211- s = calcSize (this .size , parentSize , calcParent );
212- p = calcPoint (this .end , s , parentSize , calcParent ) - s ;
212+ s = calcSize (this .size , padding , parentSize , calcParent );
213+ p = calcPoint (this .end , padding , s , parentSize , calcParent ) - s ;
213214 this .posCalculated &= this .sizeCalculated ;
214215 }
215216 }
216217 }
217218
219+ // TODO find a better place to apply the margin, is it needed at all?
218220 // apply padding and margin to size
219221 if (this .sizeCalculated && calcParent && ((this .size != null && this .size .isRelative ()) ||
220222 (this .start != null && this .end != null && (this .start .isRelative () || this .end .isRelative ())))) {
221- Box padding = relativeTo .getArea ().getPadding ();
222223 Box margin = area .getMargin ();
223- s = Math .min (s , parentSize - padding .getTotal (this .axis ) - margin .getTotal (this .axis ));
224+ // padding is applied in calcSize()
225+ s = Math .min (s , parentSize /*- padding.getTotal(this.axis)*/ - margin .getTotal (this .axis ));
224226 }
225227 area .setRelativePoint (this .axis , p );
226228 area .setPoint (this .axis , p + relativeTo .getArea ().x ); // temporary
@@ -236,9 +238,9 @@ public int postApply(Area area, Area relativeTo, int p0, int p1) {
236238 this .sizeCalculated = true ;
237239 if (!isPosCalculated ()) {
238240 if (this .start != null ) {
239- p = calcPoint (this .start , s , relativeTo .getSize (this .axis ), true );
241+ p = calcPoint (this .start , relativeTo . getPadding (), s , relativeTo .getSize (this .axis ), true );
240242 } else if (this .end != null ) {
241- p = calcPoint (this .end , s , relativeTo .getSize (this .axis ), true ) - s ;
243+ p = calcPoint (this .end , relativeTo . getPadding (), s , relativeTo .getSize (this .axis ), true ) - s ;
242244 } else {
243245 p = area .getRelativePoint (this .axis ) + p0 /* + area.getMargin().getStart(this.axis)*/ ;
244246 if (!this .cancelAutoMovement ) {
@@ -258,9 +260,9 @@ public void coverChildrenForEmpty(Area area, Area relativeTo) {
258260 if (!isPosCalculated ()) {
259261 int p ;
260262 if (this .start != null ) {
261- p = calcPoint (this .start , s , relativeTo .getSize (this .axis ), true );
263+ p = calcPoint (this .start , relativeTo . getPadding (), s , relativeTo .getSize (this .axis ), true );
262264 } else if (this .end != null ) {
263- p = calcPoint (this .end , s , relativeTo .getSize (this .axis ), true ) - s ;
265+ p = calcPoint (this .end , relativeTo . getPadding (), s , relativeTo .getSize (this .axis ), true ) - s ;
264266 } else {
265267 p = area .getRelativePoint (this .axis );
266268 }
@@ -306,23 +308,23 @@ public void applyMarginAndPaddingToPos(IGuiElement parent, Area area, Area relat
306308 }
307309 }
308310
309- private int calcSize (Unit s , int parentSize , boolean parentSizeCalculated ) {
311+ private int calcSize (Unit s , Box padding , int parentSize , boolean parentSizeCalculated ) {
310312 if (this .coverChildren ) return 18 ; // placeholder value
311313 float val = s .getValue ();
312314 if (s .isRelative ()) {
313315 if (!parentSizeCalculated ) return (int ) val ;
314- val *= parentSize ;
316+ val *= parentSize - padding . getTotal ( this . axis ) ;
315317 }
316318 val += s .getOffset ();
317319 this .sizeCalculated = true ;
318320 return (int ) val ;
319321 }
320322
321- public int calcPoint (Unit p , int width , int parentSize , boolean parentSizeCalculated ) {
323+ public int calcPoint (Unit p , Box padding , int width , int parentSize , boolean parentSizeCalculated ) {
322324 float val = p .getValue ();
323325 if (!parentSizeCalculated && (p == this .end || p .isRelative ())) return (int ) val ;
324326 if (p .isRelative ()) {
325- val = parentSize * val ;
327+ val * = parentSize + padding . getTotal ( this . axis ) ;
326328 float anchor = p .getAnchor ();
327329 if (width > 0 && anchor != 0 ) {
328330 val -= width * anchor ;
0 commit comments