using System.Globalization; using Annulus; using Azimuth; using Glass; namespace Konis.Testbed { /// /// A panel of figures for the selected emitter, or for all of them when none is selected. /// public class StatsPanel { private const double NameWidth = 84; private const double ValueWidth = 168; private const double RowGap = 2; // Overdraw is how many sprites land on a covered pixel, so 1 means no overlap. Smoke and fire sit at 2 to 4. private const double CostlyOverdraw = 4; private const double ExpensiveOverdraw = 8; // The share of the screen an emitter covers. private const double CostlyScreenFraction = 0.25; private const double ExpensiveScreenFraction = 0.5; private readonly TestbedSkin skin; private readonly Label titleLabel; private readonly Label particlesLabel; private readonly Label ageLabel; private readonly Label rateLabel; private readonly Label droppedLabel; private readonly Label updateLabel; private readonly Label spritesLabel; private readonly Label memoryLabel; private readonly Label fillLabel; private readonly Label coveredLabel; private readonly Label overdrawLabel; public StatsPanel(TestbedSkin skin) { this.skin = skin; var content = new Panel { Material = Glass.Material.Invisible, AbsorbsMouse = false }; Scalar rowHeight = skin.Text.SingleLineHeight; var rows = new RectAggregator( Rect.FromPositionAndSize(Vector.Zero, new Vector(NameWidth + ValueWidth, 0)), new Vector(0, RowGap)); Rect titleSlot = rows.NewRow(rowHeight); titleLabel = new Label(skin.Text, "", Constraints.Tight(titleSlot.Size)); titleLabel.Bounds = titleSlot; content.CanvasChildren.Add(titleLabel); particlesLabel = AddRow(skin, content, ref rows, rowHeight, "particles"); ageLabel = AddRow(skin, content, ref rows, rowHeight, "age"); rateLabel = AddRow(skin, content, ref rows, rowHeight, "birth rate"); droppedLabel = AddRow(skin, content, ref rows, rowHeight, "dropped"); updateLabel = AddRow(skin, content, ref rows, rowHeight, "update"); spritesLabel = AddRow(skin, content, ref rows, rowHeight, "sprites"); memoryLabel = AddRow(skin, content, ref rows, rowHeight, "memory"); fillLabel = AddRow(skin, content, ref rows, rowHeight, "fill"); coveredLabel = AddRow(skin, content, ref rows, rowHeight, "covered"); overdrawLabel = AddRow(skin, content, ref rows, rowHeight, "overdraw"); content.Bounds = rows.Rect; Bar = content.WrapIn(skin.Panel); Bar.DebugName = "emitter stats"; } /// /// Gets the panel to add to the world. /// public Panel Bar { get; } private static Label AddRow( TestbedSkin skin, Panel content, ref RectAggregator rows, Scalar rowHeight, string name) { Rect slot = rows.NewRow(rowHeight); Rect nameSlot = slot.NewCol(NameWidth); var nameLabel = new Label(skin.HeadingText, name, Constraints.Tight(nameSlot.Size)); var valueLabel = new Label(skin.ValueText, "", Constraints.Tight(slot.Size)); nameLabel.Bounds = nameSlot; valueLabel.Bounds = slot; content.CanvasChildren.Add(nameLabel); content.CanvasChildren.Add(valueLabel); return valueLabel; } private static string Format(string format, params object[] arguments) { return string.Format(CultureInfo.InvariantCulture, format, arguments); } private static string Bytes(long bytes) { if (bytes < 2048) { return Format("{0} B", bytes); } if (bytes < 2L * 1024 * 1024) { return Format("{0:0.0} KB", bytes / 1024.0); } return Format("{0:0.00} MB", bytes / (1024.0 * 1024.0)); } private static string Age(EmitterStats stats) { if (stats.IsTotal) { return "-"; } if (stats.Lifespan == 0) { return Format("{0:0.0} s (endless)", stats.Age); } return Format("{0:0.0} of {1:0.0} s", stats.Age, stats.Lifespan); } /// /// Returns the style that shows as cheap, costly or expensive, by the two limits. /// public TextMaterialStyle StyleFor(double value, double costly, double expensive) { if (value >= expensive) { return skin.ExpensiveValueText; } if (value >= costly) { return skin.CostlyValueText; } return skin.CheapValueText; } private static void Show(Label label, string text, TextMaterialStyle style) { label.Text = text; // Restyling bakes the text again, so it waits for a change of colour. if (!label.Style.Equals(style)) { label.Style = style; } } /// /// Shows . /// public void Refresh(EmitterStats stats) { double used = stats.Capacity == 0 ? 0 : 100.0 * stats.Particles / stats.Capacity; titleLabel.Text = stats.Title; particlesLabel.Text = Format("{0:N0} of {1:N0} ({2:0}%)", stats.Particles, stats.Capacity, used); ageLabel.Text = Age(stats); rateLabel.Text = Format("{0:0.#} /s", stats.Rate); droppedLabel.Text = Format("{0:N0} births", stats.DroppedBirths); updateLabel.Text = Format("{0:0.000} ms", stats.UpdateMilliseconds); spritesLabel.Text = Format("{0:0.000} ms", stats.WriteMilliseconds); memoryLabel.Text = Bytes(stats.Bytes); fillLabel.Text = Format("{0:N0} px", stats.FillPixels); Show( coveredLabel, Format("{0:N0} px ({1:0.0}%)", stats.CoveredPixels, 100 * stats.ScreenFraction), StyleFor(stats.ScreenFraction, CostlyScreenFraction, ExpensiveScreenFraction)); Show( overdrawLabel, Format("{0:0.00}x", stats.Overdraw), StyleFor(stats.Overdraw, CostlyOverdraw, ExpensiveOverdraw)); } } }