From 90c91c09d4bc099ed8ceb1296e9ec775809b8802 Mon Sep 17 00:00:00 2001 From: Anna Clemens Date: Fri, 30 Apr 2021 15:07:54 -0400 Subject: [PATCH] refactor(context): use generic delegates for items --- .../Functions/ContextMenu/ContextMenu.cs | 74 ++++++++++++------- .../ContextMenuItemSelectedArgsContainer.cs | 16 ---- ...xtMenuItem.cs => CustomContextMenuItem.cs} | 11 ++- .../Inventory/InventoryContextMenuItem.cs | 6 ++ .../NormalCustomContextMenuItem.cs | 6 ++ 5 files changed, 65 insertions(+), 48 deletions(-) delete mode 100755 XivCommon/Functions/ContextMenu/ContextMenuItemSelectedArgsContainer.cs rename XivCommon/Functions/ContextMenu/{ContextMenuItem.cs => CustomContextMenuItem.cs} (82%) create mode 100755 XivCommon/Functions/ContextMenu/Inventory/InventoryContextMenuItem.cs create mode 100755 XivCommon/Functions/ContextMenu/NormalCustomContextMenuItem.cs diff --git a/XivCommon/Functions/ContextMenu/ContextMenu.cs b/XivCommon/Functions/ContextMenu/ContextMenu.cs index fe6c44c..3c00357 100755 --- a/XivCommon/Functions/ContextMenu/ContextMenu.cs +++ b/XivCommon/Functions/ContextMenu/ContextMenu.cs @@ -85,7 +85,12 @@ namespace XivCommon.Functions.ContextMenu { /// /// The delegate that is run when a context menu item is selected. /// - public delegate void ContextMenuItemSelectedDelegate(ContextMenuItemSelectedArgsContainer args); + public delegate void ContextMenuItemSelectedDelegate(ContextMenuItemSelectedArgs args); + + /// + /// The delegate that is run when an inventory context menu item is selected. + /// + public delegate void InventoryContextMenuItemSelectedDelegate(InventoryContextMenuItemSelectedArgs args); private unsafe delegate byte ContextMenuOpenDelegate(IntPtr addon, int menuSize, AtkValue* atkValueArgs); @@ -302,7 +307,14 @@ namespace XivCommon.Functions.ContextMenu { this._atkValueChangeType(newItem, ValueType.String); var name = item switch { - ContextMenuItem custom => this.Language switch { + NormalContextMenuItem custom => this.Language switch { + ClientLanguage.Japanese => custom.NameJapanese, + ClientLanguage.English => custom.NameEnglish, + ClientLanguage.German => custom.NameGerman, + ClientLanguage.French => custom.NameFrench, + _ => custom.NameEnglish, + }, + InventoryContextMenuItem custom => this.Language switch { ClientLanguage.Japanese => custom.NameJapanese, ClientLanguage.English => custom.NameEnglish, ClientLanguage.German => custom.NameGerman, @@ -338,36 +350,42 @@ namespace XivCommon.Functions.ContextMenu { var item = this.Items[index]; // a custom item is being clicked - if (item is ContextMenuItem custom) { - var (inventory, agent) = this.GetContextMenuAgent(); + if (item is CustomContextMenuItem custom) { + var (_, agent) = this.GetContextMenuAgent(); var addonName = this.GetParentAddonName(addon); + var info = GetAgentInfo(agent); - ContextMenuItemSelectedArgsContainer container; - if (inventory) { - var info = GetInventoryAgentInfo(agent); - container = new ContextMenuItemSelectedArgsContainer(new InventoryContextMenuItemSelectedArgs( - addon, - agent, - addonName, - info.itemId, - info.itemAmount, - info.itemHq - )); - } else { - var info = GetAgentInfo(agent); - container = new ContextMenuItemSelectedArgsContainer(new ContextMenuItemSelectedArgs( - addon, - agent, - addonName, - info.actorId, - info.contentIdLower, - info.text, - info.actorWorld - )); - } + var args = new ContextMenuItemSelectedArgs( + addon, + agent, + addonName, + info.actorId, + info.contentIdLower, + info.text, + info.actorWorld + ); try { - custom.Action(container); + custom.Action(args); + } catch (Exception ex) { + Logger.LogError(ex, "Exception in custom context menu item"); + } + } else if (item is CustomContextMenuItem invCustom) { + var (_, agent) = this.GetContextMenuAgent(); + var addonName = this.GetParentAddonName(addon); + var info = GetInventoryAgentInfo(agent); + + var args = new InventoryContextMenuItemSelectedArgs( + addon, + agent, + addonName, + info.itemId, + info.itemAmount, + info.itemHq + ); + + try { + invCustom.Action(args); } catch (Exception ex) { Logger.LogError(ex, "Exception in custom context menu item"); } diff --git a/XivCommon/Functions/ContextMenu/ContextMenuItemSelectedArgsContainer.cs b/XivCommon/Functions/ContextMenu/ContextMenuItemSelectedArgsContainer.cs deleted file mode 100755 index 4136ac7..0000000 --- a/XivCommon/Functions/ContextMenu/ContextMenuItemSelectedArgsContainer.cs +++ /dev/null @@ -1,16 +0,0 @@ -using XivCommon.Functions.ContextMenu.Inventory; - -namespace XivCommon.Functions.ContextMenu { - public class ContextMenuItemSelectedArgsContainer { - public ContextMenuItemSelectedArgs? ItemSelectedArgs { get; } - public InventoryContextMenuItemSelectedArgs? InventoryItemSelectedArgs { get; } - - internal ContextMenuItemSelectedArgsContainer(ContextMenuItemSelectedArgs args) { - this.ItemSelectedArgs = args; - } - - internal ContextMenuItemSelectedArgsContainer(InventoryContextMenuItemSelectedArgs args) { - this.InventoryItemSelectedArgs = args; - } - } -} diff --git a/XivCommon/Functions/ContextMenu/ContextMenuItem.cs b/XivCommon/Functions/ContextMenu/CustomContextMenuItem.cs similarity index 82% rename from XivCommon/Functions/ContextMenu/ContextMenuItem.cs rename to XivCommon/Functions/ContextMenu/CustomContextMenuItem.cs index ac02eee..9170015 100755 --- a/XivCommon/Functions/ContextMenu/ContextMenuItem.cs +++ b/XivCommon/Functions/ContextMenu/CustomContextMenuItem.cs @@ -1,8 +1,11 @@ -namespace XivCommon.Functions.ContextMenu { +using System; + +namespace XivCommon.Functions.ContextMenu { /// /// A custom context menu item /// - public class ContextMenuItem : BaseContextMenuItem { + public abstract class CustomContextMenuItem : BaseContextMenuItem + where T : Delegate { /// /// The name of the context item to be shown for English clients. /// @@ -26,14 +29,14 @@ /// /// The action to perform when this item is clicked. /// - public ContextMenu.ContextMenuItemSelectedDelegate Action { get; set; } + public T Action { get; set; } /// /// Create a new context menu item. /// /// the English name of the item, copied to other languages /// the action to perform on click - public ContextMenuItem(string name, ContextMenu.ContextMenuItemSelectedDelegate action) { + public CustomContextMenuItem(string name, T action) { this.NameEnglish = name; this.NameJapanese = name; this.NameFrench = name; diff --git a/XivCommon/Functions/ContextMenu/Inventory/InventoryContextMenuItem.cs b/XivCommon/Functions/ContextMenu/Inventory/InventoryContextMenuItem.cs new file mode 100755 index 0000000..2280514 --- /dev/null +++ b/XivCommon/Functions/ContextMenu/Inventory/InventoryContextMenuItem.cs @@ -0,0 +1,6 @@ +namespace XivCommon.Functions.ContextMenu.Inventory { + public class InventoryContextMenuItem : CustomContextMenuItem { + public InventoryContextMenuItem(string name, ContextMenu.InventoryContextMenuItemSelectedDelegate action) : base(name, action) { + } + } +} diff --git a/XivCommon/Functions/ContextMenu/NormalCustomContextMenuItem.cs b/XivCommon/Functions/ContextMenu/NormalCustomContextMenuItem.cs new file mode 100755 index 0000000..0e345fe --- /dev/null +++ b/XivCommon/Functions/ContextMenu/NormalCustomContextMenuItem.cs @@ -0,0 +1,6 @@ +namespace XivCommon.Functions.ContextMenu { + public class NormalContextMenuItem : CustomContextMenuItem { + public NormalContextMenuItem(string name, ContextMenu.ContextMenuItemSelectedDelegate action) : base(name, action) { + } + } +}