|
| 1 | +// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. |
| 2 | +// See the LICENCE file in the repository root for full licence text. |
| 3 | + |
| 4 | +using System; |
| 5 | +using NUnit.Framework; |
| 6 | +using osu.Framework.Bindables; |
| 7 | +using osu.Framework.Extensions.Color4Extensions; |
| 8 | +using osu.Framework.Graphics; |
| 9 | +using osu.Framework.Graphics.Containers; |
| 10 | +using osu.Framework.Graphics.Shapes; |
| 11 | +using osu.Framework.Graphics.Sprites; |
| 12 | +using osu.Framework.Graphics.UserInterface; |
| 13 | +using osuTK; |
| 14 | +using osuTK.Graphics; |
| 15 | + |
| 16 | +namespace osu.Framework.Tests.Visual.Layout |
| 17 | +{ |
| 18 | + [TestFixture] |
| 19 | + public partial class TestSceneBorderLayoutContainer : FrameworkTestScene |
| 20 | + { |
| 21 | + private readonly BorderLayoutContainer borderLayout; |
| 22 | + |
| 23 | + public TestSceneBorderLayoutContainer() |
| 24 | + { |
| 25 | + Box top, bottom, left, right; |
| 26 | + |
| 27 | + Children = new Drawable[] |
| 28 | + { |
| 29 | + new BorderLayoutContainer |
| 30 | + { |
| 31 | + RelativeSizeAxes = Axes.Both, |
| 32 | + Center = borderLayout = new BorderLayoutContainer |
| 33 | + { |
| 34 | + RelativeSizeAxes = Axes.Both, |
| 35 | + Top = top = new Box |
| 36 | + { |
| 37 | + RelativeSizeAxes = Axes.X, |
| 38 | + Height = 100, |
| 39 | + Colour = Color4.Red, |
| 40 | + }, |
| 41 | + Bottom = bottom = new Box |
| 42 | + { |
| 43 | + RelativeSizeAxes = Axes.X, |
| 44 | + Height = 100, |
| 45 | + Colour = Color4.Blue, |
| 46 | + }, |
| 47 | + Left = left = new Box |
| 48 | + { |
| 49 | + RelativeSizeAxes = Axes.Y, |
| 50 | + Width = 100, |
| 51 | + Colour = Color4.Yellow, |
| 52 | + }, |
| 53 | + Right = right = new Box |
| 54 | + { |
| 55 | + RelativeSizeAxes = Axes.Y, |
| 56 | + Width = 100, |
| 57 | + Colour = Color4.Green, |
| 58 | + }, |
| 59 | + Center = new Box |
| 60 | + { |
| 61 | + RelativeSizeAxes = Axes.Both, |
| 62 | + Colour = Color4.Gray |
| 63 | + } |
| 64 | + }, |
| 65 | + Left = new LayoutEdgeParameters("Left") |
| 66 | + { |
| 67 | + ResizeAction = value => left.Width = value, |
| 68 | + VisibilityAction = value => left.Alpha = value ? 1 : 0, |
| 69 | + SpacingAction = value => borderLayout.Spacing = borderLayout.Spacing with { Left = value }, |
| 70 | + }, |
| 71 | + Right = new LayoutEdgeParameters("Right") |
| 72 | + { |
| 73 | + ResizeAction = value => right.Width = value, |
| 74 | + VisibilityAction = value => right.Alpha = value ? 1 : 0, |
| 75 | + SpacingAction = value => borderLayout.Spacing = borderLayout.Spacing with { Right = value } |
| 76 | + }, |
| 77 | + Top = new LayoutEdgeParameters("Top") |
| 78 | + { |
| 79 | + ResizeAction = value => top.Height = value, |
| 80 | + VisibilityAction = value => top.Alpha = value ? 1 : 0, |
| 81 | + SpacingAction = value => borderLayout.Spacing = borderLayout.Spacing with { Top = value } |
| 82 | + }, |
| 83 | + Bottom = new LayoutEdgeParameters("Bottom") |
| 84 | + { |
| 85 | + ResizeAction = value => bottom.Height = value, |
| 86 | + VisibilityAction = value => bottom.Alpha = value ? 1 : 0, |
| 87 | + SpacingAction = value => borderLayout.Spacing = borderLayout.Spacing with { Bottom = value } |
| 88 | + }, |
| 89 | + }, |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + [Test] |
| 94 | + public void TestBorderLayout() |
| 95 | + { |
| 96 | + AddStep("horizontal layout direction", () => borderLayout.LayoutDirection = Direction.Horizontal); |
| 97 | + AddStep("vertical layout direction", () => borderLayout.LayoutDirection = Direction.Vertical); |
| 98 | + AddSliderStep("total spacing", 0f, 100f, 0f, value => borderLayout.Spacing = new MarginPadding(value)); |
| 99 | + } |
| 100 | + |
| 101 | + private partial class LayoutEdgeParameters : Container |
| 102 | + { |
| 103 | + public required Action<float> ResizeAction { private get; init; } |
| 104 | + public required Action<float> SpacingAction { private get; init; } |
| 105 | + |
| 106 | + public Action<bool>? VisibilityAction { private get; init; } |
| 107 | + |
| 108 | + private readonly BindableFloat size = new BindableFloat(100) |
| 109 | + { |
| 110 | + MinValue = 0, |
| 111 | + MaxValue = 300, |
| 112 | + }; |
| 113 | + |
| 114 | + private readonly BindableFloat spacing = new BindableFloat |
| 115 | + { |
| 116 | + MinValue = 0, |
| 117 | + MaxValue = 100, |
| 118 | + }; |
| 119 | + |
| 120 | + private readonly BindableBool visible = new BindableBool(true); |
| 121 | + |
| 122 | + private readonly Drawable visibilityGroup; |
| 123 | + |
| 124 | + protected override Container<Drawable> Content { get; } |
| 125 | + |
| 126 | + public LayoutEdgeParameters(string title) |
| 127 | + { |
| 128 | + AutoSizeAxes = Axes.Y; |
| 129 | + Anchor = Anchor.Centre; |
| 130 | + Origin = Anchor.Centre; |
| 131 | + Padding = new MarginPadding(5); |
| 132 | + |
| 133 | + Width = 150; |
| 134 | + |
| 135 | + InternalChild = Content = new FillFlowContainer |
| 136 | + { |
| 137 | + RelativeSizeAxes = Axes.X, |
| 138 | + AutoSizeAxes = Axes.Y, |
| 139 | + Direction = FillDirection.Vertical, |
| 140 | + Spacing = new Vector2(0, 2), |
| 141 | + Children = new[] |
| 142 | + { |
| 143 | + new SpriteText |
| 144 | + { |
| 145 | + Text = $"{title}:", |
| 146 | + Font = new FontUsage(size: 18) |
| 147 | + }, |
| 148 | + visibilityGroup = new GridContainer |
| 149 | + { |
| 150 | + RelativeSizeAxes = Axes.X, |
| 151 | + AutoSizeAxes = Axes.Y, |
| 152 | + ColumnDimensions = new[] { new Dimension(), new Dimension(GridSizeMode.AutoSize) }, |
| 153 | + RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) }, |
| 154 | + Content = new[] |
| 155 | + { |
| 156 | + new Drawable[] |
| 157 | + { |
| 158 | + new SpriteText |
| 159 | + { |
| 160 | + Text = "Visible", |
| 161 | + Anchor = Anchor.CentreLeft, |
| 162 | + Origin = Anchor.CentreLeft, |
| 163 | + Font = new FontUsage(size: 16), |
| 164 | + }, |
| 165 | + new BasicCheckbox |
| 166 | + { |
| 167 | + Current = visible, |
| 168 | + Scale = new Vector2(0.9f) |
| 169 | + } |
| 170 | + }, |
| 171 | + } |
| 172 | + }, |
| 173 | + new Container |
| 174 | + { |
| 175 | + RelativeSizeAxes = Axes.X, |
| 176 | + AutoSizeAxes = Axes.Y, |
| 177 | + Children = new Drawable[] |
| 178 | + { |
| 179 | + new BasicSliderBar<float> |
| 180 | + { |
| 181 | + RelativeSizeAxes = Axes.X, |
| 182 | + Height = 24, |
| 183 | + BackgroundColour = Color4.RoyalBlue.Darken(0.75f), |
| 184 | + SelectionColour = Color4.RoyalBlue, |
| 185 | + FocusColour = Color4.RoyalBlue, |
| 186 | + Current = size, |
| 187 | + }, |
| 188 | + new SpriteText |
| 189 | + { |
| 190 | + Text = "Size", |
| 191 | + Anchor = Anchor.Centre, |
| 192 | + Origin = Anchor.Centre, |
| 193 | + Font = new FontUsage(size: 16) |
| 194 | + }, |
| 195 | + } |
| 196 | + }, |
| 197 | + new Container |
| 198 | + { |
| 199 | + RelativeSizeAxes = Axes.X, |
| 200 | + AutoSizeAxes = Axes.Y, |
| 201 | + Children = new Drawable[] |
| 202 | + { |
| 203 | + new BasicSliderBar<float> |
| 204 | + { |
| 205 | + RelativeSizeAxes = Axes.X, |
| 206 | + Height = 24, |
| 207 | + BackgroundColour = Color4.RoyalBlue.Darken(0.75f), |
| 208 | + SelectionColour = Color4.RoyalBlue, |
| 209 | + FocusColour = Color4.RoyalBlue, |
| 210 | + Current = spacing, |
| 211 | + }, |
| 212 | + new SpriteText |
| 213 | + { |
| 214 | + Text = "Spacing", |
| 215 | + Anchor = Anchor.Centre, |
| 216 | + Origin = Anchor.Centre, |
| 217 | + Font = new FontUsage(size: 16) |
| 218 | + }, |
| 219 | + } |
| 220 | + }, |
| 221 | + } |
| 222 | + }; |
| 223 | + } |
| 224 | + |
| 225 | + protected override void LoadComplete() |
| 226 | + { |
| 227 | + base.LoadComplete(); |
| 228 | + |
| 229 | + size.BindValueChanged(e => ResizeAction.Invoke(e.NewValue), true); |
| 230 | + spacing.BindValueChanged(e => SpacingAction.Invoke(e.NewValue), true); |
| 231 | + |
| 232 | + if (VisibilityAction != null) |
| 233 | + visible.BindValueChanged(e => VisibilityAction?.Invoke(e.NewValue), true); |
| 234 | + else |
| 235 | + visibilityGroup.Hide(); |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments