Files
RobustToolbox/Robust.Client/UserInterface/CustomControls/FrameGraph.cs
T
SilverandGitHub 25926a17b7 Renames SS14.* to Robust.* (#793)
RobustToolbox projects should be named Robust.* 

This PR changes the RobustToolbox projects from SS14.* to Robust.*

Updates SS14.* prefixes/namespaces to Robust.*
Updates SpaceStation14.sln to RobustToolbox.sln
Updates MSBUILD/SS14.* to MSBUILD/Robust.*
Updates CSProject and MSBuild references for the above
Updates git_helper.py
Removes Runserver and Runclient as they are unusable
2019-04-15 20:24:59 -06:00

92 lines
2.9 KiB
C#

using Robust.Client.Graphics.Drawing;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.Maths;
namespace Robust.Client.UserInterface.CustomControls
{
public sealed class FrameGraph : Control
{
private readonly IGameTiming _gameTiming;
/// <summary>
/// How many frames we show at once.
/// </summary>
private const int TrackedFrames = 120;
/// <summary>
/// The width of a single frame on the bar chart, in pixels.
/// </summary>
private const int FrameWidth = 4;
/// <summary>
/// The height of a single frame in pixels to the target <see cref="TargetFrameRate"/> FPS.
/// If the frame takes say 1/30 seconds to complete (30 FPS) it would be twice this value.
/// </summary>
private const int FrameHeight = 60;
/// <summary>
/// The target frame rate at which we are "good".
/// </summary>
private const int TargetFrameRate = 60;
// We keep track of frame times in a ring buffer.
private readonly float[] _frameTimes = new float[TrackedFrames];
// Position of the last frame in the ring buffer.
private int _frameIndex;
public FrameGraph(IGameTiming gameTiming)
{
_gameTiming = gameTiming;
}
protected override void Initialize()
{
base.Initialize();
SizeFlagsHorizontal = SizeFlags.None;
}
protected override Vector2 CalculateMinimumSize()
{
return (TrackedFrames * FrameWidth, FrameHeight * 2);
}
protected override void FrameUpdate(RenderFrameEventArgs args)
{
_frameTimes[_frameIndex] = (float)_gameTiming.RealFrameTime.TotalSeconds;
_frameIndex = (_frameIndex + 1) % TrackedFrames;
}
protected internal override void Draw(DrawingHandleScreen handle)
{
base.Draw(handle);
for (var i = 0; i < TrackedFrames; i++)
{
var currentFrameIndex = MathHelper.Mod(_frameIndex - 1 - i, TrackedFrames);
var frameTime = _frameTimes[currentFrameIndex];
var x = FrameWidth * (TrackedFrames - 1 - i);
var frameHeight = FrameHeight * (frameTime / (1f / TargetFrameRate));
var rect = new UIBox2(x, Height - frameHeight, x + FrameWidth, Height);
Color color;
if (frameTime > 1f / (TargetFrameRate / 2 - 1))
{
color = Color.Red;
}
else if (frameTime > 1f / (TargetFrameRate - 1))
{
color = Color.Yellow;
}
else
{
color = Color.Lime;
}
handle.DrawRect(rect, color);
}
}
}
}