refactor(context): use generic delegates for items

This commit is contained in:
Anna 2021-04-30 15:07:54 -04:00
parent a034beb937
commit 90c91c09d4
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
5 changed files with 65 additions and 48 deletions

View File

@ -85,7 +85,12 @@ namespace XivCommon.Functions.ContextMenu {
/// <summary> /// <summary>
/// The delegate that is run when a context menu item is selected. /// The delegate that is run when a context menu item is selected.
/// </summary> /// </summary>
public delegate void ContextMenuItemSelectedDelegate(ContextMenuItemSelectedArgsContainer args); public delegate void ContextMenuItemSelectedDelegate(ContextMenuItemSelectedArgs args);
/// <summary>
/// The delegate that is run when an inventory context menu item is selected.
/// </summary>
public delegate void InventoryContextMenuItemSelectedDelegate(InventoryContextMenuItemSelectedArgs args);
private unsafe delegate byte ContextMenuOpenDelegate(IntPtr addon, int menuSize, AtkValue* atkValueArgs); private unsafe delegate byte ContextMenuOpenDelegate(IntPtr addon, int menuSize, AtkValue* atkValueArgs);
@ -302,7 +307,14 @@ namespace XivCommon.Functions.ContextMenu {
this._atkValueChangeType(newItem, ValueType.String); this._atkValueChangeType(newItem, ValueType.String);
var name = item switch { 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.Japanese => custom.NameJapanese,
ClientLanguage.English => custom.NameEnglish, ClientLanguage.English => custom.NameEnglish,
ClientLanguage.German => custom.NameGerman, ClientLanguage.German => custom.NameGerman,
@ -338,36 +350,42 @@ namespace XivCommon.Functions.ContextMenu {
var item = this.Items[index]; var item = this.Items[index];
// a custom item is being clicked // a custom item is being clicked
if (item is ContextMenuItem custom) { if (item is CustomContextMenuItem<ContextMenuItemSelectedDelegate> custom) {
var (inventory, agent) = this.GetContextMenuAgent(); var (_, agent) = this.GetContextMenuAgent();
var addonName = this.GetParentAddonName(addon); var addonName = this.GetParentAddonName(addon);
var info = GetAgentInfo(agent);
ContextMenuItemSelectedArgsContainer container; var args = new ContextMenuItemSelectedArgs(
if (inventory) { addon,
var info = GetInventoryAgentInfo(agent); agent,
container = new ContextMenuItemSelectedArgsContainer(new InventoryContextMenuItemSelectedArgs( addonName,
addon, info.actorId,
agent, info.contentIdLower,
addonName, info.text,
info.itemId, info.actorWorld
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
));
}
try { try {
custom.Action(container); custom.Action(args);
} catch (Exception ex) {
Logger.LogError(ex, "Exception in custom context menu item");
}
} else if (item is CustomContextMenuItem<InventoryContextMenuItemSelectedDelegate> 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) { } catch (Exception ex) {
Logger.LogError(ex, "Exception in custom context menu item"); Logger.LogError(ex, "Exception in custom context menu item");
} }

View File

@ -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;
}
}
}

View File

@ -1,8 +1,11 @@
namespace XivCommon.Functions.ContextMenu { using System;
namespace XivCommon.Functions.ContextMenu {
/// <summary> /// <summary>
/// A custom context menu item /// A custom context menu item
/// </summary> /// </summary>
public class ContextMenuItem : BaseContextMenuItem { public abstract class CustomContextMenuItem<T> : BaseContextMenuItem
where T : Delegate {
/// <summary> /// <summary>
/// The name of the context item to be shown for English clients. /// The name of the context item to be shown for English clients.
/// </summary> /// </summary>
@ -26,14 +29,14 @@
/// <summary> /// <summary>
/// The action to perform when this item is clicked. /// The action to perform when this item is clicked.
/// </summary> /// </summary>
public ContextMenu.ContextMenuItemSelectedDelegate Action { get; set; } public T Action { get; set; }
/// <summary> /// <summary>
/// Create a new context menu item. /// Create a new context menu item.
/// </summary> /// </summary>
/// <param name="name">the English name of the item, copied to other languages</param> /// <param name="name">the English name of the item, copied to other languages</param>
/// <param name="action">the action to perform on click</param> /// <param name="action">the action to perform on click</param>
public ContextMenuItem(string name, ContextMenu.ContextMenuItemSelectedDelegate action) { public CustomContextMenuItem(string name, T action) {
this.NameEnglish = name; this.NameEnglish = name;
this.NameJapanese = name; this.NameJapanese = name;
this.NameFrench = name; this.NameFrench = name;

View File

@ -0,0 +1,6 @@
namespace XivCommon.Functions.ContextMenu.Inventory {
public class InventoryContextMenuItem : CustomContextMenuItem<ContextMenu.InventoryContextMenuItemSelectedDelegate> {
public InventoryContextMenuItem(string name, ContextMenu.InventoryContextMenuItemSelectedDelegate action) : base(name, action) {
}
}
}

View File

@ -0,0 +1,6 @@
namespace XivCommon.Functions.ContextMenu {
public class NormalContextMenuItem : CustomContextMenuItem<ContextMenu.ContextMenuItemSelectedDelegate> {
public NormalContextMenuItem(string name, ContextMenu.ContextMenuItemSelectedDelegate action) : base(name, action) {
}
}
}