A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using NUnit.Framework;
5using osu.Framework.Graphics;
6using osu.Framework.Graphics.Containers;
7using osu.Framework.Graphics.Shapes;
8using osuTK;
9using osuTK.Graphics;
10
11namespace osu.Framework.Tests.Visual.Containers
12{
13 public class TestSceneCompositeDrawable : FrameworkTestScene
14 {
15 [Test]
16 public void TestSortWithComparerChange()
17 {
18 SortableComposite composite = null;
19 Drawable firstItem = null;
20 Drawable lastItem = null;
21
22 AddStep("add composite", () => Child = composite = new SortableComposite());
23
24 AddStep("reverse comparer", () =>
25 {
26 firstItem = composite.InternalChildren[0];
27 lastItem = composite.InternalChildren[^1];
28
29 for (int i = 0; i < composite.InternalChildren.Count; i++)
30 ((SortableBox)composite.InternalChildren[i]).Id = composite.InternalChildren.Count - 1 - i;
31 composite.Sort();
32 });
33
34 AddAssert("children reversed", () => composite.InternalChildren[0] == lastItem && composite.InternalChildren[^1] == firstItem);
35 }
36
37 private class SortableComposite : CompositeDrawable
38 {
39 public SortableComposite()
40 {
41 Anchor = Anchor.Centre;
42 Origin = Anchor.Centre;
43 AutoSizeAxes = Axes.Both;
44
45 for (int i = 0; i < 128; i++)
46 {
47 AddInternal(new SortableBox
48 {
49 Id = i,
50 Colour = new Color4(i / 255f, i / 255f, i / 255f, 1.0f),
51 Position = new Vector2(3 * i),
52 Size = new Vector2(50)
53 });
54 }
55 }
56
57 public void Sort() => SortInternal();
58
59 protected override int Compare(Drawable x, Drawable y)
60 => ((SortableBox)x).Id.CompareTo(((SortableBox)y).Id);
61 }
62
63 private class SortableBox : Box
64 {
65 public int Id;
66 }
67 }
68}