InterfaceUtil.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Numerics;
  2. using Dalamud.Bindings.ImGui;
  3. using Dalamud.Interface;
  4. using Dalamud.Interface.Textures.TextureWraps;
  5. using Dalamud.Interface.Textures;
  6. namespace HuntBuddy.Utils;
  7. /// <summary>
  8. /// Interface utilities class.
  9. /// </summary>
  10. public static class InterfaceUtil {
  11. /// <summary>
  12. /// Draws hunt icons from game images.
  13. /// </summary>
  14. /// <param name="mobHuntEntry"><see cref="MobHuntEntry"/> containing relevant information.</param>
  15. public static void DrawHuntIcon(MobHuntEntry mobHuntEntry) {
  16. Vector2 cursorPos = ImGui.GetCursorScreenPos();
  17. Vector2 imageSize = mobHuntEntry.ExpansionId < 3 ? new Vector2(192f, 128f) : new Vector2(210f);
  18. imageSize *= ImGui.GetIO().FontGlobalScale * Plugin.Instance.Configuration.IconScale;
  19. ImGui.InvisibleButton("canvas", imageSize);
  20. ImDrawListPtr drawList = ImGui.GetWindowDrawList();
  21. if (mobHuntEntry is { ExpansionId: 4, IsEliteMark: false }) // Endwalker uses circle for non elite mobs
  22. {
  23. drawList.AddCircleFilled(
  24. cursorPos + (imageSize / 2f),
  25. imageSize.X / 2f,
  26. Plugin.Instance.Configuration.IconBackgroundColourU32);
  27. }
  28. else {
  29. drawList.AddRectFilled(
  30. cursorPos,
  31. cursorPos + imageSize,
  32. Plugin.Instance.Configuration.IconBackgroundColourU32);
  33. }
  34. drawList.AddImage(LoadIcon(mobHuntEntry.Icon).Handle, cursorPos, cursorPos + imageSize);
  35. }
  36. /// <summary>
  37. /// Returns a IDalamudTextureWrap for an icon.
  38. /// Will request a Hi-Res version and fallback to normal resolution otherwise.
  39. /// </summary>
  40. /// <param name="id">Icon ID</param>
  41. /// <returns>IDalamudTextureWrap for the icon, or an empty wrap if the icon is not found.</returns>
  42. private static IDalamudTextureWrap LoadIcon(uint id) {
  43. if (Service.TextureProvider.TryGetFromGameIcon(new GameIconLookup {
  44. IconId = id,
  45. HiRes = true,
  46. }, out var texture)) {
  47. return texture.GetWrapOrEmpty();
  48. }
  49. return Service.TextureProvider.GetFromGameIcon(new GameIconLookup {
  50. IconId = id,
  51. HiRes = false,
  52. }).GetWrapOrEmpty();
  53. }
  54. /// <summary>
  55. /// Renders a button with an icon.
  56. /// </summary>
  57. /// <param name="icon">Desired <see cref="FontAwesomeIcon"/> to be rendered.</param>
  58. /// <param name="id">Button ID.</param>
  59. /// <returns>True if pressed.</returns>
  60. public static bool IconButton(FontAwesomeIcon icon, string? id) {
  61. ImGui.PushFont(UiBuilder.IconFont);
  62. string? text = icon.ToIconString();
  63. if (id != null) {
  64. text += $"##{id}";
  65. }
  66. bool result = ImGui.Button(text);
  67. ImGui.PopFont();
  68. return result;
  69. }
  70. /// <summary>
  71. /// Renders a button with an icon.
  72. /// </summary>
  73. /// <param name="icon">Desired <see cref="FontAwesomeIcon"/> to be rendered.</param>
  74. /// <returns>True if pressed.</returns>
  75. public static bool IconButton(FontAwesomeIcon icon) => IconButton(icon, null);
  76. /// <summary>
  77. /// Draws horizontally-centered text in an ImGui window.
  78. /// </summary>
  79. /// <param name="text">The text to draw. Should be a single line.</param>
  80. // I hate centering text in ImGui
  81. public static void DrawCenteredText(string text) {
  82. float width = ImGui.CalcTextSize(text).X;
  83. float space = ImGui.GetContentRegionAvail().X;
  84. ImGui.SetCursorPosX((space / 2) - (width / 2));
  85. ImGui.Text(text);
  86. }
  87. /// <summary>
  88. /// Draw a tooltip with simple text content, using the specified text wrapping position.
  89. /// </summary>
  90. /// <param name="maxWidth">The textwrap position for the tooltip text.</param>
  91. /// <param name="text">The text of the tooltip.</param>
  92. public static void DrawWrappedTooltip(float maxWidth, string text) {
  93. ImGui.BeginTooltip();
  94. ImGui.PushTextWrapPos(maxWidth);
  95. ImGui.Text(text);
  96. ImGui.PopTextWrapPos();
  97. ImGui.EndTooltip();
  98. }
  99. }