Interface.cs 7.8 KB

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