Files
osu-framework/osu.Framework.Tests/Visual/TestCaseCountingText.cs
2019-01-24 17:59:35 +09:00

99 lines
3.5 KiB
C#

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Globalization;
using osuTK;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Testing;
namespace osu.Framework.Tests.Visual
{
public class TestCaseCountingText : TestCase
{
private readonly Bindable<CountType> countType = new Bindable<CountType>();
public TestCaseCountingText()
{
Counter counter;
BasicDropdown<CountType> typeDropdown;
Children = new Drawable[]
{
typeDropdown = new BasicDropdown<CountType>
{
Position = new Vector2(10),
Width = 150,
},
counter = new TestTextCounter(createResult)
{
Position = new Vector2(180)
}
};
typeDropdown.Items = (CountType[])Enum.GetValues(typeof(CountType));
countType.BindTo(typeDropdown.Current);
countType.ValueChanged += v => beginStep(lastStep)();
AddStep("1 -> 4 | 1 sec", beginStep(() => counter.CountTo(1).CountTo(4, 1000)));
AddStep("1 -> 4 | 3 sec", beginStep(() => counter.CountTo(1).CountTo(4, 3000)));
AddStep("4 -> 1 | 1 sec", beginStep(() => counter.CountTo(4).CountTo(1, 1000)));
AddStep("4 -> 1 | 3 sec", beginStep(() => counter.CountTo(4).CountTo(1, 3000)));
AddStep("1 -> 4 -> 1 | 6 sec", beginStep(() => counter.CountTo(1).CountTo(4, 3000).Then().CountTo(1, 3000)));
AddStep("1 -> 4 -> 1 | 2 sec", beginStep(() => counter.CountTo(1).CountTo(4, 1000).Then().CountTo(1, 1000)));
AddStep("1 -> 100 | 5 sec | OutQuint", beginStep(() => counter.CountTo(1).CountTo(100, 5000, Easing.OutQuint)));
}
private Action lastStep;
private Action beginStep(Action stepAction) => () =>
{
lastStep = stepAction;
stepAction?.Invoke();
};
private string createResult(double value)
{
switch (countType.Value)
{
default:
case CountType.AsDouble:
return value.ToString(CultureInfo.InvariantCulture);
case CountType.AsInteger:
return ((int)value).ToString();
case CountType.AsIntegerCeiling:
return ((int)Math.Ceiling(value)).ToString();
case CountType.AsDouble2:
return Math.Round(value, 2).ToString(CultureInfo.InvariantCulture);
case CountType.AsDouble4:
return Math.Round(value, 4).ToString(CultureInfo.InvariantCulture);
}
}
private enum CountType
{
AsInteger,
AsIntegerCeiling,
AsDouble,
AsDouble2,
AsDouble4,
}
}
public class TestTextCounter : Counter
{
private readonly Func<double, string> resultFunction;
private readonly SpriteText text;
public TestTextCounter(Func<double, string> resultFunction)
{
this.resultFunction = resultFunction;
AddInternal(text = new SpriteText { TextSize = 24 });
}
protected override void OnCountChanged(double count) => text.Text = resultFunction(count);
}
}