Interface.cs 7.3 KB

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