Interface.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. {
  60. var treeOpen = ImGui.TreeNodeEx(entry.Key.Value, ImGuiTreeNodeFlags.AllowItemOverlap);
  61. ImGui.SameLine();
  62. var killedCount = entry.Value.Count(x =>
  63. this._plugin.MobHuntStruct->CurrentKills[x.CurrentKillsOffset] == x.NeededKills);
  64. if (killedCount != entry.Value.Count)
  65. {
  66. ImGui.Text($"({killedCount}/{entry.Value.Count})");
  67. }
  68. else
  69. {
  70. ImGui.TextColored(new Vector4(0f, 1f, 0f, 1f),
  71. $"({killedCount}/{entry.Value.Count})");
  72. }
  73. return treeOpen;
  74. }))
  75. {
  76. ImGui.Indent();
  77. foreach (var mobHuntEntry in entry.Value)
  78. {
  79. if (Location.Database.ContainsKey(mobHuntEntry.MobHuntId))
  80. {
  81. if (Interface.IconButton(FontAwesomeIcon.MapMarked, $"##{mobHuntEntry.MobHuntId}"))
  82. {
  83. Location.OpenMapLink(mobHuntEntry.TerritoryType, mobHuntEntry.MapId,
  84. mobHuntEntry.MobHuntId);
  85. }
  86. if (ImGui.IsItemHovered())
  87. {
  88. ImGui.BeginTooltip();
  89. ImGui.Text("Show hunt location on the map");
  90. ImGui.EndTooltip();
  91. }
  92. ImGui.SameLine();
  93. if (Plugin.TeleportConsumer?.Subscribed == true)
  94. {
  95. if (Interface.IconButton(FontAwesomeIcon.StreetView, $"t##{mobHuntEntry.MobHuntId}"))
  96. {
  97. Location.TeleportToNearestAetheryte(mobHuntEntry.TerritoryType, mobHuntEntry.MapId,
  98. mobHuntEntry.MobHuntId);
  99. }
  100. if (ImGui.IsItemHovered())
  101. {
  102. ImGui.BeginTooltip();
  103. ImGui.Text("Teleport to nearest aetheryte");
  104. ImGui.EndTooltip();
  105. }
  106. ImGui.SameLine();
  107. }
  108. }
  109. var currentKills = this._plugin.MobHuntStruct->CurrentKills[mobHuntEntry.CurrentKillsOffset];
  110. ImGui.Text(mobHuntEntry.Name);
  111. if (ImGui.IsItemHovered())
  112. {
  113. ImGui.PushStyleColor(ImGuiCol.PopupBg, Vector4.Zero);
  114. ImGui.BeginTooltip();
  115. var imageSize = 256f * fontGlobalScale;
  116. var cursorPos = ImGui.GetCursorScreenPos();
  117. ImGui.InvisibleButton("canvas", new Vector2(imageSize));
  118. var drawList = ImGui.GetWindowDrawList();
  119. if (mobHuntEntry.ExpansionId == 4 &&
  120. mobHuntEntry.MobHuntType == 1) // Endwalker uses circle for non elite mobs
  121. {
  122. drawList.AddCircleFilled(cursorPos + new Vector2(imageSize / 2f), imageSize / 2f,
  123. this._plugin.Configuration.IconBackgroundColourU32);
  124. }
  125. else
  126. {
  127. drawList.AddRectFilled(cursorPos, cursorPos + new Vector2(imageSize), this._plugin.Configuration.IconBackgroundColourU32);
  128. }
  129. drawList.AddImage(mobHuntEntry.Icon.ImGuiHandle, cursorPos,
  130. new Vector2(cursorPos.X + imageSize, cursorPos.Y + imageSize));
  131. ImGui.PopStyleColor();
  132. ImGui.EndTooltip();
  133. }
  134. ImGui.SameLine();
  135. if (currentKills != mobHuntEntry.NeededKills)
  136. {
  137. ImGui.Text($"({currentKills}/{mobHuntEntry.NeededKills})");
  138. }
  139. else
  140. {
  141. ImGui.TextColored(new Vector4(0f, 1f, 0f, 1f),
  142. $"({currentKills}/{mobHuntEntry.NeededKills})");
  143. }
  144. }
  145. ImGui.Unindent();
  146. ImGui.TreePop();
  147. }
  148. ImGui.TreePop();
  149. }
  150. ImGui.End();
  151. if (this._drawConfigurationInterface)
  152. {
  153. this.DrawConfiguration();
  154. }
  155. return draw;
  156. }
  157. public unsafe void DrawLocalHunts()
  158. {
  159. if (!this._plugin.Configuration.ShowLocalHunts ||
  160. this._plugin.CurrentAreaMobHuntEntries.IsEmpty ||
  161. this._plugin.CurrentAreaMobHuntEntries.Count(x =>
  162. this._plugin.MobHuntStruct->CurrentKills[x.CurrentKillsOffset] == x.NeededKills) ==
  163. this._plugin.CurrentAreaMobHuntEntries.Count)
  164. {
  165. return;
  166. }
  167. var fontGlobalScale = ImGui.GetIO().FontGlobalScale;
  168. ImGui.SetNextWindowSize(Vector2.Zero, ImGuiCond.Always);
  169. var windowFlags = ImGuiWindowFlags.NoNavInputs | ImGuiWindowFlags.NoDocking;
  170. if (this._plugin.Configuration.HideLocalHuntBackground)
  171. {
  172. windowFlags |= ImGuiWindowFlags.NoBackground;
  173. }
  174. if (!ImGui.Begin("Hunts in current area", windowFlags))
  175. {
  176. return;
  177. }
  178. foreach (var mobHuntEntry in this._plugin.CurrentAreaMobHuntEntries)
  179. {
  180. var currentKills = this._plugin.MobHuntStruct->CurrentKills[mobHuntEntry.CurrentKillsOffset];
  181. if (this._plugin.Configuration.HideCompletedHunts && currentKills == mobHuntEntry.NeededKills)
  182. {
  183. continue;
  184. }
  185. if (Location.Database.ContainsKey(mobHuntEntry.MobHuntId))
  186. {
  187. if (Interface.IconButton(FontAwesomeIcon.MapMarked, $"##{mobHuntEntry.MobHuntId}"))
  188. {
  189. Location.OpenMapLink(mobHuntEntry.TerritoryType, mobHuntEntry.MapId,
  190. mobHuntEntry.MobHuntId);
  191. }
  192. ImGui.SameLine();
  193. }
  194. ImGui.Text($"{mobHuntEntry.Name} ({currentKills}/{mobHuntEntry.NeededKills})");
  195. if (!this._plugin.Configuration.ShowLocalHuntIcons)
  196. {
  197. continue;
  198. }
  199. var imageSize = 128f * fontGlobalScale;
  200. ImGui.SetCursorPosX(ImGui.GetWindowWidth() / 2f * fontGlobalScale - imageSize / 2f);
  201. var cursorPos = ImGui.GetCursorScreenPos();
  202. ImGui.InvisibleButton("canvas", new Vector2(imageSize));
  203. var drawList = ImGui.GetWindowDrawList();
  204. if (mobHuntEntry.ExpansionId == 4 &&
  205. mobHuntEntry.MobHuntType == 1) // Endwalker uses circle for non elite mobs
  206. {
  207. drawList.AddCircleFilled(cursorPos + new Vector2(imageSize / 2f), imageSize / 2f, this._plugin.Configuration.IconBackgroundColourU32);
  208. }
  209. else
  210. {
  211. drawList.AddRectFilled(cursorPos, cursorPos + new Vector2(imageSize), this._plugin.Configuration.IconBackgroundColourU32);
  212. }
  213. drawList.AddImage(mobHuntEntry.Icon.ImGuiHandle, cursorPos,
  214. new Vector2(cursorPos.X + imageSize, cursorPos.Y + imageSize));
  215. }
  216. ImGui.End();
  217. }
  218. private void DrawConfiguration()
  219. {
  220. ImGui.SetNextWindowSize(Vector2.Zero, ImGuiCond.Always);
  221. if (!ImGui.Begin($"{this._plugin.Name} settings", ImGuiWindowFlags.NoDocking))
  222. {
  223. return;
  224. }
  225. var save = false;
  226. save |= ImGui.Checkbox("Show hunts in local area", ref this._plugin.Configuration.ShowLocalHunts);
  227. save |= ImGui.Checkbox("Show icons of hunts in local area", ref this._plugin.Configuration.ShowLocalHuntIcons);
  228. save |= ImGui.Checkbox("Hide background of local hunts window",
  229. ref this._plugin.Configuration.HideLocalHuntBackground);
  230. save |= ImGui.Checkbox("Hide completed targets in local hunts window",
  231. ref this._plugin.Configuration.HideCompletedHunts);
  232. if (ImGui.ColorEdit4("Hunt icon background colour", ref this._plugin.Configuration.IconBackgroundColour))
  233. {
  234. this._plugin.Configuration.IconBackgroundColourU32 =
  235. ImGui.ColorConvertFloat4ToU32(this._plugin.Configuration.IconBackgroundColour);
  236. save = true;
  237. }
  238. if (save)
  239. {
  240. this._plugin.Configuration.Save();
  241. }
  242. ImGui.End();
  243. }
  244. private static bool IconButton(FontAwesomeIcon icon, string? id = null)
  245. {
  246. ImGui.PushFont(UiBuilder.IconFont);
  247. var text = icon.ToIconString();
  248. if (id != null)
  249. {
  250. text += $"##{id}";
  251. }
  252. var result = ImGui.Button(text);
  253. ImGui.PopFont();
  254. return result;
  255. }
  256. }
  257. }