Interface.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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.MapMarkerAlt, $"pin##{mobHuntEntry.MobHuntId}"))
  81. {
  82. Location.CreateMapMarker(
  83. mobHuntEntry.TerritoryType,
  84. mobHuntEntry.MapId,
  85. mobHuntEntry.MobHuntId,
  86. mobHuntEntry.Name,
  87. Location.OpenType.None);
  88. }
  89. if (ImGui.IsItemHovered())
  90. {
  91. ImGui.BeginTooltip();
  92. ImGui.Text("Place marker on the map");
  93. ImGui.EndTooltip();
  94. }
  95. ImGui.SameLine();
  96. if (Interface.IconButton(FontAwesomeIcon.Compass, $"openRadius##{mobHuntEntry.MobHuntId}"))
  97. {
  98. Location.CreateMapMarker(
  99. mobHuntEntry.TerritoryType,
  100. mobHuntEntry.MapId,
  101. mobHuntEntry.MobHuntId,
  102. mobHuntEntry.Name,
  103. Location.OpenType.ShowOpen);
  104. }
  105. if (ImGui.IsItemHovered())
  106. {
  107. ImGui.BeginTooltip();
  108. ImGui.Text("Show hunt area on the map");
  109. ImGui.EndTooltip();
  110. }
  111. ImGui.SameLine();
  112. if (Interface.IconButton(FontAwesomeIcon.MapMarkedAlt, $"open##{mobHuntEntry.MobHuntId}"))
  113. {
  114. Location.CreateMapMarker(
  115. mobHuntEntry.TerritoryType,
  116. mobHuntEntry.MapId,
  117. mobHuntEntry.MobHuntId,
  118. mobHuntEntry.Name);
  119. }
  120. if (ImGui.IsItemHovered())
  121. {
  122. ImGui.BeginTooltip();
  123. ImGui.Text("Show hunt location on the map");
  124. ImGui.EndTooltip();
  125. }
  126. ImGui.SameLine();
  127. if (Plugin.TeleportConsumer?.Subscribed == true)
  128. {
  129. if (Interface.IconButton(FontAwesomeIcon.StreetView, $"t##{mobHuntEntry.MobHuntId}"))
  130. {
  131. Location.TeleportToNearestAetheryte(
  132. mobHuntEntry.TerritoryType,
  133. mobHuntEntry.MapId,
  134. mobHuntEntry.MobHuntId);
  135. }
  136. if (ImGui.IsItemHovered())
  137. {
  138. ImGui.BeginTooltip();
  139. ImGui.Text("Teleport to nearest aetheryte");
  140. ImGui.EndTooltip();
  141. }
  142. ImGui.SameLine();
  143. }
  144. }
  145. var currentKills = this.plugin.MobHuntStruct->CurrentKills[mobHuntEntry.CurrentKillsOffset];
  146. ImGui.Text(mobHuntEntry.Name);
  147. if (ImGui.IsItemHovered())
  148. {
  149. ImGui.PushStyleColor(ImGuiCol.PopupBg, Vector4.Zero);
  150. ImGui.BeginTooltip();
  151. this.DrawHuntIcon(mobHuntEntry);
  152. ImGui.PopStyleColor();
  153. ImGui.EndTooltip();
  154. }
  155. ImGui.SameLine();
  156. if (currentKills != mobHuntEntry.NeededKills)
  157. {
  158. ImGui.Text($"({currentKills}/{mobHuntEntry.NeededKills})");
  159. }
  160. else
  161. {
  162. ImGui.TextColored(
  163. new Vector4(0f, 1f, 0f, 1f),
  164. $"({currentKills}/{mobHuntEntry.NeededKills})");
  165. }
  166. }
  167. ImGui.Unindent();
  168. ImGui.TreePop();
  169. }
  170. ImGui.TreePop();
  171. }
  172. ImGui.End();
  173. if (this.drawConfigurationInterface)
  174. {
  175. this.DrawConfiguration();
  176. }
  177. return draw;
  178. }
  179. public unsafe void DrawLocalHunts()
  180. {
  181. if (!this.plugin.Configuration.ShowLocalHunts ||
  182. this.plugin.CurrentAreaMobHuntEntries.IsEmpty ||
  183. this.plugin.CurrentAreaMobHuntEntries.Count(
  184. x =>
  185. this.plugin.MobHuntStruct->CurrentKills[x.CurrentKillsOffset] == x.NeededKills) ==
  186. this.plugin.CurrentAreaMobHuntEntries.Count)
  187. {
  188. return;
  189. }
  190. ImGui.SetNextWindowSize(Vector2.Zero, ImGuiCond.Always);
  191. var windowFlags = ImGuiWindowFlags.NoNavInputs | ImGuiWindowFlags.NoDocking;
  192. if (this.plugin.Configuration.HideLocalHuntBackground)
  193. {
  194. windowFlags |= ImGuiWindowFlags.NoBackground;
  195. }
  196. if (!ImGui.Begin("Hunts in current area", windowFlags))
  197. {
  198. return;
  199. }
  200. foreach (var mobHuntEntry in this.plugin.CurrentAreaMobHuntEntries)
  201. {
  202. var currentKills = this.plugin.MobHuntStruct->CurrentKills[mobHuntEntry.CurrentKillsOffset];
  203. if (this.plugin.Configuration.HideCompletedHunts && currentKills == mobHuntEntry.NeededKills)
  204. {
  205. continue;
  206. }
  207. if (Location.Database.ContainsKey(mobHuntEntry.MobHuntId))
  208. {
  209. if (Interface.IconButton(FontAwesomeIcon.MapMarkerAlt, $"pin##{mobHuntEntry.MobHuntId}"))
  210. {
  211. Location.CreateMapMarker(
  212. mobHuntEntry.TerritoryType,
  213. mobHuntEntry.MapId,
  214. mobHuntEntry.MobHuntId,
  215. mobHuntEntry.Name,
  216. Location.OpenType.None);
  217. }
  218. if (ImGui.IsItemHovered())
  219. {
  220. ImGui.BeginTooltip();
  221. ImGui.Text("Place marker on the map");
  222. ImGui.EndTooltip();
  223. }
  224. ImGui.SameLine();
  225. if (Interface.IconButton(FontAwesomeIcon.Compass, $"openRadius##{mobHuntEntry.MobHuntId}"))
  226. {
  227. Location.CreateMapMarker(
  228. mobHuntEntry.TerritoryType,
  229. mobHuntEntry.MapId,
  230. mobHuntEntry.MobHuntId,
  231. mobHuntEntry.Name,
  232. Location.OpenType.ShowOpen);
  233. }
  234. if (ImGui.IsItemHovered())
  235. {
  236. ImGui.BeginTooltip();
  237. ImGui.Text("Show hunt area on the map");
  238. ImGui.EndTooltip();
  239. }
  240. ImGui.SameLine();
  241. if (Interface.IconButton(FontAwesomeIcon.MapMarkedAlt, $"open##{mobHuntEntry.MobHuntId}"))
  242. {
  243. Location.CreateMapMarker(
  244. mobHuntEntry.TerritoryType,
  245. mobHuntEntry.MapId,
  246. mobHuntEntry.MobHuntId,
  247. mobHuntEntry.Name);
  248. }
  249. if (ImGui.IsItemHovered())
  250. {
  251. ImGui.BeginTooltip();
  252. ImGui.Text("Show hunt location on the map");
  253. ImGui.EndTooltip();
  254. }
  255. ImGui.SameLine();
  256. }
  257. ImGui.Text($"{mobHuntEntry.Name} ({currentKills}/{mobHuntEntry.NeededKills})");
  258. if (!this.plugin.Configuration.ShowLocalHuntIcons)
  259. {
  260. continue;
  261. }
  262. this.DrawHuntIcon(mobHuntEntry);
  263. }
  264. ImGui.End();
  265. }
  266. private void DrawConfiguration()
  267. {
  268. ImGui.SetNextWindowSize(Vector2.Zero, ImGuiCond.Always);
  269. if (!ImGui.Begin($"{this.plugin.Name} settings", ImGuiWindowFlags.NoDocking))
  270. {
  271. return;
  272. }
  273. var save = false;
  274. save |= ImGui.Checkbox("Show hunts in local area", ref this.plugin.Configuration.ShowLocalHunts);
  275. save |= ImGui.Checkbox(
  276. "Show icons of hunts in local area",
  277. ref this.plugin.Configuration.ShowLocalHuntIcons);
  278. save |= ImGui.Checkbox(
  279. "Hide background of local hunts window",
  280. ref this.plugin.Configuration.HideLocalHuntBackground);
  281. save |= ImGui.Checkbox(
  282. "Hide completed targets in local hunts window",
  283. ref this.plugin.Configuration.HideCompletedHunts);
  284. save |= ImGui.SliderFloat("Hunt icon scale", ref this.plugin.Configuration.IconScale, 0.2f, 2f, "%.2f");
  285. if (ImGui.ColorEdit4("Hunt icon background colour", ref this.plugin.Configuration.IconBackgroundColour))
  286. {
  287. this.plugin.Configuration.IconBackgroundColourU32 =
  288. ImGui.ColorConvertFloat4ToU32(this.plugin.Configuration.IconBackgroundColour);
  289. save = true;
  290. }
  291. if (save)
  292. {
  293. this.plugin.Configuration.Save();
  294. }
  295. ImGui.End();
  296. }
  297. private static bool IconButton(FontAwesomeIcon icon, string? id = null)
  298. {
  299. ImGui.PushFont(UiBuilder.IconFont);
  300. var text = icon.ToIconString();
  301. if (id != null)
  302. {
  303. text += $"##{id}";
  304. }
  305. var result = ImGui.Button(text);
  306. ImGui.PopFont();
  307. return result;
  308. }
  309. private void DrawHuntIcon(MobHuntEntry mobHuntEntry)
  310. {
  311. var cursorPos = ImGui.GetCursorScreenPos();
  312. var imageSize = mobHuntEntry.ExpansionId < 3 ? new Vector2(192f, 128f) : new Vector2(210f);
  313. imageSize *= ImGui.GetIO().FontGlobalScale * this.plugin.Configuration.IconScale;
  314. ImGui.InvisibleButton("canvas", imageSize);
  315. var drawList = ImGui.GetWindowDrawList();
  316. if (mobHuntEntry is { ExpansionId: 4, IsEliteMark: false }) // Endwalker uses circle for non elite mobs
  317. {
  318. drawList.AddCircleFilled(
  319. cursorPos + (imageSize / 2f),
  320. imageSize.X / 2f,
  321. this.plugin.Configuration.IconBackgroundColourU32);
  322. }
  323. else
  324. {
  325. drawList.AddRectFilled(
  326. cursorPos,
  327. cursorPos + imageSize,
  328. this.plugin.Configuration.IconBackgroundColourU32);
  329. }
  330. drawList.AddImage(mobHuntEntry.Icon.ImGuiHandle, cursorPos, cursorPos + imageSize);
  331. }
  332. }
  333. }