Interface.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Linq;
  3. using System.Numerics;
  4. using System.Threading.Tasks;
  5. using Dalamud.Game.Text.SeStringHandling.Payloads;
  6. using Dalamud.Interface;
  7. using Dalamud.Logging;
  8. using HuntBuddy.Structs;
  9. using ImGuiNET;
  10. using Lumina.Excel.GeneratedSheets;
  11. namespace HuntBuddy
  12. {
  13. public class Interface
  14. {
  15. private readonly Plugin _plugin;
  16. public bool DrawInterface;
  17. private bool _drawConfigurationInterface;
  18. public Interface(Plugin plugin)
  19. {
  20. _plugin = plugin;
  21. }
  22. public unsafe bool Draw()
  23. {
  24. var draw = true;
  25. var fontGlobalScale = ImGui.GetIO().FontGlobalScale;
  26. ImGui.SetNextWindowSize(new Vector2(400 * ImGui.GetIO().FontGlobalScale, 500), ImGuiCond.Once);
  27. if (!ImGui.Begin($"{_plugin.Name}", ref draw, ImGuiWindowFlags.NoDocking))
  28. {
  29. return draw;
  30. }
  31. if (!_plugin.MobHuntEntriesReady)
  32. {
  33. ImGui.Text("Reloading data ...");
  34. ImGui.End();
  35. return draw;
  36. }
  37. if (IconButton(FontAwesomeIcon.Redo, "Reload"))
  38. {
  39. ImGui.End();
  40. _plugin.MobHuntEntriesReady = false;
  41. Task.Run(() => _plugin.ReloadData());
  42. return draw;
  43. }
  44. if (ImGui.IsItemHovered())
  45. {
  46. ImGui.BeginTooltip();
  47. ImGui.Text("Click this button to reload daily hunt data");
  48. ImGui.EndTooltip();
  49. }
  50. ImGui.SameLine();
  51. if (IconButton(FontAwesomeIcon.Cog, "Config"))
  52. {
  53. this._drawConfigurationInterface = !this._drawConfigurationInterface;
  54. }
  55. foreach (var expansionEntry in _plugin.MobHuntEntries.Where(expansionEntry =>
  56. ImGui.TreeNode(expansionEntry.Key)))
  57. {
  58. foreach (var entry in expansionEntry.Value.Where(entry =>
  59. ImGui.TreeNode(
  60. $"{entry.Key.Value} ({entry.Value.Count(x => _plugin.MobHuntStruct->CurrentKills[x.CurrentKillsOffset] == x.NeededKills)}/{entry.Value.Count})")))
  61. {
  62. ImGui.Indent();
  63. foreach (var mobHuntEntry in entry.Value)
  64. {
  65. if (Location.Database.ContainsKey(mobHuntEntry.MobHuntId))
  66. {
  67. if (IconButton(FontAwesomeIcon.Search, $"##{mobHuntEntry.MobHuntId}"))
  68. {
  69. Location.OpenMapLink(mobHuntEntry.TerritoryType, mobHuntEntry.MapId,
  70. mobHuntEntry.MobHuntId);
  71. }
  72. ImGui.SameLine();
  73. }
  74. var currentKills = _plugin.MobHuntStruct->CurrentKills[mobHuntEntry.CurrentKillsOffset];
  75. ImGui.Text(mobHuntEntry.Name);
  76. if (ImGui.IsItemHovered())
  77. {
  78. ImGui.PushStyleColor(ImGuiCol.PopupBg, Vector4.Zero);
  79. ImGui.BeginTooltip();
  80. var imageSize = 256f * fontGlobalScale;
  81. var cursorPos = ImGui.GetCursorScreenPos();
  82. ImGui.InvisibleButton("canvas", new Vector2(imageSize));
  83. var drawList = ImGui.GetWindowDrawList();
  84. if (mobHuntEntry.ExpansionId == 4 &&
  85. mobHuntEntry.MobHuntType == 1) // Endwalker uses circle for non elite mobs
  86. {
  87. drawList.AddCircleFilled(cursorPos + new Vector2(imageSize / 2f), imageSize / 2f,
  88. _plugin.Configuration.IconBackgroundColourU32);
  89. }
  90. else
  91. {
  92. drawList.AddRectFilled(cursorPos, cursorPos + new Vector2(imageSize), _plugin.Configuration.IconBackgroundColourU32);
  93. }
  94. drawList.AddImage(mobHuntEntry.Icon.ImGuiHandle, cursorPos,
  95. new Vector2(cursorPos.X + imageSize, cursorPos.Y + imageSize));
  96. ImGui.PopStyleColor();
  97. ImGui.EndTooltip();
  98. }
  99. ImGui.SameLine();
  100. if (currentKills != mobHuntEntry.NeededKills)
  101. {
  102. ImGui.Text($"({currentKills}/{mobHuntEntry.NeededKills})");
  103. }
  104. else
  105. {
  106. ImGui.TextColored(new Vector4(0f, 1f, 0f, 1f),
  107. $"({currentKills}/{mobHuntEntry.NeededKills})");
  108. }
  109. }
  110. ImGui.Unindent();
  111. ImGui.TreePop();
  112. }
  113. ImGui.TreePop();
  114. }
  115. ImGui.End();
  116. if (_drawConfigurationInterface)
  117. {
  118. this.DrawConfiguration();
  119. }
  120. return draw;
  121. }
  122. public unsafe void DrawLocalHunts()
  123. {
  124. if (!_plugin.Configuration.ShowLocalHunts ||
  125. _plugin.CurrentAreaMobHuntEntries.IsEmpty ||
  126. _plugin.CurrentAreaMobHuntEntries.Count(x =>
  127. _plugin.MobHuntStruct->CurrentKills[x.CurrentKillsOffset] == x.NeededKills) ==
  128. _plugin.CurrentAreaMobHuntEntries.Count)
  129. {
  130. return;
  131. }
  132. var fontGlobalScale = ImGui.GetIO().FontGlobalScale;
  133. ImGui.SetNextWindowSize(Vector2.Zero, ImGuiCond.Always);
  134. var windowFlags = ImGuiWindowFlags.NoNavInputs | ImGuiWindowFlags.NoDocking;
  135. if (_plugin.Configuration.HideLocalHuntBackground)
  136. {
  137. windowFlags |= ImGuiWindowFlags.NoBackground;
  138. }
  139. if (!ImGui.Begin("Hunts in current area", windowFlags))
  140. {
  141. return;
  142. }
  143. foreach (var mobHuntEntry in _plugin.CurrentAreaMobHuntEntries)
  144. {
  145. var currentKills = _plugin.MobHuntStruct->CurrentKills[mobHuntEntry.CurrentKillsOffset];
  146. if (_plugin.Configuration.HideCompletedHunts && currentKills == mobHuntEntry.NeededKills)
  147. {
  148. continue;
  149. }
  150. if (Location.Database.ContainsKey(mobHuntEntry.MobHuntId))
  151. {
  152. if (IconButton(FontAwesomeIcon.Search, $"##{mobHuntEntry.MobHuntId}"))
  153. {
  154. Location.OpenMapLink(mobHuntEntry.TerritoryType, mobHuntEntry.MapId,
  155. mobHuntEntry.MobHuntId);
  156. }
  157. ImGui.SameLine();
  158. }
  159. ImGui.Text($"{mobHuntEntry.Name} ({currentKills}/{mobHuntEntry.NeededKills})");
  160. if (!_plugin.Configuration.ShowLocalHuntIcons)
  161. {
  162. continue;
  163. }
  164. var imageSize = 128f * fontGlobalScale;
  165. ImGui.SetCursorPosX(ImGui.GetWindowWidth() / 2f * fontGlobalScale - imageSize / 2f);
  166. var cursorPos = ImGui.GetCursorScreenPos();
  167. ImGui.InvisibleButton("canvas", new Vector2(imageSize));
  168. var drawList = ImGui.GetWindowDrawList();
  169. if (mobHuntEntry.ExpansionId == 4 &&
  170. mobHuntEntry.MobHuntType == 1) // Endwalker uses circle for non elite mobs
  171. {
  172. drawList.AddCircleFilled(cursorPos + new Vector2(imageSize / 2f), imageSize / 2f, _plugin.Configuration.IconBackgroundColourU32);
  173. }
  174. else
  175. {
  176. drawList.AddRectFilled(cursorPos, cursorPos + new Vector2(imageSize), _plugin.Configuration.IconBackgroundColourU32);
  177. }
  178. drawList.AddImage(mobHuntEntry.Icon.ImGuiHandle, cursorPos,
  179. new Vector2(cursorPos.X + imageSize, cursorPos.Y + imageSize));
  180. }
  181. ImGui.End();
  182. }
  183. private void DrawConfiguration()
  184. {
  185. ImGui.SetNextWindowSize(Vector2.Zero, ImGuiCond.Always);
  186. if (!ImGui.Begin($"{_plugin.Name} settings", ImGuiWindowFlags.NoDocking))
  187. {
  188. return;
  189. }
  190. var save = false;
  191. save |= ImGui.Checkbox("Show hunts in local area", ref _plugin.Configuration.ShowLocalHunts);
  192. save |= ImGui.Checkbox("Show icons of hunts in local area", ref _plugin.Configuration.ShowLocalHuntIcons);
  193. save |= ImGui.Checkbox("Hide background of local hunts window",
  194. ref _plugin.Configuration.HideLocalHuntBackground);
  195. save |= ImGui.Checkbox("Hide completed targets in local hunts window",
  196. ref _plugin.Configuration.HideCompletedHunts);
  197. if (ImGui.ColorEdit4("Hunt icon background colour", ref _plugin.Configuration.IconBackgroundColour))
  198. {
  199. _plugin.Configuration.IconBackgroundColourU32 =
  200. ImGui.ColorConvertFloat4ToU32(_plugin.Configuration.IconBackgroundColour);
  201. save = true;
  202. }
  203. if (save)
  204. {
  205. _plugin.Configuration.Save();
  206. }
  207. ImGui.End();
  208. }
  209. private static bool IconButton(FontAwesomeIcon icon, string? id = null)
  210. {
  211. ImGui.PushFont(UiBuilder.IconFont);
  212. var text = icon.ToIconString();
  213. if (id != null)
  214. {
  215. text += $"##{id}";
  216. }
  217. var result = ImGui.Button(text);
  218. ImGui.PopFont();
  219. return result;
  220. }
  221. }
  222. }