Interface.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. this.DrawHuntIcon(mobHuntEntry);
  116. ImGui.PopStyleColor();
  117. ImGui.EndTooltip();
  118. }
  119. ImGui.SameLine();
  120. if (currentKills != mobHuntEntry.NeededKills)
  121. {
  122. ImGui.Text($"({currentKills}/{mobHuntEntry.NeededKills})");
  123. }
  124. else
  125. {
  126. ImGui.TextColored(new Vector4(0f, 1f, 0f, 1f),
  127. $"({currentKills}/{mobHuntEntry.NeededKills})");
  128. }
  129. }
  130. ImGui.Unindent();
  131. ImGui.TreePop();
  132. }
  133. ImGui.TreePop();
  134. }
  135. ImGui.End();
  136. if (this._drawConfigurationInterface)
  137. {
  138. this.DrawConfiguration();
  139. }
  140. return draw;
  141. }
  142. public unsafe void DrawLocalHunts()
  143. {
  144. if (!this._plugin.Configuration.ShowLocalHunts ||
  145. this._plugin.CurrentAreaMobHuntEntries.IsEmpty ||
  146. this._plugin.CurrentAreaMobHuntEntries.Count(x =>
  147. this._plugin.MobHuntStruct->CurrentKills[x.CurrentKillsOffset] == x.NeededKills) ==
  148. this._plugin.CurrentAreaMobHuntEntries.Count)
  149. {
  150. return;
  151. }
  152. var fontGlobalScale = ImGui.GetIO().FontGlobalScale;
  153. ImGui.SetNextWindowSize(Vector2.Zero, ImGuiCond.Always);
  154. var windowFlags = ImGuiWindowFlags.NoNavInputs | ImGuiWindowFlags.NoDocking;
  155. if (this._plugin.Configuration.HideLocalHuntBackground)
  156. {
  157. windowFlags |= ImGuiWindowFlags.NoBackground;
  158. }
  159. if (!ImGui.Begin("Hunts in current area", windowFlags))
  160. {
  161. return;
  162. }
  163. foreach (var mobHuntEntry in this._plugin.CurrentAreaMobHuntEntries)
  164. {
  165. var currentKills = this._plugin.MobHuntStruct->CurrentKills[mobHuntEntry.CurrentKillsOffset];
  166. if (this._plugin.Configuration.HideCompletedHunts && currentKills == mobHuntEntry.NeededKills)
  167. {
  168. continue;
  169. }
  170. if (Location.Database.ContainsKey(mobHuntEntry.MobHuntId))
  171. {
  172. if (Interface.IconButton(FontAwesomeIcon.MapMarked, $"##{mobHuntEntry.MobHuntId}"))
  173. {
  174. Location.OpenMapLink(mobHuntEntry.TerritoryType, mobHuntEntry.MapId,
  175. mobHuntEntry.MobHuntId);
  176. }
  177. ImGui.SameLine();
  178. }
  179. ImGui.Text($"{mobHuntEntry.Name} ({currentKills}/{mobHuntEntry.NeededKills})");
  180. if (!this._plugin.Configuration.ShowLocalHuntIcons)
  181. {
  182. continue;
  183. }
  184. this.DrawHuntIcon(mobHuntEntry);
  185. }
  186. ImGui.End();
  187. }
  188. private void DrawConfiguration()
  189. {
  190. ImGui.SetNextWindowSize(Vector2.Zero, ImGuiCond.Always);
  191. if (!ImGui.Begin($"{this._plugin.Name} settings", ImGuiWindowFlags.NoDocking))
  192. {
  193. return;
  194. }
  195. var save = false;
  196. save |= ImGui.Checkbox("Show hunts in local area", ref this._plugin.Configuration.ShowLocalHunts);
  197. save |= ImGui.Checkbox("Show icons of hunts in local area", ref this._plugin.Configuration.ShowLocalHuntIcons);
  198. save |= ImGui.Checkbox("Hide background of local hunts window",
  199. ref this._plugin.Configuration.HideLocalHuntBackground);
  200. save |= ImGui.Checkbox("Hide completed targets in local hunts window",
  201. ref this._plugin.Configuration.HideCompletedHunts);
  202. save |= ImGui.SliderFloat("Hunt icon scale", ref this._plugin.Configuration.IconScale, 0.2f, 2f, "%.2f");
  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. private void DrawHuntIcon(MobHuntEntry mobHuntEntry)
  228. {
  229. var cursorPos = ImGui.GetCursorScreenPos();
  230. var imageSize = mobHuntEntry.ExpansionId < 3 ? new Vector2(192f, 128f) : new Vector2(210f);
  231. imageSize *= ImGui.GetIO().FontGlobalScale * this._plugin.Configuration.IconScale;
  232. ImGui.InvisibleButton("canvas", imageSize);
  233. var drawList = ImGui.GetWindowDrawList();
  234. if (mobHuntEntry.ExpansionId == 4 && !mobHuntEntry.IsEliteMark) // Endwalker uses circle for non elite mobs
  235. {
  236. drawList.AddCircleFilled(cursorPos + imageSize / 2f, imageSize.X / 2f,
  237. this._plugin.Configuration.IconBackgroundColourU32);
  238. }
  239. else
  240. {
  241. drawList.AddRectFilled(cursorPos, cursorPos + imageSize,
  242. this._plugin.Configuration.IconBackgroundColourU32);
  243. }
  244. drawList.AddImage(mobHuntEntry.Icon.ImGuiHandle, cursorPos, cursorPos + imageSize);
  245. }
  246. }
  247. }