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 System;
5using osu.Framework.Graphics;
6using osu.Framework.Graphics.Containers;
7using osu.Framework.Graphics.Shapes;
8using osu.Framework.Utils;
9using osuTK;
10
11namespace osu.Framework.Tests.Visual.Layout
12{
13 [System.ComponentModel.Description("Rewinding of transforms that are important to layout.")]
14 public class TestSceneLayoutTransformRewinding : FrameworkTestScene
15 {
16 private readonly ManualUpdateSubTreeContainer manualContainer;
17
18 public TestSceneLayoutTransformRewinding()
19 {
20 Child = manualContainer = new ManualUpdateSubTreeContainer();
21
22 testAutoSizeInstant();
23 testFlowInstant();
24 }
25
26 private void testAutoSizeInstant()
27 {
28 AddStep("Initialize autosize test", () =>
29 {
30 manualContainer.Child = new Container
31 {
32 AutoSizeAxes = Axes.Both,
33 Masking = true,
34 Child = new Box { Size = new Vector2(150) }
35 };
36 });
37
38 AddStep("Run to end", () => manualContainer.PerformUpdate(null));
39 AddAssert("Size = 150", () => Precision.AlmostEquals(new Vector2(150), manualContainer.Child.Size));
40
41 AddStep("Rewind", () => manualContainer.PerformUpdate(() => manualContainer.ApplyTransformsAt(-1, true)));
42 AddAssert("Size = 150", () => Precision.AlmostEquals(new Vector2(150), manualContainer.Child.Size));
43 }
44
45 private void testFlowInstant()
46 {
47 Box box2 = null;
48
49 AddStep("Initialize flow test", () =>
50 {
51 manualContainer.Child = new FillFlowContainer
52 {
53 AutoSizeAxes = Axes.Both,
54 Children = new[]
55 {
56 new Box { Size = new Vector2(150) },
57 box2 = new Box { Size = new Vector2(150) }
58 }
59 };
60 });
61
62 AddStep("Run to end", () => manualContainer.PerformUpdate(null));
63 AddAssert("Box2 @ (150, 0)", () => Precision.AlmostEquals(new Vector2(150, 0), box2.Position));
64
65 AddStep("Rewind", () => manualContainer.PerformUpdate(() => manualContainer.ApplyTransformsAt(-1, true)));
66 AddAssert("Box2 @ (150, 0)", () => Precision.AlmostEquals(new Vector2(150, 0), box2.Position));
67 }
68
69 private class ManualUpdateSubTreeContainer : Container
70 {
71 public override bool RemoveCompletedTransforms => false;
72
73 private Action onUpdateAfterChildren;
74
75 public ManualUpdateSubTreeContainer()
76 {
77 RelativeSizeAxes = Axes.Both;
78 }
79
80 public void PerformUpdate(Action afterChildren)
81 {
82 onUpdateAfterChildren = afterChildren;
83 base.UpdateSubTree();
84 onUpdateAfterChildren = null;
85 }
86
87 public override bool UpdateSubTree() => false;
88
89 protected override void UpdateAfterChildren()
90 {
91 base.UpdateAfterChildren();
92 onUpdateAfterChildren?.Invoke();
93 }
94 }
95 }
96}