Cleanup, refactoring of stats graph widget.

This commit is contained in:
Scott_NZ
2012-11-27 19:19:10 +13:00
parent e424883857
commit d5b37d4343
2 changed files with 28 additions and 19 deletions

View File

@@ -23,14 +23,16 @@ namespace OpenRA.Widgets
public Func<string> GetValueFormat; public Func<string> GetValueFormat;
public Func<string> GetXAxisValueFormat; public Func<string> GetXAxisValueFormat;
public Func<string> GetYAxisValueFormat; public Func<string> GetYAxisValueFormat;
public Func<int> GetVisibleNodeCount; public Func<int> GetXAxisSize;
public Func<int> GetYAxisSize;
public Func<string> GetXAxisLabel; public Func<string> GetXAxisLabel;
public Func<string> GetYAxisLabel; public Func<string> GetYAxisLabel;
public Func<bool> GetDisplayYAxisZero; public Func<bool> GetDisplayYAxisZero;
public string ValueFormat = "{0}"; public string ValueFormat = "{0}";
public string XAxisValueFormat = "{0}"; public string XAxisValueFormat = "{0}";
public string YAxisValueFormat = "{0}"; public string YAxisValueFormat = "{0}";
public int VisibleNodeCount = 10; public int XAxisSize = 10;
public int YAxisSize = 10;
public string XAxisLabel = ""; public string XAxisLabel = "";
public string YAxisLabel = ""; public string YAxisLabel = "";
public bool DisplayYAxisZero = true; public bool DisplayYAxisZero = true;
@@ -41,7 +43,8 @@ namespace OpenRA.Widgets
GetValueFormat = () => ValueFormat; GetValueFormat = () => ValueFormat;
GetXAxisValueFormat = () => XAxisValueFormat; GetXAxisValueFormat = () => XAxisValueFormat;
GetYAxisValueFormat = () => YAxisValueFormat; GetYAxisValueFormat = () => YAxisValueFormat;
GetVisibleNodeCount = () => VisibleNodeCount; GetXAxisSize = () => XAxisSize;
GetYAxisSize = () => YAxisSize;
GetXAxisLabel = () => XAxisLabel; GetXAxisLabel = () => XAxisLabel;
GetYAxisLabel = () => YAxisLabel; GetYAxisLabel = () => YAxisLabel;
GetDisplayYAxisZero = () => DisplayYAxisZero; GetDisplayYAxisZero = () => DisplayYAxisZero;
@@ -54,14 +57,16 @@ namespace OpenRA.Widgets
GetValueFormat = other.GetValueFormat; GetValueFormat = other.GetValueFormat;
GetXAxisValueFormat = other.GetXAxisValueFormat; GetXAxisValueFormat = other.GetXAxisValueFormat;
GetYAxisValueFormat = other.GetYAxisValueFormat; GetYAxisValueFormat = other.GetYAxisValueFormat;
GetVisibleNodeCount = other.GetVisibleNodeCount; GetXAxisSize = other.GetXAxisSize;
GetYAxisSize = other.GetYAxisSize;
GetXAxisLabel = other.GetXAxisLabel; GetXAxisLabel = other.GetXAxisLabel;
GetYAxisLabel = other.GetYAxisLabel; GetYAxisLabel = other.GetYAxisLabel;
GetDisplayYAxisZero = other.GetDisplayYAxisZero; GetDisplayYAxisZero = other.GetDisplayYAxisZero;
ValueFormat = other.ValueFormat; ValueFormat = other.ValueFormat;
XAxisValueFormat = other.XAxisValueFormat; XAxisValueFormat = other.XAxisValueFormat;
YAxisValueFormat = other.YAxisValueFormat; YAxisValueFormat = other.YAxisValueFormat;
VisibleNodeCount = other.VisibleNodeCount; XAxisSize = other.XAxisSize;
YAxisSize = other.YAxisSize;
XAxisLabel = other.XAxisLabel; XAxisLabel = other.XAxisLabel;
YAxisLabel = other.YAxisLabel; YAxisLabel = other.YAxisLabel;
DisplayYAxisZero = other.DisplayYAxisZero; DisplayYAxisZero = other.DisplayYAxisZero;
@@ -87,29 +92,32 @@ namespace OpenRA.Widgets
var tinyBold = Game.Renderer.Fonts["TinyBold"]; var tinyBold = Game.Renderer.Fonts["TinyBold"];
var bold = Game.Renderer.Fonts["Bold"]; var bold = Game.Renderer.Fonts["Bold"];
var visibleNodeCount = GetVisibleNodeCount(); var xAxisSize = GetXAxisSize();
var visibleNodeStep = width / visibleNodeCount; var yAxisSize = GetYAxisSize();
var xStep = width / xAxisSize;
var yStep = height / yAxisSize;
var actualNodeCount = GetDataSource().First().Second.Count(); var actualNodeCount = GetDataSource().First().Second.Count();
var visibleNodeStart = Math.Max(0, actualNodeCount - visibleNodeCount); var visibleNodeStart = Math.Max(0, actualNodeCount - xAxisSize);
var visibleNodeEnd = Math.Max(actualNodeCount, visibleNodeCount); var visibleNodeEnd = Math.Max(actualNodeCount, xAxisSize);
float maxValue = GetDataSource().Select(p => p.Second).SelectMany(d => d).Max(); float maxValue = GetDataSource().Select(p => p.Second).SelectMany(d => d).Max();
var scale = (100 / maxValue) * 3; var scale = (100 / maxValue) * 3;
//todo: make this stuff not draw outside of the RenderBounds //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); 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); tinyBold.DrawText(GetXAxisValueFormat().F(n), origin + new float2(x, 2), Color.White);
} }
bold.DrawText(GetXAxisLabel(), origin + new float2(width / 2, 20), 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; var value = y / scale;
Game.Renderer.LineRenderer.DrawLine(origin + new float2(width - 5, -i), origin + new float2(width, -i), Color.White, Color.White); 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, -i), 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); 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); var color = player.ColorRamp.GetColor(0);
if (data.Any()) if (data.Any())
{ {
data = data.Reverse().Take(visibleNodeCount).Reverse(); data = data.Reverse().Take(xAxisSize).Reverse();
var scaledData = data.Select(d => d * scale); var scaledData = data.Select(d => d * scale);
var x = 0f; var x = 0f;
scaledData.Aggregate((a, b) => scaledData.Aggregate((a, b) =>
{ {
Game.Renderer.LineRenderer.DrawLine( Game.Renderer.LineRenderer.DrawLine(
origin + new float2(x, -a), origin + new float2(x, -a),
origin + new float2(x + visibleNodeStep, -b), origin + new float2(x + xStep, -b),
color, color); color, color);
x += visibleNodeStep; x += xStep;
return b; return b;
}); });

View File

@@ -873,7 +873,8 @@ Container@OBSERVER_ROOT:
ValueFormat:${0} ValueFormat:${0}
XAxisValueFormat:{0} XAxisValueFormat:{0}
YAxisValueFormat:${0:F0} YAxisValueFormat:${0:F0}
VisibleNodeCount:20 XAxisSize:20
YAxisSize:10
XAxisLabel:m XAxisLabel:m
YAxisLabel:$ YAxisLabel:$
DisplayYAxisZero:false DisplayYAxisZero:false