From d5b37d434334aa3a46a48dc6ef089c0965b28e8a Mon Sep 17 00:00:00 2001 From: Scott_NZ Date: Tue, 27 Nov 2012 19:19:10 +1300 Subject: [PATCH] Cleanup, refactoring of stats graph widget. --- .../Widgets/ObserverStatsGraphWidget.cs | 44 +++++++++++-------- mods/ra/chrome/ingame.yaml | 3 +- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/OpenRA.Mods.RA/Widgets/ObserverStatsGraphWidget.cs b/OpenRA.Mods.RA/Widgets/ObserverStatsGraphWidget.cs index 653fb96fa3..11b16f6966 100644 --- a/OpenRA.Mods.RA/Widgets/ObserverStatsGraphWidget.cs +++ b/OpenRA.Mods.RA/Widgets/ObserverStatsGraphWidget.cs @@ -23,14 +23,16 @@ namespace OpenRA.Widgets public Func GetValueFormat; public Func GetXAxisValueFormat; public Func GetYAxisValueFormat; - public Func GetVisibleNodeCount; + public Func GetXAxisSize; + public Func GetYAxisSize; public Func GetXAxisLabel; public Func GetYAxisLabel; public Func GetDisplayYAxisZero; public string ValueFormat = "{0}"; public string XAxisValueFormat = "{0}"; public string YAxisValueFormat = "{0}"; - public int VisibleNodeCount = 10; + public int XAxisSize = 10; + public int YAxisSize = 10; public string XAxisLabel = ""; public string YAxisLabel = ""; public bool DisplayYAxisZero = true; @@ -41,7 +43,8 @@ namespace OpenRA.Widgets GetValueFormat = () => ValueFormat; GetXAxisValueFormat = () => XAxisValueFormat; GetYAxisValueFormat = () => YAxisValueFormat; - GetVisibleNodeCount = () => VisibleNodeCount; + GetXAxisSize = () => XAxisSize; + GetYAxisSize = () => YAxisSize; GetXAxisLabel = () => XAxisLabel; GetYAxisLabel = () => YAxisLabel; GetDisplayYAxisZero = () => DisplayYAxisZero; @@ -54,14 +57,16 @@ namespace OpenRA.Widgets GetValueFormat = other.GetValueFormat; GetXAxisValueFormat = other.GetXAxisValueFormat; GetYAxisValueFormat = other.GetYAxisValueFormat; - GetVisibleNodeCount = other.GetVisibleNodeCount; + GetXAxisSize = other.GetXAxisSize; + GetYAxisSize = other.GetYAxisSize; GetXAxisLabel = other.GetXAxisLabel; GetYAxisLabel = other.GetYAxisLabel; GetDisplayYAxisZero = other.GetDisplayYAxisZero; ValueFormat = other.ValueFormat; XAxisValueFormat = other.XAxisValueFormat; YAxisValueFormat = other.YAxisValueFormat; - VisibleNodeCount = other.VisibleNodeCount; + XAxisSize = other.XAxisSize; + YAxisSize = other.YAxisSize; XAxisLabel = other.XAxisLabel; YAxisLabel = other.YAxisLabel; DisplayYAxisZero = other.DisplayYAxisZero; @@ -87,29 +92,32 @@ namespace OpenRA.Widgets var tinyBold = Game.Renderer.Fonts["TinyBold"]; var bold = Game.Renderer.Fonts["Bold"]; - var visibleNodeCount = GetVisibleNodeCount(); - var visibleNodeStep = width / visibleNodeCount; + var xAxisSize = GetXAxisSize(); + var yAxisSize = GetYAxisSize(); + + var xStep = width / xAxisSize; + var yStep = height / yAxisSize; + var actualNodeCount = GetDataSource().First().Second.Count(); - var visibleNodeStart = Math.Max(0, actualNodeCount - visibleNodeCount); - var visibleNodeEnd = Math.Max(actualNodeCount, visibleNodeCount); + 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; //todo: make this stuff not draw outside of the RenderBounds - for (int n = visibleNodeStart, i = 0; n <= visibleNodeEnd; n++, i++) + for (int n = visibleNodeStart, x = 0; n <= visibleNodeEnd; n++, x += xStep) { - var x = i * visibleNodeStep; Game.Renderer.LineRenderer.DrawLine(origin + new float2(x, 0), origin + new float2(x, -5), Color.White, Color.White); tinyBold.DrawText(GetXAxisValueFormat().F(n), origin + new float2(x, 2), Color.White); } bold.DrawText(GetXAxisLabel(), origin + new float2(width / 2, 20), Color.White); - for (var i = (GetDisplayYAxisZero() ? 0f : height / 10); i <= height; i += height / 10) + for (var y = (GetDisplayYAxisZero() ? 0f : yStep); y <= height; y += yStep) { - var value = i / scale; - Game.Renderer.LineRenderer.DrawLine(origin + new float2(width - 5, -i), origin + new float2(width, -i), Color.White, Color.White); - tinyBold.DrawText(GetYAxisValueFormat().F(value), origin + new float2(width + 2, -i), Color.White); + var value = y / scale; + Game.Renderer.LineRenderer.DrawLine(origin + new float2(width - 5, -y), origin + new float2(width, -y), Color.White, Color.White); + tinyBold.DrawText(GetYAxisValueFormat().F(value), origin + new float2(width + 2, -y), Color.White); } bold.DrawText(GetYAxisLabel(), origin + new float2(width + 40, -(height / 2)), Color.White); @@ -121,16 +129,16 @@ namespace OpenRA.Widgets var color = player.ColorRamp.GetColor(0); if (data.Any()) { - data = data.Reverse().Take(visibleNodeCount).Reverse(); + data = data.Reverse().Take(xAxisSize).Reverse(); var scaledData = data.Select(d => d * scale); var x = 0f; scaledData.Aggregate((a, b) => { Game.Renderer.LineRenderer.DrawLine( origin + new float2(x, -a), - origin + new float2(x + visibleNodeStep, -b), + origin + new float2(x + xStep, -b), color, color); - x += visibleNodeStep; + x += xStep; return b; }); diff --git a/mods/ra/chrome/ingame.yaml b/mods/ra/chrome/ingame.yaml index 71852df2ba..929dbe0175 100644 --- a/mods/ra/chrome/ingame.yaml +++ b/mods/ra/chrome/ingame.yaml @@ -873,7 +873,8 @@ Container@OBSERVER_ROOT: ValueFormat:${0} XAxisValueFormat:{0} YAxisValueFormat:${0:F0} - VisibleNodeCount:20 + XAxisSize:20 + YAxisSize:10 XAxisLabel:m YAxisLabel:$ DisplayYAxisZero:false