Refactored GraphWidget to LineGraphWidget

This commit is contained in:
Scott_NZ
2012-11-28 01:52:48 +13:00
parent a78f8865b8
commit 4c729ca5ed
4 changed files with 49 additions and 30 deletions

View File

@@ -192,10 +192,10 @@
<Compile Include="Widgets\ChromeMetrics.cs" /> <Compile Include="Widgets\ChromeMetrics.cs" />
<Compile Include="Widgets\ColorBlockWidget.cs" /> <Compile Include="Widgets\ColorBlockWidget.cs" />
<Compile Include="Widgets\DropDownButtonWidget.cs" /> <Compile Include="Widgets\DropDownButtonWidget.cs" />
<Compile Include="Widgets\GraphWidget.cs" />
<Compile Include="Widgets\GridLayout.cs" /> <Compile Include="Widgets\GridLayout.cs" />
<Compile Include="Widgets\ImageWidget.cs" /> <Compile Include="Widgets\ImageWidget.cs" />
<Compile Include="Widgets\LabelWidget.cs" /> <Compile Include="Widgets\LabelWidget.cs" />
<Compile Include="Widgets\LineGraphWidget.cs" />
<Compile Include="Widgets\ListLayout.cs" /> <Compile Include="Widgets\ListLayout.cs" />
<Compile Include="Widgets\MapPreviewWidget.cs" /> <Compile Include="Widgets\MapPreviewWidget.cs" />
<Compile Include="Widgets\PasswordFieldWidget.cs" /> <Compile Include="Widgets\PasswordFieldWidget.cs" />

View File

@@ -16,9 +16,9 @@ using OpenRA.FileFormats;
namespace OpenRA.Widgets namespace OpenRA.Widgets
{ {
public class GraphWidget : Widget public class LineGraphWidget : Widget
{ {
public Func<IEnumerable<Pair<Player, IEnumerable<float>>>> GetDataSource; public Func<IEnumerable<LineGraphSeries>> GetSeries;
public Func<string> GetValueFormat; public Func<string> GetValueFormat;
public Func<string> GetXAxisValueFormat; public Func<string> GetXAxisValueFormat;
public Func<string> GetYAxisValueFormat; public Func<string> GetYAxisValueFormat;
@@ -40,7 +40,7 @@ namespace OpenRA.Widgets
public string LabelFont; public string LabelFont;
public string AxisFont; public string AxisFont;
public GraphWidget() public LineGraphWidget()
: base() : base()
{ {
GetValueFormat = () => ValueFormat; GetValueFormat = () => ValueFormat;
@@ -55,10 +55,10 @@ namespace OpenRA.Widgets
GetAxisFont = () => AxisFont; GetAxisFont = () => AxisFont;
} }
protected GraphWidget(GraphWidget other) protected LineGraphWidget(LineGraphWidget other)
: base(other) : base(other)
{ {
GetDataSource = other.GetDataSource; GetSeries = other.GetSeries;
GetValueFormat = other.GetValueFormat; GetValueFormat = other.GetValueFormat;
GetXAxisValueFormat = other.GetXAxisValueFormat; GetXAxisValueFormat = other.GetXAxisValueFormat;
GetYAxisValueFormat = other.GetYAxisValueFormat; GetYAxisValueFormat = other.GetYAxisValueFormat;
@@ -83,7 +83,7 @@ namespace OpenRA.Widgets
public override void Draw() public override void Draw()
{ {
if (GetDataSource == null || !GetDataSource().Any() if (GetSeries == null || !GetSeries().Any()
|| GetLabelFont == null || GetLabelFont() == null || GetLabelFont == null || GetLabelFont() == null
|| GetAxisFont == null || GetAxisFont() == null) || GetAxisFont == null || GetAxisFont() == null)
{ {
@@ -109,22 +109,22 @@ namespace OpenRA.Widgets
var xStep = width / xAxisSize; var xStep = width / xAxisSize;
var yStep = height / yAxisSize; var yStep = height / yAxisSize;
var actualNodeCount = GetDataSource().First().Second.Count(); var actualNodeCount = GetSeries().First().Points.Count();
var visibleNodeStart = Math.Max(0, actualNodeCount - xAxisSize); var visibleNodeStart = Math.Max(0, actualNodeCount - xAxisSize);
var visibleNodeEnd = Math.Max(actualNodeCount, xAxisSize); var visibleNodeEnd = Math.Max(actualNodeCount, xAxisSize);
float maxValue = GetDataSource().Select(p => p.Second).SelectMany(d => d).Max(); var maxValue = GetSeries().Select(p => p.Points).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, x = 0; n <= visibleNodeEnd; n++, x += xStep) 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); 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); tiny.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 y = (GetDisplayFirstYAxisValue() ? 0f : yStep); y <= height; y += yStep) for (var y = (GetDisplayFirstYAxisValue() ? 0 : yStep); y <= height; y += yStep)
{ {
var value = y / scale; var value = y / scale;
Game.Renderer.LineRenderer.DrawLine(origin + new float2(width - 5, -y), origin + new float2(width, -y), Color.White, Color.White); 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); bold.DrawText(GetYAxisLabel(), origin + new float2(width + 40, -(height / 2)), Color.White);
var playerNameOffset = 0; var keyOffset = 0;
foreach (var playerDataPair in GetDataSource()) foreach (var series in GetSeries())
{ {
var player = playerDataPair.First; var key = series.Key;
var data = playerDataPair.Second; var color = series.Color;
var color = player.ColorRamp.GetColor(0); var points = series.Points;
if (data.Any()) if (points.Any())
{ {
data = data.Reverse().Take(xAxisSize).Reverse(); points = points.Reverse().Take(xAxisSize).Reverse();
var scaledData = data.Select(d => d * scale); var scaledData = points.Select(d => d * scale);
var x = 0f; var x = 0;
scaledData.Aggregate((a, b) => scaledData.Aggregate((a, b) =>
{ {
Game.Renderer.LineRenderer.DrawLine( Game.Renderer.LineRenderer.DrawLine(
@@ -153,7 +153,7 @@ namespace OpenRA.Widgets
return b; return b;
}); });
var value = data.Last(); var value = points.Last();
if (value != 0) if (value != 0)
{ {
var scaledValue = value * scale; 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); tiny.DrawText(key, new float2(rect.Left, rect.Top) + new float2(5, 10 * keyOffset + 3), color);
playerNameOffset++; keyOffset++;
} }
} }
public override Widget Clone() public override Widget Clone()
{ {
return new GraphWidget(this); return new LineGraphWidget(this);
}
}
public class LineGraphSeries
{
public string Key;
public Color Color;
public IEnumerable<float> Points;
public LineGraphSeries(string key, Color color, IEnumerable<float> points)
{
Key = key;
Color = color;
Points = points;
} }
} }
} }

View File

@@ -151,8 +151,13 @@ namespace OpenRA.Mods.RA.Widgets.Logic
earnedThisMinuteGraphHeaders.Visible = true; earnedThisMinuteGraphHeaders.Visible = true;
var template = earnedThisMinuteGraphTemplate.Clone(); var template = earnedThisMinuteGraphTemplate.Clone();
var graph = template.Get<GraphWidget>("EARNED_THIS_MIN_GRAPH"); var graph = template.Get<LineGraphWidget>("EARNED_THIS_MIN_GRAPH");
graph.GetDataSource = () => players.Select(p => Pair.New(p, p.PlayerActor.Trait<PlayerStatistics>().EarnedSamples.Select(s => (float)s))); graph.GetSeries = () =>
players.Select(p => new LineGraphSeries(
p.PlayerName,
p.ColorRamp.GetColor(0),
p.PlayerActor.Trait<PlayerStatistics>().EarnedSamples.Select(s => (float)s)
));
playerStatsPanel.AddChild(template); playerStatsPanel.AddChild(template);
} }

View File

@@ -865,7 +865,7 @@ Container@OBSERVER_ROOT:
Width:PARENT_RIGHT-100 Width:PARENT_RIGHT-100
Height:PARENT_BOTTOM-50 Height:PARENT_BOTTOM-50
Children: Children:
Graph@EARNED_THIS_MIN_GRAPH: LineGraph@EARNED_THIS_MIN_GRAPH:
X:0 X:0
Y:0 Y:0
Width:PARENT_RIGHT Width:PARENT_RIGHT
@@ -877,7 +877,7 @@ Container@OBSERVER_ROOT:
YAxisSize:10 YAxisSize:10
XAxisLabel:m XAxisLabel:m
YAxisLabel:$ YAxisLabel:$
DisplayYAxisZero:false DisplayFirstYAxisValue:false
LabelFont:TinyBold LabelFont:TinyBold
AxisFont:Bold AxisFont:Bold
Background@FMVPLAYER: Background@FMVPLAYER: