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 System.Globalization;
6using osu.Framework.Bindables;
7using osu.Framework.Graphics;
8using osu.Framework.Graphics.Sprites;
9using osu.Framework.Graphics.UserInterface;
10using osuTK;
11
12namespace osu.Framework.Tests.Visual.UserInterface
13{
14 public class TestSceneCountingText : FrameworkTestScene
15 {
16 private readonly Bindable<CountType> countType = new Bindable<CountType>();
17
18 public TestSceneCountingText()
19 {
20 Counter counter;
21
22 BasicDropdown<CountType> typeDropdown;
23 Children = new Drawable[]
24 {
25 typeDropdown = new BasicDropdown<CountType>
26 {
27 Position = new Vector2(10),
28 Width = 150,
29 },
30 counter = new TestTextCounter(createResult)
31 {
32 Position = new Vector2(180)
33 }
34 };
35
36 typeDropdown.Items = (CountType[])Enum.GetValues(typeof(CountType));
37 countType.BindTo(typeDropdown.Current);
38 countType.ValueChanged += v => beginStep(lastStep)();
39
40 AddStep("1 -> 4 | 1 sec", beginStep(() => counter.CountTo(1).CountTo(4, 1000)));
41 AddStep("1 -> 4 | 3 sec", beginStep(() => counter.CountTo(1).CountTo(4, 3000)));
42 AddStep("4 -> 1 | 1 sec", beginStep(() => counter.CountTo(4).CountTo(1, 1000)));
43 AddStep("4 -> 1 | 3 sec", beginStep(() => counter.CountTo(4).CountTo(1, 3000)));
44 AddStep("1 -> 4 -> 1 | 6 sec", beginStep(() => counter.CountTo(1).CountTo(4, 3000).Then().CountTo(1, 3000)));
45 AddStep("1 -> 4 -> 1 | 2 sec", beginStep(() => counter.CountTo(1).CountTo(4, 1000).Then().CountTo(1, 1000)));
46 AddStep("1 -> 100 | 5 sec | OutQuint", beginStep(() => counter.CountTo(1).CountTo(100, 5000, Easing.OutQuint)));
47 }
48
49 private Action lastStep;
50
51 private Action beginStep(Action stepAction) => () =>
52 {
53 lastStep = stepAction;
54 stepAction?.Invoke();
55 };
56
57 private string createResult(double value)
58 {
59 switch (countType.Value)
60 {
61 default:
62 case CountType.AsDouble:
63 return value.ToString(CultureInfo.InvariantCulture);
64
65 case CountType.AsInteger:
66 return ((int)value).ToString();
67
68 case CountType.AsIntegerCeiling:
69 return ((int)Math.Ceiling(value)).ToString();
70
71 case CountType.AsDouble2:
72 return Math.Round(value, 2).ToString(CultureInfo.InvariantCulture);
73
74 case CountType.AsDouble4:
75 return Math.Round(value, 4).ToString(CultureInfo.InvariantCulture);
76 }
77 }
78
79 private enum CountType
80 {
81 AsInteger,
82 AsIntegerCeiling,
83 AsDouble,
84 AsDouble2,
85 AsDouble4,
86 }
87 }
88
89 public class TestTextCounter : Counter
90 {
91 private readonly Func<double, string> resultFunction;
92 private readonly SpriteText text;
93
94 public TestTextCounter(Func<double, string> resultFunction)
95 {
96 this.resultFunction = resultFunction;
97 AddInternal(text = new SpriteText { Font = new FontUsage(size: 24) });
98 }
99
100 protected override void OnCountChanged(double count) => text.Text = resultFunction(count);
101 }
102}