Skip to content

Commit fe32cab

Browse files
authored
Merge pull request #6145 from EVAST9919/grid-padding
Implement `Padding` property in `GridContainer`
2 parents 05eb920 + a13d857 commit fe32cab

File tree

6 files changed

+294
-141
lines changed

6 files changed

+294
-141
lines changed

osu.Framework.Tests/Visual/Layout/TestSceneGridContainer.cs

+159
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,160 @@ public void TestSetContentByIndex()
836836
void assertContentChangeEventWasFired() => AddAssert("Content change event was fired", () => gridContentChangeEventWasFired);
837837
}
838838

839+
[Test]
840+
public void TestPaddingRelativeSingle()
841+
{
842+
FillBox box = null;
843+
844+
AddStep("set content", () =>
845+
{
846+
grid.Content = new[] { new Drawable[] { box = new FillBox() }, };
847+
grid.RowDimensions = grid.ColumnDimensions = new[] { new Dimension(GridSizeMode.Relative, 1f) };
848+
});
849+
850+
checkCorrectBoxSize(new MarginPadding(20));
851+
checkCorrectBoxSize(new MarginPadding { Horizontal = 20 });
852+
checkCorrectBoxSize(new MarginPadding { Vertical = 20 });
853+
checkCorrectBoxSize(new MarginPadding { Left = 20 });
854+
checkCorrectBoxSize(new MarginPadding { Right = 20 });
855+
checkCorrectBoxSize(new MarginPadding { Top = 20 });
856+
checkCorrectBoxSize(new MarginPadding { Bottom = 20 });
857+
return;
858+
859+
void checkCorrectBoxSize(MarginPadding padding)
860+
{
861+
setPadding(padding);
862+
AddAssert("box size is correct", () => Precision.AlmostEquals(grid.DrawSize - padding.Total, box.DrawSize));
863+
}
864+
}
865+
866+
[Test]
867+
public void TestPaddingAutosizeSingle()
868+
{
869+
FillBox box = null;
870+
871+
AddStep("set content", () =>
872+
{
873+
gridParent.RelativeSizeAxes = Axes.None;
874+
gridParent.AutoSizeAxes = Axes.Both;
875+
876+
grid.RelativeSizeAxes = Axes.None;
877+
grid.AutoSizeAxes = Axes.Both;
878+
grid.Content = new[] { new Drawable[] { box = new FillBox { RelativeSizeAxes = Axes.None, Size = new Vector2(100) } }, };
879+
grid.RowDimensions = grid.ColumnDimensions = new[] { new Dimension(GridSizeMode.AutoSize) };
880+
});
881+
882+
checkCorrectGridSize(new MarginPadding(20));
883+
checkCorrectGridSize(new MarginPadding { Horizontal = 20 });
884+
checkCorrectGridSize(new MarginPadding { Vertical = 20 });
885+
checkCorrectGridSize(new MarginPadding { Left = 20 });
886+
checkCorrectGridSize(new MarginPadding { Right = 20 });
887+
checkCorrectGridSize(new MarginPadding { Top = 20 });
888+
checkCorrectGridSize(new MarginPadding { Bottom = 20 });
889+
return;
890+
891+
void checkCorrectGridSize(MarginPadding padding)
892+
{
893+
setPadding(padding);
894+
AddAssert("grid size is correct", () => Precision.AlmostEquals(grid.DrawSize, box.DrawSize + padding.Total));
895+
}
896+
}
897+
898+
[Test]
899+
public void TestPaddingAutosizeMultiple()
900+
{
901+
FillBox[] boxes = new FillBox[3];
902+
const float box_size = 100f;
903+
904+
AddStep("set content", () =>
905+
{
906+
gridParent.RelativeSizeAxes = Axes.None;
907+
gridParent.AutoSizeAxes = Axes.Both;
908+
909+
grid.RelativeSizeAxes = Axes.None;
910+
grid.AutoSizeAxes = Axes.Both;
911+
grid.RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) };
912+
grid.ColumnDimensions = new[]
913+
{
914+
new Dimension(GridSizeMode.AutoSize),
915+
new Dimension(GridSizeMode.AutoSize),
916+
new Dimension(GridSizeMode.AutoSize)
917+
};
918+
grid.Content = new[]
919+
{
920+
new Drawable[]
921+
{
922+
boxes[0] = new FillBox
923+
{
924+
RelativeSizeAxes = Axes.None,
925+
Size = new Vector2(box_size)
926+
},
927+
boxes[1] = new FillBox
928+
{
929+
RelativeSizeAxes = Axes.None,
930+
Size = new Vector2(box_size)
931+
},
932+
boxes[2] = new FillBox
933+
{
934+
RelativeSizeAxes = Axes.None,
935+
Size = new Vector2(box_size)
936+
}
937+
}
938+
};
939+
});
940+
941+
checkCorrectGridSize(new MarginPadding(20));
942+
checkCorrectGridSize(new MarginPadding { Horizontal = 20 });
943+
checkCorrectGridSize(new MarginPadding { Vertical = 20 });
944+
checkCorrectGridSize(new MarginPadding { Left = 20 });
945+
checkCorrectGridSize(new MarginPadding { Right = 20 });
946+
checkCorrectGridSize(new MarginPadding { Top = 20 });
947+
checkCorrectGridSize(new MarginPadding { Bottom = 20 });
948+
return;
949+
950+
void checkCorrectGridSize(MarginPadding padding)
951+
{
952+
setPadding(padding);
953+
AddAssert("grid size is correct", () => Precision.AlmostEquals(grid.DrawSize, new Vector2(box_size * 3, box_size) + padding.Total));
954+
}
955+
}
956+
957+
[TestCase(false)]
958+
[TestCase(true)]
959+
public void TestPaddingRelativeMultiple(bool row)
960+
{
961+
FillBox[] boxes = new FillBox[3];
962+
963+
setSingleDimensionContent(() => new[]
964+
{
965+
new Drawable[] { boxes[0] = new FillBox(), boxes[1] = new FillBox(), boxes[2] = new FillBox() }
966+
}, row: row);
967+
968+
checkSizes(new MarginPadding(20));
969+
checkSizes(new MarginPadding { Horizontal = 20 });
970+
checkSizes(new MarginPadding { Vertical = 20 });
971+
checkSizes(new MarginPadding { Left = 20 });
972+
checkSizes(new MarginPadding { Right = 20 });
973+
checkSizes(new MarginPadding { Top = 20 });
974+
checkSizes(new MarginPadding { Bottom = 20 });
975+
return;
976+
977+
void checkSizes(MarginPadding padding)
978+
{
979+
setPadding(padding);
980+
981+
for (int i = 0; i < 3; i++)
982+
{
983+
int local = i;
984+
985+
if (row)
986+
AddAssert($"box {local} has correct size", () => Precision.AlmostEquals(boxes[local].DrawSize, new Vector2((grid.DrawWidth - padding.TotalHorizontal) / 3f, grid.DrawHeight - padding.TotalVertical)));
987+
else
988+
AddAssert($"box {local} has correct size", () => Precision.AlmostEquals(boxes[local].DrawSize, new Vector2(grid.DrawWidth - padding.TotalHorizontal, (grid.DrawHeight - padding.TotalVertical) / 3f)));
989+
}
990+
}
991+
}
992+
839993
/// <summary>
840994
/// Returns drawable dimension along desired axis.
841995
/// </summary>
@@ -888,6 +1042,11 @@ private void setSingleDimensionContent(Func<Drawable[][]> contentFunc, Dimension
8881042
grid.ColumnDimensions = dimensions;
8891043
});
8901044

1045+
private void setPadding(MarginPadding padding) => AddStep($"set padding {padding.Left}, {padding.Top}, {padding.Right}, {padding.Bottom}", () =>
1046+
{
1047+
grid.Padding = padding;
1048+
});
1049+
8911050
private partial class FillBox : Box
8921051
{
8931052
public FillBox()

osu.Framework.Tests/Visual/Sprites/TestSceneAnimationLayout.cs

+25-29
Original file line numberDiff line numberDiff line change
@@ -39,47 +39,43 @@ public TestSceneAnimationLayout()
3939
Cell(1, 2).Child = createTest("drawable - fixed size", () => new TestDrawableAnimation(Axes.Both) { Size = new Vector2(100, 50) });
4040
}
4141

42-
private Drawable createTest(string name, Func<Drawable> animationCreationFunc) => new Container
42+
private Drawable createTest(string name, Func<Drawable> animationCreationFunc) => new GridContainer
4343
{
4444
RelativeSizeAxes = Axes.Both,
4545
Padding = new MarginPadding(10),
46-
Child = new GridContainer
46+
Content = new[]
4747
{
48-
RelativeSizeAxes = Axes.Both,
49-
Content = new[]
48+
new Drawable[]
5049
{
51-
new Drawable[]
50+
new SpriteText
5251
{
53-
new SpriteText
54-
{
55-
Anchor = Anchor.TopCentre,
56-
Origin = Anchor.TopCentre,
57-
Text = name
58-
},
52+
Anchor = Anchor.TopCentre,
53+
Origin = Anchor.TopCentre,
54+
Text = name
5955
},
60-
new Drawable[]
56+
},
57+
new Drawable[]
58+
{
59+
new Container
6160
{
62-
new Container
61+
RelativeSizeAxes = Axes.Both,
62+
Masking = true,
63+
BorderColour = Color4.OrangeRed,
64+
BorderThickness = 2,
65+
Children = new[]
6366
{
64-
RelativeSizeAxes = Axes.Both,
65-
Masking = true,
66-
BorderColour = Color4.OrangeRed,
67-
BorderThickness = 2,
68-
Children = new[]
67+
new Box
6968
{
70-
new Box
71-
{
72-
RelativeSizeAxes = Axes.Both,
73-
Alpha = 0,
74-
AlwaysPresent = true
75-
},
76-
animationCreationFunc()
77-
}
69+
RelativeSizeAxes = Axes.Both,
70+
Alpha = 0,
71+
AlwaysPresent = true
72+
},
73+
animationCreationFunc()
7874
}
79-
},
75+
}
8076
},
81-
RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) }
82-
}
77+
},
78+
RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) }
8379
};
8480

8581
private partial class TestDrawableAnimation : DrawableAnimation

osu.Framework.Tests/Visual/Sprites/TestSceneLargeTextureStore.cs

+18-22
Original file line numberDiff line numberDiff line change
@@ -64,35 +64,31 @@ private void load(IRenderer renderer, GameHost host, Game game)
6464
Origin = Anchor.TopCentre,
6565
Text = $"useManaulMipmaps: {useManualMipmaps}"
6666
},
67-
new Container
67+
new GridContainer
6868
{
6969
RelativeSizeAxes = Axes.Both,
7070
Padding = new MarginPadding { Top = 20 },
71-
Child = new GridContainer
71+
ColumnDimensions = new[]
7272
{
73-
RelativeSizeAxes = Axes.Both,
74-
ColumnDimensions = new[]
75-
{
76-
new Dimension(GridSizeMode.Relative, 0.5f),
77-
new Dimension(GridSizeMode.Relative, 0.5f),
78-
},
79-
RowDimensions = new[]
73+
new Dimension(GridSizeMode.Relative, 0.5f),
74+
new Dimension(GridSizeMode.Relative, 0.5f),
75+
},
76+
RowDimensions = new[]
77+
{
78+
new Dimension(GridSizeMode.Relative, 0.5f),
79+
new Dimension(GridSizeMode.Relative, 0.5f),
80+
},
81+
Content = new[]
82+
{
83+
new Drawable[]
8084
{
81-
new Dimension(GridSizeMode.Relative, 0.5f),
82-
new Dimension(GridSizeMode.Relative, 0.5f),
85+
new MipmapSprite(0),
86+
new MipmapSprite(1)
8387
},
84-
Content = new[]
88+
new Drawable[]
8589
{
86-
new Drawable[]
87-
{
88-
new MipmapSprite(0),
89-
new MipmapSprite(1)
90-
},
91-
new Drawable[]
92-
{
93-
new MipmapSprite(2),
94-
new MipmapSprite(3)
95-
}
90+
new MipmapSprite(2),
91+
new MipmapSprite(3)
9692
}
9793
}
9894
}

osu.Framework.Tests/Visual/Sprites/TestSceneVideoLayout.cs

+25-29
Original file line numberDiff line numberDiff line change
@@ -41,47 +41,43 @@ private void load(Game game)
4141
});
4242
}
4343

44-
private Drawable createTest(string name, Func<Drawable> animationCreationFunc) => new Container
44+
private Drawable createTest(string name, Func<Drawable> animationCreationFunc) => new GridContainer
4545
{
4646
RelativeSizeAxes = Axes.Both,
4747
Padding = new MarginPadding(10),
48-
Child = new GridContainer
48+
Content = new[]
4949
{
50-
RelativeSizeAxes = Axes.Both,
51-
Content = new[]
50+
new Drawable[]
5251
{
53-
new Drawable[]
52+
new SpriteText
5453
{
55-
new SpriteText
56-
{
57-
Anchor = Anchor.TopCentre,
58-
Origin = Anchor.TopCentre,
59-
Text = name
60-
},
54+
Anchor = Anchor.TopCentre,
55+
Origin = Anchor.TopCentre,
56+
Text = name
6157
},
62-
new Drawable[]
58+
},
59+
new Drawable[]
60+
{
61+
new Container
6362
{
64-
new Container
63+
RelativeSizeAxes = Axes.Both,
64+
Masking = true,
65+
BorderColour = Color4.OrangeRed,
66+
BorderThickness = 2,
67+
Children = new[]
6568
{
66-
RelativeSizeAxes = Axes.Both,
67-
Masking = true,
68-
BorderColour = Color4.OrangeRed,
69-
BorderThickness = 2,
70-
Children = new[]
69+
new Box
7170
{
72-
new Box
73-
{
74-
RelativeSizeAxes = Axes.Both,
75-
Alpha = 0,
76-
AlwaysPresent = true
77-
},
78-
animationCreationFunc()
79-
}
71+
RelativeSizeAxes = Axes.Both,
72+
Alpha = 0,
73+
AlwaysPresent = true
74+
},
75+
animationCreationFunc()
8076
}
81-
},
77+
}
8278
},
83-
RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) }
84-
}
79+
},
80+
RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) }
8581
};
8682
}
8783
}

0 commit comments

Comments
 (0)