Interface.cs 8.2 KB

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