ProfileButton.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using OpenTK;
  3. using OpenTK.Graphics.OpenGL;
  4. using Ryujinx.Profiler.UI.SharpFontHelpers;
  5. namespace Ryujinx.Profiler.UI
  6. {
  7. public class ProfileButton
  8. {
  9. // Store font service
  10. private FontService _fontService;
  11. // Layout information
  12. private int _left, _right;
  13. private int _bottom, _top;
  14. private int _height;
  15. private int _padding;
  16. // Label information
  17. private int _labelX, _labelY;
  18. private string _label;
  19. // Misc
  20. private Action _clicked;
  21. private bool _visible;
  22. public ProfileButton(FontService fontService, Action clicked)
  23. : this(fontService, clicked, 0, 0, 0, 0, 0)
  24. {
  25. _visible = false;
  26. }
  27. public ProfileButton(FontService fontService, Action clicked, int x, int y, int padding, int height, int width)
  28. : this(fontService, "", clicked, x, y, padding, height, width)
  29. {
  30. _visible = false;
  31. }
  32. public ProfileButton(FontService fontService, string label, Action clicked, int x, int y, int padding, int height, int width = -1)
  33. {
  34. _fontService = fontService;
  35. _clicked = clicked;
  36. UpdateSize(label, x, y, padding, height, width);
  37. }
  38. public int UpdateSize(string label, int x, int y, int padding, int height, int width = -1)
  39. {
  40. _visible = true;
  41. _label = label;
  42. if (width == -1)
  43. {
  44. // Dummy draw to measure size
  45. width = (int)_fontService.DrawText(label, 0, 0, height, false);
  46. }
  47. UpdateSize(x, y, padding, width, height);
  48. return _right - _left;
  49. }
  50. public void UpdateSize(int x, int y, int padding, int width, int height)
  51. {
  52. _height = height;
  53. _left = x;
  54. _bottom = y;
  55. _labelX = x + padding / 2;
  56. _labelY = y + padding / 2;
  57. _top = y + height + padding;
  58. _right = x + width + padding;
  59. }
  60. public void Draw()
  61. {
  62. if (!_visible)
  63. {
  64. return;
  65. }
  66. // Draw backing rectangle
  67. GL.Begin(PrimitiveType.Triangles);
  68. GL.Color3(Color.Black);
  69. GL.Vertex2(_left, _bottom);
  70. GL.Vertex2(_left, _top);
  71. GL.Vertex2(_right, _top);
  72. GL.Vertex2(_right, _top);
  73. GL.Vertex2(_right, _bottom);
  74. GL.Vertex2(_left, _bottom);
  75. GL.End();
  76. // Use font service to draw label
  77. _fontService.DrawText(_label, _labelX, _labelY, _height);
  78. }
  79. public bool ProcessClick(int x, int y)
  80. {
  81. // If button contains x, y
  82. if (x > _left && x < _right &&
  83. y > _bottom && y < _top)
  84. {
  85. _clicked();
  86. return true;
  87. }
  88. return false;
  89. }
  90. }
  91. }