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 osu.Framework.Graphics;
5using osu.Framework.Graphics.Containers;
6using osu.Framework.Graphics.Effects;
7using osu.Framework.Graphics.Shapes;
8using osu.Framework.Graphics.Sprites;
9using osu.Framework.Graphics.Transforms;
10using osu.Framework.Testing;
11using osuTK;
12using osuTK.Graphics;
13
14namespace osu.Framework.Tests.Visual.Drawables
15{
16 public class TestSceneTransformSequence : GridTestScene
17 {
18 private readonly Container[] boxes;
19
20 public TestSceneTransformSequence()
21 : base(4, 3)
22 {
23 boxes = new Container[Rows * Cols];
24 }
25
26 protected override void LoadComplete()
27 {
28 base.LoadComplete();
29
30 testFinish();
31 testClear();
32 }
33
34 private void testFinish()
35 {
36 AddStep("Animate", delegate
37 {
38 setup();
39 animate();
40 });
41
42 AddStep($"{nameof(FinishTransforms)}", delegate
43 {
44 foreach (var box in boxes)
45 box.FinishTransforms();
46 });
47
48 AddAssert("finalize triggered", () => finalizeTriggered);
49 }
50
51 private void testClear()
52 {
53 AddStep("Animate", delegate
54 {
55 setup();
56 animate();
57 });
58
59 AddStep($"{nameof(ClearTransforms)}", delegate
60 {
61 foreach (var box in boxes)
62 box.ClearTransforms();
63 });
64
65 AddAssert("finalize triggered", () => finalizeTriggered);
66 }
67
68 private void setup()
69 {
70 finalizeTriggered = false;
71
72 string[] labels =
73 {
74 "Spin after 2 seconds",
75 "Spin immediately",
76 "Spin 2 seconds in the past",
77 "Complex rotation with preemption",
78 "Loop(1 sec pause; 1 sec rotate)",
79 "Complex transform 1 (should end in sync with CT2)",
80 "Complex transform 2 (should end in sync with CT1)",
81 $"Red on {nameof(TransformSequence<Container>)}.{nameof(TransformSequence<Container>.OnAbort)}",
82 $"Red on {nameof(TransformSequence<Container>)}.{nameof(TransformSequence<Container>.Finally)}",
83 "Red after instant transform",
84 "Red after instant transform 1 sec in the past",
85 "Red after 1 sec transform 1 sec in the past",
86 };
87
88 for (int i = 0; i < Rows * Cols; ++i)
89 {
90 Cell(i).Children = new Drawable[]
91 {
92 new SpriteText
93 {
94 Text = labels[i],
95 Font = new FontUsage(size: 20),
96 },
97 boxes[i] = new Container
98 {
99 RelativeSizeAxes = Axes.Both,
100 Size = new Vector2(0.25f),
101 Anchor = Anchor.Centre,
102 Origin = Anchor.Centre,
103 Masking = true,
104 EdgeEffect = new EdgeEffectParameters
105 {
106 Type = EdgeEffectType.Glow,
107 Radius = 20,
108 Colour = Color4.Blue,
109 },
110 Child = new Box
111 {
112 RelativeSizeAxes = Axes.Both,
113 }
114 }
115 };
116 }
117 }
118
119 private bool finalizeTriggered;
120
121 private void animate()
122 {
123 boxes[0].Delay(500).Then(500).Then(500).Then(
124 b => b.Delay(500).Spin(1000, RotationDirection.Counterclockwise)
125 );
126
127 boxes[1].Spin(1000, RotationDirection.Counterclockwise);
128
129 boxes[2].Delay(-2000).Spin(1000, RotationDirection.Counterclockwise);
130
131 boxes[3].RotateTo(90)
132 .Then().Delay(1000).RotateTo(0)
133 .Then().RotateTo(180, 1000).Loop();
134
135 boxes[4].Delay(1000).Loop(1000, 10, b => b.RotateTo(0).RotateTo(340, 1000));
136
137 boxes[5].RotateTo(0).ScaleTo(1).RotateTo(360, 1000)
138 .Then(1000,
139 b => b.RotateTo(0, 1000),
140 b => b.ScaleTo(2, 500)
141 )
142 .Then().RotateTo(360, 1000).ScaleTo(0.5f, 1000)
143 .Then().FadeEdgeEffectTo(Color4.Red, 1000).ScaleTo(2, 500);
144
145 boxes[6].RotateTo(0).ScaleTo(1).RotateTo(360, 500)
146 .Then(1000,
147 b => b.RotateTo(0),
148 b => b.ScaleTo(2)
149 )
150 .Then(
151 b => b.Loop(500, 2, d => d.RotateTo(0).RotateTo(360, 1000)).Delay(500).ScaleTo(0.5f, 500)
152 )
153 .Then().FadeEdgeEffectTo(Color4.Red, 1000).ScaleTo(2, 500)
154 .Finally(_ => finalizeTriggered = true);
155
156 boxes[7].RotateTo(0).ScaleTo(1).RotateTo(360, 500)
157 .Then(1000,
158 b => b.RotateTo(0),
159 b => b.ScaleTo(2)
160 )
161 .Then(
162 b => b.Loop(500, 2, d => d.RotateTo(0).RotateTo(360, 1000)),
163 b => b.ScaleTo(0.5f, 500)
164 )
165 .OnAbort(b => b.FadeEdgeEffectTo(Color4.Red, 1000));
166
167 boxes[8].RotateTo(0).ScaleTo(1).RotateTo(360, 500)
168 .Then(1000,
169 b => b.RotateTo(0),
170 b => b.ScaleTo(2)
171 )
172 .Then(
173 b => b.Loop(500, 2, d => d.RotateTo(0).RotateTo(360, 1000)),
174 b => b.ScaleTo(0.5f, 500)
175 )
176 .Finally(b => b.FadeEdgeEffectTo(Color4.Red, 1000));
177
178 boxes[9].RotateTo(200)
179 .Finally(b => b.FadeEdgeEffectTo(Color4.Red, 1000));
180
181 boxes[10].Delay(-1000).RotateTo(200)
182 .Finally(b => b.FadeEdgeEffectTo(Color4.Red, 1000));
183
184 boxes[11].Delay(-1000).RotateTo(200, 1000)
185 .Finally(b => b.FadeEdgeEffectTo(Color4.Red, 1000));
186 }
187 }
188}