From 24dc1a14ae91b60bec068aee73b4a3fd55c10f9d Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Fri, 26 Jun 2026 14:22:40 +0900 Subject: [PATCH] Refactor test scene --- .../Layout/TestSceneFillFlowContainer.cs | 410 ++++++++---------- 1 file changed, 181 insertions(+), 229 deletions(-) diff --git a/osu.Framework.Tests/Visual/Layout/TestSceneFillFlowContainer.cs b/osu.Framework.Tests/Visual/Layout/TestSceneFillFlowContainer.cs index a536412ead..22914154ce 100644 --- a/osu.Framework.Tests/Visual/Layout/TestSceneFillFlowContainer.cs +++ b/osu.Framework.Tests/Visual/Layout/TestSceneFillFlowContainer.cs @@ -1,11 +1,9 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -#nullable disable - using System; -using System.Linq; -using System.Reflection; +using NUnit.Framework; +using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Shapes; @@ -13,7 +11,6 @@ using osu.Framework.Graphics.UserInterface; using osu.Framework.Localisation; using osu.Framework.Utils; -using osu.Framework.Threading; using osuTK; using osuTK.Graphics; @@ -21,168 +18,150 @@ namespace osu.Framework.Tests.Visual.Layout { public partial class TestSceneFillFlowContainer : FrameworkTestScene { - private FillDirectionDropdown selectionDropdown; - - private Anchor childAnchor = Anchor.TopLeft; - private AnchorDropdown anchorDropdown; - - private Anchor childOrigin = Anchor.TopLeft; - private AnchorDropdown originDropdown; - - private FillFlowContainer fillContainer; - private ScheduledDelegate scheduledAdder; - private bool doNotAddChildren; - - public TestSceneFillFlowContainer() + [Test] + public void TestDemo() { - reset(); - } - - private void reset() - { - doNotAddChildren = false; - scheduledAdder?.Cancel(); + FillFlowContainer fillContainer = null!; + AnchorDropdown originDropdown = null!; + AutomatedBoxSpawner spawner = null!; - Child = new Container + AddStep("Setup", () => { - RelativeSizeAxes = Axes.Both, - Width = 0.2f, - Anchor = Anchor.TopRight, - Origin = Anchor.TopRight, - Depth = float.MinValue, - Children = new[] + FillDirectionDropdown selectionDropdown; + AnchorDropdown anchorDropdown; + + Children = new Drawable[] { - new FillFlowContainer + new Container { - Direction = FillDirection.Vertical, - RelativeSizeAxes = Axes.X, - AutoSizeAxes = Axes.Y, - Children = new Drawable[] + RelativeSizeAxes = Axes.Both, + Width = 0.2f, + Anchor = Anchor.TopRight, + Origin = Anchor.TopRight, + Depth = float.MinValue, + Children = new[] { - new SpriteText { Text = @"Fill mode" }, - selectionDropdown = new FillDirectionDropdown + new FillFlowContainer { + Direction = FillDirection.Vertical, RelativeSizeAxes = Axes.X, - Items = (FlowTestType[])Enum.GetValues(typeof(FlowTestType)), + AutoSizeAxes = Axes.Y, + Children = new Drawable[] + { + new SpriteText { Text = "Fill direction" }, + selectionDropdown = new FillDirectionDropdown + { + RelativeSizeAxes = Axes.X, + Items = Enum.GetValues(), + Current = { Value = FillDirection.Full } + }, + new SpriteText { Text = "Child anchor" }, + anchorDropdown = new AnchorDropdown + { + RelativeSizeAxes = Axes.X, + Items = new[] + { + Anchor.TopLeft, + Anchor.TopCentre, + Anchor.TopRight, + Anchor.CentreLeft, + Anchor.Centre, + Anchor.CentreRight, + Anchor.BottomLeft, + Anchor.BottomCentre, + Anchor.BottomRight, + }, + }, + new SpriteText { Text = "Child origin" }, + originDropdown = new AnchorDropdown + { + RelativeSizeAxes = Axes.X, + Items = new[] + { + Anchor.TopLeft, + Anchor.TopCentre, + Anchor.TopRight, + Anchor.CentreLeft, + Anchor.Centre, + Anchor.CentreRight, + Anchor.BottomLeft, + Anchor.BottomCentre, + Anchor.BottomRight, + }, + }, + } + } + } + }, + new Container + { + Padding = new MarginPadding(25f), + RelativeSizeAxes = Axes.Both, + Children = new Drawable[] + { + fillContainer = new FillFlowContainer + { + RelativeSizeAxes = Axes.Both, + AutoSizeAxes = Axes.None, + Spacing = new Vector2(5) }, - new SpriteText { Text = @"Child anchor" }, - anchorDropdown = new AnchorDropdown + new Box { - RelativeSizeAxes = Axes.X, - Items = new[] - { - Anchor.TopLeft, - Anchor.TopCentre, - Anchor.TopRight, - Anchor.CentreLeft, - Anchor.Centre, - Anchor.CentreRight, - Anchor.BottomLeft, - Anchor.BottomCentre, - Anchor.BottomRight, - }, + Anchor = Anchor.CentreLeft, + Origin = Anchor.Centre, + RelativeSizeAxes = Axes.Y, + Size = new Vector2(3, 1), + Colour = Color4.HotPink, + }, + new Box + { + Anchor = Anchor.CentreRight, + Origin = Anchor.Centre, + RelativeSizeAxes = Axes.Y, + Size = new Vector2(3, 1), + Colour = Color4.HotPink, }, - new SpriteText { Text = @"Child origin" }, - originDropdown = new AnchorDropdown + new Box { + Anchor = Anchor.TopCentre, + Origin = Anchor.Centre, RelativeSizeAxes = Axes.X, - Items = new[] - { - Anchor.TopLeft, - Anchor.TopCentre, - Anchor.TopRight, - Anchor.CentreLeft, - Anchor.Centre, - Anchor.CentreRight, - Anchor.BottomLeft, - Anchor.BottomCentre, - Anchor.BottomRight, - }, + Size = new Vector2(1, 3), + Colour = Color4.HotPink, }, + new Box + { + Anchor = Anchor.BottomCentre, + Origin = Anchor.Centre, + RelativeSizeAxes = Axes.X, + Size = new Vector2(1, 3), + Colour = Color4.HotPink, + } } + }, + spawner = new AutomatedBoxSpawner(fillContainer) + { + ChildAnchor = { BindTarget = anchorDropdown.Current }, + ChildOrigin = { BindTarget = originDropdown.Current } } - } - }; - - selectionDropdown.Current.ValueChanged += e => changeTest(e.NewValue); - buildTest(); - selectionDropdown.Current.Value = FlowTestType.Full; - changeTest(FlowTestType.Full); - } - - protected override void Update() - { - base.Update(); - - if (childAnchor != anchorDropdown.Current.Value) - { - childAnchor = anchorDropdown.Current.Value; - foreach (var child in fillContainer.Children) - child.Anchor = childAnchor; - } + }; - if (childOrigin != originDropdown.Current.Value) - { - childOrigin = originDropdown.Current.Value; - foreach (var child in fillContainer.Children) - child.Origin = childOrigin; - } - } + anchorDropdown.Current.BindValueChanged(v => + { + foreach (var child in fillContainer.Children) + child.Anchor = v.NewValue; + }); - private void changeTest(FlowTestType testType) - { - var method = - GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).SingleOrDefault(m => m.GetCustomAttribute()?.TestType == testType); - if (method != null) - method.Invoke(this, Array.Empty()); - } + originDropdown.Current.BindValueChanged(v => + { + foreach (var child in fillContainer.Children) + child.Origin = v.NewValue; + }); - private void buildTest() - { - Add(new Container - { - Padding = new MarginPadding(25f), - RelativeSizeAxes = Axes.Both, - Children = new Drawable[] + selectionDropdown.Current.BindValueChanged(v => { - fillContainer = new FillFlowContainer - { - RelativeSizeAxes = Axes.Both, - AutoSizeAxes = Axes.None, - }, - new Box - { - Anchor = Anchor.CentreLeft, - Origin = Anchor.Centre, - RelativeSizeAxes = Axes.Y, - Size = new Vector2(3, 1), - Colour = Color4.HotPink, - }, - new Box - { - Anchor = Anchor.CentreRight, - Origin = Anchor.Centre, - RelativeSizeAxes = Axes.Y, - Size = new Vector2(3, 1), - Colour = Color4.HotPink, - }, - new Box - { - Anchor = Anchor.TopCentre, - Origin = Anchor.Centre, - RelativeSizeAxes = Axes.X, - Size = new Vector2(1, 3), - Colour = Color4.HotPink, - }, - new Box - { - Anchor = Anchor.BottomCentre, - Origin = Anchor.Centre, - RelativeSizeAxes = Axes.X, - Size = new Vector2(1, 3), - Colour = Color4.HotPink, - } - } + fillContainer.Direction = v.NewValue; + }); }); AddToggleStep("Rotate Container", state => { fillContainer.RotateTo(state ? 45f : 0, 1000); }); @@ -205,6 +184,7 @@ private void buildTest() fillContainer.Height = 1; } }); + AddToggleStep("Rotate children", state => { if (state) @@ -218,6 +198,7 @@ private void buildTest() child.RotateTo(0f, 1000); } }); + AddToggleStep("Shear children", state => { if (state) @@ -231,6 +212,7 @@ private void buildTest() child.Shear = Vector2.Zero; } }); + AddToggleStep("Scale children", state => { if (state) @@ -244,6 +226,7 @@ private void buildTest() child.ScaleTo(1f, 1000); } }); + AddToggleStep("Randomly scale children", state => { if (state) @@ -257,6 +240,7 @@ private void buildTest() child.ScaleTo(1f, 1000); } }); + AddToggleStep("Randomly set child origins", state => { if (state) @@ -310,71 +294,7 @@ private void buildTest() } }); - AddToggleStep("Stop adding children", state => { doNotAddChildren = state; }); - - scheduledAdder?.Cancel(); - scheduledAdder = Scheduler.AddDelayed( - () => - { - if (fillContainer.Parent == null) - scheduledAdder.Cancel(); - - if (doNotAddChildren) - { - fillContainer.Invalidate(); - } - - if (fillContainer.Children.Count < 1000 && !doNotAddChildren) - { - fillContainer.Add(new Container - { - Anchor = childAnchor, - Origin = childOrigin, - AutoSizeAxes = Axes.Both, - Children = new Drawable[] - { - new Box - { - Width = 50, - Height = 50, - Colour = Color4.White - }, - new SpriteText - { - Colour = Color4.Black, - RelativePositionAxes = Axes.Both, - Position = new Vector2(0.5f, 0.5f), - Origin = Anchor.Centre, - Text = fillContainer.Children.Count.ToString() - } - } - }); - } - }, - 100, - true - ); - } - - [FlowTestCase(FlowTestType.Full)] - private void test1() - { - fillContainer.Direction = FillDirection.Full; - fillContainer.Spacing = new Vector2(5, 5); - } - - [FlowTestCase(FlowTestType.Horizontal)] - private void test2() - { - fillContainer.Direction = FillDirection.Horizontal; - fillContainer.Spacing = new Vector2(5, 5); - } - - [FlowTestCase(FlowTestType.Vertical)] - private void test3() - { - fillContainer.Direction = FillDirection.Vertical; - fillContainer.Spacing = new Vector2(5, 5); + AddToggleStep("Stop adding children", v => spawner.AddChildren = !v); } private partial class TestSceneDropdownHeader : DropdownHeader @@ -410,32 +330,64 @@ private partial class BasicDropdownSearchBar : DropdownSearchBar } } - private partial class AnchorDropdown : BasicDropdown + private partial class AutomatedBoxSpawner : Component { - protected override DropdownHeader CreateHeader() => new TestSceneDropdownHeader(); - } + public bool AddChildren { get; set; } = true; - private partial class FillDirectionDropdown : BasicDropdown - { - protected override DropdownHeader CreateHeader() => new TestSceneDropdownHeader(); - } + public readonly IBindable ChildAnchor = new Bindable(); + public readonly IBindable ChildOrigin = new Bindable(); - [AttributeUsage(AttributeTargets.Method)] - private class FlowTestCaseAttribute : Attribute - { - public FlowTestType TestType { get; } + private readonly FillFlowContainer target; - public FlowTestCaseAttribute(FlowTestType testType) + public AutomatedBoxSpawner(FillFlowContainer target) { - TestType = testType; + this.target = target; + } + + protected override void LoadComplete() + { + base.LoadComplete(); + + Scheduler.AddDelayed(() => + { + if (AddChildren) + { + target.Add(new Container + { + Anchor = ChildAnchor.Value, + Origin = ChildOrigin.Value, + AutoSizeAxes = Axes.Both, + Children = new Drawable[] + { + new Box + { + Width = 50, + Height = 50, + Colour = Color4.White + }, + new SpriteText + { + Colour = Color4.Black, + RelativePositionAxes = Axes.Both, + Position = new Vector2(0.5f, 0.5f), + Origin = Anchor.Centre, + Text = target.Children.Count.ToString() + } + } + }); + } + }, 100, true); } } - private enum FlowTestType + private partial class AnchorDropdown : BasicDropdown { - Full, - Horizontal, - Vertical, + protected override DropdownHeader CreateHeader() => new TestSceneDropdownHeader(); + } + + private partial class FillDirectionDropdown : BasicDropdown + { + protected override DropdownHeader CreateHeader() => new TestSceneDropdownHeader(); } } }