Interface.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System.Linq;
  2. using System.Numerics;
  3. using System.Threading.Tasks;
  4. using Dalamud.Interface;
  5. using ImGuiNET;
  6. namespace HuntBuddy
  7. {
  8. public class Interface
  9. {
  10. private readonly Plugin plugin;
  11. public bool DrawInterface;
  12. private bool drawConfigurationInterface;
  13. public Interface(Plugin plugin)
  14. {
  15. this.plugin = plugin;
  16. }
  17. public unsafe bool Draw()
  18. {
  19. var draw = true;
  20. ImGui.SetNextWindowSize(new Vector2(400 * ImGui.GetIO().FontGlobalScale, 500), ImGuiCond.Once);
  21. if (!ImGui.Begin($"{this.plugin.Name}", ref draw, ImGuiWindowFlags.NoDocking))
  22. {
  23. return draw;
  24. }
  25. if (!this.plugin.MobHuntEntriesReady)
  26. {
  27. ImGui.Text("Reloading data ...");
  28. ImGui.End();
  29. return draw;
  30. }
  31. if (Interface.IconButton(FontAwesomeIcon.Redo, "Reload"))
  32. {
  33. ImGui.End();
  34. this.plugin.MobHuntEntriesReady = false;
  35. Task.Run(() => this.plugin.ReloadData());
  36. return draw;
  37. }
  38. if (ImGui.IsItemHovered())
  39. {
  40. ImGui.BeginTooltip();
  41. ImGui.Text("Click this button to reload daily hunt data");
  42. ImGui.EndTooltip();
  43. }
  44. ImGui.SameLine();
  45. if (Interface.IconButton(FontAwesomeIcon.Cog, "Config"))
  46. {
  47. this.drawConfigurationInterface = !this.drawConfigurationInterface;
  48. }
  49. foreach (var expansionEntry in this.plugin.MobHuntEntries.Where(
  50. expansionEntry =>
  51. ImGui.TreeNode(expansionEntry.Key)))
  52. {
  53. foreach (var entry in expansionEntry.Value.Where(
  54. entry =>
  55. {
  56. var treeOpen = ImGui.TreeNodeEx(entry.Key.Value, ImGuiTreeNodeFlags.AllowItemOverlap);
  57. ImGui.SameLine();
  58. var killedCount = entry.Value.Count(
  59. x =>
  60. this.plugin.MobHuntStruct->CurrentKills[x.CurrentKillsOffset] ==
  61. x.NeededKills);
  62. if (killedCount != entry.Value.Count)
  63. {
  64. ImGui.Text($"({killedCount}/{entry.Value.Count})");
  65. }
  66. else
  67. {
  68. ImGui.TextColored(
  69. new Vector4(0f, 1f, 0f, 1f),
  70. $"({killedCount}/{entry.Value.Count})");
  71. }
  72. return treeOpen;
  73. }))
  74. {
  75. ImGui.Indent();
  76. foreach (var mobHuntEntry in entry.Value)
  77. {
  78. if (Location.Database.ContainsKey(mobHuntEntry.MobHuntId))
  79. {
  80. if (Interface.IconButton(FontAwesomeIcon.MapMarked, $"##{mobHuntEntry.MobHuntId}"))
  81. {
  82. Location.OpenMapLink(
  83. mobHuntEntry.TerritoryType,
  84. mobHuntEntry.MapId,
  85. mobHuntEntry.MobHuntId);
  86. }
  87. if (ImGui.IsItemHovered())
  88. {
  89. ImGui.BeginTooltip();
  90. ImGui.Text("Show hunt location on the map");
  91. ImGui.EndTooltip();
  92. }
  93. ImGui.SameLine();
  94. if (Plugin.TeleportConsumer?.Subscribed == true)
  95. {
  96. if (Interface.IconButton(FontAwesomeIcon.StreetView, $"t##{mobHuntEntry.MobHuntId}"))
  97. {
  98. Location.TeleportToNearestAetheryte(
  99. mobHuntEntry.TerritoryType,
  100. mobHuntEntry.MapId,
  101. mobHuntEntry.MobHuntId);
  102. }
  103. if (ImGui.IsItemHovered())
  104. {
  105. ImGui.BeginTooltip();
  106. ImGui.Text("Teleport to nearest aetheryte");
  107. ImGui.EndTooltip();
  108. }
  109. ImGui.SameLine();
  110. }
  111. }
  112. var currentKills = this.plugin.MobHuntStruct->CurrentKills[mobHuntEntry.CurrentKillsOffset];
  113. ImGui.Text(mobHuntEntry.Name);
  114. if (ImGui.IsItemHovered())
  115. {
  116. ImGui.PushStyleColor(ImGuiCol.PopupBg, Vector4.Zero);
  117. ImGui.BeginTooltip();
  118. this.DrawHuntIcon(mobHuntEntry);
  119. ImGui.PopStyleColor();
  120. ImGui.EndTooltip();
  121. }
  122. ImGui.SameLine();
  123. if (currentKills != mobHuntEntry.NeededKills)
  124. {
  125. ImGui.Text($"({currentKills}/{mobHuntEntry.NeededKills})");
  126. }
  127. else
  128. {
  129. ImGui.TextColored(
  130. new Vector4(0f, 1f, 0f, 1f),
  131. $"({currentKills}/{mobHuntEntry.NeededKills})");
  132. }
  133. }
  134. ImGui.Unindent();
  135. ImGui.TreePop();
  136. }
  137. ImGui.TreePop();
  138. }
  139. ImGui.End();
  140. if (this.drawConfigurationInterface)
  141. {
  142. this.DrawConfiguration();
  143. }
  144. return draw;
  145. }
  146. public unsafe void DrawLocalHunts()
  147. {
  148. if (!this.plugin.Configuration.ShowLocalHunts ||
  149. this.plugin.CurrentAreaMobHuntEntries.IsEmpty ||
  150. this.plugin.CurrentAreaMobHuntEntries.Count(
  151. x =>
  152. this.plugin.MobHuntStruct->CurrentKills[x.CurrentKillsOffset] == x.NeededKills) ==
  153. this.plugin.CurrentAreaMobHuntEntries.Count)
  154. {
  155. return;
  156. }
  157. ImGui.SetNextWindowSize(Vector2.Zero, ImGuiCond.Always);
  158. var windowFlags = ImGuiWindowFlags.NoNavInputs | ImGuiWindowFlags.NoDocking;
  159. if (this.plugin.Configuration.HideLocalHuntBackground)
  160. {
  161. windowFlags |= ImGuiWindowFlags.NoBackground;
  162. }
  163. if (!ImGui.Begin("Hunts in current area", windowFlags))
  164. {
  165. return;
  166. }
  167. foreach (var mobHuntEntry in this.plugin.CurrentAreaMobHuntEntries)
  168. {
  169. var currentKills = this.plugin.MobHuntStruct->CurrentKills[mobHuntEntry.CurrentKillsOffset];
  170. if (this.plugin.Configuration.HideCompletedHunts && currentKills == mobHuntEntry.NeededKills)
  171. {
  172. continue;
  173. }
  174. if (Location.Database.ContainsKey(mobHuntEntry.MobHuntId))
  175. {
  176. if (Interface.IconButton(FontAwesomeIcon.MapMarked, $"##{mobHuntEntry.MobHuntId}"))
  177. {
  178. Location.OpenMapLink(
  179. mobHuntEntry.TerritoryType,
  180. mobHuntEntry.MapId,
  181. mobHuntEntry.MobHuntId);
  182. }
  183. ImGui.SameLine();
  184. }
  185. ImGui.Text($"{mobHuntEntry.Name} ({currentKills}/{mobHuntEntry.NeededKills})");
  186. if (!this.plugin.Configuration.ShowLocalHuntIcons)
  187. {
  188. continue;
  189. }
  190. this.DrawHuntIcon(mobHuntEntry);
  191. }
  192. ImGui.End();
  193. }
  194. private void DrawConfiguration()
  195. {
  196. ImGui.SetNextWindowSize(Vector2.Zero, ImGuiCond.Always);
  197. if (!ImGui.Begin($"{this.plugin.Name} settings", ImGuiWindowFlags.NoDocking))
  198. {
  199. return;
  200. }
  201. var save = false;
  202. save |= ImGui.Checkbox("Show hunts in local area", ref this.plugin.Configuration.ShowLocalHunts);
  203. save |= ImGui.Checkbox(
  204. "Show icons of hunts in local area",
  205. ref this.plugin.Configuration.ShowLocalHuntIcons);
  206. save |= ImGui.Checkbox(
  207. "Hide background of local hunts window",
  208. ref this.plugin.Configuration.HideLocalHuntBackground);
  209. save |= ImGui.Checkbox(
  210. "Hide completed targets in local hunts window",
  211. ref this.plugin.Configuration.HideCompletedHunts);
  212. save |= ImGui.SliderFloat("Hunt icon scale", ref this.plugin.Configuration.IconScale, 0.2f, 2f, "%.2f");
  213. if (ImGui.ColorEdit4("Hunt icon background colour", ref this.plugin.Configuration.IconBackgroundColour))
  214. {
  215. this.plugin.Configuration.IconBackgroundColourU32 =
  216. ImGui.ColorConvertFloat4ToU32(this.plugin.Configuration.IconBackgroundColour);
  217. save = true;
  218. }
  219. if (save)
  220. {
  221. this.plugin.Configuration.Save();
  222. }
  223. ImGui.End();
  224. }
  225. private static bool IconButton(FontAwesomeIcon icon, string? id = null)
  226. {
  227. ImGui.PushFont(UiBuilder.IconFont);
  228. var text = icon.ToIconString();
  229. if (id != null)
  230. {
  231. text += $"##{id}";
  232. }
  233. var result = ImGui.Button(text);
  234. ImGui.PopFont();
  235. return result;
  236. }
  237. private void DrawHuntIcon(MobHuntEntry mobHuntEntry)
  238. {
  239. var cursorPos = ImGui.GetCursorScreenPos();
  240. var imageSize = mobHuntEntry.ExpansionId < 3 ? new Vector2(192f, 128f) : new Vector2(210f);
  241. imageSize *= ImGui.GetIO().FontGlobalScale * this.plugin.Configuration.IconScale;
  242. ImGui.InvisibleButton("canvas", imageSize);
  243. var drawList = ImGui.GetWindowDrawList();
  244. if (mobHuntEntry.ExpansionId == 4 && !mobHuntEntry.IsEliteMark) // Endwalker uses circle for non elite mobs
  245. {
  246. drawList.AddCircleFilled(
  247. cursorPos + (imageSize / 2f),
  248. imageSize.X / 2f,
  249. this.plugin.Configuration.IconBackgroundColourU32);
  250. }
  251. else
  252. {
  253. drawList.AddRectFilled(
  254. cursorPos,
  255. cursorPos + imageSize,
  256. this.plugin.Configuration.IconBackgroundColourU32);
  257. }
  258. drawList.AddImage(mobHuntEntry.Icon.ImGuiHandle, cursorPos, cursorPos + imageSize);
  259. }
  260. }
  261. }