Skip to content

Commit 7e7c5c2

Browse files
author
Marvin Schürz
committed
Add runtime check for relative sizing to BorderLayoutContainer
1 parent 03c54ed commit 7e7c5c2

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

osu.Framework/Graphics/Containers/BorderLayoutContainer.cs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ protected override void UpdateAfterChildren()
189189
}
190190
}
191191

192+
private static Drawable? getChildIfPresent(Container c) => c.Children.Count > 0 && c.Children[0].IsPresent ? c.Children[0] : null;
193+
194+
private Drawable? topDrawable => getChildIfPresent(top);
195+
private Drawable? bottomDrawable => getChildIfPresent(bottom);
196+
private Drawable? leftDrawable => getChildIfPresent(left);
197+
private Drawable? rightDrawable => getChildIfPresent(right);
198+
192199
private void updateLayout()
193200
{
194201
if (layoutDirection == Direction.Vertical)
@@ -198,17 +205,17 @@ private void updateLayout()
198205

199206
var padding = new MarginPadding
200207
{
201-
Top = getPadding(top, top.DrawHeight + spacing.Top),
202-
Bottom = getPadding(bottom, bottom.DrawHeight + spacing.Bottom),
208+
Top = getPadding(top, Direction.Vertical, spacing.Top),
209+
Bottom = getPadding(bottom, Direction.Vertical, spacing.Bottom),
203210
};
204211

205212
right.Padding = padding;
206213
left.Padding = padding;
207214

208215
center.Padding = padding with
209216
{
210-
Left = getPadding(left, left.DrawWidth + spacing.Left),
211-
Right = getPadding(right, right.DrawWidth + spacing.Right),
217+
Left = getPadding(left, Direction.Horizontal, spacing.Left),
218+
Right = getPadding(right, Direction.Horizontal, spacing.Right),
212219
};
213220
}
214221
else
@@ -218,22 +225,45 @@ private void updateLayout()
218225

219226
var padding = new MarginPadding
220227
{
221-
Left = getPadding(left, left.DrawWidth + spacing.Left),
222-
Right = getPadding(right, right.DrawWidth + spacing.Right),
228+
Left = getPadding(left, Direction.Horizontal, spacing.Left),
229+
Right = getPadding(right, Direction.Horizontal, spacing.Right),
223230
};
224231

225232
top.Padding = padding;
226233
bottom.Padding = padding;
227234

228235
center.Padding = padding with
229236
{
230-
Top = getPadding(top, top.DrawHeight + spacing.Top),
231-
Bottom = getPadding(bottom, bottom.DrawHeight + spacing.Bottom),
237+
Top = getPadding(top, Direction.Vertical, spacing.Top),
238+
Bottom = getPadding(bottom, Direction.Vertical, spacing.Bottom),
232239
};
233240
}
234241

235-
static float getPadding(Container container, float value) =>
236-
container.Count > 0 && container.Children[0].IsPresent ? value : 0;
242+
static float getPadding(Container container, Direction direction, float spacing)
243+
{
244+
if (container.Children.Count == 0 || !container.Children[0].IsPresent)
245+
return 0;
246+
247+
var drawable = container.Children[0];
248+
249+
switch (direction)
250+
{
251+
case Direction.Horizontal:
252+
if ((drawable.RelativeSizeAxes & Axes.X) != 0)
253+
throw new InvalidOperationException($"Drawables positioned on the left/right edge of a {nameof(BorderLayoutContainer)} cannot be sized relatively along the X axis.");
254+
255+
return drawable.LayoutSize.X + spacing;
256+
257+
case Direction.Vertical:
258+
if ((drawable.RelativeSizeAxes & Axes.Y) != 0)
259+
throw new InvalidOperationException($"Drawables positioned on the top/bottom edge of a {nameof(BorderLayoutContainer)} cannot be sized relatively along the Y axis.");
260+
261+
return drawable.LayoutSize.Y + spacing;
262+
263+
default:
264+
throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
265+
}
266+
}
237267
}
238268
}
239269
}

0 commit comments

Comments
 (0)