Improve graph. Add things like scales/axis labelling.

This commit is contained in:
Scott_NZ
2012-11-27 17:11:47 +13:00
parent b6e8c9c9ea
commit e424883857
6 changed files with 109 additions and 40 deletions

View File

@@ -75,7 +75,7 @@ namespace OpenRA.Mods.RA
public void Tick(Actor self) public void Tick(Actor self)
{ {
if (self.World.FrameNumber % 1500 == 0) if (self.World.FrameNumber % 1500 == 1)
{ {
UpdateEarnedThisMinute(); UpdateEarnedThisMinute();
} }

View File

@@ -153,8 +153,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var graph = template.Get<ObserverStatsGraphWidget>("EARNED_THIS_MIN_GRAPH"); var graph = template.Get<ObserverStatsGraphWidget>("EARNED_THIS_MIN_GRAPH");
graph.GetDataSource = () => players.Select(p => Pair.New(p, p.PlayerActor.Trait<PlayerStatistics>().EarnedSamples.Select(s => (float)s))); graph.GetDataSource = () => players.Select(p => Pair.New(p, p.PlayerActor.Trait<PlayerStatistics>().EarnedSamples.Select(s => (float)s)));
graph.GetDataScale = () => 1 / 100f;
graph.GetLastValueFormat = () => "${0}";
playerStatsPanel.AddChild(template); playerStatsPanel.AddChild(template);
} }

View File

@@ -79,8 +79,8 @@ namespace OpenRA.Mods.RA.Widgets
var clock = clocks[queue.Trait]; var clock = clocks[queue.Trait];
clock.PlayFetchIndex("idle", clock.PlayFetchIndex("idle",
() => (item.TotalTime - item.RemainingTime) () => item.TotalTime == 0 ? 0 : ((item.TotalTime - item.RemainingTime)
* (clock.CurrentSequence.Length - 1) / item.TotalTime); * (clock.CurrentSequence.Length - 1) / item.TotalTime));
clock.Tick(); clock.Tick();
WidgetUtils.DrawSHP(clock.Image, location, worldRenderer, size); WidgetUtils.DrawSHP(clock.Image, location, worldRenderer, size);

View File

@@ -18,68 +18,132 @@ namespace OpenRA.Widgets
{ {
public class ObserverStatsGraphWidget : Widget public class ObserverStatsGraphWidget : Widget
{ {
public Func<IEnumerable<Pair<Player, IEnumerable<float>>>> GetDataSource = () => null; public Func<IEnumerable<Pair<Player, IEnumerable<float>>>> GetDataSource;
public Func<float> GetDataScale = () => 1.0f; public Func<float> GetDataScale;
public Func<string> GetLastValueFormat = () => "{0}"; public Func<string> GetValueFormat;
public Func<int> GetNodeCount = () => 20; public Func<string> GetXAxisValueFormat;
public Func<int> GetNodeStep = () => 5; public Func<string> GetYAxisValueFormat;
public Func<int> GetVisibleNodeCount;
public Func<string> GetXAxisLabel;
public Func<string> GetYAxisLabel;
public Func<bool> GetDisplayYAxisZero;
public string ValueFormat = "{0}";
public string XAxisValueFormat = "{0}";
public string YAxisValueFormat = "{0}";
public int VisibleNodeCount = 10;
public string XAxisLabel = "";
public string YAxisLabel = "";
public bool DisplayYAxisZero = true;
public ObserverStatsGraphWidget() : base() { } public ObserverStatsGraphWidget()
: base()
{
GetValueFormat = () => ValueFormat;
GetXAxisValueFormat = () => XAxisValueFormat;
GetYAxisValueFormat = () => YAxisValueFormat;
GetVisibleNodeCount = () => VisibleNodeCount;
GetXAxisLabel = () => XAxisLabel;
GetYAxisLabel = () => YAxisLabel;
GetDisplayYAxisZero = () => DisplayYAxisZero;
}
protected ObserverStatsGraphWidget(ObserverStatsGraphWidget other) protected ObserverStatsGraphWidget(ObserverStatsGraphWidget other)
: base(other) : base(other)
{ {
GetDataSource = other.GetDataSource; GetDataSource = other.GetDataSource;
GetValueFormat = other.GetValueFormat;
GetXAxisValueFormat = other.GetXAxisValueFormat;
GetYAxisValueFormat = other.GetYAxisValueFormat;
GetVisibleNodeCount = other.GetVisibleNodeCount;
GetXAxisLabel = other.GetXAxisLabel;
GetYAxisLabel = other.GetYAxisLabel;
GetDisplayYAxisZero = other.GetDisplayYAxisZero;
ValueFormat = other.ValueFormat;
XAxisValueFormat = other.XAxisValueFormat;
YAxisValueFormat = other.YAxisValueFormat;
VisibleNodeCount = other.VisibleNodeCount;
XAxisLabel = other.XAxisLabel;
YAxisLabel = other.YAxisLabel;
DisplayYAxisZero = other.DisplayYAxisZero;
} }
public override void Draw() public override void Draw()
{ {
if (GetDataSource == null || !GetDataSource().Any())
{
return;
}
var rect = RenderBounds; var rect = RenderBounds;
var origin = new float2(rect.Left, rect.Bottom); var origin = new float2(rect.Left, rect.Bottom);
var basis = new float2(rect.Width / 100, rect.Height / 100);
Game.Renderer.LineRenderer.DrawLine(origin, origin + new float2(100, 0) * basis, Color.White, Color.White); var width = rect.Width;
Game.Renderer.LineRenderer.DrawLine(origin, origin - new float2(0, 100) * basis, Color.White, Color.White); var height = rect.Height;
Game.Renderer.LineRenderer.DrawLine(origin + new float2(100, 0) * basis, origin + new float2(100, -100) * basis, Color.White, Color.White);
Game.Renderer.LineRenderer.DrawLine(origin, origin + new float2(width, 0), Color.White, Color.White);
Game.Renderer.LineRenderer.DrawLine(origin, origin + new float2(0, -height), Color.White, Color.White);
Game.Renderer.LineRenderer.DrawLine(origin + new float2(width, 0), origin + new float2(width, -height), Color.White, Color.White);
Game.Renderer.LineRenderer.DrawLine(origin + new float2(0, -height), origin + new float2(width, -height), Color.White, Color.White);
var tinyBold = Game.Renderer.Fonts["TinyBold"]; var tinyBold = Game.Renderer.Fonts["TinyBold"];
var bold = Game.Renderer.Fonts["Bold"];
var i = 0; var visibleNodeCount = GetVisibleNodeCount();
foreach (var pair in GetDataSource()) var visibleNodeStep = width / visibleNodeCount;
var actualNodeCount = GetDataSource().First().Second.Count();
var visibleNodeStart = Math.Max(0, actualNodeCount - visibleNodeCount);
var visibleNodeEnd = Math.Max(actualNodeCount, visibleNodeCount);
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++)
{ {
var player = pair.First; var x = i * visibleNodeStep;
var data = pair.Second.Reverse().Take(GetNodeCount()).Reverse(); 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)
{
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);
}
bold.DrawText(GetYAxisLabel(), origin + new float2(width + 40, -(height / 2)), Color.White);
var playerNameOffset = 0;
foreach (var playerDataPair in GetDataSource())
{
var player = playerDataPair.First;
var data = playerDataPair.Second;
var color = player.ColorRamp.GetColor(0); var color = player.ColorRamp.GetColor(0);
if (data.Any()) if (data.Any())
{ {
var scale = GetDataScale(); data = data.Reverse().Take(visibleNodeCount).Reverse();
var scaledData = data.Select(d => d * scale); var scaledData = data.Select(d => d * scale);
var n = 0; var x = 0f;
var step = GetNodeStep();
scaledData.Aggregate((a, b) => scaledData.Aggregate((a, b) =>
{ {
Game.Renderer.LineRenderer.DrawLine( Game.Renderer.LineRenderer.DrawLine(
origin + new float2(n, -a) * basis, origin + new float2(x, -a),
origin + new float2(n + step, -b) * basis, origin + new float2(x + visibleNodeStep, -b),
color, color); color, color);
n += step; x += visibleNodeStep;
return b; return b;
}); });
var lastValue = data.Last(); var value = data.Last();
if (lastValue != 0) if (value != 0)
{ {
var scaledLastValue = lastValue * scale; var scaledValue = value * scale;
var lastValueFormat = GetLastValueFormat(); tinyBold.DrawText(GetValueFormat().F(value), origin + new float2(x, -scaledValue - 2), color);
if (lastValueFormat != null)
{
tinyBold.DrawText(lastValueFormat.F(lastValue), origin + new float2(n, -scaledLastValue - 2) * basis, color);
}
} }
} }
tinyBold.DrawText(player.PlayerName, new float2(rect.Left, rect.Top) + new float2(5, 10 * i - 3), color); tinyBold.DrawText(player.PlayerName, new float2(rect.Left, rect.Top) + new float2(5, 10 * playerNameOffset + 3), color);
i++; playerNameOffset++;
} }
} }

View File

@@ -79,8 +79,8 @@ namespace OpenRA.Mods.RA.Widgets
var clock = clocks[power.a.Key]; var clock = clocks[power.a.Key];
clock.PlayFetchIndex("idle", clock.PlayFetchIndex("idle",
() => (item.TotalTime - item.RemainingTime) () => item.TotalTime == 0 ? 0 : ((item.TotalTime - item.RemainingTime)
* (clock.CurrentSequence.Length - 1) / item.TotalTime); * (clock.CurrentSequence.Length - 1) / item.TotalTime));
clock.Tick(); clock.Tick();
WidgetUtils.DrawSHP(clock.Image, location, worldRenderer, size); WidgetUtils.DrawSHP(clock.Image, location, worldRenderer, size);

View File

@@ -862,14 +862,21 @@ Container@OBSERVER_ROOT:
Container@EARNED_THIS_MIN_GRAPH_TEMPLATE: Container@EARNED_THIS_MIN_GRAPH_TEMPLATE:
X:0 X:0
Y:0 Y:0
Width:PARENT_RIGHT-35 Width:PARENT_RIGHT-100
Height:300 Height:PARENT_BOTTOM-50
Children: Children:
ObserverStatsGraph@EARNED_THIS_MIN_GRAPH: ObserverStatsGraph@EARNED_THIS_MIN_GRAPH:
X:0 X:0
Y:0 Y:0
Width:800 Width:PARENT_RIGHT
Height:PARENT_BOTTOM Height:PARENT_BOTTOM
ValueFormat:${0}
XAxisValueFormat:{0}
YAxisValueFormat:${0:F0}
VisibleNodeCount:20
XAxisLabel:m
YAxisLabel:$
DisplayYAxisZero:false
Background@FMVPLAYER: Background@FMVPLAYER:
Width:WINDOW_RIGHT Width:WINDOW_RIGHT
Height:WINDOW_BOTTOM Height:WINDOW_BOTTOM