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\ColorBlockWidget.cs" />
<Compile Include="Widgets\DropDownButtonWidget.cs" />
<Compile Include="Widgets\GraphWidget.cs" />
<Compile Include="Widgets\GridLayout.cs" />
<Compile Include="Widgets\ImageWidget.cs" />
<Compile Include="Widgets\LabelWidget.cs" />
<Compile Include="Widgets\LineGraphWidget.cs" />
<Compile Include="Widgets\ListLayout.cs" />
<Compile Include="Widgets\MapPreviewWidget.cs" />
<Compile Include="Widgets\PasswordFieldWidget.cs" />

View File

@@ -16,9 +16,9 @@ using OpenRA.FileFormats;
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> GetXAxisValueFormat;
public Func<string> 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,12 +109,12 @@ 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)
@@ -124,7 +124,7 @@ namespace OpenRA.Widgets
}
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<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;
var template = earnedThisMinuteGraphTemplate.Clone();
var graph = template.Get<GraphWidget>("EARNED_THIS_MIN_GRAPH");
graph.GetDataSource = () => players.Select(p => Pair.New(p, p.PlayerActor.Trait<PlayerStatistics>().EarnedSamples.Select(s => (float)s)));
var graph = template.Get<LineGraphWidget>("EARNED_THIS_MIN_GRAPH");
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);
}

View File

@@ -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: