MonoDebugHud.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using Urho.Gui;
  3. namespace Urho
  4. {
  5. public class MonoDebugHud
  6. {
  7. const int FrameSampleCount = 20;
  8. Application application;
  9. Text text;
  10. int frameCount = 0;
  11. DateTime dateTime;
  12. TimeSpan span;
  13. public MonoDebugHud(Application application)
  14. {
  15. this.application = application;
  16. }
  17. void OnPostUpdate(PostUpdateEventArgs args)
  18. {
  19. Graphics graphics = application.Graphics;
  20. var now = DateTime.UtcNow;
  21. span += now - dateTime;
  22. dateTime = now;
  23. if (++frameCount >= FrameSampleCount)
  24. {
  25. float average = (float)(span.TotalMilliseconds / frameCount);
  26. float fps = 1000;
  27. if (average != 0)
  28. fps /= average;
  29. frameCount = 0;
  30. span = TimeSpan.Zero;
  31. if (FpsOnly)
  32. text.Value = $"{(int)fps} FPS\n{AdditionalText}";
  33. else if (!InnerCacheDetails)
  34. text.Value = $"{(int)fps} FPS\n{graphics.NumBatches} batches\n{Runtime.RefCountedCache.Count} MCW\n{graphics.ApiName}\n{AdditionalText}";
  35. else
  36. text.Value = $"{(int)fps} FPS\n{graphics.NumBatches} batches\n{Runtime.RefCountedCache.GetCacheStatus()}\n{AdditionalText}";
  37. }
  38. }
  39. public bool InnerCacheDetails { get; set; }
  40. public bool FpsOnly { get; set; }
  41. public string AdditionalText { get; set; }
  42. public void Show()
  43. {
  44. Show(Color.White);
  45. }
  46. public void Show(Color color, int fontSize = 18)
  47. {
  48. if (text != null)
  49. return;
  50. text = new Text();
  51. text.SetColor(color);
  52. text.VerticalAlignment = VerticalAlignment.Top;
  53. text.HorizontalAlignment = HorizontalAlignment.Right;
  54. text.TextAlignment = HorizontalAlignment.Right;
  55. text.SetFont(CoreAssets.Fonts.AnonymousPro, fontSize);
  56. application.UI.Root.AddChild(text);
  57. application.Engine.PostUpdate += OnPostUpdate;
  58. }
  59. public void Hide()
  60. {
  61. if (text == null)
  62. return;
  63. application.Engine.PostUpdate -= OnPostUpdate;
  64. application.UI.Root.RemoveChild(text, 0);
  65. text = null;
  66. }
  67. }
  68. }