using Amphora;
using Glass.Text;
using System;
using System.Collections.Generic;
using System.Globalization;
using Tessera.Text;
namespace Glass.Testbed.TestCases.Text
{
///
/// A value sweeping through every digit count, shown as plain text and as counters of each alignment, in two
/// languages, and then as a clock, a duration and a scientific number. The plain line moves the text after it each
/// time the number gains a digit. The counters don't.
///
public class CounterTestCase : TestCase
{
// Slow enough to see a digit arrive, and a full sweep passes every digit count of a five digit counter.
private const double SweepSeconds = 12;
private const int Digits = 5;
private const int MaxValue = 99999;
private const int NoValue = -1;
private static readonly DurationFormat Everything = new DurationFormat
{
Largest = DurationUnit.Hours,
Smallest = DurationUnit.Seconds,
};
private delegate StyledText HoleMaker(int value);
private readonly List<(Label Label, HoleMaker Hole, LocaleBehavior Locale)> rows =
new List<(Label Label, HoleMaker Hole, LocaleBehavior Locale)>();
private double sweepTime;
private int shownValue = NoValue;
private static LocaleBehavior LocaleOf(string name)
{
var returnMe = new LocaleBehavior();
returnMe.SetCulture(CultureInfo.GetCultureInfo(name));
return returnMe;
}
private void AddRow(HoleMaker hole, LocaleBehavior locale)
{
var label = new Label(Widgets.CaptionStyle, "");
ShowValue(label, hole, locale, MaxValue);
label.Bounds = ContentAggregator.NewRow(label.Material, margin: CaptionMargin);
rows.Add((label, hole, locale));
Add(label);
}
private static void ShowValue(Label label, HoleMaker hole, LocaleBehavior locale, int value)
{
label.SetLine($"{hole(value)} after", locale);
}
private static HoleMaker CountOf(CounterAlignment? alignment)
{
if (alignment == null)
{
return value => Styles.Count(value);
}
return value => Styles.Counter(Styles.Count(value), Digits, alignment.Value);
}
// The value is seconds, so the sweep passes every hour and minute digit a time can have.
private static HoleMaker ClockOf(CounterAlignment? alignment)
{
if (alignment == null)
{
return value => Styles.Clock(value * 3.7);
}
return value => Styles.Counter(Styles.Clock(value * 3.7), alignment.Value);
}
private static HoleMaker DurationOf(CounterAlignment? alignment)
{
if (alignment == null)
{
return value => Styles.Duration(value, Everything);
}
return value => Styles.Counter(Styles.Duration(value, Everything), digits: 2, alignment.Value);
}
// The power of ten follows the value, so the exponent gains a digit and the mantissa changes every step.
private static HoleMaker ScientificOf(CounterAlignment? alignment)
{
if (alignment == null)
{
return value => Styles.Scientific(value * Math.Pow(10, -value / 1000), 3);
}
return value => Styles.Counter(Styles.Scientific(value * Math.Pow(10, -value / 1000), 3), digits: 2,
alignment.Value);
}
// The power of ten follows the value, so the sweep passes through the suffixes.
private static HoleMaker ShortOf(CounterAlignment? alignment)
{
if (alignment == null)
{
return value => Styles.ShortExponent(value * Math.Pow(10, value / 4000.0), 3);
}
return value => Styles.Counter(Styles.ShortExponent(value * Math.Pow(10, value / 4000.0), 3),
alignment.Value);
}
private void AddKind(string caption,Func makerOf, LocaleBehavior locale)
{
AddCaption(caption);
AddRow(makerOf(null), locale);
AddRow(makerOf(CounterAlignment.Left), locale);
AddRow(makerOf(CounterAlignment.Right), locale);
AddRow(makerOf(CounterAlignment.Digit), locale);
}
protected override void Populate()
{
AddCaption("The value sweeps from 1 to 99,999. Watch where \"after\" sits.");
LocaleBehavior english = LocaleOf("en-US");
LocaleBehavior french = LocaleOf("fr-FR");
foreach (LocaleBehavior locale in new[] { english, french })
{
string language = locale == english ? "English" : "French";
AddKind(language + " count", CountOf, locale);
}
AddKind("English clock", ClockOf, english);
AddKind("French clock", ClockOf, french);
AddKind("English duration", DurationOf, english);
AddKind("English scientific", ScientificOf, english);
AddKind("English short", ShortOf, english);
AddKind("French short", ShortOf, french);
AddCaption("In each group: plain, then a counter left, right and digit aligned.");
}
protected override void Setup()
{
sweepTime = 0;
shownValue = NoValue;
}
public override void Update(Duration elapsed)
{
sweepTime = (sweepTime + elapsed.TotalSeconds) % SweepSeconds;
// Exponential, so each digit count gets an equal share of the sweep.
int value = (int)Math.Pow(MaxValue, sweepTime / SweepSeconds);
if (value == shownValue)
{
return;
}
shownValue = value;
foreach (var row in rows)
{
ShowValue(row.Label, row.Hole, row.Locale, value);
}
}
}
}