// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using OpenTK; 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 = new Bindable(); public TestCaseCountingText() { Counter counter; BasicDropdown typeDropdown; Children = new Drawable[] { typeDropdown = new BasicDropdown { Position = new Vector2(10), Width = 150, }, counter = new TestTextCounter(createResult) { Position = new Vector2(180) } }; typeDropdown.Items = Enum.GetNames(typeof(CountType)).Select(n => new KeyValuePair(n, (CountType)Enum.Parse(typeof(CountType), n))); 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 resultFunction; private readonly SpriteText text; public TestTextCounter(Func resultFunction) { this.resultFunction = resultFunction; AddInternal(text = new SpriteText { TextSize = 24 }); } protected override void OnCountChanged(double count) => text.Text = resultFunction(count); } }