From 4c729ca5ed9cea0dd659badffd6b9bc0264175e9 Mon Sep 17 00:00:00 2001 From: Scott_NZ Date: Wed, 28 Nov 2012 01:52:48 +1300 Subject: [PATCH] Refactored GraphWidget to LineGraphWidget --- OpenRA.Game/OpenRA.Game.csproj | 4 +- .../{GraphWidget.cs => LineGraphWidget.cs} | 62 ++++++++++++------- .../Widgets/Logic/ObserverStatsLogic.cs | 9 ++- mods/ra/chrome/ingame.yaml | 4 +- 4 files changed, 49 insertions(+), 30 deletions(-) rename OpenRA.Game/Widgets/{GraphWidget.cs => LineGraphWidget.cs} (79%) diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index c969802aae..edf239d036 100755 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -192,10 +192,10 @@ - - + + diff --git a/OpenRA.Game/Widgets/GraphWidget.cs b/OpenRA.Game/Widgets/LineGraphWidget.cs similarity index 79% rename from OpenRA.Game/Widgets/GraphWidget.cs rename to OpenRA.Game/Widgets/LineGraphWidget.cs index 6eddb55c90..8a6046409d 100644 --- a/OpenRA.Game/Widgets/GraphWidget.cs +++ b/OpenRA.Game/Widgets/LineGraphWidget.cs @@ -16,9 +16,9 @@ using OpenRA.FileFormats; namespace OpenRA.Widgets { - public class GraphWidget : Widget + public class LineGraphWidget : Widget { - public Func>>> GetDataSource; + public Func> GetSeries; public Func GetValueFormat; public Func GetXAxisValueFormat; public Func GetYAxisValueFormat; @@ -40,7 +40,7 @@ namespace OpenRA.Widgets public string LabelFont; public string AxisFont; - public GraphWidget() + public LineGraphWidget() : base() { GetValueFormat = () => ValueFormat; @@ -55,10 +55,10 @@ namespace OpenRA.Widgets GetAxisFont = () => AxisFont; } - protected GraphWidget(GraphWidget other) + protected LineGraphWidget(LineGraphWidget other) : base(other) { - GetDataSource = other.GetDataSource; + GetSeries = other.GetSeries; GetValueFormat = other.GetValueFormat; GetXAxisValueFormat = other.GetXAxisValueFormat; GetYAxisValueFormat = other.GetYAxisValueFormat; @@ -83,7 +83,7 @@ namespace OpenRA.Widgets public override void Draw() { - if (GetDataSource == null || !GetDataSource().Any() + if (GetSeries == null || !GetSeries().Any() || GetLabelFont == null || GetLabelFont() == null || GetAxisFont == null || GetAxisFont() == null) { @@ -109,22 +109,22 @@ namespace OpenRA.Widgets var xStep = width / xAxisSize; var yStep = height / yAxisSize; - var actualNodeCount = GetDataSource().First().Second.Count(); + var actualNodeCount = GetSeries().First().Points.Count(); var visibleNodeStart = Math.Max(0, actualNodeCount - xAxisSize); var visibleNodeEnd = Math.Max(actualNodeCount, xAxisSize); - float maxValue = GetDataSource().Select(p => p.Second).SelectMany(d => d).Max(); - var scale = (100 / maxValue) * 3; + var maxValue = GetSeries().Select(p => p.Points).SelectMany(d => d).Max(); + var scale = 100 / maxValue * 3; //todo: make this stuff not draw outside of the RenderBounds for (int n = visibleNodeStart, x = 0; n <= visibleNodeEnd; n++, x += xStep) { Game.Renderer.LineRenderer.DrawLine(origin + new float2(x, 0), origin + new float2(x, -5), Color.White, Color.White); tiny.DrawText(GetXAxisValueFormat().F(n), origin + new float2(x, 2), Color.White); - } + } bold.DrawText(GetXAxisLabel(), origin + new float2(width / 2, 20), Color.White); - for (var y = (GetDisplayFirstYAxisValue() ? 0f : yStep); y <= height; y += yStep) + for (var y = (GetDisplayFirstYAxisValue() ? 0 : yStep); y <= height; y += yStep) { var value = y / scale; Game.Renderer.LineRenderer.DrawLine(origin + new float2(width - 5, -y), origin + new float2(width, -y), Color.White, Color.White); @@ -132,17 +132,17 @@ namespace OpenRA.Widgets } bold.DrawText(GetYAxisLabel(), origin + new float2(width + 40, -(height / 2)), Color.White); - var playerNameOffset = 0; - foreach (var playerDataPair in GetDataSource()) + var keyOffset = 0; + foreach (var series in GetSeries()) { - var player = playerDataPair.First; - var data = playerDataPair.Second; - var color = player.ColorRamp.GetColor(0); - if (data.Any()) + var key = series.Key; + var color = series.Color; + var points = series.Points; + if (points.Any()) { - data = data.Reverse().Take(xAxisSize).Reverse(); - var scaledData = data.Select(d => d * scale); - var x = 0f; + points = points.Reverse().Take(xAxisSize).Reverse(); + var scaledData = points.Select(d => d * scale); + var x = 0; scaledData.Aggregate((a, b) => { Game.Renderer.LineRenderer.DrawLine( @@ -153,7 +153,7 @@ namespace OpenRA.Widgets return b; }); - var value = data.Last(); + var value = points.Last(); if (value != 0) { var scaledValue = value * scale; @@ -161,14 +161,28 @@ namespace OpenRA.Widgets } } - tiny.DrawText(player.PlayerName, new float2(rect.Left, rect.Top) + new float2(5, 10 * playerNameOffset + 3), color); - playerNameOffset++; + tiny.DrawText(key, new float2(rect.Left, rect.Top) + new float2(5, 10 * keyOffset + 3), color); + keyOffset++; } } public override Widget Clone() { - return new GraphWidget(this); + return new LineGraphWidget(this); + } + } + + public class LineGraphSeries + { + public string Key; + public Color Color; + public IEnumerable Points; + + public LineGraphSeries(string key, Color color, IEnumerable points) + { + Key = key; + Color = color; + Points = points; } } } diff --git a/OpenRA.Mods.RA/Widgets/Logic/ObserverStatsLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/ObserverStatsLogic.cs index 03fb7fb9c4..c26a47d968 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/ObserverStatsLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/ObserverStatsLogic.cs @@ -151,8 +151,13 @@ namespace OpenRA.Mods.RA.Widgets.Logic earnedThisMinuteGraphHeaders.Visible = true; var template = earnedThisMinuteGraphTemplate.Clone(); - var graph = template.Get("EARNED_THIS_MIN_GRAPH"); - graph.GetDataSource = () => players.Select(p => Pair.New(p, p.PlayerActor.Trait().EarnedSamples.Select(s => (float)s))); + var graph = template.Get("EARNED_THIS_MIN_GRAPH"); + graph.GetSeries = () => + players.Select(p => new LineGraphSeries( + p.PlayerName, + p.ColorRamp.GetColor(0), + p.PlayerActor.Trait().EarnedSamples.Select(s => (float)s) + )); playerStatsPanel.AddChild(template); } diff --git a/mods/ra/chrome/ingame.yaml b/mods/ra/chrome/ingame.yaml index 7446384b43..28e54ded51 100644 --- a/mods/ra/chrome/ingame.yaml +++ b/mods/ra/chrome/ingame.yaml @@ -865,7 +865,7 @@ Container@OBSERVER_ROOT: Width:PARENT_RIGHT-100 Height:PARENT_BOTTOM-50 Children: - Graph@EARNED_THIS_MIN_GRAPH: + LineGraph@EARNED_THIS_MIN_GRAPH: X:0 Y:0 Width:PARENT_RIGHT @@ -877,7 +877,7 @@ Container@OBSERVER_ROOT: YAxisSize:10 XAxisLabel:m YAxisLabel:$ - DisplayYAxisZero:false + DisplayFirstYAxisValue:false LabelFont:TinyBold AxisFont:Bold Background@FMVPLAYER: