From df1dd701a1cefdc210c33b5dbac50bf92fde1797 Mon Sep 17 00:00:00 2001 From: Anna Date: Fri, 15 Jan 2021 20:12:46 -0500 Subject: [PATCH] feat: add commands and option to dock to pf window --- BetterPartyFinder/Commands.cs | 38 +++++++++++ BetterPartyFinder/Configuration.cs | 8 +++ BetterPartyFinder/GameFunctions.cs | 7 ++ BetterPartyFinder/Plugin.cs | 7 +- BetterPartyFinder/PluginUi.cs | 103 ++++++++++++++++++++++++++++- 5 files changed, 158 insertions(+), 5 deletions(-) create mode 100755 BetterPartyFinder/Commands.cs diff --git a/BetterPartyFinder/Commands.cs b/BetterPartyFinder/Commands.cs new file mode 100755 index 0000000..d69c9aa --- /dev/null +++ b/BetterPartyFinder/Commands.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using Dalamud.Game.Command; + +namespace BetterPartyFinder { + public class Commands : IDisposable { + private static readonly Dictionary CommandNames = new() { + ["/betterpartyfinder"] = "Opens the main interface. Use with args \"c\" or \"config\" to open the settings.", + ["/bpf"] = "Alias for /betterpartyfinder", + }; + + private Plugin Plugin { get; } + + internal Commands(Plugin plugin) { + this.Plugin = plugin; + + foreach (var name in CommandNames) { + this.Plugin.Interface.CommandManager.AddHandler(name.Key, new CommandInfo(this.OnCommand) { + HelpMessage = name.Value, + }); + } + } + + public void Dispose() { + foreach (var name in CommandNames.Keys) { + this.Plugin.Interface.CommandManager.RemoveHandler(name); + } + } + + private void OnCommand(string command, string args) { + if (args == "c" || args == "config") { + this.Plugin.Ui.SettingsVisible = !this.Plugin.Ui.SettingsVisible; + } else { + this.Plugin.Ui.Visible = !this.Plugin.Ui.Visible; + } + } + } +} diff --git a/BetterPartyFinder/Configuration.cs b/BetterPartyFinder/Configuration.cs index c134911..67b76f0 100755 --- a/BetterPartyFinder/Configuration.cs +++ b/BetterPartyFinder/Configuration.cs @@ -12,6 +12,9 @@ namespace BetterPartyFinder { public Dictionary Presets { get; } = new(); public Guid? SelectedPreset { get; set; } + public bool ShowWhenPfOpen { get; set; } + public WindowSide WindowSide { get; set; } = WindowSide.Left; + internal static Configuration? Load(Plugin plugin) { return (Configuration?) plugin.Interface.GetPluginConfig(); } @@ -115,4 +118,9 @@ namespace BetterPartyFinder { Whitelist, Blacklist, } + + public enum WindowSide { + Left, + Right, + } } diff --git a/BetterPartyFinder/GameFunctions.cs b/BetterPartyFinder/GameFunctions.cs index ac9f430..50d0d49 100755 --- a/BetterPartyFinder/GameFunctions.cs +++ b/BetterPartyFinder/GameFunctions.cs @@ -48,6 +48,13 @@ namespace BetterPartyFinder { var needToRewrite = false; + var raw = Marshal.AllocHGlobal(PacketInfo.PacketSize); + Marshal.StructureToPtr(packet, raw, false); + var bytes = new byte[PacketInfo.PacketSize]; + Marshal.Copy(raw, bytes, 0, PacketInfo.PacketSize); + PluginLog.Log(string.Join("", bytes.Select(b => b.ToString("x2")))); + Marshal.FreeHGlobal(raw); + for (var i = 0; i < packet.listings.Length; i++) { if (packet.listings[i].IsNull()) { continue; diff --git a/BetterPartyFinder/Plugin.cs b/BetterPartyFinder/Plugin.cs index c0cf6a1..40e24c3 100755 --- a/BetterPartyFinder/Plugin.cs +++ b/BetterPartyFinder/Plugin.cs @@ -8,8 +8,9 @@ namespace BetterPartyFinder { internal DalamudPluginInterface Interface { get; private set; } = null!; internal Configuration Config { get; private set; } = null!; internal GameFunctions Functions { get; private set; } = null!; - internal Filter Filter { get; set; } = null!; - private PluginUi Ui { get; set; } = null!; + private Filter Filter { get; set; } = null!; + internal PluginUi Ui { get; set; } = null!; + private Commands Commands { get; set; } = null!; public void Initialize(DalamudPluginInterface pluginInterface) { this.Interface = pluginInterface; @@ -20,12 +21,14 @@ namespace BetterPartyFinder { this.Functions = new GameFunctions(this); this.Filter = new Filter(this); this.Ui = new PluginUi(this); + this.Commands = new Commands(this); // start task to determine maximum item level (based on max chestpiece) Task.Run(() => Util.CalculateMaxItemLevel(this.Interface.Data)); } public void Dispose() { + this.Commands.Dispose(); this.Ui.Dispose(); this.Filter.Dispose(); this.Functions.Dispose(); diff --git a/BetterPartyFinder/PluginUi.cs b/BetterPartyFinder/PluginUi.cs index 95c347f..e00da2f 100755 --- a/BetterPartyFinder/PluginUi.cs +++ b/BetterPartyFinder/PluginUi.cs @@ -6,6 +6,7 @@ using Dalamud.Data; using Dalamud.Interface; using ImGuiNET; using Lumina.Excel.GeneratedSheets; +using GameAddon = Dalamud.Game.Internal.Gui.Addon.Addon; namespace BetterPartyFinder { public class PluginUi : IDisposable { @@ -23,12 +24,24 @@ namespace BetterPartyFinder { private Plugin Plugin { get; } + private bool _visible; + + public bool Visible { + get => this._visible; + set => this._visible = value; + } + + private bool _settingsVisible; + + public bool SettingsVisible { + get => this._settingsVisible; + set => this._settingsVisible = value; + } + private string DutySearchQuery { get; set; } = string.Empty; private string PresetName { get; set; } = string.Empty; - private bool[] _openSlots = new bool[8]; - internal PluginUi(Plugin plugin) { this.Plugin = plugin; @@ -54,10 +67,73 @@ namespace BetterPartyFinder { return result; } + private GameAddon? PartyFinderAddon() { + return this.Plugin.Interface.Framework.Gui.GetAddonByName("LookingForGroup", 1); + } + private void Draw() { + this.DrawFiltersWindow(); + this.DrawSettingsWindow(); + } + + private void DrawSettingsWindow() { + ImGui.SetNextWindowSize(new Vector2(-1f, -1f), ImGuiCond.FirstUseEver); + + if (!this.SettingsVisible || !ImGui.Begin($"{this.Plugin.Name} settings", ref this._settingsVisible)) { + return; + } + + var openWithPf = this.Plugin.Config.ShowWhenPfOpen; + if (ImGui.Checkbox("Open with PF", ref openWithPf)) { + this.Plugin.Config.ShowWhenPfOpen = openWithPf; + this.Plugin.Config.Save(); + } + + var sideOptions = new[] { + "Left", + "Right", + }; + var sideIdx = this.Plugin.Config.WindowSide == WindowSide.Left ? 0 : 1; + + ImGui.TextUnformatted("Side of PF window to dock to"); + if (ImGui.Combo("###window-side", ref sideIdx, sideOptions, sideOptions.Length)) { + this.Plugin.Config.WindowSide = sideIdx switch { + 0 => WindowSide.Left, + 1 => WindowSide.Right, + _ => this.Plugin.Config.WindowSide, + }; + + this.Plugin.Config.Save(); + } + + ImGui.End(); + } + + private void DrawFiltersWindow() { ImGui.SetNextWindowSize(new Vector2(550f, 510f), ImGuiCond.FirstUseEver); - if (!ImGui.Begin($"{this.Plugin.Name} settings")) { + var showWindow = this.Visible; + if (!showWindow && this.Plugin.Config.ShowWhenPfOpen) { + var addon = this.PartyFinderAddon(); + if (addon != null && addon.Visible) { + showWindow = true; + + if (this.Plugin.Config.WindowSide == WindowSide.Right) { + float? width; + try { + width = addon.Width; + } catch (NullReferenceException) { + width = null; + } + + if (width != null) { + ImGui.SetNextWindowPos(new Vector2(addon.X + addon.Width, addon.Y)); + } + } + } + } + + if (!showWindow || !ImGui.Begin(this.Plugin.Name, ref this._visible)) { return; } @@ -142,12 +218,33 @@ namespace BetterPartyFinder { } } + ImGui.SameLine(); + + if (IconButton(FontAwesomeIcon.Cog, "settings")) { + this.SettingsVisible = true; + } + ImGui.Separator(); if (selected != null && this.Plugin.Config.Presets.TryGetValue(selected.Value, out var filter)) { this.DrawPresetConfiguration(filter); } + if (this.Plugin.Config.ShowWhenPfOpen && this.Plugin.Config.WindowSide == WindowSide.Left) { + var addon = this.PartyFinderAddon(); + var currentWidth = ImGui.GetWindowWidth(); + float? addonWidth; + try { + addonWidth = addon?.Width; + } catch (NullReferenceException) { + addonWidth = null; + } + + if (addon != null && addonWidth != null) { + ImGui.SetWindowPos(new Vector2(addon.X - currentWidth, addon.Y)); + } + } + ImGui.End(); }